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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
 
namespace XImaging.Automation.Library.HxDriverLib.Messaging
{
    public class BiosenDriverMessage : IMessage
    {
        #region 消息类型定义
        /// <summary>
        /// 消息类型0: 心跳包
        /// </summary>
        public static int MESSAGE_TYPE_HEARTBEAT { get; } = 0;
        /// <summary>
        /// 消息类型1:方法消息(延迟返回消息)
        /// </summary>
        public static int MESSAGE_TYPE_TASK { get; } = 1;
        /// <summary>
        /// 消息类型2:查询方法消息(即时返回消息)
        /// </summary>
        public static int MESSAGE_TYPE_QUERY { get; } = 2;
        /// <summary>
        /// 消息类型3:错误处理消息(无返回消息)
        /// </summary>
        public static int MESSAGE_TYPE_TROUBLESHOOT { get; } = 3;
        /// <summary>
        /// 消息类型4:确认消息
        /// </summary>
        public static int MESSAGE_TYPE_ACK { get; } = 4;
        /// <summary>
        /// 消息类型5:完成消息
        /// </summary>
        public static int MESSAGE_TYPE_FINISH { get; } = 5;
        #endregion
 
        #region 方法状态定义
        public static int METHOD_STATUS_NULL { get; } = -1;
        /// <summary>
        /// 方法状态0:准备执行(指令队列中)
        /// </summary>
        public static int METHOD_STATUS_SCHEDULED { get; } = 0;
        /// <summary>
        /// 方法状态1:正在执行
        /// </summary>
        public static int METHOD_STATUS_INPROGRES { get; } = 1;
        /// <summary>
        /// 方法状态2:执行完成且成功
        /// </summary>
        public static int METHOD_STATUS_COMPLETED { get; } = 2;
        /// <summary>
        /// 方法状态3:执行完成且失败
        /// </summary>
        public static int METHOD_STATUS_FAILED { get; } = 3;
        /// <summary>
        /// 方法状态4:该方法在指令队列中被取消
        /// </summary>
        public static int METHOD_STATUS_CANCELED { get; } = 4;
        #endregion
 
        protected string m_strGUID;
        protected int m_strEndpoint;
        protected JObject m_jsonOriginMessage;
        protected string m_strMessageID;
        protected int m_iMessageType;
        protected string m_strMethod;
        protected string m_strEquipmentID;
        protected string m_strWorkflowID;
        protected string m_strExperimentID;
        protected JObject m_jsonParameters;
        protected int m_iTroubleshoot;
        protected string m_strTimestamp;
 
        public string GUID { get { return m_strGUID; } }
 
        public int LocalEndpoint { get { return m_strEndpoint; } }
 
        /// <summary>
        /// 消息ID,唯一
        /// </summary>
        public string MessageID { get { return m_strMessageID; } }
        /// <summary>
        /// 消息类型,见定义
        /// </summary>
        public int MessageType { get { return m_iMessageType; } }
        /// <summary>
        /// 方法名称
        /// </summary>
        public string Method { get { return m_strMethod; } }
        /// <summary>
        /// 设备ID
        /// </summary>
        public string EquipmentID { get { return m_strEquipmentID; } }
        /// <summary>
        /// 工作流ID
        /// </summary>
        public string WorkflowID { get { return m_strWorkflowID; } }
        /// <summary>
        /// 
        /// </summary>
        public string ExperimentID { get { return m_strExperimentID; } }
        /// <summary>
        /// 消息生成时间
        /// </summary>
        public string Timestamp { get { return m_strTimestamp; } }
    }
 
    public class ReqMessage : BiosenDriverMessage
    {
        #region 私有成员
 
        protected string m_strDescription;
        protected int m_iEstimateTime;
        protected int m_iTimeout;
        protected long m_lExpiredTime;
 
        #endregion
        /// <summary>
        /// 原始消息
        /// </summary>
        public JObject OriginJSON { get { return m_jsonOriginMessage; } }
 
        /// <summary>
        /// 方法需要的参数
        /// </summary>
        public JObject Parameters { get { return m_jsonParameters; } }
        /// <summary>
        /// 
        /// </summary>
        public int Troubleshoot { get { return m_iTroubleshoot; } }
        /// <summary>
        /// 方法描述文字
        /// </summary>
        public string Description { get { return m_strDescription; } }
 
        public int EstimateTime { get { return m_iEstimateTime; } }
 
