111
cjs
2025-06-18 8ab19ae17dc98d3c79f87ca54aa4cdc75d174b12
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
using Hanhe.iWCS.Business;
using Hanhe.iWCS.Common;
using Hanhe.iWCS.Event;
using Hanhe.iWCS.Interface;
using Hanhe.iWCS.MData;
using Hanhe.iWCS.Model;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ERPService;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.PLCControl;
using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ProcessHelper;
 
namespace Hanhe.iWCS.TaizhouGEMTwoProtocol
{
    /// <summary>
    /// 事件监听处理和触发事件回调
    /// </summary>
    public class EnentListen : IEnentListen
    {
        private EquipmentCommandEQBLL CommandEQBLL = new EquipmentCommandEQBLL();
        private ICallTaskProcessing iCallTask = new ICallTaskProcessing();
        static object locker = new object();
        static object lock1027 = new object();
        static object lock10001 = new object();
        static object lock10014 = new object();
        static object lock10015 = new object();
        static object lock10017 = new object();
        static EnentListen() {
        }
        /// <summary>
        /// 事件订阅
        /// </summary>
        /// <param name="currActionModel">当前动作模型</param>
        /// <returns></returns>
        public bool EnentSubscribe(TN_I_TASK_DTL_ACTION currActionModel) {
            try {
                EventDelegate eventDelegate = new EventDelegate();
                eventDelegate.CurrActionModel = currActionModel;
                //eventDelegate.iSleep = 3000;
                eventDelegate.StartListening();
 
                return true;
            }
            catch (Exception ex) {
                throw new Exception(ex.Message);
            }
        }
 
        private SortByDocument sortBy = new SortByDocument { { "createTime", 1 } };
        /// <summary>
        /// 事件监听处理
        /// </summary>
        /// <param name="currActionModel">当前动作模型</param>
        /// <returns>true:条件成立 false:条件不成立</returns>
        public bool MonitorProcess(TN_I_TASK_DTL_ACTION currActionModel) {
            bool bResult = false;
            string msg = string.Empty;
 
            #region   处理任务状态
            if (currActionModel.CN_N_ACTION_CODE == 1027)
            {
                lock (lock1027)
                {
                    var request = MongoDBSingleton.Instance.FindAll<Mongo_AGV_TASK_STATE>();
 
                    if (request.Count > 0)
                    {
                        request.ForEach(a =>
                        {
                            try
                            {
                                CMMLog.Info($"进入workflowToDeviceDriver,CN_S_TASK_NO:{a.TaskNo},CN_N_ACTION_CODE:{a.State},CN_S_DEVICE_CODE:{a.ForkliftNo},Ext2:{a.LockNo},Ext3:{a.ExtData}");
                                new ProtocolAnalysis().workflowToDeviceDriver(new TN_I_TASK_DTL_ACTION { CN_S_TASK_NO = a.TaskNo, CN_N_ACTION_CODE = a.State, CN_S_DEVICE_CODE = a.ForkliftNo, Ext2 = a.LockNo, Ext3 = a.ExtData });
                                MongoDBSingleton.Instance.Remove<Mongo_AGV_TASK_STATE>(Query.EQ("_id", a._id), RemoveFlags.Single);
                            }
                            catch (Exception ex)
                            {
                                CMMLog.Error(ex.Message, ex);
                            }
                        });
                    }
                    Thread.Sleep(1000);
                }
 
            }
            #endregion
 
            #region   3楼设备任务
            #region 10001-读取包装线信号-3楼包装取料--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10001)
            {
                lock (lock10001)
                {
                    var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "1").ToList();
                    if (list.Count > 0)
                    {
                        list.ForEach(a =>
                        {
                            // 包装机启动时读取【翻页通道】,根据页号获取五条数据,然后写入包装机
                            ERPService.WriteItemInfo(a);
 
                            PLCControl.CheckPackingMachine(a);
                        });
                    }
 
                    Thread.Sleep(1500);
                }
            }
            #endregion
 
            #region 10002-读取包装线信号-3楼复称入缓存架--弃用
            //if (currActionModel.CN_N_ACTION_CODE == 10002)
            //{
            //    var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "2").ToList();
            //    if (list.Count > 0)
            //    {
            //        list.ForEach(a =>
            //        {
            //            PLCControl.SecondWeightInCache(a);
            //        });
            //    }
            //    //Thread.Sleep(3000);
            //}
            #endregion
 
