杨前锦
2025-06-19 d42ad8b01195def2a9309930e14f90ff13ad47b1
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
using HH.WCS.Mobox3.HD.api;
using HH.WCS.Mobox3.HD.core;
using HH.WCS.Mobox3.HD.device;
using HH.WCS.Mobox3.HD.dispatch;
using HH.WCS.Mobox3.HD.models;
using HH.WCS.Mobox3.HD.util;
using HH.WCS.Mobox3.HD.wms;
using Newtonsoft.Json;
using NLog.Fluent;
using S7.Net;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Cryptography;
using static HH.WCS.Mobox3.HD.api.DigitHelper;
using static HH.WCS.Mobox3.HD.api.WmsController;
using static HH.WCS.Mobox3.HD.core.Monitor;
using static HH.WCS.Mobox3.HD.dispatch.ShopFloorControl;
using static HH.WCS.Mobox3.HD.process.TaskProcess;
using static HH.WCS.Mobox3.HD.util.Settings;
using static HH.WCS.Mobox3.HD.wms.WCSHelper;
using static System.Runtime.CompilerServices.RuntimeHelpers;
 
namespace HH.WCS.Mobox3.HD.process {
    internal class TaskProcess {
        #region 任务相关
        //--------------------------------------------------任务相关--------------------------------------------------
        /// <summary>
        /// 取货卸货完成,缓存位状态更新
        /// </summary>
        /// <param name="mst"></param>
        /// <param name="v"></param>
        internal static void CacheBitUpdate(WCSTask mst, bool load) {
            var trayCarryCount = mst.N_CNTR_COUNT > 0 ? mst.N_CNTR_COUNT : 1;
            if (load) {
                Console.WriteLine($"任务{mst.S_CODE} 货位{mst.S_START_LOC}取货完成,起点解绑容器{mst.S_CNTR_CODE}");
                LogHelper.Info($"任务{mst.S_CODE} 货位{mst.S_START_LOC}取货完成,起点解绑容器{mst.S_CNTR_CODE}");
                LocationHelper.UnBindingLoc(mst.S_START_LOC, mst.S_CNTR_CODE.Split(',').ToList());
            }
            else {
                Console.WriteLine($"任务{mst.S_CODE} 货位{mst.S_END_LOC}卸货完成,终点绑定容器{mst.S_CNTR_CODE}");
                LogHelper.Info($"任务{mst.S_CODE} 货位{mst.S_END_LOC}卸货完成,起点解绑容器{mst.S_CNTR_CODE}");
                LocationHelper.BindingLoc(mst.S_END_LOC, mst.S_CNTR_CODE.Split(',').ToList());
            }
 
        }
 
        /// <summary>
        /// 任务取消,缓存位状态更新
        /// </summary>
        /// <param name="mst"></param>
        internal static void CacheBitCancelUpdate(WCSTask mst) {
            //任务取消,取货完成前的,起点的loadingCount和终点unLoadingCount都清除,取货完成的只处理终点
            if (WCSHelper.CheckActionRecordExist(mst.S_CODE, 4)) {
                //根据客户现场要求,如果取货完成任务失败人工拉到终点,我们就当卸货完成处理;如果是人工拉走到其它区域,我们就解锁终点,删除托盘。
                //终点绑定
                CacheBitUpdate(mst, false);
                LocationHelper.UnLockLoc(mst.S_END_LOC);
            }
            else {
                //起点终点解锁
                LocationHelper.UnLockLoc(mst.S_START_LOC);
                LocationHelper.UnLockLoc(mst.S_END_LOC);
                //货位容器解绑
                LocationHelper.UnBindingLoc(mst.S_START_LOC, new List<string> { mst.S_CNTR_CODE });
 
            }
 
        }
 