        public int Timeout { get { return m_iTimeout; } }
        /// <summary>
        /// 消息逾期时间
        /// </summary>
        public long ExpiredTime { get { return m_lExpiredTime; } }
 
        /// <summary>
        /// 构造类1
        /// </summary>
        /// <param name="message"></param>
        public ReqMessage(string message)
        {
            JObject obj = (JObject)JsonConvert.DeserializeObject(message);
            LogConstant.logger.Print("Parse message " + message + " to HxMessage.");
 
            this.m_jsonOriginMessage = obj;
            this.m_strGUID = obj["guid"].ToString();
            this.m_strEndpoint = obj.Value<int>("endpoint");
            this.m_strMessageID = obj["message_id"].ToString();
            this.m_iMessageType = obj.Value<int>("message_type");
            this.m_strEquipmentID = obj["equipment_id"].ToString();
 
            if (this.MessageType != MESSAGE_TYPE_HEARTBEAT)
            {
                this.m_strWorkflowID = obj["workflow_id"].ToString();
                this.m_strExperimentID = obj["experiment_id"].ToString();
                this.m_strMethod = obj["method"].ToString();
                this.m_strDescription = obj["description"].ToString();
            }
 
            if (this.MessageType == MESSAGE_TYPE_TASK || this.MessageType == MESSAGE_TYPE_QUERY)
            {
                this.m_jsonParameters = (JObject)obj["parameters"];
                this.m_iEstimateTime = obj.Value<int>("estimate_time");
                this.m_iTimeout = obj.Value<int>("timeout");
            }
            else
            {
                this.m_jsonParameters = null;
                this.m_iEstimateTime = 0;
                this.m_iTimeout = 0;
            }
 
            if (this.MessageType == MESSAGE_TYPE_TROUBLESHOOT)
                this.m_iTroubleshoot = obj.Value<int>("troubleshoot");
            else
                this.m_iTroubleshoot = 0;
 
            this.m_lExpiredTime = obj.Value<long>("expired");
            this.m_strTimestamp = obj["timestamp"].ToString();
        }
    }
 
    public class ResMessage : BiosenDriverMessage
    {
        #region 私有成员
        private JObject m_jsonData;
        private int m_iMethodStatus;
        private string m_strErrorCode;
        private string m_strErrorText;
        
        #endregion
        /// <summary>
        /// 方法需要的参数
        /// </summary>
        public JObject Data { get { return m_jsonData; } }
        /// <summary>
        /// 
        /// </summary>
        public int MethodStatus { get { return m_iMethodStatus; } }
        /// <summary>
        /// 错误码
        /// </summary>
        public string ErrorCode { get { return m_strErrorCode; } }
        /// <summary>
        /// 错误描述
        /// </summary>
        public string ErrorText { get { return m_strErrorText; } }
        /// <summary>
        /// 错误处理方案
        /// </summary>
        public int Troubleshoot { get { return m_iTroubleshoot; } }
 
        public ResMessage(string message)
        {
            JObject obj = (JObject)JsonConvert.DeserializeObject(message);
            LogConstant.logger.Print("Parse message " + message + " to HxMessage.");
            
            this.m_strGUID = obj["guid"].ToString();
 
            if (obj.ContainsKey("endpoint"))
                this.m_strEndpoint = obj.Value<int>("endpoint");
            else
                this.m_strEndpoint = 0;
 
            this.m_strMessageID = obj["message_id"].ToString();
            this.m_iMessageType = obj.Value<int>("message_type");
            this.m_strEquipmentID = obj["equipment_id"].ToString();
 
            if (this.MessageType != MESSAGE_TYPE_HEARTBEAT)
            {
                this.m_strWorkflowID = obj["workflow_id"].ToString();
                this.m_strExperimentID = obj["experiment_id"].ToString();
                this.m_strMethod = obj["method"].ToString();
            }
 
            if (this.MessageType == MESSAGE_TYPE_FINISH)
            {
                this.m_jsonData = (JObject)obj["data"];
                this.m_iMethodStatus = obj.Value<int>("method_status");
 
                if (this.MethodStatus == METHOD_STATUS_COMPLETED)
                {
                    this.m_strErrorCode = "";
                    this.m_strErrorText = "";
                    this.m_iTroubleshoot = 0;
                }
                else
                {
                    this.m_strErrorCode = obj["error_code"].ToString();
                    this.m_strErrorText = obj["error_text"].ToString();
                    this.m_iTroubleshoot = obj.Value<int>("troubleshoot");
                }
            }
            else
            {
                this.m_jsonData = null;
                this.m_iMethodStatus = METHOD_STATUS_NULL;
                this.m_strErrorCode = null;
                this.m_strErrorText = null;
                this.m_iTroubleshoot = 0;
            }
            
            this.m_strTimestamp = obj["timestamp"].ToString();
        }
    }
 