            #region   10003-读取叠包机信号(三楼双层缓存架入叠盘机流程)-3楼缓存架入叠托--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10003)
            {
                var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "3" || a.deviceType == "23")).ToList();
                if (list.Count > 0)
                {
                    list.ForEach(a =>
                    {
                        PLCControl.CacheStackingMouth(a);
                    });
                }
                Thread.Sleep(1500);
            }
            #endregion
 
            #region   10004-读包装机信号(包装机补空托流程)-3楼包装补空--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10004)
            {
                bool action = false; //2025/03/19 变更开关 包装机补空流程生成从包装机前得拆盘位到指定包装机
                if (action)
                {
                    var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "1").ToList();
                    if (list.Count > 0)
                    {
                        list.ForEach(a =>
                        {
                            PLCControl.PickUpBlank(a);
                        });
                    }
                }
                else
                {
                    var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "1" || a.deviceType == "21")).ToList();
                    if (list.Count > 0)
                    {
                        list.ForEach(a =>
                        {
                            PLCControl.PickUpBlankTwo(a);
                        });
                    }
                }
 
                Thread.Sleep(1500);
            }
            #endregion
 
            #region 10005-打包线空托补料请求(3楼拆盘机补空托)-3楼拆盘补空--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10005)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "6" || a.deviceType == "24")).FirstOrDefault();
                if (plc != null)
                {
                    if (PickUpEndFree(plc.location))
                    {
                        PLCControl.CheckPackingLineEmpty(plc);
                    }
                }
                //Thread.Sleep(500);
            }
            #endregion
 
            #region 10006-叠盘机满托下线(3楼叠盘下线)-3楼叠盘下线--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10006)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && (a.deviceType == "15" || a.deviceType == "25")).ToList();
                if (plc.Count >0 )
                {
                    foreach(var a in plc)
                    {
                        if (PickUpStartFree(a.location))
                        {
                            PLCControl.StackingLineEmpty(a);
                        }
                    }
                }
                //Thread.Sleep(500);
            }
            #endregion
 
            #region 10007-打包线下线-3楼打包下线--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 10007)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "4").FirstOrDefault();
                if (plc != null)
                {
                    if (CheckStartFree(plc.location))
                    {
                        PLCControl.CheckPackingLineFull(plc);
                    }
                }
                //Thread.Sleep(500);
            }
            #endregion
            #endregion
 
            #region 1000-电梯任务处理--待确认
            if (currActionModel.CN_N_ACTION_CODE == 1000)
            {
                ProcessHelper.CheckElevatorTask();
                //Thread.Sleep(500);
            }
            #endregion
 
            #region   2000-充电任务--无需改动
            if (currActionModel.CN_N_ACTION_CODE == 2000)
            {
                ProcessHelper.AGVBatteryTime();
                Thread.Sleep(500);
                ERPService.deletePackageInfo();
            }
            #endregion
 
            #region   2001-包装机写电量--已完成(待测试)
            if (currActionModel.CN_N_ACTION_CODE == 2001)
            {
                PLCControl.WriteBattery();
            }
            #endregion
 
            #region   10010-任务动作线程
 
            if (currActionModel.CN_N_ACTION_CODE == 10010)
            {
                lock (locker)
                {
                    var request = MongoDBSingleton.Instance.FindAll<ActionControlModel>();
 
                    if (request.Count > 0)
                    {
                        request.ForEach(a =>
                        {
                            try
                            {
                                if (a.actionNo == "1") SecondWeightActionOne(a);
                            }
                            catch (Exception ex)
                            {
                                CMMLog.Error($"10010" + ex.Message, ex);
                            }
                        });
                    }
                }
                
                Thread.Sleep(3000);
            }
 
            #endregion
 
            #region 10012-荆门变更
            if (currActionModel.CN_N_ACTION_CODE == 10012)
            {
                PLCControl.changeSignal();
            }
            #endregion
 
            #region 10013-ERP变更功能
            if (currActionModel.CN_N_ACTION_CODE == 10013)
            {
                ERPService.SendERPTaskCompleteFunc();
            }
            #endregion
 
            #region 10017-给电梯发送心跳指令
            if (currActionModel.CN_N_ACTION_CODE == 10018)
            {
                lock (lock10017)
                {
                    PLCControl.SendHeartBeat();
                }
            }
            #endregion
 
            #region 四钴车间
 
            #region 10014-读取包装线信号-3楼包装取料--开发中
            if (currActionModel.CN_N_ACTION_CODE == 10014)
            {
                lock (lock10014)
                {
                    var list = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "21").ToList();
                    if (list.Count > 0)
                    {
                        list.ForEach(a =>
                        {
                            // 包装机启动时读取【翻页通道】,根据页号获取五条数据,然后写入包装机
                            //ERPService.WriteItemInfoTwo(a);
 
                            PLCControl.CheckPackingMachineTetracobalt(a);
                        });
                    }
 
                    Thread.Sleep(1500);
                }
            }
            #endregion
 
            #region 10015-ERP变更功能 将物料表数据插入中间表
            if (currActionModel.CN_N_ACTION_CODE == 10015)
            {
                lock (lock10015)
                {
                    //ERPService.insertMidTable();
                }
            }
            #endregion
 
            #region 10016-打包线下线-3楼打包下线--已完成(待测试)
            //四钴车间打包下线物料信息需要从下线口获取
            if (currActionModel.CN_N_ACTION_CODE == 10016)
            {
                var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "26").FirstOrDefault();
                if (plc != null)
                {
                    if (CheckStartFree(plc.location))
                    {
                        PLCControl.CheckPackingLineFullThree(plc);
                    }
                }
                //Thread.Sleep(500);
            }
            #endregion
 
            #endregion
            return bResult;
        }
 
 
 
        /// <summary>
        /// 触发回调处理
        /// </summary>
        /// <param name="currActionModel">当前动作模型</param>
        /// <returns></returns>
        public bool EventCallbacks(TN_I_TASK_DTL_ACTION currActionModel) {
            bool bResult = true;
            //测试码检测
            if (currActionModel.CN_N_ACTION_CODE == 99999) {
            }
            return bResult;
        }
 
        private DateTime GetTime(int hour, int min = 0,int second = 0) {
            var now = DateTime.Today;
            var dt = new DateTime(now.Year, now.Month, now.Day, hour, min, second);
            Console.WriteLine(dt.ToString());
            return dt;
        }
        public class Mongo_AGV_TASK_STATE
        {
            public ObjectId _id { get; set; }
            public int State { get; set; }
            public string TaskNo { get; set; }
            public string ForkliftNo { get; set; }
            public string LockNo { get; set; }
            /// <summary>
            /// 扩展参数 可接收任何变量目前对应ext3
            /// </summary>
            public string ExtData { get; set; }
        }
 
    }
}