自动化立体仓库 - WCS系统
Junjie
2023-07-24 56a19be6c03e729b112bf8197960d7cc4d9d498e
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
package com.zy.core.thread;
 
import HslCommunication.Core.Transfer.DataFormat;
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.ModBus.ModbusTcpNet;
import com.alibaba.fastjson.JSON;
import com.core.common.DateUtils;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.NavigateNode;
import com.zy.common.model.enums.NavigationMapType;
import com.zy.common.utils.*;
import com.zy.core.News;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.*;
import com.zy.core.model.ShuttleSlave;
import com.zy.core.model.Task;
import com.zy.core.model.command.*;
import com.zy.core.model.protocol.LiftProtocol;
import com.zy.core.model.protocol.ShuttleProtocol;
import com.zy.core.model.protocol.StaProtocol;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 四向穿梭车线程
 */
@Data
@Slf4j
public class ShuttleThread implements  Runnable, ThreadHandler {
 
    private ModbusTcpNet modbusTcpNet;
    private ShuttleSlave slave;
    private ShuttleProtocol shuttleProtocol;
    private RedisUtil redisUtil;
 
    public ShuttleThread(ShuttleSlave slave,RedisUtil redisUtil) {
        this.slave = slave;
        this.redisUtil = redisUtil;
    }
 