    public class PostMessage
    {
        #region 私有成员
        private string m_strMessageType;
        private string m_strMessageID;
        private string m_strEquipmentID;
        private string m_strWorkflowID;
        private string m_strExperimentID;
        private string m_strMethodMessageID;
        private string m_strMethod;
        // private JObject m_jsonData;
        private string m_strTimestamp;
        #endregion
 
        public string MessageType { get { return this.m_strMessageType; } }
        public string MessageID { get { return this.m_strMessageID; } }
        public string EquipmentID { get { return this.m_strEquipmentID; } }
        public string WorkflowID { get { return this.m_strWorkflowID; } }
 
        public string ExperimentID { get { return this.m_strExperimentID; } }
        public string MethodMessageID { get { return this.m_strMethodMessageID; } }
        public string Method { get { return this.m_strMethod; } }
        // public JObject Data { get { return this.m_jsonData; } }
        public string Timestamp { get { return this.m_strTimestamp; } }
 
        public PostMessage(ReqMessage rMessage)
        {
            this.m_strMessageID = Guid.NewGuid().ToString();
            this.m_strEquipmentID = rMessage.EquipmentID;
            this.m_strWorkflowID = rMessage.WorkflowID;
            this.m_strExperimentID = rMessage.ExperimentID;
            this.m_strMethodMessageID = rMessage.MessageID;
            this.m_strMethod = rMessage.Method;
            this.m_strTimestamp = DateTime.Now.ToString();
        }
 
        public virtual JObject ToJObject()
        {
            return JObject.FromObject(new { });
        }
    }
 
    public class DataObjectMessage : PostMessage
    {
        private string m_strMessageType;
        private JObject m_jsonData;
 
        public new string MessageType { get { return this.m_strMessageType; } }
 
        public JObject Data { get { return this.m_jsonData; } }
 
        public DataObjectMessage(ReqMessage rMessage, JObject obj) : base(rMessage)
        {
            this.m_strMessageType = "data";
            this.m_jsonData = obj;
        }
 
        public override JObject ToJObject()
        {
            JObject obj = JObject.FromObject(
                new
                {
                    message_type = MessageType,
                    message_id = MessageID,
                    method = Method,
                    equipment_id = EquipmentID,
                    workflow_id = WorkflowID,
                    experiment_id = ExperimentID,
                    method_message_id = MethodMessageID,
                    data = Data,
                    timestamp = DateTime.Now
                }
            );
            return obj;
        }
 
        public override string ToString()
        {
            return this.ToJObject().ToString();
        }
    }
 
 
    public class FileObjectMessage : PostMessage
    {
        private string m_strMessageType;
        private string m_strFilepath;
 
        public new string MessageType { get { return this.m_strMessageType; } }
 
        public string FilePath { get { return this.m_strFilepath; } }
 
        public FileObjectMessage(ReqMessage rMessage, string fileFullPath) : base(rMessage)
        {
            this.m_strMessageType = "file";
            this.m_strFilepath = fileFullPath;
        }
 
        public override JObject ToJObject()
        {
            JObject obj = JObject.FromObject(
                new
                {
                    message_type = MessageType,
                    message_id = MessageID,
                    method = Method,
                    equipment_id = EquipmentID,
                    workflow_id = WorkflowID,
                    experiment_id = ExperimentID,
                    method_message_id = MethodMessageID,
                    file = m_strFilepath,
                    timestamp = DateTime.Now
                }
            );
            return obj;
        }
 
        public override string ToString()
        {
            return this.ToJObject().ToString();
        }
    }
 
    public class ErrorObjectMessage : PostMessage
    {
        private string m_strMessageType;
        private JObject m_jsonError;
        public new string MessageType { get { return this.m_strMessageType; } }
        public JObject Data { get { return this.m_jsonError; } }
 
        public ErrorObjectMessage(ReqMessage rMessage, JObject obj) : base(rMessage)
        {
            this.m_strMessageType = "error";
            this.m_jsonError = obj;
        }
 
        public override JObject ToJObject()
        {
            return this.m_jsonError;
        }
 
        public override string ToString()
        {
            return this.ToJObject().ToString();
        }
    }
    
}