        /// <summary>
        /// 安全请求
        /// </summary>
        /// <param name="no"></param>
        /// <param name="state"></param>
        /// <param name="forkliftNo"></param>
        /// <param name="extData"></param>
        internal static void OperateReq(string no, int state, string forkliftNo, string extData) {
            if (state == 1101) {
                //请求取货,
            }
            if (state == 1102) {
                //请求卸货,
                //根据终点判断,是cb02的入口,判断内存中状态(要状态时间),允许卸货,通知agv改参数
                var dic = new Dictionary<string, string>();
                //< Req >< Order No = 'TN2302020002' ParamNo = '18' Param1 = '12' /></ Req >
                dic.Add("No", no);
                dic.Add("ParamNo", "8");
                dic.Add("Param1", "1");
                NDC.ChangeOrder(dic);
                //改完参数车子就会自己卸货
            }
            if (state == 1103) {
                //大铁框叉走以后通知,我们要通知输送线
            }
        }
 
        /// <summary>
        /// 任务拦截
        /// </summary>
        /// <param name="mst"></param>
        /// <returns></returns>
        internal static bool Intercept(WCSTask mst) {
            var result = true;
            if (! mst.S_TYPE.Equals("移库")) {
                List<WCSTask> wcsTasks = WCSHelper.GetTaskListBySrcNo(mst.S_OP_CODE);
                foreach (var task in wcsTasks)
                {
                    if (task.N_PRIORITY == 9 && task.N_B_STATE < 3)
                    {
                        result = false;
                    }
                }
            }
            return result;
        }
 
        /// <summary>
        /// 任务状态更新处理
        /// </summary>
        /// <param name="mst"></param>
        /// <param name="state"></param>
        internal static void OperateStatus(WCSTask mst, int state) {
            if (state == 4) {
                CacheBitUpdate(mst, true);
            }
            if (state == 6)//卸货完成
            {
                CacheBitUpdate(mst, false);
            }
            if (state == 7) {
                CacheBitCancelUpdate(mst);
            }
        }
 
        private static object locLocker = new object();
 
        /// <summary>
        /// 堆叠库区出入库任务申请
        /// </summary>
        /// <param name="ls"></param>
        /// <param name="cntrs"></param>
        /// <param name="area"></param>
        /// <param name="itemCode"></param>
        /// <param name="itemBatch"></param>
        /// <param name="taskType"></param>
        /// <param name="insStock"></param>
        /// <returns></returns>
        internal static bool ApplyTN_Task(Location ls, ref List<string> cntrs, string area, string itemCode, string itemBatch, string taskType, bool insStock = true) {
            var result = false;
            lock (locLocker) {
                try {
                    if (insStock) {
                        Console.WriteLine($"MoboxHelperCreateTask: {area}-{itemCode}-{itemBatch}-{taskType}");
                        var endTN_Location = GetLocation4In(area, itemCode, itemBatch, 3);
                        if (endTN_Location != null) {
                            var endLayer = endTN_Location.N_CURRENT_NUM == 0 ? 1 : 2;
                            var taskNo = DateTime.Now.Ticks.ToString();
                            result = TaskProcess.CreateTransport(ls.S_CODE, endTN_Location.S_CODE, taskType, cntrs, 1, endLayer, 3, 70);
                        }
                        else {
                            Console.WriteLine($"MoboxHelperCreateTask: 未找到终点货位");
                        }
                    }
                    else {
                        var startTN_Location = GetLocation4Out(area, itemCode, itemBatch, 3);
                        if (startTN_Location != null) {
                            var startLayer = startTN_Location.N_CURRENT_NUM <= 3 ? 1 : 2;
                            var taskNo = DateTime.Now.Ticks.ToString();
                            var carryCount = startTN_Location.N_CURRENT_NUM > 3 ? startTN_Location.N_CURRENT_NUM - 3 : startTN_Location.N_CURRENT_NUM;
                            //出库要从起点获取托盘
                            var cntrList = LocationHelper.GetLocCntr(startTN_Location.S_CODE);
                            if (cntrList.Count == startTN_Location.N_CURRENT_NUM) {
                                cntrs = cntrList.OrderByDescending(a => a.T_CREATE).Take(carryCount).Select(a => a.S_CNTR_CODE).ToList();
                                result = TaskProcess.CreateTransport(startTN_Location.S_CODE, ls.S_CODE, taskType, cntrs, startLayer, 1, carryCount, 65);
                            }
                            else {
                                Console.WriteLine($"起点托盘数量和货位容器表不符合,请检查【货位表】和【货位容器表】");
                            }
 
                        }
                    }
                }
                catch (Exception ex) {
 
                    Console.WriteLine("MoboxHelperCreateTask:" + ex.Message);
                    LogHelper.Error("MoboxHelperCreateTask:" + ex.Message, ex);
                }
            }
            return result;
 
 
        }
 
