领胜LDS 键盘AOI检测项目
wells.liu
2020-07-02 22658ee90bd0f8b7b89a6030c2edc7fbf6719c3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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<string> _avaiableMethods { get; set; } = new List<string>();
 
        private IProcess process = null;
        public IProcess Process
        {
            get => process;
            set
            {
                if (process != value)
                {
                    process = value;
                    OnProcessUpdated();
                }
            }
        }
 
        #region ILogoutput
        public Action<LogMsg> 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<IDevice> devices = new List<IDevice>(Process.DeviceCollection);
 
                List<ISimpleDevice> sDevices = new List<ISimpleDevice>(devices);
                sDevices.Insert(0, new SimpleDevice() { Id = "", Name = "未指定" });
 
                UIHelper.SetCombo(cboTestExecuteDevice, new List<ISimpleDevice>(sDevices), "Name", "Id");
                UIHelper.SetCombo(cboTestSourceDevices, new List<ISimpleDevice>(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;
    //    }
    //}
}