    @Override
    public void run() {
        this.connect();
        while (true) {
            try {
                int step = 1;
                Task task = MessageQueue.poll(SlaveType.Shuttle, slave.getId());
                if (task != null) {
                    step = task.getStep();
                }
                switch (step) {
                    // 读数据
                    case 1:
                        read();
                        break;
                    // 写入数据
                    case 2:
                        write((ShuttleCommand) task.getData());
                        break;
                    //下发任务
                    case 3:
                        assignWork((ShuttleAssignCommand) task.getData());
                        break;
                    default:
                        break;
                }
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    @Override
    public boolean connect() {
        boolean result = false;
        //-------------------------四向穿梭车连接方法------------------------//
        modbusTcpNet = new ModbusTcpNet(slave.getIp(), slave.getPort(), (byte) 0x01);
        // 当你需要指定格式的数据解析时,就需要设置下面的这个信息
        modbusTcpNet.setDataFormat(DataFormat.ABCD);
        OperateResult connect = modbusTcpNet.ConnectServer();
        if(connect.IsSuccess){
            result = true;
            OutputQueue.CRN.offer(MessageFormat.format( "【{0}】四向穿梭车plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.info("四向穿梭车plc连接成功 ===>> [id:{}] [ip:{}] [port:{}] ", slave.getId(), slave.getIp(), slave.getPort());
        } else {
            OutputQueue.CRN.offer(MessageFormat.format("【{0}】四向穿梭车plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            log.error("四向穿梭车plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}] ", slave.getId(), slave.getIp(), slave.getPort());
        }
        modbusTcpNet.ConnectClose();
        //-------------------------四向穿梭车连接方法------------------------//
        return result;
    }
 
    @Override
    public void close() {
        modbusTcpNet.ConnectClose();
    }
 
    private void read() {
        try {
            readStatus();
            //四向穿梭车空闲、有任务、标记为true、存在任务指令,需要执行任务的下一条指令
            if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE
                    && shuttleProtocol.getTaskNo() != 0
                    && shuttleProtocol.getPakMk()) {
                //执行下一步指令
                executeWork(shuttleProtocol.getTaskNo());
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            initShuttle();
        }
    }
 
    private void readStatus() {
        try {
            OperateResultExOne<byte[]> result = modbusTcpNet.Read("200", (short) 17);
            if (result.IsSuccess) {
                if (null == shuttleProtocol) {
                    shuttleProtocol = new ShuttleProtocol();
                    shuttleProtocol.setShuttleNo(slave.getId().shortValue());
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                }
 
                //----------读取四向穿梭车状态-----------
                //获取数据
                byte[] content = result.Content;
                //小车忙状态位
                shuttleProtocol.setBusyStatus(modbusTcpNet.getByteTransform().TransInt16(content,0));
                //当前二维码
                shuttleProtocol.setCurrentCode(modbusTcpNet.getByteTransform().TransInt16(content,2));
                //电池电量百分比
                shuttleProtocol.setBatteryPower(modbusTcpNet.getByteTransform().TransInt16(content,4));
                //电池温度
                shuttleProtocol.setBatteryTemp(modbusTcpNet.getByteTransform().TransUInt16(content, 6));
                //错误编号
                shuttleProtocol.setErrorCode(modbusTcpNet.getByteTransform().TransInt16(content,8));
                //Plc输出状态IO
                int plcOutIo = modbusTcpNet.getByteTransform().TransUInt16(content, 10);
                int[] plcOutIos = CommonUtils.byteToBits((byte) plcOutIo);
                shuttleProtocol.setPlcOutputLift(plcOutIos[1] == 1);
                shuttleProtocol.setPlcOutputTransfer(plcOutIos[2] == 1);
                shuttleProtocol.setPlcOutputBrake(plcOutIos[3] == 1);
                shuttleProtocol.setPlcOutputCharge(plcOutIos[4] == 1);
                shuttleProtocol.setPlcOutputStatusIO(modbusTcpNet.getByteTransform().TransInt16(content, 10));
                //错误信息码
                shuttleProtocol.setStatusErrorCode(modbusTcpNet.getByteTransform().TransInt16(content,12));
                int plcInIo = modbusTcpNet.getByteTransform().TransUInt16(content, 14);
                int[] plcInIos = CommonUtils.byteToBits((byte) plcInIo);
                //PLC输入状态
                shuttleProtocol.setPlcInputStatus((short) plcInIos[6]);
                //当前或者之前读到的二维码值
                shuttleProtocol.setCurrentOrBeforeCode(modbusTcpNet.getByteTransform().TransInt16(content,16));
                //读到的二维码X方向偏移量
                shuttleProtocol.setCodeOffsetX(modbusTcpNet.getByteTransform().TransInt16(content,18));
                //读到的二维码Y方向偏移量
                shuttleProtocol.setCodeOffsetY(modbusTcpNet.getByteTransform().TransInt16(content,20));
                //当前的电压值
                shuttleProtocol.setCurrentVoltage(modbusTcpNet.getByteTransform().TransUInt16(content, 22));
                //当前的模拟量值
                shuttleProtocol.setCurrentAnalogValue(modbusTcpNet.getByteTransform().TransInt16(content,24));
                //当前的升降伺服速度
                shuttleProtocol.setCurrentLiftServoSpeed(modbusTcpNet.getByteTransform().TransInt16(content,26));
                //当前的行走伺服速度
                shuttleProtocol.setCurrentMoveServoSpeed(modbusTcpNet.getByteTransform().TransInt16(content,28));
                //当前的升降伺服负载率
                shuttleProtocol.setCurrentLiftServoLoad(modbusTcpNet.getByteTransform().TransInt16(content,30));
                //当前的行走伺服负载率
                shuttleProtocol.setCurrentMoveServoLoad(modbusTcpNet.getByteTransform().TransInt16(content,32));
 
                ///读取四向穿梭车状态-end
 
                //小车处于忙碌状态,将标记置为true
                if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.BUSY) {
                    shuttleProtocol.setPakMk(true);
                }
 
                //将四向穿梭车状态保存至数据库
                BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class);
                BasShuttle basShuttle = shuttleService.selectById(slave.getId());
                if (basShuttle == null) {
                    basShuttle = new BasShuttle();
                    //四向穿梭车号
                    basShuttle.setShuttleNo(slave.getId());
                    shuttleService.insert(basShuttle);
                }
                //小车忙状态位
                basShuttle.setBusyStatus(shuttleProtocol.getBusyStatus().intValue());
                //当前二维码
                basShuttle.setCurrentCode(shuttleProtocol.getCurrentCode().intValue());
                //电池电量百分比
                basShuttle.setBatteryPower(shuttleProtocol.getBatteryPower().intValue());
                //电池温度
                basShuttle.setBatteryTemp(shuttleProtocol.getBatteryTemp().intValue());
                //错误编号
                basShuttle.setErrorCode(shuttleProtocol.getErrorCode().intValue());
                //Plc输出状态IO
                basShuttle.setPlcOutputStatusIo(shuttleProtocol.getPlcOutputStatusIO().intValue());
                //错误信息码
                basShuttle.setStatusErrorCode(shuttleProtocol.getStatusErrorCode().intValue());
                //PLC输入状态
                basShuttle.setPlcInputStatus(shuttleProtocol.getPlcInputStatus().intValue());
                //当前或者之前读到的二维码值
                basShuttle.setCurrentOrBeforeCode(shuttleProtocol.getCurrentOrBeforeCode().intValue());
                //读到的二维码X方向偏移量
                basShuttle.setCodeOffsetX(shuttleProtocol.getCodeOffsetX().intValue());
                //读到的二维码Y方向偏移量
                basShuttle.setCodeOffsetY(shuttleProtocol.getCodeOffsetY().intValue());
                //当前的电压值
                basShuttle.setCurrentVoltage(shuttleProtocol.getCurrentVoltage().intValue());
                //当前的模拟量值
                basShuttle.setCurrentAnalogValue(shuttleProtocol.getCurrentAnalogValue().intValue());
                //当前的升降伺服速度
                basShuttle.setCurrentLiftServoSpeed(shuttleProtocol.getCurrentLiftServoSpeed().intValue());
                //当前的行走伺服速度
                basShuttle.setCurrentMoveServoSpeed(shuttleProtocol.getCurrentMoveServoSpeed().intValue());
                //当前的升降伺服负载率
                basShuttle.setCurrentLiftServoLoad(shuttleProtocol.getCurrentLiftServoLoad().intValue());
                //当前的行走伺服负载率
                basShuttle.setCurrentMoveServoLoad(shuttleProtocol.getCurrentMoveServoLoad().intValue());
                //当前小车状态(内部自我维护)
                basShuttle.setShuttleStatus(shuttleProtocol.getProtocolStatus());
                //任务号
                basShuttle.setWrkNo(shuttleProtocol.getTaskNo().intValue());
                //修改时间
                basShuttle.setUpdateTime(new Date());
                //作业标记
                basShuttle.setPakMk(shuttleProtocol.getPakMk());
                if (shuttleService.updateById(basShuttle)) {
                    OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
//                    log.info(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId()));
                }
 
            }else {
                OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】{1}四向穿梭车plc状态信息失败", DateUtils.convert(new Date()), slave.getId()));
                throw new CoolException(MessageFormat.format( "四向穿梭车plc状态信息失败 ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            initShuttle();
        }
    }
 
    private boolean write(ShuttleCommand command){
        if (null == command) {
            News.error("四向穿梭车写入命令为空");
            return false;
        }
        BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class);
        if (shuttleService == null) {
            News.error("系统错误");
            return false;
        }
 
        BasShuttle basShuttle = shuttleService.selectById(slave.getId().shortValue());
        if (basShuttle == null) {
            News.error("四向穿梭车不存在");
            return false;
        }
        command.setShuttleNo(slave.getId().shortValue());
        short[] array = getCommandArr(command);//获取命令报文
        OperateResult result = modbusTcpNet.Write("0", array);
        if (result != null && result.IsSuccess) {
            News.info("四向穿梭车命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command));
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command)));
 
            try {
                Thread.sleep(3000);//命令下发后休眠
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
 
            for (int i = 0; i < 5; i++) {
                if (command.getCommandWord().intValue() == 5 || command.getCommandWord().intValue() == 6) {
                    break;//充电开关和系统复位不需要重发机制
                }
                readStatus();//重新读取状态
                if (shuttleProtocol.getBusyStatusType().equals(ShuttleStatusType.BUSY)) {
                    break;
                }
 
                //判断是否运行中,如不运行,重新下发命令
                result = modbusTcpNet.Write("0", array);
                News.info("四向穿梭车命令下发[id:{}] >>>>> {},次数:{}", slave.getId(), JSON.toJSON(command), i);
                OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2},次数:{}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command), i));
                try {
                    Thread.sleep(300);//命令下发后休眠
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
 
            return true;
        } else {
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】写入四向穿梭车plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}],次数:{}", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
            News.error("写入四向穿梭车plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
            return false;
        }
    }
 
    //获取命令报文
    private short[] getCommandArr(ShuttleCommand command) {
        // 开始任务
        short[] array = new short[17];
        //控制指令字
        array[0] = command.getCommandWord();
        if (command.getStartCodeNum() != null) {
            //启始二维编号
            array[1] = command.getStartCodeNum();
        }
 
        if (command.getMiddleCodeNum() != null) {
            //中间二维编号
            array[2] = command.getMiddleCodeNum();
        }
 
        if (command.getDistCodeNum() != null) {
            //目标二维编号
            array[3] = command.getDistCodeNum();
        }
 
        if (command.getStartToDistDistance() != null) {
            //起点到目标点的距离长度,先将int转为byte数组,再将byte数组转成两个short,分别占用4和5空间
            short[] startToDistDistances = CommonUtils.intToShorts(command.getStartToDistDistance());
            array[4] = startToDistDistances[0];
            array[5] = startToDistDistances[1];
        }
 
        if (command.getMiddleToDistDistance() != null) {
            //中间点到目标点的距离长度,先将int转为byte数组,再将byte数组转成两个short,分别占用4和5空间
            short[] middleToDistDistances = CommonUtils.intToShorts(command.getMiddleToDistDistance());
            array[6] = middleToDistDistances[0];
            array[7] = middleToDistDistances[1];
        }
 
        if (command.getRunDirection() != null) {
            //小车运行方向
            array[8] = command.getRunDirection();
        }
 
        if (command.getPalletLift() != null) {
            //托盘顶升
            array[9] = command.getPalletLift();
        }
 
        if (command.getForceMoveDistance() != null) {
            //小车强制移动距离,先将int转为byte数组,再将byte数组转成两个short,分别占用4和5空间
            short[] forceMoveDistances = CommonUtils.intToShorts(command.getForceMoveDistance());
            array[10] = forceMoveDistances[0];
            array[11] = forceMoveDistances[1];
        }
 
        if (command.getChargeSwitch() != null) {
            //充电开关
            array[12] = command.getChargeSwitch();
        }
 
        if (command.getIOControl() != null) {
            //小车IO控制
            array[13] = command.getIOControl();
        }
 
        if (command.getRunSpeed() != null) {
            //小车运行速度
            array[14] = command.getRunSpeed();
        }
 
        if (command.getRadarTmp() != null) {
            //小车雷达备用
            array[15] = command.getRadarTmp();
        }
        //指令结束位
        array[16] = command.getCommandEnd();
        return array;
    }
 
    /**
     * 初始化四向穿梭车
     */
    private void initShuttle() {
        this.connect();
        if (null == shuttleProtocol) {
            shuttleProtocol = new ShuttleProtocol();
        }
    }
 
    //分配任务
    private void assignWork(ShuttleAssignCommand assignCommand) {
        ShuttleRedisCommand redisCommand = new ShuttleRedisCommand();
 
        if (!assignCommand.getAuto()) {
            List<NavigateNode> allNode = new ArrayList<>();
            List<ShuttleCommand> commands = new ArrayList<>();
            LocMastService locMastService = SpringUtils.getBean(LocMastService.class);
            BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class);
            NavigateMapData navigateMapData;
 
            //获取小车移动速度
            BasShuttle basShuttle = shuttleService.selectById(slave.getId());
            Integer runSpeed = 1000;
            if (basShuttle != null) {
                Integer runSpeed1 = basShuttle.getRunSpeed();
                if (runSpeed1 != null) {
                    runSpeed = runSpeed1;
                }
            }
 
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
 
            switch (assignCommand.getTaskMode()) {
                case 1://入库
                case 2://出库
                    //小车移动到提升机口,计算路径
                    //计算小车起点到中点所需命令
                    LocMast currentLocMast = locMastService.queryByQrCode(shuttleProtocol.getCurrentCode().toString());
                    List<NavigateNode> firstMastResult = NavigateUtils.calc(currentLocMast.getLocNo(), assignCommand.getSourceLocNo(), NavigationMapType.NORMAL.id, Utils.getShuttlePoints(assignCommand.getShuttleNo().intValue(), Utils.getLev(currentLocMast.getLocNo())));//小车到中点,处于无货状态,使用正常通道地图
                    boolean checkResult = Utils.checkShuttlePath(firstMastResult, shuttleProtocol.getShuttleNo().intValue());
                    if (firstMastResult != null && checkResult) {
                        allNode.addAll(firstMastResult);//将节点进行保存
                        //获取分段路径
                        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(firstMastResult);
                        //将每一段路径分成command指令
                        for (ArrayList<NavigateNode> nodes : data) {
                            //开始路径
                            NavigateNode startPath = nodes.get(0);
                            //中间路径
                            NavigateNode middlePath = nodes.get(nodes.size() - 2);
                            //目标路径
                            NavigateNode endPath = nodes.get(nodes.size() - 1);
                            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
                            Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
 
                            //正常移动命令
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码
                            Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//目标二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed));
                        }
 
                        //托盘顶升
                        commands.add(getPalletCommand((short) 1));
                    }else {
                        //没有计算到路径,可能存在小车位置就是起点位置
                        if (currentLocMast.getLocNo().equals(assignCommand.getSourceLocNo())) {
                            //小车位置就是起点位置,无需移动,直接顶升
                            //托盘顶升
                            commands.add(getPalletCommand((short) 1));
                        }
                    }
 
                    //计算中点到终点路径
                    List<NavigateNode> secMastResult = NavigateUtils.calc(assignCommand.getSourceLocNo(), assignCommand.getLocNo(), NavigationMapType.DFX.id, Utils.getShuttlePoints(assignCommand.getShuttleNo().intValue(), Utils.getLev(assignCommand.getSourceLocNo())));//小车从中点到终点,处于有货状态,使用DFX地图
                    boolean checkResult2 = Utils.checkShuttlePath(secMastResult, shuttleProtocol.getShuttleNo().intValue());
                    if (secMastResult != null && checkResult2) {
                        allNode.addAll(secMastResult);//将节点进行保存
                        //获取分段路径
                        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(secMastResult);
                        //将每一段路径分成command指令
                        for (ArrayList<NavigateNode> nodes : data) {
                            //开始路径
                            NavigateNode startPath = nodes.get(0);
                            //中间路径
                            NavigateNode middlePath = nodes.get(nodes.size() - 2);
                            //目标路径
                            NavigateNode endPath = nodes.get(nodes.size() - 1);
                            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
                            Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
 
                            //正常移动命令
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码
                            Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed));
                        }
 
                        //托盘下降
                        commands.add(getPalletCommand((short) 2));
                    }
 
                    if (firstMastResult == null || secMastResult == null) {
                        throw new CoolException(MessageFormat.format( "四向穿梭车出入库路径搜索失败 ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort()));
                    }
 
                    navigateMapData = new NavigateMapData(Utils.getLev(currentLocMast.getLocNo()));
                    //所使用的路径进行锁定禁用
                    navigateMapData.writeNavigateNodeToRedisMap(firstMastResult, true);////所使用的路径进行锁定禁用
                    navigateMapData.writeNavigateNodeToRedisMap(secMastResult, true);////所使用的路径进行锁定禁用
                    break;
                case 3://托盘顶升
                case 4://托盘下降
                    commands.add(getPalletCommand(assignCommand.getTaskMode() == 3 ? (short) 1 : (short) 2));
                    break;
                case 5://强制左移
                    commands.add(getForceMoveCommand((short) 2));
                    break;
                case 6://强制右移
                    commands.add(getForceMoveCommand((short) 1));
                    break;
                case 7://强制上移
                    commands.add(getForceMoveCommand((short) 3));
                    break;
                case 8://强制下移
                    commands.add(getForceMoveCommand((short) 4));
                    break;
                case 9://状态复位
                    ShuttleCommand reset = getResetCommand();
                    commands.add(reset);
                    break;
                case 10://正方向(右)寻库位
                    commands.add(getFindLocCommand((short) 1));
                    break;
                case 11://负方向(左)寻库位
                    commands.add(getFindLocCommand((short) 2));
                    break;
                case 12://向正方向(前)寻库位
                    commands.add(getFindLocCommand((short) 4));
                    break;
                case 13://向负方向(后)寻库位
                    commands.add(getFindLocCommand((short) 3));
                    break;
                case 14://移动到目标库位
                    String startQr = shuttleProtocol.getCurrentCode().toString();//起始位置
                    //如果穿梭车在提升机内,移动时需要先下发出提升机命令
                    if (liftProtocol.getBarcode().intValue() == shuttleProtocol.getCurrentCode().intValue()) {
                        //穿梭车出提升机
                        Short liftArrival = liftProtocol.getPositionArrivalFeedback();//提升机位置反馈
                        String liftSiteLocNo = Utils.liftArrivalToOutInStaLocNo(liftArrival);
                        LocMast locMast1 = locMastService.selectById(liftSiteLocNo);
                        ShuttleCommand moveCommand = getMoveCommand(liftProtocol.getBarcode(), Short.parseShort(locMast1.getQrCodeValue()), 1600, ShuttleRunDirection.BOTTOM.id, null, null, runSpeed);
                        commands.add(moveCommand);
 
                        //起始位置修改为提升机口站点位置
                        startQr = locMast1.getQrCodeValue();
                    }
 
                    LocMast locMast = locMastService.queryByQrCode(startQr);
                    List<NavigateNode> result = NavigateUtils.calc(locMast.getLocNo(), assignCommand.getLocNo(), NavigationMapType.NONE.id, Utils.getShuttlePoints(assignCommand.getShuttleNo().intValue(), Utils.getLev(locMast.getLocNo())));//手动命令-移动命令,使用无过滤地图
                    boolean checkResult3 = Utils.checkShuttlePath(result, shuttleProtocol.getShuttleNo().intValue());
                    if (result != null && checkResult3) {
                        //所使用的路径进行锁定禁用
                        navigateMapData = new NavigateMapData(Utils.getLev(locMast.getLocNo()));
                        navigateMapData.writeNavigateNodeToRedisMap(result, true);////所使用的路径进行锁定禁用
 
                        allNode.addAll(result);//将节点进行保存
                        //获取分段路径
                        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(result);
                        //将每一段路径分成command指令
                        for (ArrayList<NavigateNode> nodes : data) {
                            //开始路径
                            NavigateNode startPath = nodes.get(0);
                            //中间路径
                            NavigateNode middlePath = nodes.get(nodes.size() - 2);
                            //目标路径
                            NavigateNode endPath = nodes.get(nodes.size() - 1);
                            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
                            Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码
                            Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码
                            //正常移动命令
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed));
                        }
                    }
                    break;
                case 15://充电开关
                    commands.add(getChargeSwitchCommand());
                    break;
                case 16://移动到提升机
                    LocMast locMast1 = locMastService.queryByQrCode(shuttleProtocol.getCurrentCode().toString());
                    int lev = Utils.getLev(locMast1.getLocNo());//穿梭车当前高度
                    String liftSiteLocNo = Utils.levToOutInStaLocNo(lev);//当前楼层站点库位号
                    LocMast liftSitelocMast = locMastService.selectById(liftSiteLocNo);
                    List<NavigateNode> result1 = NavigateUtils.calc(locMast1.getLocNo(), liftSiteLocNo, NavigationMapType.NONE.id, Utils.getShuttlePoints(assignCommand.getShuttleNo().intValue(), Utils.getLev(locMast1.getLocNo())));//移动到提升机,使用无过滤地图
                    boolean checkResult4 = Utils.checkShuttlePath(result1, shuttleProtocol.getShuttleNo().intValue());
                    Short endStartCode = null;
                    if (result1 != null && checkResult4) {
                        //所使用的路径进行锁定禁用
                        navigateMapData = new NavigateMapData(Utils.getLev(locMast1.getLocNo()));
                        navigateMapData.writeNavigateNodeToRedisMap(result1, true);////所使用的路径进行锁定禁用
 
                        allNode.addAll(result1);//将节点进行保存
                        //获取分段路径
                        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(result1);
                        //将每一段路径分成command指令
                        for (ArrayList<NavigateNode> nodes : data) {
                            //开始路径
                            NavigateNode startPath = nodes.get(0);
                            //中间路径
                            NavigateNode middlePath = nodes.get(nodes.size() - 2);
                            //目标路径
                            NavigateNode endPath = nodes.get(nodes.size() - 1);
                            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
                            Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
                            Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码
                            Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码
                            Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码
                            endStartCode = distCode;
                            //正常移动命令
                            commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed));
                        }
                    }
 
                    if (endStartCode == null && shuttleProtocol.getCurrentCode() == Short.parseShort(liftSitelocMast.getQrCodeValue())) {
                        //穿梭车已经在提升机站点口
                        endStartCode = shuttleProtocol.getCurrentCode();
                    }
 
                    //增加移动进提升机命令
                    ShuttleCommand moveCommand = getMoveCommand(endStartCode, liftProtocol.getBarcode(), 1600, ShuttleRunDirection.TOP.id, null, null, runSpeed);
                    commands.add(moveCommand);
                    break;
                default:
            }
            assignCommand.setCommands(commands);
            assignCommand.setNodes(allNode);//当前任务所占用的节点list
        }
 
        redisCommand.setShuttleNo(assignCommand.getShuttleNo());//四向穿梭车号
        redisCommand.setWrkNo(assignCommand.getTaskNo());//工作号
        redisCommand.setCommandStep(0);//命令执行步序
        redisCommand.setAssignCommand(assignCommand);//命令
        redisCommand.setErrorCommands(new ArrayList<ShuttleCommand>());//发生错误时尝试执行的指令,优先级最高
        shuttleProtocol.setTaskNo(assignCommand.getTaskNo());
        shuttleProtocol.setAssignCommand(assignCommand);
        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WORKING);
        //任务数据保存到redis
        redisUtil.set("shuttle_wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand));
        //执行下发任务
        executeWork(assignCommand.getTaskNo());
    }
 
    //执行下发的指令
    private boolean executeWork(Short wrkNo) {
        //读取redis数据
        if (wrkNo == null) {
            return false;
        }
 
        WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class);
 
        Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo);
        if (o == null) {
            return false;
        }
        ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class);
 
        if (shuttleProtocol.getBusyStatus().intValue() == ShuttleStatusType.BUSY.id) {
            return false;//小车状态忙
        }
 
        if (!checkLiftStation(wrkNo)) {//检测是否有提升机站点,有则调度提升机
            return false;
        }
 
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
        LiftProtocol liftProtocol = liftThread.getLiftProtocol();
 
        List<ShuttleCommand> commands = redisCommand.getAssignCommand().getCommands();
        //当前步序
        int commandStep = redisCommand.getCommandStep();
        //path路径数目
        int size = commands.size();
        ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand();
 
        if (commandStep != 0) {
            //判断上一条指令是否完成
            ShuttleCommand command = commands.get(commandStep - 1);
            if (command.getCommandWord().intValue() == 1) {
                //移动命令
                if (command.getDistCodeNum().intValue() == shuttleProtocol.getCurrentCode().intValue()) {
                    //上一条指令的目标位置和当前小车位置相同,则认定上一条任务完成
                    command.setComplete(true);
 
                    //上一条指令起点是提升机二维码,则清零提升机任务号
                    if (command.getStartCodeNum().intValue() == liftProtocol.getBarcode().intValue()) {
                        //判断提升机是否处于空闲
                        if (liftProtocol.isIdleNoTask() && liftProtocol.getTaskNo().intValue() == redisCommand.getWrkNo().intValue()) {
                            liftProtocol.setTaskNo((short) 0);//清空任务号
                            WrkMast wrkMast = wrkMastService.selectById(wrkNo);
                            if (wrkMast != null) {
                                wrkMast.setLiftNo(null);//解锁提升机
                                wrkMastService.updateById(wrkMast);
                            }
                        }
                    }
 
                    //入库命令,当小车取完货后,需要将提升机释放
                    if (assignCommand.getTaskMode().intValue() == ShuttleTaskModeType.PAK_IN.id) {
                        //判断上一条指令的起点是否为输送站点且目标点不是提升机内部二维码
                        Short startCodeNum = command.getStartCodeNum();
                        BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                        BasDevp basDevp = basDevpService.queryByQrCode(startCodeNum.intValue());//目标站点
                        if (basDevp != null && command.getDistCodeNum().intValue() != liftProtocol.getBarcode().intValue()) {
                            //上一条指令的起点为输送站点且目标点不是提升机内部二维码
                            //此时小车应该已经离开输送站点,判断提升机是否空闲且有工作号
                            if (liftProtocol.isIdleNoTask() && liftProtocol.getTaskNo().intValue() == redisCommand.getWrkNo().intValue()) {
                                liftProtocol.setTaskNo((short) 0);//清空任务号
                                WrkMast wrkMast = wrkMastService.selectById(wrkNo);
                                if (wrkMast != null) {
                                    wrkMast.setLiftNo(null);//解锁提升机
                                    wrkMastService.updateById(wrkMast);
                                }
                            }
                        }
                    }
 
                }
            } else if (command.getCommandWord().intValue() == 2) {
                //托盘顶升命令
                if (command.getPalletLift().intValue() == 1) {
                    //顶升
                    //判断是否顶升到位
                    if (shuttleProtocol.getPlcOutputLift()) {
                        //自动模式
                        if (assignCommand.getAuto() && shuttleProtocol.getPlcInputStatus().intValue() == 1) {
                            //顶升到位,且托盘雷达有物,认定任务完成
                            command.setComplete(true);
                        }else {
                            //手动模式,不判断托盘雷达
                            //顶升到位,认定任务完成
                            command.setComplete(true);
                        }
                    }
                }else {
                    //下降
                    //判断是否下降到位,判断托盘雷达是否无物
                    if (!shuttleProtocol.getPlcOutputLift() && !shuttleProtocol.getPlcOutputTransfer()) {
                        //自动模式
                        if (assignCommand.getAuto() && shuttleProtocol.getPlcInputStatus().intValue() == 0) {
                            //下降到位,且托盘雷达无物,认定任务完成
                            command.setComplete(true);
                        }else {
                            //手动模式,不判断托盘雷达
                            //下降到位,且托盘雷达无物,认定任务完成
                            command.setComplete(true);
                        }
                    }
                }
            } else if (command.getCommandWord().intValue() == 5) {
                //充电命令
                //判断小车充电开关
                if (shuttleProtocol.getPlcOutputCharge()) {
                    //正常充电,认定任务完成
                    command.setComplete(true);
                }
            }
            //任务数据保存到redis
            redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
 
            if (!command.getComplete()) {
                //上一条任务未完成,禁止下发命令
                return false;
            }
        }
 
        if (commands.size() == 0) {
            return false;
        }
 
        //取出命令
        ShuttleCommand command = commands.get(commandStep);
 
        if (assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_IN.id.shortValue()
            || assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_OUT.id.shortValue()
        ) {
            //小车失去坐标,禁止下发命令
            if (shuttleProtocol.getCurrentCode() == 0) {
                return false;
            }
        }
 
 
        //判断小车当前二维码是否为提升机二维码
        if (shuttleProtocol.getCurrentCode().intValue() == liftProtocol.getBarcode().intValue()) {
            //小车当前命令起始位置就是提升机二维码,说明小车需要向提升机外移动,则需要判断状态是否满足
            if (command.getStartCodeNum().intValue() == liftProtocol.getBarcode().intValue()){
                //提升机是否空闲,提升机是否到达目标楼层,目标楼层是否给出提升机到位信号位
                if (!liftProtocol.isIdleNoTask()) {
                    return false;//提升机忙,禁止下发命令
                }
                if (liftProtocol.getTaskNo().intValue() != 0 && liftProtocol.getTaskNo().intValue() != wrkNo) {
                    //提升机工作号和当前工作不相同,禁止下发命令
                    return false;
                }
 
                Short distCodeNum = command.getDistCodeNum();//目标二维码
                BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
                BasDevp basDevp = basDevpService.queryByQrCode(distCodeNum.intValue());//目标站点
                if (basDevp == null) {
                    return false;//找不到目标站,禁止下发命令
                }
 
                int lev = Utils.getLev(basDevp.getLocNo());//目标二维码所在楼层
                int liftLev = liftProtocol.getLev().intValue();//提升机所在楼层
                if (liftLev != lev) {
                    return false;//提升机不在目标楼层,禁止下发命令
                }
 
                //获取目标站信息
                SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                StaProtocol staProtocol = devpThread.getStation().get(basDevp.getDevNo());
                if (staProtocol == null) {
                    return false;//站点信息不存在,禁止下发命令
                }
                if (!staProtocol.isLiftArrival()) {
                    return false;//站点提升机到位信号false,禁止下发命令
                }
 
                //条件满足,占用提升机
                liftProtocol.setTaskNo(wrkNo);
            }
        }
 
        //下发命令
        if (!write(command)) {
            News.error("四向穿梭车命令下发失败,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
            return false;
        } else {
            News.info("四向穿梭车命令下发成功,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
 
            //将标记置为false(防止重发)
            shuttleProtocol.setPakMk(false);
 
            //保存数据到数据库做流水
            BasShuttleOptService shuttleOptService = SpringUtils.getBean(BasShuttleOptService.class);
            if (shuttleOptService != null) {
                short[] commandArr = getCommandArr(command);//获取命令报文
                BasShuttleOpt opt = new BasShuttleOpt(
                        assignCommand.getTaskNo().intValue(),
                        assignCommand.getShuttleNo().intValue(),
                        new Date(),
                        ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc,
                        assignCommand.getSourceLocNo(),
                        assignCommand.getLocNo(),
                        null,
                        null,
                        null,
                        JSON.toJSONString(command),
                        JSON.toJSONString(commandArr)
                );
                shuttleOptService.insert(opt);
            }
 
            //判断数据是否执行完成
            if (commandStep < size - 1) {
                //更新redis数据
                //步序增加
                commandStep++;
                redisCommand.setCommandStep(commandStep);
                //任务数据保存到redis
                redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
            }else {
                //已执行完成
 
                if (redisCommand.getLiftSecurityMk()) {
                    //曾锁定过提升机,需要进行解锁
                    if (liftProtocol != null) {
                        liftProtocol.setSecurityMk(false);
                    }
                }
 
                String locNo = shuttleProtocol.getLocNo() == null ? shuttleProtocol.getSourceLocNo() : shuttleProtocol.getLocNo();
                if (locNo != null) {
                    //解除锁定的库位路径
                    NavigateMapData navigateMapData = new NavigateMapData(Utils.getLev(locNo));
                    navigateMapData.writeNavigateNodeToRedisMap(redisCommand.getAssignCommand().getNodes(), false);
                }
 
                //删除redis
                redisUtil.del("shuttle_wrk_no_" + redisCommand.getWrkNo());
 
                if (!assignCommand.getAuto()) {
                    //手动模式不抛出等待状态,直接复位
                    //设置四向穿梭车为空闲状态
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                    //任务号清零
                    shuttleProtocol.setTaskNo((short) 0);
                    //标记复位
                    shuttleProtocol.setPakMk(true);
                    News.info("四向穿梭车手动任务执行完成,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
                }else {
                    if (!assignCommand.getCharge()) {
                        //对主线程抛出等待确认状态waiting
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WAITING);
                    }else {
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING_WAITING);
                    }
                    News.info("四向穿梭车任务执行下发完成等待执行结束,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
                }
 
            }
 
        }
        return true;
    }
 
    /**
     * 检测是否有提升机站点,有则调度提升机
     */
    private boolean checkLiftStation(Short wrkNo) {
        //读取redis数据
        if (wrkNo == null) {
            return false;
        }
 
        //拿到提升机线程
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
        if (liftThread == null) {
            return false;
        }
        LiftProtocol liftProtocol = liftThread.getLiftProtocol();
        if (liftProtocol == null) {
            return false;
        }
 
        Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo);
        if (o == null) {
            return false;
        }
        ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class);
        //当前步序
        int commandStep = redisCommand.getCommandStep();
 
        //检测是否存在提升机口的指令
        List<ShuttleCommand> commands = redisCommand.getAssignCommand().getCommands();
        if (commands.size() > 0) {
            if (commands.get(commandStep).getCommandWord() != 1) {
                //不是行走命令,直接放行
                return true;
            }
 
            if (commands.get(0).getStartCodeNum() == null) {
                return false;
            }
        }
 
        if (commands.size() == 0) {
            return false;
        }
 
        //当前等待执行的指令
        ShuttleCommand command = commands.get(commandStep);
 
        BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class);
        ArrayList<Short> qrCodeValues = new ArrayList<>();
        for (BasDevp basDevp : basDevpService.selectList(null)) {
            //将所有提升机口二维码存入list
            qrCodeValues.add(Short.parseShort(basDevp.getQrCodeValue()));
        }
 
        Integer siteNo = null;//站点号
        for (Short qrCodeValue : qrCodeValues) {
            //目标位置是提升机口,或起点位置是提升机口且目标是去提升机内
            if (command.getDistCodeNum() == null || command.getStartCodeNum() == null) {
                continue;
            }
 
            if (qrCodeValue.intValue() == command.getDistCodeNum().intValue() || (qrCodeValue.intValue() == command.getStartCodeNum().intValue() && command.getDistCodeNum().intValue() == liftProtocol.getBarcode().intValue())) {
                //存在
                BasDevp basDevp = basDevpService.queryByQrCode(qrCodeValue.intValue());
                siteNo = basDevp.getDevNo();
                break;
            }
        }
 
        if (siteNo == null) {
            //找不到站点,说明还未到提升机
            return true;
        }
 
 