        private static Location GetLocation4Out(string area, string itemCode, string itemBatch, int v) {
            throw new NotImplementedException();
        }
 
        private static Location GetLocation4In(string area, string itemCode, string itemBatch, int v) {
            throw new NotImplementedException();
        }
 
        /// <summary>
        /// 普通货架区的出入库申请
        /// </summary>
        /// <param name="ls"></param>
        /// <param name="cntrs"></param>
        /// <param name="area"></param>
        /// <param name="taskType"></param>
        /// <param name="itemCode"></param>
        /// <param name="insStock"></param>
        /// <returns></returns>
        internal static bool ApplyNormalTN_Task(Location ls, ref List<string> cntrs, string area, string taskType, string itemCode, bool insStock = true) {
            var result = false;
            lock (locLocker) {
                try {
                    if (insStock) {
                        Console.WriteLine($"MoboxHelperCreateTask: {area}-{taskType}");
                        var endTN_Location = new Location();
                        if (endTN_Location != null) {
                            var taskNo = DateTime.Now.Ticks.ToString();
                            result = TaskProcess.CreateTransport(ls.S_CODE, endTN_Location.S_CODE, taskType, cntrs, 70);
                        }
                        else {
                            Console.WriteLine($"MoboxHelperCreateTask: 未找到终点货位");
                        }
                    }
                    else {
                        var startTN_Location = new Location();
                        if (startTN_Location != null) {
                            //出库要从起点获取托盘
                            var cntrList = LocationHelper.GetLocCntr(startTN_Location.S_CODE);
                            if (cntrList.Count == startTN_Location.N_CURRENT_NUM) {
                                result = TaskProcess.CreateTransport(startTN_Location.S_CODE, ls.S_CODE, taskType, new List<string> { cntrList[0].S_CNTR_CODE }, 65);
                            }
                            else {
                                Console.WriteLine($"起点托盘数量和货位容器表不符合,请检查【货位表】和【货位容器表】");
                            }
                        }
                    }
                }
                catch (Exception ex) {
 
                    Console.WriteLine("MoboxHelperCreateTask:" + ex.Message);
                    LogHelper.Error("MoboxHelperCreateTask:" + ex.Message, ex);
                }
            }
            return result;
        }
       
 
        /// <summary>
        /// 推送任务
        /// </summary>
        /// <param name="mst"></param>
        internal static bool SendTask(WCSTask mst) {
            var result = true;
            var startLoc = LocationHelper.GetLocation(mst.S_START_LOC);
            var endLoc = LocationHelper.GetLocation(mst.S_END_LOC);
            if (mst.N_B_STATE == 0) {
                if (mst.S_SCHEDULE_TYPE == "RB")
                {
                    var itemOutLoc = getOutLocation(mst.S_START_LOC);
                    var itemInLoc = getOutLocation(mst.S_END_LOC);
                    if (itemOutLoc != null && itemOutLoc.N_CURRENT_NUM > 0)
                    {
                        if (itemOutLoc.N_LOCK_STATE == 0) {
                            ApiHelper.shiftStock(itemOutLoc, mst.S_OP_CODE);
                        }
                    }
                    else if(itemInLoc != null && itemInLoc.N_CURRENT_NUM > 0)
                    {
                        if (itemInLoc.N_LOCK_STATE == 0)
                        {
                            ApiHelper.shiftStock(itemInLoc, mst.S_OP_CODE);
                        }
                    }
                    else
                    {
                        // 发送堆垛机任务数据
                        LogHelper.Info("推送堆垛机任务,任务号:" + mst.S_CODE, "哈电");
                        if (sendDDJTaskData(mst))
                        {
                            mst.N_B_STATE = 1;
                            WCSHelper.UpdateStatus(mst);
                        }
                    }
                }
                else if (mst.S_SCHEDULE_TYPE == "HA")
                {
                    //调第三方接口
                    var model = new HanAo.TaskInfoModel
                    {
                        requestPk = mst.S_CODE,
                        frmPos = mst.S_START_LOC,
                        toPos = mst.S_END_LOC,
                        trkType = mst.S_OP_NAME == "入库" ? "1" : "2",
                        contNo = mst.S_CNTR_CODE
                    };
                    if (HanAo.CreateOrder(model))
                    {
                        mst.N_B_STATE = 1;
                        WCSHelper.UpdateStatus(mst);
                    }
                }
                else if (mst.S_SCHEDULE_TYPE == "HL" && endLoc != null) 
                {
                    var model = new HL.TaskInfoModel
                    {
                        orderId = mst.S_CODE,
                        operateType = mst.S_OP_NAME == "入库" ? "10" : "20",
                        trayCode = mst.S_CNTR_CODE,
                        fetchPosition = startLoc.S_CODE,
                        fetchLevel = startLoc.N_LAYER.ToString(),
                        deliverPosition = endLoc.S_CODE,
                        deliverLevel = endLoc.N_LAYER.ToString(),
                        priority = mst.N_PRIORITY.ToString(),
                        source = "wms",
                    };
                    if (mst.S_TYPE.Contains("钢卷")) {
                        var cntrItemRels = ItemHelper.GetCntrItemByCntrCode(mst.S_CNTR_CODE);
                        model.coilNo = cntrItemRels[0].S_COIL_NO;
                        model.shootNo = cntrItemRels[0].S_SHOOT_NO;
                        model.heatNo = cntrItemRels[0].S_HEAT_NO;
                        model.spec = cntrItemRels[0].S_ITEM_SPEC;
                    }
                    else if (mst.S_TYPE.Contains("模具"))
                    {
                        var mouldCntr = MouldHelper.GetMouldCntr(new models.MouldCntr() { S_CNTR_CODE = mst.S_CNTR_CODE });
                        model.mouldTyep = mouldCntr.S_MOULD_TYPE;
                        model.mouldNo = mouldCntr.S_MOULD_NO;
                    }
 
                    if (HL.CreateOrder(model))
                    {
                        mst.N_B_STATE = 1;
                        WCSHelper.UpdateStatus(mst);
                    }
                } else if (mst.S_SCHEDULE_TYPE == "LD") {
 
                    TransferTiledInfo transferTiledInfo = new TransferTiledInfo()
                    {
                        businessType = "station_to_station",
                        comment = mst.S_TYPE,
                        endStationCode = mst.S_END_LOC,
                        endAreaCode = mst.S_END_AREA,
                        startStationCode = mst.S_START_LOC,
                        startAreaCode = mst.S_START_AREA
                    };
                    var cntrItems = ItemHelper.GetCntrItemByCntrCode(mst.S_CNTR_CODE);
                    if (cntrItems != null && cntrItems.Count > 0) {
                        transferTiledInfo.workCode = cntrItems[0].S_WORK_NO;
                        transferTiledInfo.setCode = cntrItems[0].S_DEPART_NO;
                        transferTiledInfo.partDrawingCode = cntrItems[0].S_PARTDRAW_NO;
                        transferTiledInfo.processCode = cntrItems[0].S_OPER_NO;
                    }
                    // 物流调度 TODO logisticsTaskId != null
                    string logisticsTaskId = ShopFloorControl.transferTiled(transferTiledInfo);
                    if (logisticsTaskId != null)
                    {
                        WMSTask wmsTask = WMSHelper.GetWmsTask(mst.S_OP_CODE);
                        wmsTask.S_OP_DEF_CODE = logisticsTaskId;
                        WMSHelper.UpdateTask(wmsTask);
 
                        mst.N_B_STATE = 1;
                        WCSHelper.UpdateStatus(mst);
                    };
                }
            }
            return result;
        }
 
