schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using DataEntity.Device;
using DataEntity.Share;
using HxEnum;
using HxSocket;
using HxSocketImplement.Sockets;
using HxSocketImplement.Sockets.HxService;
//using MySqlX.XDevAPI.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Xml;
using XCommon;
using XCommon.Log;
using XCore;
using XHandler.Controls.Run.Com;
using XHandler.View;
using XImagingXhandler.XDAL;
using static HxEnum.OperationTypeEnum;
using static HxEnum.StateEnum;
 
namespace XHandler.Controls
{
    /// <summary>
    /// 第三方设备控制逻辑类
    /// </summary>
    public class ThirdPartControl
    {
        #region 变量
        // 系统语言
        private string m_currentCulture = string.Empty;
 
        #region BLL
        private ThirdPartBll m_thirdPartBll = new ThirdPartBll();
        #endregion
 
        // 运行界面
        public static RunWnd LaunchView = null;
        #endregion
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public ThirdPartControl()
        {
            m_currentCulture = Shared.SoftwareInformation.currentculture;
        }
 
        #region 执行第三方设备方法
        /// <summary>
        /// 执行第三方设备方法
        /// </summary>
        /// <param name="xmlEnv"></param>
        /// <param name="methodNode"></param>
        /// <param name="isSimulator"></param>
        /// <returns></returns>
        public bool ExecuteCoatingTransfer(XmlNode xmlEnv, XmlNode methodNode, bool isSimulator)
        {
            bool bResult = false;
            string strMethodName = methodNode.SelectSingleNode("name").InnerText;
            string log = string.Empty;
 
            #region 任务被取消
            if (LaunchView._cancelSource.IsCancellationRequested)
            {
                bResult = false;
                return bResult;
            }
            #endregion
 
            try
            {
                #region 数据准备
                MethodThirdPart thirdPartData = m_thirdPartBll.GenerateMethodThirdPartDataByXmlNode(methodNode);
                #endregion
 
                #region 数据检查
                if (thirdPartData.device == null)
                {
                    LaunchView.AddLogs(string.Format("【{0}】> Xhandler: 【{1}】{2}", DateTime.Now.ToString("HH:mm:ss:fff"), strMethodName, Properties.ThirdPartResource.strSendMethodError.ToString()));
                    LoggerHelper.ErrorLog(string.Format("设备不存在! deviceId = {0}", thirdPartData.deviceValue));
                    return bResult;
                }
 
                if (thirdPartData.deviceMethod == null)
                {
                    LaunchView.AddLogs(string.Format("【{0}】> Xhandler: 【{1}】{2}", DateTime.Now.ToString("HH:mm:ss:fff"), strMethodName, Properties.ThirdPartResource.strSendMethodError.ToString()));
                    LoggerHelper.ErrorLog(string.Format("设备方法不存在! deviceMethodId = {0}", thirdPartData.deviceMethodValue));
                    return bResult;
                }
                #endregion
 
                #region 运行日志
                if (m_currentCulture.Equals("zh-CN"))
                {
                    log = string.Format("【{0}】> Xhandler: 【{1}({2})】{3}", DateTime.Now.ToString("HH:mm:ss:fff"), strMethodName, thirdPartData.deviceMethod.ParameterName, Properties.MachineRunResource.strStart.ToString());
                    LaunchView.AddLogs(log);
                }
                else
                {
                    log = string.Format("【{0}】> Xhandler: 【{1}({2})】{3}", DateTime.Now.ToString("HH:mm:ss:fff"), strMethodName, thirdPartData.deviceMethod.ParameterName, "start");
                    LaunchView.AddLogs(log);
                }
                #endregion
 
                #region 化学工作站 判断离心机开关门前是否机械臂在安全位置
                if (thirdPartData.deviceMethod != null)
                {
                    if ((thirdPartData.deviceMethod.ParameterName.Equals("OpenDoor") || thirdPartData.deviceMethod.ParameterName.Equals("CloseDoor"))&&Shared.SoftwareInformation.software_device_number.Equals("HXFX"))
                    {
                        //强制控制机械臂到安全位
                        Dictionary<string, object> dicParamsRobot = new Dictionary<string, object>();
                        string xmlText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><method id=\"135\"><isrun></isrun><status>enable</status><name>机械臂</name><label>机械臂</label>\r\n<strIndex>14-17</strIndex><isThirdPart>True</isThirdPart><device><id>d40b4dfc-6ae9-402f-8946-2fe5ef908b2d</id>\r\n<name>机械臂</name><communicateType>0</communicateType></device><method><id>83c2225e-9942-46a5-8b80-b661b55d131a</id>\r\n<name>Pick</name></method><paramtersSocket><paramtersGroup><id>b7ab14c7-89a4-436a-a999-550432ec5ce3</id><name>target</name>\r\n<value>SAFE_01</value></paramtersGroup></paramtersSocket></method></root>";
                        XmlDocument xmldoc = new XmlDocument();
                        xmldoc.LoadXml(xmlText);
                        XmlNode methodNodeRobot = xmldoc.SelectSingleNode("root/method");
                        MethodThirdPart thirdPartDataRobot = m_thirdPartBll.GenerateMethodThirdPartDataByXmlNode(methodNodeRobot);
                        GetDeviceInParams(thirdPartDataRobot, ref dicParamsRobot);
                        InstructCommonMethodSendModel sendModelRobot = new InstructCommonMethodSendModel();
                        sendModelRobot.DeviceId = thirdPartDataRobot.device.Id;
                        sendModelRobot.DeviceName = thirdPartDataRobot.device.Name;
                        sendModelRobot.EquipmentType = thirdPartDataRobot.device.Type;
                        sendModelRobot.Ip = thirdPartDataRobot.device.Ip;
                        sendModelRobot.Port = Convert.ToInt32(thirdPartDataRobot.device.Port);
                        sendModelRobot.EquipmentId = thirdPartDataRobot.device.EquipmentId;
                        sendModelRobot.VirtualConnectionState = thirdPartDataRobot.device.VirtualConnectionState;
                        sendModelRobot.CommunicateType = thirdPartDataRobot.device.CommunicateType;
                        sendModelRobot.SendData = new HxSendBase { experiment_id = Shared.Exp.ExperimentId, parameters = dicParamsRobot, method = thirdPartDataRobot.deviceMethod.ParameterName };
 
                        InstructCommonMethodSendModel instructCommonMethodSendModelRobot = HxSocketParameterService.GetInstructCommonMethodSendModel(sendModelRobot);
 
                        // 给设备节点发送指令
                        LockDeviceAndSendMsg(instructCommonMethodSendModelRobot, isSimulator);
                    }
                }
                #endregion
 
                // 指令发送之前
                BeforeSend(thirdPartData);
 
                #region Send
                LoggerHelper.DebugLog(string.Format("deviceId = {0}, methodId = {1} 执行开始.", thirdPartData.device.Id, thirdPartData.deviceMethod.Id));
 
                // 获取指令发送参数
                Dictionary<string, object> dicParams = new Dictionary<string, object>();
                if (!GetDeviceInParams(thirdPartData, ref dicParams))
                {
                    LaunchView.AddLogs(string.Format("【{0}】> Xhandler: 【{1}】{2}", DateTime.Now.ToString("HH:mm:ss:fff"), strMethodName, Properties.ThirdPartResource.strSendMethodError.ToString()));
                    return false;
                }
 
                #region 作成发送参数
                InstructCommonMethodSendModel sendModel = new InstructCommonMethodSendModel();
                sendModel.DeviceId = thirdPartData.device.Id;
                sendModel.DeviceName = thirdPartData.device.Name;
                sendModel.EquipmentType = thirdPartData.device.Type;
                sendModel.Ip = thirdPartData.device.Ip;
                sendModel.Port = Convert.ToInt32(thirdPartData.device.Port);
                sendModel.EquipmentId = thirdPartData.device.EquipmentId;
                sendModel.VirtualConnectionState = thirdPartData.device.VirtualConnectionState;
                sendModel.CommunicateType = thirdPartData.device.CommunicateType;
                sendModel.SendData = new HxSendBase { experiment_id = Shared.Exp.ExperimentId, parameters = dicParams, method = thirdPartData.deviceMethod.ParameterName };
 
                InstructCommonMethodSendModel instructCommonMethodSendModel = HxSocketParameterService.GetInstructCommonMethodSendModel(sendModel);
                #endregion
 
                // 给设备节点发送指令
                Result deviceResult = LockDeviceAndSendMsg(instructCommonMethodSendModel, isSimulator);
                #endregion
 
                // 指令发送之后
                AfterSend(thirdPartData);
 
                if (deviceResult.status != StateEnum_Equipment.Completed)
                {
                    bResult = deviceResult.OperMark == NodeOperationTypeEnum.Continue ? true : false;
                }
                else
                {
                    bResult = true;
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
 
                #region Exception
                LaunchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】>Xhandler: 【" + strMethodName + "】error:source:" + ex.Source + ";error:" + ex.Message + ";");
                bResult = false;
                #endregion
            }
 
            return bResult;
        }
        #endregion
 
        #region 指令发送之前
        /// <summary>
        /// 指令发送之前
        /// </summary>
        /// <param name="thirdPartData"></param>
        private void BeforeSend(MethodThirdPart thirdPartData)
        {
            LoggerHelper.DebugLog($"[BeforeSend]: start. methodId = {thirdPartData.deviceMethod.Id}");
 
            try
            {
 
            }
            finally
            {
                LoggerHelper.DebugLog($"[BeforeSend]: end. methodId = {thirdPartData.deviceMethod.Id}");
            }
        }
        #endregion
 
        #region 指令发送之后
        /// <summary>
        /// 指令发送之后
        /// </summary>
        /// <param name="thirdPartData"></param>
        private void AfterSend(MethodThirdPart thirdPartData)
        {
            LoggerHelper.DebugLog($"[AfterSend]: start. methodId = {thirdPartData.deviceMethod.Id}");
 
            try
            {
 
            }
            finally
            {
                LoggerHelper.DebugLog($"[AfterSend]: end. methodId = {thirdPartData.deviceMethod.Id}");
            }
        }
        #endregion
 
        #region 获取发送参数
        /// <summary>
        /// 获取发送参数
        /// </summary>
        /// <param name="thirdPartData"></param>
        /// <param name="dicParams"></param>
        /// <returns></returns>
        public bool GetDeviceInParams(MethodThirdPart thirdPartData, ref Dictionary<string, object> dicParams)
        {
            #region Socket-TCP标准协议
            if (thirdPartData.device.CommunicateType == EnumManagement.GetEnumValue(CommunicateTypeEnum.Socket))
            {
                var selSendParameters = thirdPartData.methodParametersData.Where(it=>it.Type == EnumManagement.GetEnumValue(ParameterTypeEnum.Send));
                if (selSendParameters.Any())
                {
                    List<DeviceConfigMethodParametersModel> parametersData = selSendParameters.ToList();
                    foreach (var inParamItem in parametersData)
                    {
                        // 获取变量值
                        string varValue = inParamItem.Value;
 
                        //如果值是变量,取出变量的值
                        string globalVariableValue = "";
                        globalVariableValue = ControlCom.SearchValueOfGlobalVariable(LaunchView, varValue);
                        if (!string.IsNullOrEmpty(globalVariableValue))
                        {
                            varValue = globalVariableValue;
                        }
 
                        #region 字符串类型
                        if (inParamItem.DataType == EnumManagement.GetEnumValue(ParameterDataTypeEnum.Varchar))
                        {
                            dicParams.Add(inParamItem.ParameterName, varValue);
                        }
                        #endregion
 
                        #region 数字类型
                        else if (inParamItem.DataType == EnumManagement.GetEnumValue(ParameterDataTypeEnum.Int))
                        {
                            int nVarValue = 0;
                            // 转成数字类型
                            if (!ComUtility.ParseToInt(varValue, out nVarValue))
                            {
                                return false;
                            }
 
                            dicParams.Add(inParamItem.ParameterName, nVarValue);
                        }
                        #endregion
 
                        #region Json类型
                        else if (inParamItem.DataType == EnumManagement.GetEnumValue(ParameterDataTypeEnum.JSON))
                        {
                            if (!string.IsNullOrEmpty(varValue))
                            {
                                dicParams = JsonConvert.DeserializeObject<Dictionary<string, object>>(varValue);
                            }
                            break;
                        }
                        #endregion
 
                        #region bool类型
                        else if (inParamItem.DataType == EnumManagement.GetEnumValue(ParameterDataTypeEnum.Bool))
                        {
                            bool bVarValue = false;
                            // 转成数字类型
                            if (!ComUtility.ParseToBool(varValue, ref bVarValue))
                            {
                                LoggerHelper.ErrorLog(string.Format("ParseToInt,value = {0}, 参数不是bool!!!", varValue));
                                return false;
                            }
 
                            dicParams.Add(inParamItem.ParameterName, bVarValue);
                        }
                        #endregion
                    }
                }
            }
            #endregion
 
            return true;
        }
        #endregion
 
        #region 给设备节点发送指令
        /// <summary>
        /// 给设备节点发送指令
        /// </summary>
        /// <param name="commonMethodSendModel"></param>
        /// <param name="isSimulator"></param>
        /// <returns></returns>
        private Result LockDeviceAndSendMsg(InstructCommonMethodSendModel commonMethodSendModel, bool isSimulator)
        {
            return new ResultHandle().StepResult(commonMethodSendModel, isSimulator);
        }
        #endregion
 
 
    }
}