//        //遍历所有指令,判断是否有到提升机口的指令或从提升机口前往提升机内的指令,并获取到达该提升机口所需步序
//        int step = 0;
//        Integer siteNo = null;//站点号
//        ShuttleCommand command = null;
//        for (int i = 0; i < commands.size(); i++) {
//            command = commands.get(i);
//            for (Short qrCodeValue : qrCodeValues) {
//                //目标位置是提升机口,或起点位置是提升机口且目标是去提升机内
//                if (command.getDistCodeNum() == null || command.getStartCodeNum() == null) {
//                    continue;
//                }
//
//                if (qrCodeValue.intValue() == command.getDistCodeNum().intValue() || (qrCodeValue.intValue() == command.getStartCodeNum().intValue() && command.getDistCodeNum().intValue() == liftProtocol.getBarcode().intValue())) {
//                    //存在
//                    step = i + 1;
//                    BasDevp basDevp = basDevpService.queryByQrCode(qrCodeValue.intValue());
//                    siteNo = basDevp.getDevNo();
//                    break;
//                }
//            }
//        }
//
//        if (step == 0) {
//            //无需后续检测,直接放行
//            return true;
//        }
 
        //判断下一步是否为提升机口或提升机内
        if (commandStep < commands.size()) {
            ShuttleCommand command1 = commands.get(commandStep);
            Short distCodeNum = command1.getDistCodeNum();
            if (distCodeNum != null) {
                BasDevp basDevp = basDevpService.queryByQrCode(Integer.valueOf(distCodeNum));
                if (basDevp == null && distCodeNum.intValue() != liftProtocol.getBarcode().intValue()) {
                    return true;//下一步不是提升机口,跳过后续流程
                }
            }
        }
 
        //获取四向穿梭车当前楼层
        String shuttleLocNo = shuttleProtocol.getCurrentLocNo();//二维码对应库位号
        Integer shuttleLocNoLev = shuttleLocNo == null ? 0 : Utils.getLev(shuttleLocNo);//库位号对应层高
 
        //判断穿梭车和提升机是否在目标楼层
        if (shuttleLocNoLev >= 2) {
            shuttleLocNoLev++;
        }
 
        //判断输送线站点是否给出提升机到位信号
        SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
        StaProtocol staProtocol = siemensDevpThread.getStation().get(siteNo);
        if (!staProtocol.isLiftArrival()) {
            //输送线反馈提升机没有到位
            executeLift(liftThread, liftProtocol, redisCommand, shuttleLocNoLev);//调度提升机
            return false;
        }
 
        if (shuttleProtocol.getCurrentCode().intValue() == liftProtocol.getBarcode().intValue()) {
            //小车处于提升机内
            return true;
        }else {
            if (liftProtocol.getPositionArrivalFeedback$() == shuttleLocNoLev) {
                //判断提升机是否有任务号
                if (liftProtocol.getTaskNo().intValue() != 0) {
                    //判断任务号是否和当前小车任务一致
                    if (liftProtocol.getTaskNo().intValue() != wrkNo.intValue()) {
                        return false;//任务号不一致,且提升机任务号不为0
                    }
                }
                liftProtocol.setTaskNo(wrkNo);//给提升机写工作号,防止被占用
                WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class);
                WrkMast wrkMast = wrkMastService.selectById(shuttleProtocol.getTaskNo());
                if (wrkMast != null) {
                    wrkMast.setLiftNo(liftProtocol.getLiftNo().intValue());//锁定提升机,防止被抢占
                    wrkMastService.updateById(wrkMast);
                }
                return true;//提升机到位
            }
            executeLift(liftThread, liftProtocol, redisCommand, shuttleLocNoLev);//调度提升机
            return false;//提升机没有到位
        }
 
    }
 
    private boolean executeLift(LiftThread liftThread, LiftProtocol liftProtocol, ShuttleRedisCommand redisCommand, Integer shuttleLocNoLev) {//调度提升机
        if (!liftProtocol.isIdle()) {
            //提升机不空闲禁止下发
            return false;
        }
 
        if (liftProtocol.getPlatShuttleCheck()) {
            //提升机内有车禁止下发
            return false;
        }
 
        if (redisCommand.getLiftSecurityMk()) {
            //已经执行过提升机命令,禁止下发
            return false;
        }
 
        WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class);
        WrkMast wrkMast = wrkMastService.selectById(shuttleProtocol.getTaskNo());
        if (wrkMast != null) {
            wrkMast.setLiftNo(liftProtocol.getLiftNo().intValue());//锁定提升机,防止被抢占
            wrkMastService.updateById(wrkMast);
        }
 
        //给提升机分配任务
        liftProtocol.setTaskNo(shuttleProtocol.getTaskNo());//设置任务号
        liftProtocol.setShuttleNo(shuttleProtocol.getShuttleNo());//设置四向穿梭车号
        liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
        liftProtocol.setSecurityMk(true);//标记置为true,防止其他任务占用当前提升机
        redisCommand.setLiftSecurityMk(true);//标记置为true,防止其他任务占用当前提升机
        //任务数据保存到redis
        redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
 
        //命令list
        ArrayList<LiftCommand> liftCommands = new ArrayList<>();
        LiftCommand liftCommand = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), shuttleLocNoLev);
        liftCommands.add(liftCommand);//将命令添加进list
 
        LiftAssignCommand liftAssignCommand = new LiftAssignCommand();
        liftAssignCommand.setCommands(liftCommands);
        liftAssignCommand.setLiftNo(liftProtocol.getLiftNo());
        liftAssignCommand.setTaskNo(liftProtocol.getTaskNo());
        //下发任务
        MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, liftAssignCommand));
        return true;
    }
 
    /**
     * 复位并尝试修复错误
     */
    private boolean resetAndTryFix(Short wrkNo) {
        //读取redis数据
        if (wrkNo == null) {
            return false;
        }
 
        Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo);
        if (o == null) {
            return false;
        }
 
        ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class);
        List<ShuttleCommand> commands = redisCommand.getAssignCommand().getCommands();
        //当前步序
        int commandStep = redisCommand.getCommandStep();
        //path路径数目
        int size = commands.size();
 
        ArrayList<ShuttleCommand> list = new ArrayList<>();
 
        //取出命令
        ShuttleCommand command = commands.get(commandStep - 1);
 
        //复位命令
        ShuttleCommand resetCommand = getResetCommand();
        list.add(resetCommand);
 
        //车辆空闲,等待写入找库位命令
        //找库位命令
        short direction = 1;
        switch (command.getRunDirection()) {//转换运行方向
            case 1:
                direction = 2;
                break;
            case 2:
                direction = 1;
                break;
            case 3:
                direction = 4;
                break;
            case 4:
                direction = 3;
                break;
            default:
                direction = 1;
        }
        //找库位命令
        ShuttleCommand searchCommand = getFindLocCommand(direction, 1200, (short) 1000);
        list.add(searchCommand);
 
        //移动车辆,需要在执行完寻找定位点后再执行
        ShuttleCommand moveCommand = new ShuttleCommand();
        moveCommand.setCommandWord((short) 1);
        moveCommand.setStartCodeNum(command.getStartCodeNum());//存入目标库位号
        list.add(moveCommand);
 
        redisCommand.setErrorCommands(list);
        //任务数据保存到redis
        redisUtil.set("shuttle_wrk_no_" + wrkNo, JSON.toJSONString(redisCommand));
        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.FIXING);
        return true;
    }
 
    /**
     * 获取托盘顶升命令
     * @param lift 1顶升,2下降
     */
    public ShuttleCommand getPalletCommand(Short lift) {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 2);
        command.setStartCodeNum((short) 0);
        command.setMiddleCodeNum((short) 0);
        command.setDistCodeNum((short) 0);
        command.setStartToDistDistance(0);
        command.setMiddleToDistDistance(0);
        command.setRunDirection((short) 0);
        command.setForceMoveDistance(0);
        command.setPalletLift(lift);
        command.setRunSpeed((short) 0);
        command.setCommandEnd((short) 1);
        return command;
    }
 
    /**
     * 正常移动命令,默认移动速度1000
     */
    public ShuttleCommand getMoveCommand(Short startCodeNum, Short distCodeNum, Integer startToDistDistance, Short runDirection, Short middleCodeNum, Integer middleToDistDistance) {
        return getMoveCommand(startCodeNum, distCodeNum, startToDistDistance, runDirection, middleCodeNum, middleToDistDistance, 500);
    }
 
    /**
     * 正常移动命令
     */
    public ShuttleCommand getMoveCommand(Short startCodeNum, Short distCodeNum, Integer startToDistDistance, Short runDirection, Short middleCodeNum, Integer middleToDistDistance, Integer runSpeed) {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 1);
        command.setStartCodeNum(startCodeNum);
        command.setMiddleCodeNum(middleCodeNum);
        command.setDistCodeNum(distCodeNum);
        command.setStartToDistDistance(startToDistDistance);
        command.setMiddleToDistDistance(middleToDistDistance);
        command.setRunDirection(runDirection);
        command.setForceMoveDistance(0);
        command.setIOControl((short) 0);
        command.setRunSpeed(runSpeed.shortValue());
        command.setCommandEnd((short) 1);
        return command;
    }
 
    /**
     * 强制移动命令
     * @param direction 移动方向 1左移,2右移,3前移,4后移
     */
    public ShuttleCommand getForceMoveCommand(Short direction) {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 3);
        command.setStartCodeNum((short) 0);
        command.setMiddleCodeNum((short) 0);
        command.setDistCodeNum((short) 0);
        command.setStartToDistDistance(600);
        command.setRunDirection(direction);
        command.setForceMoveDistance(600);
        command.setIOControl((short) 0);
        command.setCommandEnd((short) 1);
        command.setRunSpeed((short) 1000);
        return command;
    }
 
    /**
     * 状态复位命令
     */
    public ShuttleCommand getResetCommand() {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 6);
        command.setStartCodeNum((short) 0);
        command.setMiddleCodeNum((short) 0);
        command.setDistCodeNum((short) 0);
        command.setStartToDistDistance(0);
        command.setMiddleToDistDistance(0);
        command.setRunDirection((short) 0);
        command.setPalletLift((short) 0);
        command.setPalletLift((short) 0);
        command.setForceMoveDistance(0);
        command.setChargeSwitch((short) 0);
        command.setIOControl((short) 0);
        command.setRunSpeed((short) 0);
        command.setCommandEnd((short) 1);
        return command;
    }
 
    /**
     * 获取寻库位命令
     * @param direction 1:向正方向(左)寻库位,2:向负方向(右)寻库位,3:向负方向(后)寻库位,4:向正方向(前)寻库位
     */
    public ShuttleCommand getFindLocCommand(Short direction, Integer startToDistance, Short runSpeed) {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 4);
        command.setRunDirection(direction);
        command.setStartToDistDistance(startToDistance);
        command.setRunSpeed((short) runSpeed);
        command.setCommandEnd((short) 1);
        return command;
    }
 
    /**
     * 获取寻库位命令,默认移动距离3000,运行速度1000
     * @param direction 1:向正方向(左)寻库位,2:向负方向(右)寻库位,3:向负方向(后)寻库位,4:向正方向(前)寻库位
     */
    public ShuttleCommand getFindLocCommand(Short direction) {
        return getFindLocCommand(direction, 3000, (short) 1000);
    }
 
    /**
     * 获取充电开关命令
     */
    public ShuttleCommand getChargeSwitchCommand() {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 5);
        command.setChargeSwitch((short) 1);
        command.setCommandEnd((short) 1);
        return command;
    }
 
    /**
     * charge:1=>开始充电,2=>断开充电
     */
    public ShuttleCommand getChargeSwitchCommand(Short charge) {
        ShuttleCommand command = new ShuttleCommand();
        command.setCommandWord((short) 5);//充电
        command.setShuttleNo(shuttleProtocol.getShuttleNo());
        command.setChargeSwitch(charge);//开始充电
        command.setCommandEnd((short) 1);
        return command;
    }
 
    /******************************************************************************************/
    /**************************************** 测试专用 *****************************************/
    /*****************************************************************************************/
    public static void main(String[] args) throws InterruptedException {
        ShuttleSlave slave = new ShuttleSlave();
        slave.setId(1);
        slave.setIp("192.168.4.24");
        slave.setPort(502);
//        ShuttleThread thread = new ShuttleThread(slave);
//        thread.connect();
//        thread.readStatus();
//
//        ShuttleCommand command = new ShuttleCommand();
//        command.setCommandWord((short) 0);
//        command.setStartCodeNum((short) 12323);
//        command.setMiddleCodeNum((short) 22323);
//        command.setDistCodeNum((short) 29999);
//        command.setStartToDistDistance(109999);
//        command.setMiddleToDistDistance(5000);
//        command.setRunDirection((short) 1);
//        command.setPalletLift((short) 2);
//        command.setForceMoveDistance(3000);
//        command.setChargeSwitch((short) 2);
//        command.setIOControl((short) 0);
//        command.setRunSpeed((short) 0);
//        command.setRadarTmp((short) 0);
//        command.setCommandEnd((short) 1);
//        thread.write(command);
 
    }
}