        public static Location getOutLocation(string locCode) {
            var db = new SqlHelper<object>().GetInstance();
            Location loc = LocationHelper.GetLoc(locCode);
            if (loc != null && loc.N_DEEP == 2 && loc.N_POS == 1) {
               return db.Queryable<Location>().Where(it => it.S_AREA_CODE == loc.S_AREA_CODE && (it.N_ROW == loc.N_ROW + 1 || it.N_ROW == loc.N_ROW - 1) && it.N_COL == loc.N_COL && it.N_LAYER == loc.N_LAYER && it.N_ROADWAY == loc.N_ROADWAY && it.N_POS == 2).First();
            }
            return null;
        }
 
 
        internal static bool sendDDJTaskData(WCSTask mst)
        {
            var startLoc = LocationHelper.GetLocation(mst.S_START_LOC);
            var endLoc = LocationHelper.GetLocation(mst.S_END_LOC);
 
            // 1.查询对应输送线的堆垛机是否可入库、出库
            bool allow = false;
            if (mst.S_TYPE.Contains("入库"))
            {
                LinePlcInfo linePlcInfo = WCSHelper.GetLinePlcInfo(mst.S_START_LOC);
                PipelineSignalInfo lineSignalInfo = WCSHelper.readPipelineInfo(linePlcInfo);
                LogHelper.Info("堆垛机任务推送 ==> 检测输送线堆垛机信息,线体:" + linePlcInfo.code + ",(入 ddjInfo = 2)信息:" + JsonConvert.SerializeObject(lineSignalInfo), "哈电");
                if (lineSignalInfo != null && lineSignalInfo.ddjInfo == 2 || mst.S_ERR != null)
                {
                    endLoc.N_ROW = rowChange(endLoc.N_ROW);
                    startLoc.N_LAYER = 0;
                    startLoc.N_COL = linePlcInfo.code;
                    allow = true;
                }
            }
            else if (mst.S_TYPE.Contains("出库"))
            {
                LinePlcInfo linePlcInfo = WCSHelper.GetLinePlcInfo(mst.S_END_LOC);
                PipelineSignalInfo lineSignalInfo = WCSHelper.readPipelineInfo(linePlcInfo);
                LogHelper.Info("堆垛机任务推送 ==> 检测输送线堆垛机信息,线体:" + linePlcInfo.code + ",(出 ddjInfo = 1)信息:" + JsonConvert.SerializeObject(lineSignalInfo), "哈电");
                if (lineSignalInfo != null && lineSignalInfo.ddjInfo == 1 || mst.S_ERR != null)
                {
                    startLoc.N_ROW = rowChange(startLoc.N_ROW);
                    endLoc.N_LAYER = 0;
                    endLoc.N_COL = linePlcInfo.code;
                    allow = true;
                }
            }
 
            if (mst.S_TYPE.Contains("移库")) {
                startLoc.N_ROW = rowChange(startLoc.N_ROW);
                endLoc.N_ROW = rowChange(endLoc.N_ROW);
                allow = true;
            }
 
            string plc = mst.S_EQ_NO;
            if (plc != null)
            {
                short[] resSignal = S7Helper.ReadInt(plc, 551, 0, 8);
                int taskNo1 = S7Helper.ReadDint(plc, 551, 16);
                int taskNo2 = S7Helper.ReadDint(plc, 550, 24);
 
                LogHelper.Info(plc + "号堆垛机任务推送 ==> 条件:taskNo1(读)= 0 ,taskNo2(写)= 0 ;系统任务号:" + mst.S_CODE + ",taskNo1:" + JsonConvert.SerializeObject(taskNo1)  + " taskNo2:" + JsonConvert.SerializeObject(taskNo2), "哈电");
                LogHelper.Info(plc + "号堆垛机任务推送 ==> 条件:resSignal[2] = 1,resSignal[3] = 1,resSignal[4] = 1;推送前堆垛机信号:" + JsonConvert.SerializeObject(resSignal), "哈电");
                // 判断是否可发送堆垛机任务
                if (allow  && taskNo1 == 0  && taskNo2 == 0 && resSignal != null && resSignal[2] == 1 && resSignal[3] == 1 && resSignal[4] == 1)
                {
                    // 3.写入堆垛机任务
                    short HandShake = 0;
                    short handShakeValues = S7Helper.ReadInt(plc, 551, 0);
                    if (handShakeValues != null)
                    {
                        if (handShakeValues == 0)
                        {
                            HandShake = 1;
                        }
                        else if (handShakeValues == 1)
                        {
                            HandShake = 0;
                        }
                    }
                    LogHelper.Info("hhhhhhhhhhh", "哈电");
                    // 正常任务 命令 1.搬运 重置任务 命令 9.仅放货
                    short commandType = 1;
                    if (mst.S_ERR != null) {
                        commandType = 9;
                    }
                   
                    // TODO s7 wcs -> plc 写入任务
                    try 
                    {
                        RBWriteTaskInfo rBWriteTaskInfo = new RBWriteTaskInfo()
                        {
                            HandShake = HandShake,
                            SRM_Num = short.Parse(mst.S_EQ_NO),
                            CommandType = commandType,
                            Source_Z_S1 = (short)startLoc.N_ROW,
                            Source_X_S1 = (short)startLoc.N_COL,
                            Source_Y_S1 = (short)startLoc.N_LAYER,
                            Source_L_S1 = (short)startLoc.N_POS,
                            Dest_Z_S1 = (short)endLoc.N_ROW,
                            Dest_X_S1 = (short)endLoc.N_COL,
                            Dest_Y_S1 = (short)endLoc.N_LAYER,
                            Dest_L_S1 = (short)endLoc.N_POS,
                            Task_S1 = int.Parse(mst.S_CODE.Substring(8)),                    // TODO 任务号待确定
                            ConfirmSignal1 = 1,
                        };
                        RBWriteTask(plc, rBWriteTaskInfo);
 
                        Console.WriteLine("堆垛机任务信号写入成功:" + JsonConvert.SerializeObject(rBWriteTaskInfo));
                        LogHelper.Info("堆垛机任务信号写入成功:" + JsonConvert.SerializeObject(rBWriteTaskInfo), "哈电");
                    }
                    catch(Exception e) 
                    {
                        LogHelper.Info("堆垛机任务信号写入错误,错误原因:" + e.Message, "哈电");
                        return false;
                    }
                    return true;
                }
            }
            else {
                Console.WriteLine("设备号不能为空");
            }
            return false;
        }
        public static int rowChange(int row) {
            switch (row) {
                case 5:
                    row = 1;
                    break;
                case 6:
                    row = 2;
                    break;
                case 7:
                    row = 3;
                    break;
            }
            return row;
        }
 
 
        /// <summary>
        /// 堆垛机任务写入(推送任务)
        /// </summary>
        public static void RBWriteTask(string plc ,RBWriteTaskInfo model)
        {
            S7Helper.WriteInt(plc,550, 0, model.HandShake);
            S7Helper.WriteInt(plc, 550, 2, model.SRM_Num);
            S7Helper.WriteInt(plc, 550, 4, model.CommandType);
            S7Helper.WriteInt(plc, 550, 6, model.FireCommandType);
            S7Helper.WriteInt(plc, 550, 8, model.Source_Z_S1);
            S7Helper.WriteInt(plc, 550, 10, model.Source_X_S1);
            S7Helper.WriteInt(plc, 550, 12, model.Source_Y_S1);
            S7Helper.WriteInt(plc, 550, 14, model.Source_L_S1);
            S7Helper.WriteInt(plc, 550, 16, model.Dest_Z_S1);
            S7Helper.WriteInt(plc, 550, 18, model.Dest_X_S1);
            S7Helper.WriteInt(plc, 550, 20, model.Dest_Y_S1);
            S7Helper.WriteInt(plc, 550, 22, model.Dest_L_S1);
            S7Helper.WriteDint(plc, 550, 24, model.Task_S1);
            S7Helper.WriteInt(plc, 550, 28, model.ConfirmSignal1);
            S7Helper.WriteInt(plc, 550, 30, model.GoodType1);
        }
 
