using Autofac; using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Windows.Forms; namespace Bro.UI.Config.Ctrls { public partial class CtrlMethodInvoke : UserControl, IProcessObserver, ILogOutput { public CtrlMethodInvoke() { InitializeComponent(); } InvokeType _invokeType = InvokeType.NoneInvoke; List _avaiableMethods { get; set; } = new List(); private IProcess process = null; public IProcess Process { get => process; set { if (process != value) { process = value; OnProcessUpdated(); } } } #region ILogoutput public Action OnLogMsgOutput { get; set; } public void LogDisplay(LogMsg msg) { } #endregion public CtrlMethodInvoke(InvokeType invokeType) { InitializeComponent(); _invokeType = invokeType; } public void OnProcessUpdated() { if (InvokeRequired) { Invoke(new Action(() => OnProcessUpdated())); } else { if (Process != null) { _avaiableMethods = Process.ProcessMethodCollection.Where(u => u.InvokeType == _invokeType).Select(u => u.MethodCode).ToList(); LoadDevices(); LoadProcessMethods(); } } } public void DownloadProcess(IProcess process) { Process = process; } private void LoadProcessMethods() { var msList = Process.IConfig.GetAllMonitorSet().Where(u => _avaiableMethods.Contains(u.MethodCode)).ToList(); //_avaiableMethods.ForEach(u => //{ // if (!msList.Any(m => m.MethodCode == u)) // { // SimpleMonitorSet ms = new SimpleMonitorSet() // { // MethodCode = u, // Name = "无调用/无绑定", // }; // msList.Add(ms); // } //}); UIHelper.SetCombo(cboTestMethod, msList, "DisplayText", "Id"); cboTestMethod.SelectedIndexChanged += cboTestMethod_SelectedIndexChanged; cboTestMethod_SelectedIndexChanged(null, null); } private void LoadDevices() { if (Process.DeviceCollection.Count > 0) { List devices = new List(Process.DeviceCollection); List sDevices = new List(devices); sDevices.Insert(0, new SimpleDevice() { Id = "", Name = "未指定" }); UIHelper.SetCombo(cboTestExecuteDevice, new List(sDevices), "Name", "Id"); UIHelper.SetCombo(cboTestSourceDevices, new List(sDevices), "Name", "Id"); } } #region event IDevice _sourceDevice = null, _invokeDevice = null; string _methodCode = ""; MethodInfo _method = null; private void cboTestMethod_SelectedIndexChanged(object sender, EventArgs e) { if (cboTestMethod.SelectedIndex >= 0) { IMonitorSet ms = cboTestMethod.SelectedItem as IMonitorSet; _methodCode = ms.MethodCode; _method = Process.ProcessMethodDict[_methodCode]; propOpConfig.SelectedObject = ms.OpConfig; cboTestSourceDevices.SelectedValue = ms.SourceDevice; cboTestSourceDevices.Enabled = ms.SourceDevice == null; cboTestExecuteDevice.SelectedValue = ms.InvokeDevice; cboTestExecuteDevice.Enabled = ms.InvokeDevice == null; } } private void btnInvoke_Click(object sender, EventArgs e) { if (cboTestSourceDevices.SelectedIndex >= 0) { _sourceDevice = cboTestSourceDevices.SelectedItem as IDevice; } if (cboTestExecuteDevice.SelectedIndex >= 0) { _invokeDevice = cboTestExecuteDevice.SelectedItem as IDevice; } if (_sourceDevice == null || _invokeDevice == null) { if (MessageBox.Show("发起设备或者执行设备为空,是否继续", "设备为空提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != DialogResult.OK) return; } IOperationConfig opConfig = propOpConfig.SelectedObject as IOperationConfig; Task.Run(() => { OnLogMsgOutput?.Invoke(new LogMsg(DateTime.Now, $"{_methodCode}调用开始", "")); ProcessResponse response = _method.Invoke(Process, new object[] { opConfig, _invokeDevice, _sourceDevice }) as ProcessResponse; if (response != null) { OnLogMsgOutput?.Invoke(new LogMsg(DateTime.Now, $"{_methodCode}调用完成", $"反馈:{JsonConvert.SerializeObject(response)}")); } }); } #endregion } //public class FullMonitorSet : IComplexDisplay //{ // public IMonitorSet MonitorSetInfo { get; set; } // public IDevice SourceDevice { get; set; } // public string GetDisplayText() // { // string desc = MonitorSetInfo.DisplayText; // desc = $"{SourceDevice?.Name ?? "无源设备"} " + desc; // return desc; // } //} }