        public class RBWriteTaskInfo {
            //WCS与堆垛机握手信号,WCS每个周期将HandShake的值取反写入HandShake,3秒内握手信号未发生改变认为通讯中断
            public short HandShake { get; set; } 
            public short SRM_Num { get; set; } //堆垛机编号
 
            //1:工位1搬运;4: 工位1移动(仅移动到目标地址);7 :工位1仅取货;9 :工位1仅放货;12:删除任务13:复位
            public short CommandType { get; set; } 
            public short FireCommandType { get; set; } // 消防备用
            public short Source_Z_S1 { get; set; } // 取货排 1/2/3/4
            public short Source_X_S1 { get; set; } // 取货列
            public short Source_Y_S1 { get; set; } // 取货层
            public short Source_L_S1 { get; set; } // 取货深(保留)
            public short Dest_Z_S1 { get; set; } // 放货排
            public short Dest_X_S1 { get; set; } // 放货列
            public short Dest_Y_S1 { get; set; } // 放货层
            public short Dest_L_S1 { get; set; } // 放货深(保留)
            public int Task_S1 { get; set; } // 任务号
            /// <summary>
            /// 写1时,PLC开始解析工位1任务命令类型及储位信息,注意写1时确保位置信息,命令类型等已写入,否则会出现解析错误或不解析情况
            /// 当收到PLC的完成时(TaskStatus1=3)上位写3确认,PLC收到确认,清除任务状态
            /// 0:默认值;
            /// 1:开始执行任务;
            /// 3:工位1任务正常完成确认;
            /// 8:上位写8为任务删除
            /// 10.收到上位强制完成信号
            /// </summary>
            public short ConfirmSignal1 { get; set; } // 状态码
            public short GoodType1 { get; set; } // 货物类型(备用)
        }
 
 
        private static List<FreeLineInfo> freeLineInfos = new List<FreeLineInfo>();
        internal static freeLineBit GetFreeLineBit(string area, int row, int col) {
            if (freeLineInfos.Count == 0) {
                freeLineInfos = LocationHelper.GetAllFreeLineInfo();
            }
            var res = freeLineInfos.Where(a => a.S_AREA_CODE == area && a.N_ROW == row && col >= a.START_COL && col <= a.END_COL).FirstOrDefault();
            if (res != null) {
                return new freeLineBit { area = area, row = row, minCol = res.START_COL, maxCol = res.END_COL, bit1 = res.FIRST_POINT, bit2 = res.SECOND_POINT };
            }
            return null;
        }
        
        public class freeLineBit {
            public string area { get; set; }
            public int row { get; set; }
            public int minCol { get; set; }
            public int maxCol { get; set; }
            public int bit1 { get; set; }
            public int bit2 { get; set; }
        }
        /// <summary>
        /// 创建搬运任务
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="taskType"></param>
        /// <param name="cntrs"></param>
        /// <param name="startLayer"></param>
        /// <param name="endLayer"></param>
        /// <param name="trayCarryCount"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public static bool CreateTransport(string start, string end, string taskType, List<string> cntrs, int startLayer, int endLayer, int trayCarryCount = 1, int priority = 1) {
            var result = false;
            //批次号存托盘号,1~3个托盘
            var trayCodes = string.Join(",", cntrs);
            var taskNo = DateTime.Now.Ticks.ToString();
 
 
            var res = WCSHelper.CreateTask(taskNo, start, end, taskType, priority, trayCodes, trayCarryCount, startLayer, endLayer);
            if (res) {
                result = true;
                //任务创建成功,起点货位出库锁定,终点货位入库锁定
                LocationHelper.LockLoc(start, 1);
                LocationHelper.LockLoc(end, 2);
            }
            return result;
        }
 
 
        public static bool CreateTransport(string start, string end, string taskType, List<string> cntrs, int priority = 1) {
            var result = false;
            //批次号存托盘号,1~3个托盘
            var trayCodes = string.Join(",", cntrs);
            var taskNo = DateTime.Now.Ticks.ToString();
 
 
            var res = WCSHelper.CreateTask(taskNo, start, end, taskType, priority, trayCodes, 1, 1, 1);
            if (res) {
                result = true;
                LocationHelper.LockLoc(start, 2);
                LocationHelper.LockLoc(end, 1);
 
            }
            return result;
        }
        #endregion
 
 
 
    }
}