自动化立体仓库 - WCS系统
Junjie
2023-07-27 84a9e3a9a624526116b42ab15e27ec852eaa7c21
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
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
package com.zy.asrs.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.Cools;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
import com.zy.asrs.mapper.*;
import com.zy.asrs.service.*;
import com.zy.asrs.utils.Utils;
import com.zy.common.model.*;
import com.zy.common.model.enums.NavigationMapType;
import com.zy.common.model.enums.WrkChargeType;
import com.zy.common.service.CommonService;
import com.zy.common.service.erp.ErpService;
import com.zy.common.utils.*;
import com.zy.core.CrnThread;
import com.zy.core.DevpThread;
import com.zy.core.News;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.*;
import com.zy.core.model.*;
import com.zy.core.model.command.*;
import com.zy.core.model.protocol.*;
import com.zy.core.properties.SlaveProperties;
import com.zy.core.thread.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 立体仓库WCS系统主流程业务
 * Created by vincent on 2020/8/6
 */
@Slf4j
@Service("mainService")
@Transactional
public class MainServiceImpl {
 
    @Value("${wms.url}")
    private String wmsUrl;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private WrkMastMapper wrkMastMapper;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private StaDescService staDescService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private BasErrLogService basErrLogService;
    @Autowired
    private BasSteErrLogService basSteErrLogService;
    @Autowired
    private BasShuttleService basShuttleService;
    @Autowired
    private BasShuttleErrLogService basShuttleErrLogService;
    @Autowired
    private BasShuttleErrService basShuttleErrService;
    @Autowired
    private BasCrnErrorMapper basCrnErrorMapper;
    @Autowired
    private BasSteService basSteService;
    @Autowired
    private WrkChargeService wrkChargeService;
    @Autowired
    private BasSteErrService basSteErrService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private WrkChargeMapper wrkChargeMapper;
    @Autowired
    private BasMapService basMapService;
    @Autowired
    private ErpService erpService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetlMapper orderDetlMapper;
    @Autowired
    private RedisUtil redisUtil;
 
    /**
     * 组托
     * 入库站,根据条码扫描生成入库工作档,工作状态 2
     */
    public void generateStoreWrkFile() {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                // 尺寸检测异常
                boolean back = false;
                String errMsg = "异常:";
                if (staProtocol.isFrontErr()) {
                    errMsg = errMsg+"前超限;";
                    back = true;
                }
                if (staProtocol.isBackErr()) {
                    errMsg = errMsg+"后超限";
                    back = true;
                }
                if (staProtocol.isHighErr()) {
                    errMsg = errMsg+"高超限";
                    back = true;
                }
                if (staProtocol.isLeftErr()) {
                    errMsg = errMsg+"左超限";
                    back = true;
                }
                if (staProtocol.isRightErr()) {
                    errMsg = errMsg+"右超限";
                    back = true;
                }
                if (staProtocol.isWeightErr()) {
                    errMsg = errMsg+"超重";
                    back = true;
                }
                if (staProtocol.isBarcodeErr()) {
                    errMsg = errMsg+"扫码失败";
                    back = true;
                }
                // 退回
                if (back) {
                    // led 异常显示
                    LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                    if (ledThread != null) {
                        MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errMsg));
                    }
                    continue;
                }
 
                // 判断是否满足入库条件
                if (staProtocol.isAutoing() && staProtocol.isLoading()
                        && staProtocol.isInEnable()
                        && !staProtocol.isEmptyMk() && (workNo == 0 || (workNo >= 9990 && workNo <= 9999))
                        ) {
 
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
                    if(!Cools.isEmpty(barcode)) {
//                        News.info("{}号条码扫描器检测条码信息:{}", inSta.getBarcode(), barcode);
                        if("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)|| "00000000".equals(barcode)) {
//                            staProtocol.setWorkNo((short) 32002);
//                            staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
                            // led 异常显示
                            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                            if (ledThread != null) {
                                String errorMsg = "扫码失败,请重试";
                                MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errorMsg));
                            }
                            continue;
                        }
                    } else {
//                        staProtocol.setWorkNo((short) 32002);
//                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
//                        devpThread.setPakMk(staProtocol.getSiteId(), false);
//                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
                        // led 异常显示
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                        if (ledThread != null) {
                            String errorMsg = "扫码失败,请重试";
                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errorMsg));
                        }
                        continue;
                    }
 
                    // 过滤盘点/拣料/并板任务
                    WrkMast wrkMast1 = wrkMastMapper.selectPickStepByBarcode(barcode);
                    if (null != wrkMast1) {
                        continue;
                    }
 
                    // 判断重复工作档
                    WrkMast wrkMast2 = wrkMastMapper.selectPakInStep1(inSta.getStaNo(), barcode);
                    if (wrkMast2 != null) {
                        News.error("工作档中已存在该站状态为( 2.设备上走 )的数据,工作号={}", wrkMast2.getWrkNo());
                        continue;
                    }
 
                    try {
                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
                        SearchLocParam param = new SearchLocParam();
                        param.setBarcode(barcode);
                        param.setIoType(1);
                        param.setSourceStaNo(inSta.getStaNo());
//                        param.setLocType1(locTypeDto.getLocType1());
                        String response = new HttpHandler.Builder()
                                .setUri(wmsUrl)
                                .setPath("/rpc/pakin/loc/v1")
                                .setJson(JSON.toJSONString(param))
                                .build()
                                .doPost();
                        JSONObject jsonObject = JSON.parseObject(response);
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                        Integer code = jsonObject.getInteger("code");
                        if (code.equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                            // plc 处理
                            barcodeThread.setBarcode("");
                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
                            staProtocol.setStaNo(dto.getStaNo().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
 
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            if (!result) {
                                throw new CoolException("更新plc站点信息失败");
                            }
 
                            // 判断重复工作档
                            WrkMast wrkMast = wrkMastMapper.selectPakInStep11(inSta.getStaNo());
                            if (wrkMast == null) { continue; }
 
                            // 更新工作主档
                            wrkMast.setWrkSts(2L); // 工作状态:2.设备上走
                            wrkMast.setModiTime(new Date());
                            if (wrkMastMapper.updateById(wrkMast) == 0) {
                                News.error("更新工作档失败!!! [工作号:{}]", wrkMast.getWrkNo());
                            }
 
                        } else if (code == 500){
                            if (ledThread != null) {
                                String errorMsg = jsonObject.getString("msg");
                                if (!Cools.isEmpty(errorMsg)) {
                                    MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errorMsg));
                                }
                            }
                            News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                        } else if (code == 700) {
                            staProtocol.setWorkNo((short) 32002);
                            staProtocol.setRollback102(1);//102站回退信号
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(5, staProtocol));
 
                            // led 异常显示
                            if (ledThread != null) {
                                String errorMsg = barcode + "托盘识别异常,请先进行组托!";
                                MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errorMsg));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
                }
            }
        }
    }
 
    /**
     * wms入库
     * 入库站,根据条码扫描生成入库工作档,工作状态 1 ==>> 2
     */
    @Deprecated
    public void generateStoreWrkFile0() {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                // 判断是否满足入库条件
                if (staProtocol.isAutoing() && staProtocol.isLoading()
                        && staProtocol.isInEnable()
                        && !staProtocol.isEmptyMk() && (workNo == 0 || (workNo >= 9990 && workNo <= 9999))
                        && staProtocol.isPakMk()) {
 
                    // 尺寸检测异常
                    boolean back = false;
                    String errMsg = "";
                    if (staProtocol.isFrontErr()) {
                        errMsg = "前超限";
                        back = true;
                    }
                    if (!back && staProtocol.isBackErr()) {
                        errMsg = "后超限";
                        back = true;
                    }
                    if (!back && staProtocol.isHighErr()) {
                        errMsg = "高超限";
                        back = true;
                    }
                    if (!back && staProtocol.isLeftErr()) {
                        errMsg = "左超限";
                        back = true;
                    }
                    if (!back && staProtocol.isRightErr()) {
                        errMsg = "右超限";
                        back = true;
                    }
                    if (!back && staProtocol.isWeightErr()) {
                        errMsg = "超重";
                        back = true;
                    }
                    if (!back && staProtocol.isBarcodeErr()) {
                        errMsg = "扫码失败";
                        back = true;
                    }
 
                    // 退回
                    if (back) {
                        News.warn("扫码入库失败,{}入库站因{}异常,托盘已被退回", inSta.getStaNo(), errMsg);
                        staProtocol.setWorkNo((short) 32002);
                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
                        devpThread.setPakMk(staProtocol.getSiteId(), false);
                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
                        // led 异常显示
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                        if (ledThread != null) {
                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errMsg));
                        }
                        continue;
                    }
 
                    // 判断重复工作档
                    WrkMast wrkMast = wrkMastMapper.selectPakInStep11(inSta.getStaNo());
                    if (wrkMast == null) { continue; }
 
                    // 命令下发区 --------------------------------------------------------------------------
 
                    // 更新站点信息 且 下发plc命令
                    staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                    staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                    devpThread.setPakMk(staProtocol.getSiteId(), false);
                    boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                    if (result) {
                        // 更新工作主档
                        wrkMast.setWrkSts(2L); // 工作状态:2.设备上走
                        wrkMast.setModiTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            News.error("更新工作档失败!!! [工作号:{}]", wrkMast.getWrkNo());
                        }
                    } else {
                        News.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                    }
                }
            }
        }
    }
 
    /**
     * 拣料、并板、盘点再入库
     */
    @Transactional
    public synchronized void stnToCrnStnPick(){
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历拣料入库口
            for (DevpSlave.Sta pickSta : devp.getPickInSta()) {
 
                // 获取拣料入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(pickSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                if (staProtocol.isAutoing()
                        && staProtocol.isLoading()
                        && (staProtocol.getWorkNo() > 0 && staProtocol.getWorkNo() < 9999)
                        && staProtocol.getStaNo().equals(staProtocol.getSiteId().shortValue())
                        && staProtocol.isPakMk()){
 
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, pickSta.getBarcode());
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
 
                    WrkMast wrkMast = null;
                    wrkMast = wrkMastMapper.selectPickStepByBarcode(barcode);
                    if (wrkMast == null) {
                        continue;
                    }
//                    if (staProtocol.getWorkNo() == 9996) {
//                        String barcode = barcodeThread.getBarcode();
//                        if(!Cools.isEmpty(barcode)) {
//                            News.info("{}号条码扫描器检测条码信息:{}", pickSta.getBarcode(), barcode);
//                            if("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode)) {
//                                staProtocol.setWorkNo((short) 32002);
//                                staProtocol.setStaNo(pickSta.getBackSta().shortValue());
//                                devpThread.setPakMk(staProtocol.getSiteId(), false);
//                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//
//                                // led 异常显示
//                                LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
//                                if (ledThread != null) {
//                                    String errorMsg = "扫码失败,请重试";
//                                    MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
//                                }
//                                continue;
//                            }
//                        } else {
//                            staProtocol.setWorkNo((short) 32002);
//                            staProtocol.setStaNo(pickSta.getBackSta().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//
//                            // led 异常显示
//                            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
//                            if (ledThread != null) {
//                                String errorMsg = "扫码失败,请重试";
//                                MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
//                            }
//                            continue;
//                        }
//                        wrkMast = wrkMastMapper.selectPickStepByBarcode(barcode);
//                        if (null == wrkMast) {
//                            News.error("{}条码错误,暂无拣料任务!", barcode);
//                        }
//                    }
 
                    if ((wrkMast.getIoType() != 103 && wrkMast.getIoType() != 104 && wrkMast.getIoType() != 107)
                        || Cools.isEmpty(wrkMast.getStaNo()) || Cools.isEmpty(wrkMast.getSourceStaNo()) ) {
                        continue;
                    }
 
                    try {
                        // 访问 WMS 获取入库库位
                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
                        SearchLocParam param = new SearchLocParam();
                        param.setWrkNo(wrkMast.getWrkNo());
                        param.setIoType(wrkMast.getIoType());
                        param.setSourceStaNo(pickSta.getStaNo());
//                        param.setLocType1(locTypeDto.getLocType1());
                        String response = new HttpHandler.Builder()
                                .setUri(wmsUrl)
                                .setPath("/rpc/pakin/loc/v1")
                                .setJson(JSON.toJSONString(param))
                                .build()
                                .doPost();
                        JSONObject jsonObject = JSON.parseObject(response);
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
                        Integer code = jsonObject.getInteger("code");
                        if (code.equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
 
//                            // 获取目标站
//                            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
//                                    .eq("type_no", wrkMast.getIoType() - 50)
//                                    .eq("stn_no", pickSta.getStaNo()) // 作业站点 = 拣料出库的目标站
//                                    .eq("crn_no", dto.getCrnNo()); // 堆垛机号
//                            StaDesc staDesc = staDescService.selectOne(wrapper);
//                            if (Cools.isEmpty(staDesc)) {
//                                News.error("入库路径不存在!type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
//                                continue;
//                            }
//                            // 堆垛机站点(目标站)
//                            Integer staNo = staDesc.getCrnStn();
 
                            // 保存工作明细档历史档
//                        if (wrkMastMapper.saveWrkDetlLog(wrkMast.getWrkNo()) == 0) {
//                            throw new CoolException("保存工作明细档历史档失败");
//                        }
                            // 保存工作主档历史档
                            if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
                                throw new CoolException(wrkMast.getWrkNo() + "保存工作主档历史档失败");
                            }
 
                            String sourceLocNo = wrkMast.getSourceLocNo().trim();
                            // 更新工作档数据状态
                            wrkMast.setIoType(wrkMast.getIoType() - 50); // 入出库类型: 103->53,104->54,107->57
                            wrkMast.setWrkSts(2L); // 工作状态: 2.设备上走
                            wrkMast.setSourceStaNo(pickSta.getStaNo()); // 源站
                            wrkMast.setStaNo(dto.getStaNo()); // 目标站
                            wrkMast.setCrnNo(dto.getCrnNo());
                            wrkMast.setLocNo(sourceLocNo); // 目标库位 = 出库时的源库位
                            wrkMast.setShuttleNo(null); // 穿梭车清空
                            wrkMast.setModiTime(new Date());
                            if (wrkMastMapper.updateById(wrkMast) == 0) {
                                throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
                            }
                            if (wrkMastMapper.setSteEmpty(wrkMast.getWrkNo()) == 0) {
                                throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
                            }
 
                            // 修改库位状态 Q.拣料/盘点/并板再入库
                            LocMast locMast = locMastService.selectById(sourceLocNo);
                            locMast.setLocSts("Q");
                            locMast.setModiTime(new Date());
                            if (!locMastService.updateById(locMast)) {
                                throw new CoolException("修改库位状态失败");
                            }
 
                            // 更新站点信息 且 下发plc命令
                            staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                            staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            if (!result) {
                                News.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                            }
 
                        } else if (code == 500){
                            if (ledThread != null) {
                                String errorMsg = jsonObject.getString("msg");
                                if (!Cools.isEmpty(errorMsg)) {
                                    MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
                                }
                            }
                            News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                        } else {
                            staProtocol.setWorkNo((short) 32002);
                            staProtocol.setStaNo(pickSta.getBackSta().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
 
                            // led 异常显示
                            if (ledThread != null) {
                                String errorMsg = jsonObject.getString("msg");
//                                String errorMsg = barcode + "托盘识别异常,请先进行组托!";
                                MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
                            }
                        }
 
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
 
                }
            }
 
        }
    }
 
    /**
     * 初始化实时地图
     */
    public synchronized void initRealtimeBasMap() {
        for (int i = 1; i <= 4; i++) {//总共四层楼
            Object data = redisUtil.get("realtimeBasMap_" + i);
            if (data == null) {//redis地图数据为空,从数据库中获取
                BasMap basMap = basMapService.selectLatestMap(i);
                if (basMap == null) {
                    //数据库中也不存在地图数据,从地图文件中获取
                    //载入地图
                    NavigateMapData mapData = new NavigateMapData(i);
                    List<List<MapNode>> lists = mapData.getJsonData(-1, null, null);//获取完整地图(包括入库出库)
 
                    //存入数据库
                    basMap = new BasMap();
                    basMap.setData(JSON.toJSONString(lists));
                    basMap.setCreateTime(new Date());
                    basMap.setUpdateTime(new Date());
                    basMap.setLev(i);
 
                    if (!basMapService.insert(basMap)) {
                        log.info("地图数据存储失败");
                    }
                }
 
                //将数据库地图数据存入redis
                redisUtil.set("realtimeBasMap_" + i, JSON.toJSONString(basMap));
            }
        }
    }
 
    /**
     * 从redis中重启任务
     */
    public synchronized void restartTaskFromRedis() {
        HashMap<Object, Object> map = redisUtil.getRedis();
        for (Object key : map.keySet()) {
            if (key.toString().contains("lift_wrk_no_")) {//提升机任务
                LiftRedisCommand redisCommand = JSON.parseObject(map.get(key).toString(), LiftRedisCommand.class);
                if (redisCommand == null) {
                    continue;
                }
 
                Short liftNo = redisCommand.getLiftNo();
                LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftNo.intValue());
                if (liftThread == null) {
                    continue;
                }
                LiftProtocol liftProtocol = liftThread.getLiftProtocol();
                if (liftProtocol == null) {
                    continue;
                }
                if (!liftProtocol.isIdle()) {
                    continue;
                }
 
                //提升机处于空闲状态,进行任务的恢复
                liftProtocol.setTaskNo(redisCommand.getWrkNo());//将提升机线程分配任务号
                liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//工作状态
 
            }else if(key.toString().contains("shuttle_wrk_no_")){//四向穿梭车任务
                ShuttleRedisCommand redisCommand = JSON.parseObject(map.get(key).toString(), ShuttleRedisCommand.class);
                if (redisCommand == null) {
                    continue;
                }
 
                Short shuttleNo = redisCommand.getShuttleNo();
                ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo.intValue());
                if (shuttleThread == null) {
                    continue;
                }
                ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                if (shuttleProtocol == null) {
                    continue;
                }
                if (!shuttleProtocol.isIdle()) {
                    continue;
                }
 
                //四向穿梭车处于空闲状态,进行任务的恢复
                shuttleProtocol.setTaskNo(redisCommand.getWrkNo());//将四向穿梭车线程分配任务号
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WORKING);//工作状态
            }
        }
 
    }
 
    /**
     * 入库  ===>>  四向穿梭车入库作业下发
     */
    public synchronized void shuttleIoInExecute() {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库站
            for (DevpSlave.StaRack staRack : devp.getRackInStn()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(staRack.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
 
                // 判断是否满足入库条件,自动、有物、四向穿梭车可取信号
                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isShuttleTakeEnable()) {
                    WrkMast wrkMast = wrkMastMapper.selectRackInStep48(workNo, staProtocol.getSiteId());
                    if (wrkMast != null) {
                        if (wrkMast.getWrkSts() == 4 || wrkMast.getWrkSts() == 8) {
                            ShuttleThread shuttleThread = null;
                            ShuttleProtocol shuttleProtocol = null;
                            HashMap<String, Object> searchIdleShuttle = null;
 
                            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
                            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
 
                            if (wrkMast.getWrkSts() == 4) {
                                if (wrkMast.getShuttleNo() == null) {
                                    //寻找最近且空闲的四向穿梭车
                                    searchIdleShuttle = this.searchIdleShuttle(wrkMast);
                                    shuttleThread = (ShuttleThread) searchIdleShuttle.get("result");
                                    wrkMast.setShuttleNo(shuttleThread.getSlave().getId());//给工作档分配四向穿梭车号
                                    wrkMastMapper.updateById(wrkMast);
                                    shuttleProtocol = shuttleThread.getShuttleProtocol();
                                    if (!shuttleProtocol.isIdle()) {
                                        continue;
                                    }
                                }else {
                                    //直接使用任务保存中的小车
                                    shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
                                    shuttleProtocol = shuttleThread.getShuttleProtocol();
                                    if (!shuttleProtocol.isIdle(workNo)) {
                                        continue;
                                    }
                                }
                            }else if(wrkMast.getWrkSts() == 8){//状态8,需要向小车下发命令从提升机移动出去,需要判断提升机状是否空闲、提升机是否到达目标楼层、目标楼层站点是否存在、目标楼层站点是否给出提升机到位信号
                                //状态8,等待命令进行入库搬运动作
 
                                //判断提升机是否空闲
                                if (!liftProtocol.isIdleNoTask()) {
                                    try {
                                        Thread.sleep(1000);//休眠1s
                                    } catch (InterruptedException e) {
                                        throw new RuntimeException(e);
                                    }
                                    continue;//提升机忙
                                }
                                //判断提升机任务号和当前工作档任务号是否一致
                                if (liftProtocol.getTaskNo().intValue() != 0 && liftProtocol.getTaskNo().intValue() != wrkMast.getWrkNo()) {
                                    continue;
                                }
 
                                //判断提升机楼层是否到位,判断站点是否给出提升机到位信号
                                String locNo = wrkMast.getLocNo();
                                int lev = Utils.getLev(locNo);//目标二维码所在楼层
                                int liftLev = liftProtocol.getLev().intValue();//提升机所在楼层
                                if (liftLev != lev) {
                                    continue;//提升机不在目标楼层跳过
                                }
 
                                Integer staNo = Utils.levToOutInStaNo(lev >= 2 ? lev + 1 : lev);
                                //获取目标站信息
                                StaProtocol staProtocol1 = devpThread.getStation().get(staNo);
                                if (staProtocol1 == null) {
                                    continue;//站点信息不存在
                                }
                                if (!staProtocol1.isLiftArrival()) {
                                    continue;//站点提升机到位信号false
                                }
 
                                Integer shuttleNo = wrkMast.getShuttleNo();//四向穿梭车号
                                shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
                                shuttleProtocol = shuttleThread.getShuttleProtocol();
                                if (!shuttleProtocol.isIdle(workNo)) {
                                    continue;
                                }
                            }
 
                            if (shuttleThread == null) {
                                continue;
                            }
 
                            if (shuttleProtocol == null) {
                                continue;
                            }
 
                            wrkMast.setShuttleNo(shuttleProtocol.getShuttleNo().intValue());//给工作档分配四向穿梭车号
 
                            //分配任务号
                            shuttleProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //分配源库位
                            shuttleProtocol.setSourceLocNo(wrkMast.getSourceLocNo());
 
                            //创建分配命令
                            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
                            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
                            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());//入出库模式
                            String currentLocNo = shuttleProtocol.getCurrentLocNo();
                            assignCommand.setSourceLocNo(currentLocNo);//源库位(小车当前位置)
 
                            String locNo = wrkMast.getLocNo();//当前工作档库位号
                            Integer currentLev = wrkMast.getWrkSts() == 4 ? Utils.getLev(currentLocNo) : liftProtocol.getLev();//小车当前层高
 
                            //提升机口站点库位号
                            String liftSiteLocNo = Utils.levToOutInStaLocNo(currentLev);
 
                            if (wrkMast.getWrkSts() == 4) {
                                if (currentLev == Utils.getLev(locNo)) {
                                    //小车和目标在同一楼层
                                    //直接计算车到提升机取货再到库位路径指令
                                    List<ShuttleCommand> commands = this.shuttleAssignCommand(wrkMast.getWrkSts() == 4 ? currentLocNo : liftSiteLocNo, liftSiteLocNo, locNo, assignCommand, shuttleThread);
                                    if (commands == null) {
                                        continue;//找不到路径等待下一次
                                    }
                                    assignCommand.setCommands(commands);
                                    //分配目标库位
                                    shuttleProtocol.setLocNo(wrkMast.getLocNo());
                                    //目标库位
                                    assignCommand.setLocNo(wrkMast.getLocNo());
                                    wrkMast.setWrkSts(9L);//小车入库中
                                }else {
                                    //小车和目标不在同一楼层
                                    //小车移动到提升机口,计算路径
                                    List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, liftSiteLocNo, NavigationMapType.NONE.id, assignCommand, shuttleThread);
                                    if (commands == null) {
                                        continue;//未找到路径
                                    }
 
                                    //获取当前小车所在楼层的站点信息
                                    BasDevp basDevp = basDevpService.queryByLocNo(liftSiteLocNo);
                                    Short endStartCode = Short.parseShort(basDevp.getQrCodeValue());//站点二维码
 
                                    //增加移动进提升机命令
                                    ShuttleCommand moveCommand = shuttleThread.getMoveCommand(endStartCode, liftProtocol.getBarcode(), 1600, ShuttleRunDirection.TOP.id, null, null, 500);
                                    commands.add(moveCommand);
 
                                    //分配目标库位
                                    shuttleProtocol.setLocNo(liftSiteLocNo);
                                    //目标库位
                                    assignCommand.setLocNo(liftSiteLocNo);
                                    assignCommand.setCommands(commands);
                                    wrkMast.setWrkSts(5L);//小车迁移状态
                                }
                            } else if (wrkMast.getWrkSts() == 8) {
                                //直接计算车到提升机取货再到库位路径指令
                                List<ShuttleCommand> commands = this.shuttleAssignCommand(wrkMast.getWrkSts() == 4 ? currentLocNo : liftSiteLocNo, liftSiteLocNo, locNo, assignCommand, shuttleThread);
                                if (commands == null) {
                                    continue;//找不到路径等待下一次
                                }
 
                                //此时车在提升机内部,下达一步指令让车移动到提升机口
                                Integer staNo = Utils.levToOutInStaNo(currentLev >= 2 ? currentLev + 1 : currentLev);//站点号
                                BasDevp basDevp = basDevpService.selectById(staNo);
 
                                short startCode = liftProtocol.getBarcode();//提升机内部二维码
                                Short distCode = Short.parseShort(basDevp.getQrCodeValue());//提升机口站点二维码
                                Short runDirection = ShuttleRunDirection.BOTTOM.id;//运行方向
                                //获取命令
                                ShuttleCommand moveCommand = shuttleThread.getMoveCommand(startCode, distCode, 1600, runDirection, null, null, 500);
                                commands.add(0, moveCommand);//将该指令添加到队头
 
                                assignCommand.setCommands(commands);
                                //分配目标库位
                                shuttleProtocol.setLocNo(wrkMast.getLocNo());
                                //目标库位
                                assignCommand.setLocNo(wrkMast.getLocNo());
                                wrkMast.setWrkSts(9L);//小车入库中
                            }
 
//                            if (wrkMast.getWrkSts() == 8 || Boolean.parseBoolean(searchIdleShuttle.get("sameLay").toString())) {
//                                //同一层直接取货无需经过提升机
//                                //直接计算车到提升机取货再到库位路径指令
//                                List<ShuttleCommand> commands = this.shuttleAssignCommand(wrkMast.getWrkSts() == 4 ? currentLocNo : liftSiteLocNo, liftSiteLocNo, locNo, assignCommand, shuttleThread);
//                                if (commands == null) {
//                                    continue;//找不到路径等待下一次
//                                }
//                                if (wrkMast.getWrkSts() == 8) {
//                                    //此时车在提升机内部,下达一步指令让车移动到提升机口
//                                    Integer staNo = Utils.levToOutInStaNo(currentLev >= 2 ? currentLev + 1 : currentLev);//站点号
//                                    BasDevp basDevp = basDevpService.selectById(staNo);
//
//                                    short startCode = liftProtocol.getBarcode();//提升机内部二维码
//                                    Short distCode = Short.parseShort(basDevp.getQrCodeValue());//提升机口站点二维码
//                                    Short runDirection = ShuttleRunDirection.BOTTOM.id;//运行方向
//                                    //获取命令
//                                    ShuttleCommand moveCommand = shuttleThread.getMoveCommand(startCode, distCode, 1600, runDirection, startCode, 1600, 500);
//                                    commands.add(0, moveCommand);//将该指令添加到队头
//                                }
//                                assignCommand.setCommands(commands);
//                                //分配目标库位
//                                shuttleProtocol.setLocNo(wrkMast.getLocNo());
//                                //目标库位
//                                assignCommand.setLocNo(wrkMast.getLocNo());
//                                wrkMast.setWrkSts(9L);//小车入库中
//                            }else {
//                                //不同层,将目标库位分配成提升机库位号
//
//                                //小车移动到提升机口,计算路径
//                                List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, liftSiteLocNo, NavigationMapType.NONE.id, assignCommand, shuttleThread);
//                                if (commands == null) {
//                                    continue;//未找到路径
//                                }
//
//                                //获取当前小车所在楼层的站点信息
//                                BasDevp basDevp = basDevpService.queryByLocNo(liftSiteLocNo);
//                                Short endStartCode = Short.parseShort(basDevp.getQrCodeValue());//站点二维码
//
//                                //增加移动进提升机命令
//                                ShuttleCommand moveCommand = shuttleThread.getMoveCommand(endStartCode, liftProtocol.getBarcode(), 1600, ShuttleRunDirection.TOP.id, endStartCode, 1600, 500);
//                                commands.add(moveCommand);
//
//                                //分配目标库位
//                                shuttleProtocol.setLocNo(liftSiteLocNo);
//                                //目标库位
//                                assignCommand.setLocNo(liftSiteLocNo);
//                                assignCommand.setCommands(commands);
//                                wrkMast.setWrkSts(5L);//小车迁移状态
//                            }
 
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                //下发任务
                                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                            }
                        }
 
                    }
                }
 
            }
        }
 
    }
 
    //获取起点-终点指令
    public synchronized List<ShuttleCommand> shuttleAssignCommand(String startLocNo, String locNo, Integer mapType, ShuttleAssignCommand assignCommand, ShuttleThread shuttleThread) {
        //获取小车移动速度
        BasShuttle basShuttle = basShuttleService.selectById(assignCommand.getShuttleNo());
        Integer runSpeed = 1000;
//        if (basShuttle != null) {
//            Integer runSpeed1 = basShuttle.getRunSpeed();
//            if (runSpeed1 != null) {
//                runSpeed = runSpeed1;
//            }
//        }
 
        //计算小车起点到中点所需命令
        List<NavigateNode> calc = NavigateUtils.calc(startLocNo, locNo, mapType, Utils.getShuttlePoints(shuttleThread.getSlave().getId(), Utils.getLev(startLocNo)));
        List<ShuttleCommand> commands = new ArrayList<>();
        if (calc == null) {
            return null;
        }
        if (!Utils.checkShuttlePath(calc, shuttleThread.getSlave().getId())) {//检测穿梭车行走路径,是否存在其他小车,如有其他小车则进行调离
            return null;
        }
        List<NavigateNode> allNode = new ArrayList<>();
        allNode.addAll(calc);
 
        //获取分段路径
        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(calc);
        //将每一段路径分成command指令
        for (ArrayList<NavigateNode> nodes : data) {
            //开始路径
            NavigateNode startPath = nodes.get(0);
 
            //中间路径
            NavigateNode middlePath = null;
            //通过xy坐标小车二维码
            Short middleCodeNum = null;
            Integer middleToDistDistance = null;//计算中间点到目标点行走距离
            if (nodes.size() > 10) {//中段码传倒数第三个
                //中间路径
                middlePath = nodes.get(nodes.size() - 3);
                //通过xy坐标小车二维码
                middleCodeNum = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());
                middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
            } else if (nodes.size() > 5) {//中段码传倒数第二个
                //中间路径
                middlePath = nodes.get(nodes.size() - 2);
                //通过xy坐标小车二维码
                middleCodeNum = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());
                middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
            }
 
            //目标路径
            NavigateNode endPath = nodes.get(nodes.size() - 1);
            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
            //通过xy坐标小车二维码
            Short startCodeNum = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());
            //通过xy坐标小车二维码
            Short distCodeNum = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());
            //获取移动命令
            ShuttleCommand command = shuttleThread.getMoveCommand(startCodeNum, distCodeNum, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCodeNum, middleToDistDistance, runSpeed);
            commands.add(command);
        }
 
        assignCommand.setNodes(allNode);//当前任务所占用的节点list
        //所使用的路径进行锁定禁用
        NavigateMapData navigateMapData = new NavigateMapData(Utils.getLev(startLocNo));
        navigateMapData.writeNavigateNodeToRedisMap(allNode, true);////所使用的路径进行锁定禁用
 
        return commands;
    }
 
    //获取起点-中点-终点指令
    public synchronized List<ShuttleCommand> shuttleAssignCommand(String startLocNo, String middleLocNo, String locNo, ShuttleAssignCommand assignCommand, ShuttleThread shuttleThread) {
        //获取小车移动速度
        BasShuttle basShuttle = basShuttleService.selectById(assignCommand.getShuttleNo());
        Integer runSpeed = 1000;
//        if (basShuttle != null) {
//            Integer runSpeed1 = basShuttle.getRunSpeed();
//            if (runSpeed1 != null) {
//                runSpeed = runSpeed1;
//            }
//        }
 
        List<NavigateNode> allNode = new ArrayList<>();
 
        //计算小车起点到中点所需命令
        List<NavigateNode> calc = NavigateUtils.calc(startLocNo, middleLocNo, NavigationMapType.NORMAL.id, null);//小车无货,走正常库位通道
        List<ShuttleCommand> commands = new ArrayList<>();
 
        if (calc != null) {
            if (!Utils.checkShuttlePath(calc, shuttleThread.getSlave().getId())) {//检测穿梭车行走路径,是否存在其他小车,如有其他小车则进行调离
                return null;
            }
 
            allNode.addAll(calc);
            //获取分段路径
            ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(calc);
            //将每一段路径分成command指令
            for (ArrayList<NavigateNode> nodes : data) {
                //开始路径
                NavigateNode startPath = nodes.get(0);
 
                //中间路径
                NavigateNode middlePath = null;
                //通过xy坐标小车二维码
                Short middleCodeNum = null;
                Integer middleToDistDistance = null;//计算中间点到目标点行走距离
                if (nodes.size() > 10) {//中段码传倒数第三个
                    //中间路径
                    middlePath = nodes.get(nodes.size() - 3);
                    //通过xy坐标小车二维码
                    middleCodeNum = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());
                    middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
                } else if (nodes.size() > 5) {//中段码传倒数第二个
                    //中间路径
                    middlePath = nodes.get(nodes.size() - 2);
                    //通过xy坐标小车二维码
                    middleCodeNum = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());
                    middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
                }
 
                //目标路径
                NavigateNode endPath = nodes.get(nodes.size() - 1);
                Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
 
                //通过xy坐标小车二维码
                Short startCodeNum = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());
                //通过xy坐标小车二维码
                Short distCodeNum = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());
                //获取移动命令
                ShuttleCommand command = shuttleThread.getMoveCommand(startCodeNum, distCodeNum, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCodeNum, middleToDistDistance, runSpeed);
                commands.add(command);
            }
        }
 
        //小车指令到达目标位置后,再发出一条顶升指令
        commands.add(shuttleThread.getPalletCommand((short) 1));
 
        //计算小车中点到终点所需命令
        List<NavigateNode> calc2 = NavigateUtils.calc(middleLocNo, locNo, NavigationMapType.DFX.id, null);//小车有货,走禁用过DFX库位的地图通道
        if (calc2 == null) {
            return null;
        }
        if (!Utils.checkShuttlePath(calc2, shuttleThread.getSlave().getId())) {//检测穿梭车行走路径,是否存在其他小车,如有其他小车则进行调离
            return null;
        }
        allNode.addAll(calc2);
 
        //获取分段路径
        ArrayList<ArrayList<NavigateNode>> data2 = NavigateUtils.getSectionPath(calc2);
        for (ArrayList<NavigateNode> nodes : data2) {
            //开始路径
            NavigateNode startPath = nodes.get(0);
 
            //中间路径
            NavigateNode middlePath = null;
            //通过xy坐标小车二维码
            Short middleCodeNum = null;
            Integer middleToDistDistance = null;//计算中间点到目标点行走距离
            if (nodes.size() > 10) {//中段码传倒数第三个
                //中间路径
                middlePath = nodes.get(nodes.size() - 3);
                //通过xy坐标小车二维码
                middleCodeNum = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());
                middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
            } else if (nodes.size() > 5) {//中段码传倒数第二个
                //中间路径
                middlePath = nodes.get(nodes.size() - 2);
                //通过xy坐标小车二维码
                middleCodeNum = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());
                middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离
            }
 
            //目标路径
            NavigateNode endPath = nodes.get(nodes.size() - 1);
            Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离
 
            //通过xy坐标小车二维码
            Short startCodeNum = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());
            //通过xy坐标小车二维码
            Short distCodeNum = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());
            ShuttleCommand moveCommand = shuttleThread.getMoveCommand(startCodeNum, distCodeNum, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCodeNum, middleToDistDistance, runSpeed);
            commands.add(moveCommand);
        }
 
        //小车指令到达目标位置后,再发出一条托盘下降指令
        commands.add(shuttleThread.getPalletCommand((short) 2));
 
        assignCommand.setNodes(allNode);//当前任务所占用的节点list
        //所使用的路径进行锁定禁用
        NavigateMapData navigateMapData = new NavigateMapData(Utils.getLev(startLocNo));
        navigateMapData.writeNavigateNodeToRedisMap(allNode, true);////所使用的路径进行锁定禁用
 
        return commands;
    }
 
    /**
     * 出库  ===>>  四向穿梭车出库作业下发
     */
    public synchronized void shuttleIoOutExecute() {
        for (WrkMast wrkMast : wrkMastMapper.selectBy2125()) {
            //提取一条待出库任务
            if (wrkMast != null) {
                String outStaLocNo = null;//出库站点库位号
                //获取出库站点
                for (DevpSlave devpSlave : slaveProperties.getDevp()) {
                    for (DevpSlave.StaRack staOutRack : devpSlave.getRackOutStn()) {
                        if (staOutRack.getStaNo().equals(wrkMast.getStaNo())) {
                            //出库站点和工作档出库站点一致
                            outStaLocNo = CommonUtils.getLocNoFromRBL(staOutRack.getRow(), staOutRack.getBay(), staOutRack.getLev());
                        }
                    }
 
                }
 
                if (wrkMast.getWrkSts() == 21
                        || wrkMast.getWrkSts() == 25
                        || wrkMast.getWrkSts() == 31) {
                    ShuttleThread shuttleThread = null;
                    HashMap<String, Object> searchIdleShuttle = null;
 
                    LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
                    LiftProtocol liftProtocol = liftThread.getLiftProtocol();
 
                    if (wrkMast.getWrkSts() == 21) {
                        if (wrkMast.getShuttleNo() == null) {
                            //寻找最近且空闲的四向穿梭车
                            searchIdleShuttle = this.searchIdleShuttle(wrkMast);
                            shuttleThread = (ShuttleThread) searchIdleShuttle.get("result");
                            if (shuttleThread == null) {
                                continue;
                            }
                            wrkMast.setShuttleNo(shuttleThread.getSlave().getId());//给工作档分配四向穿梭车号
                            wrkMastMapper.updateById(wrkMast);
                        }else {
                            //直接使用任务保存中的小车
                            shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
                        }
                    }else if(wrkMast.getWrkSts() == 25) {//状态25,需要向小车下发命令从提升机移动出去,需要判断提升机状是否空闲、提升机是否到达目标楼层、目标楼层站点是否存在、目标楼层站点是否给出提升机到位信号
 
                        //判断提升机是否空闲
                        if (!liftProtocol.isIdleNoTask()) {
                            try {
                                Thread.sleep(1000);//休眠1s
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            continue;//提升机忙
                        }
                        //判断提升机任务号和当前工作档任务号是否一致
                        if (liftProtocol.getTaskNo().intValue() != 0 && liftProtocol.getTaskNo().intValue() != wrkMast.getWrkNo()) {
                            continue;
                        }
 
                        //判断提升机楼层是否到位,判断站点是否给出提升机到位信号
                        String locNo = wrkMast.getSourceLocNo();
                        int lev = Utils.getLev(locNo);//目标二维码所在楼层
                        Short liftLev = liftProtocol.getLev();//提升机所在楼层
                        if (liftLev == null) {//提升机可能在输送线楼层
                            continue;
                        }
                        if (liftLev.intValue() != lev) {
                            continue;//提升机不在目标楼层跳过
                        }
 
                        Integer staNo = Utils.levToOutInStaNo(lev >= 2 ? lev + 1 : lev);
                        DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                        //获取目标站信息
                        StaProtocol staProtocol1 = devpThread.getStation().get(staNo);
                        if (staProtocol1 == null) {
                            continue;//站点信息不存在
                        }
                        if (!staProtocol1.isLiftArrival()) {
                            continue;//站点提升机到位信号false
                        }
 
                        //继续完成之前小车未完成的任务
                        shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
                    } else if (wrkMast.getWrkSts() == 31) {
                        //继续完成之前小车未完成的任务
                        shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
                    }
 
                    if (shuttleThread == null) {
                        continue;
                    }
 
                    ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                    if (shuttleProtocol == null) {
                        continue;
                    }
 
                    if (outStaLocNo == null) {
                        continue;
                    }
 
                    if (wrkMast.getWrkSts() == 21) {
 
                        if (!shuttleProtocol.isIdle()) {
                            continue;
                        }
 
                        //源库位(小车当前位置)
                        String currentLocNo = shuttleProtocol.getCurrentLocNo();
 
                        //小车当前层高
                        Integer currentLev = Utils.getLev(currentLocNo);
                        //当前楼层提升机输送站点库位号
                        String liftSiteLocNo = Utils.levToOutInStaLocNo(currentLev);
 
                        ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                        //四向穿梭车号
                        assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                        //任务号
                        assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                        //入出库模式
                        assignCommand.setTaskMode(ShuttleTaskModeType.PAK_OUT.id.shortValue());
                        assignCommand.setSourceLocNo(currentLocNo);
 
                        //判断小车和库位是否在同一层
                        if (currentLev == Utils.getLev(wrkMast.getSourceLocNo())) {
                            //同一层(将小车移动到货物位置)
 
                            List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, wrkMast.getSourceLocNo(), liftSiteLocNo, assignCommand, shuttleThread);
                            if (commands == null) {
                                //未找到路径,等待下一次
                                continue;
                            }
 
                            //分配目标库位
                            shuttleProtocol.setLocNo(wrkMast.getSourceLocNo());
                            //分配任务号
                            shuttleProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //分配源库位
                            shuttleProtocol.setSourceLocNo(currentLocNo);
                            //目标库位
                            assignCommand.setLocNo(wrkMast.getSourceLocNo());
                            assignCommand.setCommands(commands);
                            wrkMast.setWrkSts(26L);//小车搬运中
 
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                //下发任务
                                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                            }
                        }else {
                            //不同层,将目标库位分配成提升机库位号(将小车移动到提升机位置)
 
                            //小车到提升机口指令
                            List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, liftSiteLocNo, ShuttleTaskModeType.PAK_IN.id, assignCommand, shuttleThread);
                            if (commands == null) {
                                if (!currentLocNo.equals(liftSiteLocNo)) {//当前位置也不在提升机口
                                    continue;//未找到路径
                                }
                                commands = new ArrayList<>();
                            }
                            shuttleProtocol.setLocNo(liftSiteLocNo);
                            //分配任务号
                            shuttleProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //分配源库位
                            shuttleProtocol.setSourceLocNo(currentLocNo);
 
                            //获取当前小车所在楼层的站点信息
                            BasDevp basDevp = basDevpService.queryByLocNo(liftSiteLocNo);
                            Short endStartCode = Short.parseShort(basDevp.getQrCodeValue());//站点二维码
 
                            //增加移动进提升机命令
                            ShuttleCommand moveCommand = shuttleThread.getMoveCommand(endStartCode, liftProtocol.getBarcode(), 1600, ShuttleRunDirection.TOP.id, null, null, 500);
                            commands.add(moveCommand);
 
                            //目标库位
                            assignCommand.setLocNo(liftSiteLocNo);
                            assignCommand.setCommands(commands);
                            wrkMast.setWrkSts(22L);//小车迁移状态
 
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                //下发任务
                                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                            }
                        }
                    } else if (wrkMast.getWrkSts() == 25) {
                        if (!shuttleProtocol.isIdle(wrkMast.getWrkNo().shortValue())) {
                            continue;
                        }
 
                        wrkMast.setShuttleNo(shuttleProtocol.getShuttleNo().intValue());//给工作档分配四向穿梭车号
 
                        //当前楼层提升机输送站点库位号
                        String liftSiteLocNo = Utils.levToOutInStaLocNo(liftProtocol.getLev().intValue());
 
                        ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                        //四向穿梭车号
                        assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                        //任务号
                        assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                        //入出库模式
                        assignCommand.setTaskMode(ShuttleTaskModeType.PAK_OUT.id.shortValue());
                        assignCommand.setSourceLocNo(liftSiteLocNo);
 
                        List<ShuttleCommand> commands = this.shuttleAssignCommand(liftSiteLocNo, wrkMast.getSourceLocNo(), liftSiteLocNo, assignCommand, shuttleThread);
                        if (commands == null) {
                            continue;//未找到路径
                        }
 
                        //此时车在提升机内部,需要多下达一步指令让车移动到提升机口
                        short startCode = liftProtocol.getBarcode();//提升机内部二维码
                        Short distCode = commands.get(0).getStartCodeNum();//目标二维码
                        //获取移动命令
                        ShuttleCommand moveCommand = shuttleThread.getMoveCommand(startCode, distCode, 1600, commands.get(0).getRunDirection(), null, null, 500);
                        commands.add(0, moveCommand);//将该指令添加到队头
 
                        //分配目标库位
                        shuttleProtocol.setLocNo(wrkMast.getSourceLocNo());
                        //分配任务号
                        shuttleProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());
                        //分配源库位
                        shuttleProtocol.setSourceLocNo(liftSiteLocNo);
                        //目标库位
                        assignCommand.setLocNo(wrkMast.getSourceLocNo());
                        assignCommand.setCommands(commands);
                        wrkMast.setWrkSts(26L);//小车搬运中
 
                        if (wrkMastMapper.updateById(wrkMast) > 0) {
                            //下发任务
                            MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                        }
                    }
 
                }
            }
        }
    }
 
    /**
     * 搜索空闲且最近的四向穿梭车(以工作档目标库位为基点计算最近且空闲的车)
     */
    public HashMap<String,Object> searchIdleShuttle(WrkMast wrkMast) {
        HashMap<String, Object> map = new HashMap<>();
        String locNo = wrkMast.getIoType() < 101 ? wrkMast.getLocNo() : wrkMast.getSourceLocNo();//库位号
        LocMast locMast = locMastService.queryByLoc(locNo);//找到库位记录
        int lev = Utils.getLev(locNo);//当前工作档库位层高
        ShuttleThread recentShuttle = null;//当前距离最近的四向穿梭车线程
 
        ArrayList<ShuttleThread> sameLev = new ArrayList<>();//相同楼层的穿梭车
        ArrayList<ShuttleThread> diffLev = new ArrayList<>();//不同楼层的穿梭车
 
        //判断其他空闲穿梭车是否离任务最近
        String distLocNo = null;//目标地点,入库=》提升机口,出库=》货物库位号
        if (wrkMast.getIoType() < 101 && wrkMast.getIoType() != 11) {
            //入库
            distLocNo = Utils.levToOutInStaLocNo(lev);
        }else if(wrkMast.getIoType() >= 101){
            //出库
            distLocNo = locNo;
        } else if (wrkMast.getIoType() == 11) {
            //库位移转
            distLocNo = wrkMast.getSourceLocNo();
        }
 
        //判断当前任务所在楼层是否有其他任务已经分配了小车,如有则直接用该小车(一层楼仅分配一台车)
        List<WrkMast> wrkMasts = wrkMastService.selectShuttleWrkByLev(lev);//判断当前穿梭车楼层是否已有分配车辆的任务,如果有则分配这辆车
        if (wrkMasts.size() > 0) {
            //存在其他任务,分配这辆车
            WrkMast wrkMast1 = wrkMasts.get(0);
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast1.getShuttleNo());
 
            map.put("sameLay", true);//同层
            map.put("result", shuttleThread);
            return map;
        }
 
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            //获取四向穿梭车线程
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null || shuttleProtocol.getShuttleNo() == null) {
                continue;
            }
            if (!shuttleProtocol.isIdle()) {
                continue;
            }
 
            String shuttleLocNo = shuttleProtocol.getCurrentLocNo();//二维码对应库位号
            if (shuttleLocNo == null) {
                continue;
            }
 
            if (shuttleLocNo.equals(distLocNo)) {
                //车辆当前位置已经是目标库位,无需计算
                map.put("sameLay", true);//同层
                map.put("result", shuttleThread);
                return map;
            }
 
            int shuttleLocNoLey = Utils.getLev(shuttleLocNo);//库位号对应层高
            if (lev == shuttleLocNoLey) {
                //工作档楼层相同的穿梭车
                sameLev.add(shuttleThread);
            }else {
                //工作档不同楼层的穿梭车
                diffLev.add(shuttleThread);
            }
 
        }
 
        Integer recentAllDistance = 9999999;
        if (sameLev.size() > 0) {
            //同一楼层有空闲穿梭车,则只在工作档楼层寻找
            //寻找离任务最近的穿梭车
            for (ShuttleThread shuttleThread : sameLev) {
                //当前穿梭车库位号
                String currentLocNo = shuttleThread.getShuttleProtocol().getCurrentLocNo();
                //当前穿梭车线程到目标地点距离
                List<NavigateNode> currentShuttlePath = NavigateUtils.calc(currentLocNo, distLocNo, NavigationMapType.NORMAL.id, Utils.getShuttlePoints(shuttleThread.getSlave().getId(), Utils.getLev(currentLocNo)));//搜索空闲穿梭车,使用正常通道地图
                if (currentShuttlePath == null) {
                    continue;
                }
                Integer currentAllDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
                if (currentAllDistance < recentAllDistance) {
                    //如果当前楼层的车路径更小,则更新最近穿梭车
                    recentShuttle = shuttleThread;
                }
            }
 
            map.put("sameLay", true);//同层
            map.put("result", recentShuttle);
        }else {
            //同一楼层,没有空闲穿梭车,只能从其他楼层调度
            //寻找离任务最近的穿梭车
            for (ShuttleThread shuttleThread : diffLev) {
 
                //当前穿梭车库位号
                String currentLocNo = shuttleThread.getShuttleProtocol().getCurrentLocNo();
                int currentLev = Utils.getLev(currentLocNo);
                List<WrkMast> wrkMasts1 = wrkMastService.selectNoShuttleWrkByLev(currentLev);//判断当前穿梭车楼层是否有待分配车辆的任务,如果有则不分配这辆车
                if (wrkMasts1.size() > 0) {
                    //存在其他任务,跳过这辆车
                    continue;
                }
 
                //当前穿梭车线程到当前车子所在楼层的提升机口距离
                List<NavigateNode> currentShuttlePath = NavigateUtils.calc(currentLocNo, Utils.levToOutInStaLocNo(currentLev), NavigationMapType.NORMAL.id, Utils.getShuttlePoints(shuttleThread.getSlave().getId(), currentLev));//搜索空闲穿梭车,使用正常通道地图
                if (currentShuttlePath == null) {
                    continue;
                }
 
                Integer currentAllDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
                if (currentAllDistance < recentAllDistance) {
                    //如果当前楼层的车路径更小,则更新最近穿梭车
                    recentShuttle = shuttleThread;
                }
            }
 
            map.put("sameLay", false);//不同层
            map.put("result", recentShuttle);
        }
        return map;
    }
 
    /**
     * 四向穿梭车任务完成
     */
    public synchronized void shuttleFinished() {
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            //获取四向穿梭车信息
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
 
            //四向穿梭车状态为等待确认、小车处于空闲状态
            if (shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.WAITING.id  //任务完成等待确认
                    && shuttleProtocol.getTaskNo() != 0
                    && shuttleProtocol.getBusyStatus() == 0
            ) {
                //标记复位
                shuttleProtocol.setPakMk(true);
 
                ShuttleAssignCommand assignCommand = shuttleProtocol.getAssignCommand();
 
                //将任务档标记为完成
                WrkMast wrkMast = wrkMastMapper.selectByWorkNo(shuttleProtocol.getTaskNo().intValue());
                ShuttleAssignCommand moveAssignCommand = null;//避让命令
                if (wrkMast != null) {
                    switch (wrkMast.getWrkSts().intValue()) {
                        case 9://9.小车入库搬运中 ==> 14.入库完成
                            wrkMast.setWrkSts(14L);
                            //任务号清零
                            shuttleProtocol.setTaskNo((short) 0);
                            break;
                        case 5://5.迁移小车至提升机口 ==> 6.迁移小车至提升机口完成
                            wrkMast.setWrkSts(6L);
                            break;
                        case 22://22.迁移小车至提升机口 ==> 23.迁移小车至提升机口完成
                            wrkMast.setWrkSts(23L);
                            break;
                        case 26://26.小车出库搬运中 ==> 27.小车出库搬运完成
                            //任务执行完后,小车进入移开提升机口站点位置,以免坠落
                            //搜索一条没有小车的空巷道,并调度小车
                            int distLev = Utils.getLev(wrkMast.getSourceLocNo());//避让楼层
                            String startLocNo = "180020" + distLev;
                            moveAssignCommand = Utils.searchEmptyGroupToMoveShuttle(distLev, shuttleThread.getSlave().getId(), shuttleThread, startLocNo);
                            if (moveAssignCommand == null) {//调度小车命令为空
                                continue;
                            }
 
                            wrkMast.setWrkSts(27L);
                            //任务号清零
                            shuttleProtocol.setTaskNo((short) 0);
                            break;
                        default:
                    }
 
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        //设置四向穿梭车为空闲状态
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                        //源库位清零
                        shuttleProtocol.setSourceLocNo(null);
                        //目标库位清零
                        shuttleProtocol.setLocNo(null);
                        //任务指令清零
                        shuttleProtocol.setAssignCommand(null);
                        News.info("四向穿梭车已确认且任务完成状态,复位。四向穿梭车号={}", shuttleProtocol.getShuttleNo());
 
                        if (wrkMast.getWrkSts() == 27) {
                            if (moveAssignCommand != null) {
                                try {
                                    Thread.sleep(4000);
                                    //下发任务
                                    shuttleProtocol.setPakMk(true);
                                    MessageQueue.offer(SlaveType.Shuttle, shuttleThread.getSlave().getId(), new Task(3, moveAssignCommand));
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        }
 
                    } else {
                        News.error("四向穿梭车已确认且任务完成状态,复位失败,但未找到工作档。四向穿梭车号={},工作号={}", shuttleProtocol.getShuttleNo(), shuttleProtocol.getTaskNo());
                    }
                }
 
                if (assignCommand != null) {
                    if (assignCommand.getTaskMode().intValue() == ShuttleTaskModeType.AVOID.id) {
                        //避让任务
                        //设置四向穿梭车为空闲状态
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                        //任务号清零
                        shuttleProtocol.setTaskNo((short) 0);
                        //源库位清零
                        shuttleProtocol.setSourceLocNo(null);
                        //目标库位清零
                        shuttleProtocol.setLocNo(null);
                        //任务指令清零
                        shuttleProtocol.setAssignCommand(null);
                        News.info("四向穿梭车避让任务已确认且任务完成状态,复位。四向穿梭车号={}", shuttleProtocol.getShuttleNo());
                    }
                }
 
            }
 
            //四向穿梭车状态为充电状态
            if (shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.CHARGING_WAITING.id  //充电标识
                    && shuttleProtocol.getTaskNo() != 0) {
                //查询是否有充电任务
                WrkCharge wrkCharge = wrkChargeMapper.selectByWorkNo(shuttleProtocol.getTaskNo().intValue());
                if (wrkCharge != null) {
                    switch (wrkCharge.getWrkSts().intValue()) {
                        case 52://四向穿梭车迁移到提升机口
                            wrkCharge.setWrkSts(53L);//迁移完成
                            shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);
                            break;
                        case 56://小车去充电桩中
                            wrkCharge.setWrkSts(57L);//到达充电桩
                            break;
                        default:
                    }
 
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        if (wrkCharge.getWrkSts() == 60) {
                            //设置四向穿梭车为空闲状态
                            shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                        }
                        if (wrkCharge.getWrkSts() != 57) {
                            //任务号清零
                            shuttleProtocol.setTaskNo((short) 0);
                            //源库位清零
                            shuttleProtocol.setSourceLocNo(null);
                            //目标库位清零
                            shuttleProtocol.setLocNo(null);
                            //标记复位
                            shuttleProtocol.setPakMk(true);
                            //任务指令清零
                            shuttleProtocol.setAssignCommand(null);
                        }
                        News.info("四向穿梭车已确认且任务完成状态,复位。四向穿梭车号={}", shuttleProtocol.getShuttleNo());
                    } else {
                        News.error("四向穿梭车已确认且任务完成状态,复位失败,但未找到工作档。四向穿梭车号={},工作号={}", shuttleProtocol.getShuttleNo(), shuttleProtocol.getTaskNo());
                    }
                }
            }
 
        }
    }
 
    /**
     * 提升机任务
     */
    public synchronized void liftIoExecute() {
        for (LiftSlave liftSlave : slaveProperties.getLift()) {
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId());
            if (liftThread == null) {
                continue;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                continue;
            }
 
 
//            if (!liftProtocol.isIdle()) {
//                continue;
//            }
 
            //判断提升机是否处于空闲状态,没有判断任务号,可能提升机处于空闲,但是还有任务未完成
            if (!liftProtocol.isIdleNoTask()) {
                continue;
            }
 
            //搜索是否有待处理的任务
            List<WrkMast> wrkMasts = wrkMastMapper.selectLiftStep262327();
            if (wrkMasts.size() == 0) {
                continue;
            }
 
            for (WrkMast wrkMast : wrkMasts) {
                //搜索是否有其他任务占用了提升机,如果占用提升机的任务和当前任务相同,则运行执行
                WrkMast wrkMast1 = wrkMastMapper.selectLiftWrkMast(liftProtocol.getLiftNo().intValue());
                if (wrkMast1 != null && wrkMast1.getWrkNo().intValue() != wrkMast.getWrkNo().intValue()) {
                    continue;
                }
 
                //命令list
                ArrayList<LiftCommand> commands = new ArrayList<>();
 
                DevpThread devpThread = null;
                Integer devpId = null;
                for (DevpSlave devp : slaveProperties.getDevp()){
                    // 获取入库站信息
                    devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    devpId = devp.getId();
                }
 
                if (wrkMast.getWrkSts() == 2) {//2.设备上走
                    if (liftProtocol.getTaskNo().intValue() != 0) {
                        //存在未完成任务号
                        continue;
                    }
                    if (liftProtocol.getPlatShuttleCheck()) {
                        //提升机此时有四向车,可能有未完成的任务,禁止分配新任务
                        continue;
                    }
 
                    //获取目标站
                    StaProtocol staProtocol = devpThread.getStation().get(wrkMast.getStaNo());
                    if (staProtocol.isLoading() || !staProtocol.isInEnable()) {//目标站有物,不可入,禁止分配任务
                        continue;
                    }
 
                    //工作档目标库位号
                    String wrkMastLocNo = wrkMast.getLocNo();
                    //工作档目标库位楼层
                    int wrkMastLocNoLey = Utils.getLev(wrkMastLocNo);
 
                    Integer levTmp = wrkMastLocNoLey;
                    if (wrkMastLocNoLey >= 2) {
                        levTmp += 1;
                    }
                    Integer distStaNo = Utils.levToOutInStaNo(levTmp);
 
                    if (liftProtocol.getPositionArrivalFeedback().intValue() != LiftLevType.TWO.realLev.intValue()) {
                        //提升机不在输送线楼层,获取到输送线层的提升机命令
                        LiftCommand command1 = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), LiftLevType.TWO.lev);
                        commands.add(command1);//将命令添加进list
                    }
 
                    //输送线将货物运进来(无货正转)
                    LiftCommand command2 = liftThread.getLiftTurnCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), 3);
                    command2.setOperaStaNo((short) 102);//操作102站
                    command2.setRotationDire(1);//给输送线下发链条转动信号,正转
                    command2.setDevpId(devpId);
                    command2.setStaNo(distStaNo.shortValue());//设置目标站
                    commands.add(command2);//将命令添加进list
 
                    //提升机前往目标楼层(工作档目标楼层)
                    LiftCommand command3 = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), levTmp);
                    commands.add(command3);//将命令添加进list
 
                    //提升机到达指定楼层,输送线将货物移出去(正转)
                    //输送线将货物移出去
                    LiftCommand command4 = liftThread.getLiftTurnCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), 1);
                    command4.setOperaStaNo(distStaNo.shortValue());//操作目标楼层站点
                    command4.setRotationDire(1);//给输送线下发链条转动信号,正转
                    command4.setDevpId(devpId);
                    command4.setStaNo(distStaNo.shortValue());//设置目标站
                    commands.add(command4);//将命令添加进list
 
                    //给提升机分配任务
                    liftProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());//设置任务号
                    liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
 
                    wrkMast.setLiftNo(liftProtocol.getLiftNo().intValue());//设置提升机号用于锁定提升机防止被其他任务占用
                    wrkMast.setWrkSts(3L);//3.提升机搬运中
                } else if (wrkMast.getWrkSts() == 6) {//6.迁移小车至提升机口完成 => 7.提升机迁移小车中
                    if (liftProtocol.getTaskNo().intValue() != 0 && liftProtocol.getTaskNo().intValue() != wrkMast.getWrkNo()) {
                        //提升机存在未完成任务,且提升机任务号和当前工作档任务号不一致
                        continue;
                    }
                    liftProtocol.setShuttleNo(wrkMast.getShuttleNo().shortValue());//设置四向穿梭车号
 
                    //判断小车是否在提升机内,且处于空闲状态
                    ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
                    if (shuttleThread == null) {
                        continue;
                    }
                    ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                    if (shuttleProtocol == null) {
                        continue;
                    }
                    if (!shuttleProtocol.isIdle(wrkMast.getWrkNo().shortValue())) {
                        continue;//小车状态忙
                    }
                    if (shuttleProtocol.getCurrentCode().intValue() != liftProtocol.getBarcode().intValue()) {
                        continue;//小车当前二维码和提升机内部二维码不一致,不允许执行
                    }
                    if (!liftProtocol.getPlatShuttleCheck()) {
                        //提升机未检测到小车,禁止执行
                        continue;
                    }
 
                    //工作档目标库位号
                    String wrkMastLocNo = wrkMast.getLocNo();
                    //工作档目标库位楼层
                    int wrkMastLocNoLey = Utils.getLev(wrkMastLocNo);
                    if (wrkMastLocNoLey >= 2) {
                        wrkMastLocNoLey++;
                    }
 
                    //提升机前往目标楼层(工作档目标楼层)
                    LiftCommand command1 = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), wrkMastLocNoLey);
                    commands.add(command1);//将命令添加进list
 
                    //给提升机分配任务
                    liftProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());//设置任务号
                    liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
 
                    wrkMast.setLiftNo(liftProtocol.getLiftNo().intValue());//设置提升机号用于锁定提升机防止被其他任务占用
                    wrkMast.setWrkSts(7L);//6.迁移小车至提升机口完成 => 7.提升机迁移小车中
                } else if(wrkMast.getWrkSts() == 23) {//23.迁移小车至提升机口完成 => 24.提升机迁移小车中
                    if (liftProtocol.getTaskNo().intValue() != 0 && liftProtocol.getTaskNo().intValue() != wrkMast.getWrkNo()) {
                        //提升机存在未完成任务,且提升机任务号和当前工作档任务号不一致
                        continue;
                    }
                    liftProtocol.setShuttleNo(wrkMast.getShuttleNo().shortValue());//设置四向穿梭车号
 
                    //判断小车是否在提升机内,且处于空闲状态
                    ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
                    if (shuttleThread == null) {
                        continue;
                    }
                    ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                    if (shuttleProtocol == null) {
                        continue;
                    }
 
                    if (!shuttleProtocol.isIdle(wrkMast.getWrkNo().shortValue())) {
                        continue;//小车状态忙
                    }
                    if (shuttleProtocol.getCurrentCode().intValue() != liftProtocol.getBarcode().intValue()) {
                        continue;//小车当前二维码和提升机内部二维码不一致,不允许执行
                    }
                    if (!liftProtocol.getPlatShuttleCheck()) {
                        //提升机未检测到小车,禁止执行
                        continue;
                    }
 
                    //工作档目标库位号
                    String wrkMastLocNo = wrkMast.getSourceLocNo();
                    //工作档目标库位楼层
                    int wrkMastLocNoLey = Utils.getLev(wrkMastLocNo);
                    if (wrkMastLocNoLey >= 2) {
                        wrkMastLocNoLey++;
                    }
 
                    //提升机前往目标楼层(工作档目标楼层)
                    LiftCommand command1 = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), wrkMastLocNoLey);
                    commands.add(command1);//将命令添加进list
 
                    //给提升机分配任务
                    liftProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());//设置任务号
                    liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
 
                    wrkMast.setLiftNo(liftProtocol.getLiftNo().intValue());//设置提升机号用于锁定提升机防止被其他任务占用
                    wrkMast.setWrkSts(24L);//23.迁移小车至提升机口完成 => 24.提升机迁移小车中
                } else if (wrkMast.getWrkSts() == 27) {//27.小车出库搬运完成
                    if (liftProtocol.getTaskNo().intValue() != 0 && liftProtocol.getTaskNo().intValue() != wrkMast.getWrkNo()) {
                        //提升机存在未完成任务,且提升机任务号和当前工作档任务号不一致
                        continue;
                    }
                    if (liftProtocol.getPlatShuttleCheck()) {
                        //提升机此时有四向车,可能有未完成的任务,禁止分配新任务
                        continue;
                    }
 
                    //工作档源库位号
                    String wrkMastLocNo = wrkMast.getSourceLocNo();
                    //工作档源库位楼层
                    int wrkMastLocNoLey = Utils.getLev(wrkMastLocNo);
 
                    //提升机当前楼层
                    int liftLev = liftProtocol.getLev().intValue();
 
                    //判断提升机是否到位
                    StaProtocol staProtocol = devpThread.getStation().get(Utils.levToOutInStaNo(wrkMastLocNoLey >= 2 ? wrkMastLocNoLey + 1 : wrkMastLocNoLey));//起始站点
                    if (liftLev != wrkMastLocNoLey && !staProtocol.isLiftArrival()) {
                        //提升机不在工作档源库位楼层,调度提升机
                        LiftCommand command1 = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), wrkMastLocNoLey);
                        commands.add(command1);//将命令添加进list
                    }
 
                    //输送线将货物运进来(无货反转)
                    LiftCommand command2 = liftThread.getLiftTurnCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), 4);
                    command2.setOperaStaNo(staProtocol.getSiteId().shortValue());//输送线操作站点号
                    command2.setRotationDire(2);//给输送线下发链条转动信号,反转
                    command2.setDevpId(devpId);//输送线ID
                    command2.setStaNo((short) 104);//写入出库目标站104
                    commands.add(command2);//将命令添加进list
 
                    //提升机前往出库口,输送线楼层
                    LiftCommand command3 = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), LiftLevType.TWO.lev);
                    command3.setDevpId(devpId);//输送线ID
                    commands.add(command3);//将命令添加进list
 
                    //提升机到达指定楼层,输送线将货物移出去(反转)
                    //输送线将货物移出去
                    LiftCommand command4 = liftThread.getLiftTurnCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), 2);
                    command4.setOperaStaNo((short) 102);//操作102站
                    command4.setRotationDire(2);//给输送线下发链条转动信号,反转
                    command4.setDevpId(devpId);//输送线ID
                    commands.add(command4);//将命令添加进list
 
                    //提升机链条执行完毕后,给102站写入资料
                    LiftCommand command5 = liftThread.getResetCommand();
                    command5.setDevpId(devpId);//输送线ID
                    command5.setOperaStaNo((short) 102);//操作102站
                    command5.setStaNo((short) 104);//写入出库目标站104
                    command5.setRotationDire(0);//链条转动停止
                    commands.add(command5);
 
                    //给提升机分配任务
                    liftProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());//设置任务号
                    liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
 
                    wrkMast.setLiftNo(liftProtocol.getLiftNo().intValue());//设置提升机号用于锁定提升机防止被其他任务占用
                    wrkMast.setWrkSts(28L);//28.提升机搬运中
                }
 
                //所需命令组合完毕,更新数据库,提交到线程去工作
                LiftAssignCommand assignCommand = new LiftAssignCommand();
                assignCommand.setCommands(commands);
                assignCommand.setLiftNo(liftProtocol.getLiftNo());
                assignCommand.setTaskNo(liftProtocol.getTaskNo());
                if (wrkMastMapper.updateById(wrkMast) > 0) {
                    //下发任务
                    MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
                }
            }
 
        }
    }
 
    /**
     * 提升机任务完成
     */
    public synchronized void liftFinished() {
        for (LiftSlave liftSlave : slaveProperties.getLift()) {
            //获取提升机信息
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId());
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                continue;
            }
 
            //提升机为等待确认且空闲
            if (liftProtocol.getProtocolStatus() == LiftProtocolStatusType.WAITING.id
                    && liftProtocol.getTaskNo() != 0
                    && !liftProtocol.getRunning()
            ) {
 
                DevpThread devpThread = null;
                Integer devpId = null;
                for (DevpSlave devp : slaveProperties.getDevp()){
                    // 获取入库站信息
                    devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    devpId = devp.getId();
                }
 
                //将任务档标记为完成
                WrkMast wrkMast = wrkMastMapper.selectByWorkNo372428(liftProtocol.getTaskNo().intValue());
                if (wrkMast != null) {
                    //判断提升机是否到达任务楼层
                    String locNo = wrkMast.getIoType() < 101 ? wrkMast.getLocNo() : wrkMast.getSourceLocNo();
                    int lev = Utils.getLev(locNo);//任务目标楼层
 
                    //标记复位
                    liftProtocol.setPakMk(true);
 
                    switch (wrkMast.getWrkSts().intValue()) {
                        case 3://3.提升机搬运中 ==> 4.提升机搬运完成
                            if (liftProtocol.getLev().intValue() != lev) {
                                continue;//提升机没有到达目标
                            }
                            //给目标站设置工作号
                            StaProtocol staProtocol = devpThread.getStation().get(wrkMast.getStaNo());//目标站
                            staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                            staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                            boolean result = MessageQueue.offer(SlaveType.Devp, devpId, new Task(2, staProtocol));
                            if (!result) {
                                throw new CoolException("更新plc站点信息失败");
                            }
                            wrkMast.setWrkSts(4L);
                            //任务号清零
                            liftProtocol.setTaskNo((short) 0);
                            wrkMast.setLiftNo(null);//提升机解锁
                            break;
                        case 7://7.提升机迁移小车中 ==> 8.提升机迁移小车完成
                            if (liftProtocol.getLev().intValue() != lev) {
                                continue;//提升机没有到达目标
                            }
                            wrkMast.setWrkSts(8L);
                            break;
                        case 24://24.提升机迁移小车中 ==> 25.提升机迁移小车完成
                            if (liftProtocol.getLev().intValue() != lev) {
                                continue;//提升机没有到达目标
                            }
                            wrkMast.setWrkSts(25L);
                            break;
                        case 28://28.提升机搬运中 ==> 29.提升机搬运完成
                            if (liftProtocol.getPositionArrivalFeedback().intValue() != 2) {
                                continue;//提升机是否达到输送线楼层
                            }
                            wrkMast.setWrkSts(29L);
                            wrkMast.setWrkSts(34L);//34.出库完成,暂时先直接完成出库工作档,后续需要根据输送线给出的状态来确定34.出库完成状态
                            //任务号清零
                            liftProtocol.setTaskNo((short) 0);
                            wrkMast.setLiftNo(null);//提升机解锁
                            break;
                        default:
                    }
 
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        //设置提升机为空闲状态
                        liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                        //任务指令清零
                        liftProtocol.setAssignCommand(null);
                        News.info("提升机已确认且任务完成状态。提升机号={}", liftProtocol.getLiftNo());
                    } else {
                        News.error("提升机已确认且任务完成状态,复位失败,但未找到工作档。提升机号={},工作号={}", liftProtocol.getLiftNo(), liftProtocol.getTaskNo());
                    }
                }
 
                //查询是否有充电任务
                WrkCharge wrkCharge = wrkChargeMapper.selectByWorkNo(liftProtocol.getTaskNo().intValue());
                if (wrkCharge != null) {
                    //标记复位
                    liftProtocol.setPakMk(true);
                    
                    switch (wrkCharge.getWrkSts().intValue()) {
                        case 54://提升机搬运中
                            wrkCharge.setWrkSts(55L);//提升机搬运完成
                            break;
                        default:
                    }
 
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        //设置提升机为空闲状态
                        liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                        //任务号清零
                        liftProtocol.setTaskNo((short) 0);
                        //标记复位
                        liftProtocol.setPakMk(true);
                        //任务指令清零
                        liftProtocol.setAssignCommand(null);
                        News.info("提升机已确认且任务完成状态,复位。提升机号={}", liftProtocol.getLiftNo());
                    } else {
                        News.error("提升机已确认且任务完成状态,复位失败,但未找到工作档。提升机号={},工作号={}", liftProtocol.getLiftNo(), liftProtocol.getTaskNo());
                    }
                }
 
                //不是入出库调度任务、工作档任务,进行提升机的复位
                if (liftProtocol.getAssignCommand() != null) {
                    //设置提升机为空闲状态
                    liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                    //判断是否为四向穿梭车调度提升机,如是则无需清理任务号
                    if (!liftProtocol.getSecurityMk()) {
                        //任务号清零
                        liftProtocol.setTaskNo((short) 0);
                    }
                    //标记复位
                    liftProtocol.setPakMk(true);
                    //任务指令清零
                    liftProtocol.setAssignCommand(null);
                    News.info("提升机已确认且任务完成状态,复位。提升机号={}", liftProtocol.getLiftNo());
                }
 
            }
        }
    }
 
    /**
     * 库位移转
     */
    public synchronized void locToLocExecute() {
        //获取出入库工作档
        List<WrkMast> wrkMasts = wrkMastMapper.selectInOutWrkMast();
        if (wrkMasts.size() > 0) {
            //有出入库任务,必须等待任务执行完毕再执行库位移转
            return;
        }
 
        //查询库位移转工作档
        List<WrkMast> wrkMasts1 = wrkMastMapper.selectLocToLocWrkMast();
        for (WrkMast wrkMast : wrkMasts1) {
 
            boolean step1 = this.locToLocExecuteStep1(wrkMast);//绑定小车
            if (!step1) {
                continue;
            }
 
            boolean step2 = this.locToLocExecuteStep2(wrkMast);//调度小车到目标楼层
            if (!step2) {
                continue;
            }
 
            boolean step3 = this.locToLocExecuteStep3(wrkMast);//同楼层库位移转
            if (!step3) {
                continue;
            }
 
        }
    }
 
    /**
     * 绑定小车并调度车
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean locToLocExecuteStep1(WrkMast wrkMast) {
        if (wrkMast.getShuttleNo() == null) {//给库位移转绑定穿梭车号
            //寻找最近且空闲的四向穿梭车
            HashMap<String,Object> searchIdleShuttle = this.searchIdleShuttle(wrkMast);
            ShuttleThread shuttleThread = (ShuttleThread) searchIdleShuttle.get("result");
            if (shuttleThread == null) {
                //没有找到空闲穿梭车
                return false;
            }
            wrkMast.setShuttleNo(shuttleThread.getSlave().getId());//给工作档分配四向穿梭车号
            wrkMastMapper.updateById(wrkMast);
        }
        return true;
    }
 
    /**
     * 调度小车到目标楼层
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean locToLocExecuteStep2(WrkMast wrkMast) {
        if (wrkMast.getWrkSts() == 1 && wrkMast.getShuttleNo() != null) {
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (!shuttleProtocol.isIdle(wrkMast.getWrkNo().shortValue())) {
                return false;//小车处于不空闲状态
            }
 
            String currentLocNo = shuttleProtocol.getCurrentLocNo();//小车当前库位号
            int shuttleLev = Utils.getLev(currentLocNo);//小车所在楼层
 
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
 
            //判断小车是否再目标楼层
            if (shuttleLev != Utils.getLev(wrkMast.getLocNo())) {
                //小车和目标不在同一楼层
 
                //提升机口站点库位号
                String liftSiteLocNo = Utils.levToOutInStaLocNo(shuttleLev);
 
                //创建分配命令
                ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
                assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
                assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());//入出库模式
                assignCommand.setSourceLocNo(currentLocNo);//源库位(小车当前位置)
 
                //小车移动到提升机口,计算路径
                List<ShuttleCommand> commands = this.shuttleAssignCommand(shuttleProtocol.getLocNo(), liftSiteLocNo, NavigationMapType.NONE.id, assignCommand, shuttleThread);
                if (commands == null) {
                    return false;//未找到路径
                }
 
                //获取当前小车所在楼层的站点信息
                BasDevp basDevp = basDevpService.queryByLocNo(liftSiteLocNo);
                Short endStartCode = Short.parseShort(basDevp.getQrCodeValue());//站点二维码
 
                //增加移动进提升机命令
                ShuttleCommand moveCommand = shuttleThread.getMoveCommand(endStartCode, liftProtocol.getBarcode(), 1600, ShuttleRunDirection.TOP.id, null, null, 500);
                commands.add(moveCommand);
 
                //分配目标库位
                shuttleProtocol.setLocNo(liftSiteLocNo);
                //目标库位
                assignCommand.setLocNo(liftSiteLocNo);
                assignCommand.setCommands(commands);
                wrkMast.setWrkSts(5L);//小车迁移状态
 
                if (wrkMastMapper.updateById(wrkMast) > 0) {
                    //下发任务
                    MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                }
            }
        }
        return true;
    }
 
    /**
     * 同楼层库位移转
     * 如需主方法执行continue,请返回false
     * ps:返回值true并不代表该方法执行成功,返回值仅做标记用于主方法是否执行continue
     */
    private boolean locToLocExecuteStep3(WrkMast wrkMast) {
        if (wrkMast.getShuttleNo() == null) {
            return false;
        }
 
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
        ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
        if (!shuttleProtocol.isIdle(wrkMast.getWrkNo().shortValue())) {
            return false;//小车处于不空闲状态
        }
 
        LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
        LiftProtocol liftProtocol = liftThread.getLiftProtocol();
 
        DevpThread devpThread = null;
        for (DevpSlave devp : slaveProperties.getDevp()){
            // 获取入库站信息
            devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
        }
 
        //判断小车是否在工作档任务目标楼层
        String currentLocNo = shuttleProtocol.getCurrentLocNo();//小车当前库位号
        int shuttleLev = Utils.getLev(currentLocNo);//小车所在楼层
        if (shuttleLev != Utils.getLev(wrkMast.getLocNo())) {
            return false;//不在同一楼层
        }
 
        if (wrkMast.getWrkSts() == 1 || wrkMast.getWrkSts() == 8) {
            //调度小车执行同楼层移库任务
 
            //创建分配命令
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号
            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());//任务号
            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());//入出库模式
            assignCommand.setSourceLocNo(currentLocNo);//源库位(小车当前位置)
 
            List<ShuttleCommand> commands = new ArrayList<>();
 
            if (wrkMast.getWrkSts() == 8) {//8.提升机迁移小车完成,需要将小车移出提升机
                //判断提升机是否空闲
                if (!liftProtocol.isIdleNoTask()) {
                    return false;//提升机忙
                }
                //判断提升机任务号和当前工作档任务号是否一致
                if (liftProtocol.getTaskNo().intValue() != 0 && liftProtocol.getTaskNo().intValue() != wrkMast.getWrkNo()) {
                    return false;
                }
 
                //判断提升机楼层是否到位,判断站点是否给出提升机到位信号
                String locNo = wrkMast.getLocNo();
                int lev = Utils.getLev(locNo);//目标二维码所在楼层
                int liftLev = liftProtocol.getLev().intValue();//提升机所在楼层
                if (liftLev != lev) {
                    return false;//提升机不在目标楼层跳过
                }
 
                Integer staNo = Utils.levToOutInStaNo(lev >= 2 ? lev + 1 : lev);
                //获取目标站信息
                StaProtocol staProtocol1 = devpThread.getStation().get(staNo);
                if (staProtocol1 == null) {
                    return false;//站点信息不存在
                }
                if (!staProtocol1.isLiftArrival()) {
                    return false;//站点提升机到位信号false
                }
 
                BasDevp basDevp = basDevpService.selectById(staNo);
                short startCode = liftProtocol.getBarcode();//提升机内部二维码
                Short distCode = Short.parseShort(basDevp.getQrCodeValue());//提升机口站点二维码
                Short runDirection = ShuttleRunDirection.BOTTOM.id;//运行方向
                //获取命令
                ShuttleCommand moveCommand = shuttleThread.getMoveCommand(startCode, distCode, 1600, runDirection, null, null, 500);
                commands.add(0, moveCommand);//将该指令添加到队头
 
                currentLocNo = basDevp.getLocNo();//使用输送站点口作为起点坐标
            }
 
            //直接计算车到源库位到目标库位路径
            List<ShuttleCommand> commands1 = this.shuttleAssignCommand(currentLocNo, wrkMast.getSourceLocNo(), wrkMast.getLocNo(), assignCommand, shuttleThread);
            if (commands1 == null) {
                return false;//找不到路径等待下一次
            }
            commands.addAll(commands1);
 
            //分配任务号
            shuttleProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());
            //分配源库位
            shuttleProtocol.setSourceLocNo(wrkMast.getSourceLocNo());
 
            assignCommand.setCommands(commands);
            //分配目标库位
            shuttleProtocol.setLocNo(wrkMast.getLocNo());
            //目标库位
            assignCommand.setLocNo(wrkMast.getLocNo());
            wrkMast.setWrkSts(9L);//小车入库中
            if (wrkMastMapper.updateById(wrkMast) > 0) {
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            }
        }
 
        return true;
    }
 
    /**
     * 异常信息记录
     */
    public void recErr() {
        try {
            this.recShuttleErr();
        } catch (Exception e) {
            News.error("recErr fail", e);
        }
    }
 
    /**
     * 四向穿梭车异常信息记录
     */
    private void recShuttleErr() {
        Date now = new Date();
        for (ShuttleSlave shuttleSlave : slaveProperties.getShuttle()) {
            // 获取堆垛机信息
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleSlave.getId());
            if (shuttleThread == null) {
                continue;
            }
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
 
            if (shuttleProtocol.getTaskNo() != 0) {
                //有任务
                BasShuttleErrLog latest = basShuttleErrLogService.findLatestByTaskNo(shuttleSlave.getId(), shuttleProtocol.getTaskNo().intValue());
                // 有异常
                if (latest == null) {
                    if (shuttleProtocol.getStatusErrorCode() != null && shuttleProtocol.getStatusErrorCode() > 0) {
                        WrkMast wrkMast = wrkMastMapper.selectById(shuttleProtocol.getTaskNo());
                        if (wrkMast == null) {
                            continue;
                        }
                        BasShuttleErr basShuttleErr = basShuttleErrService.queryByCode(shuttleProtocol.getStatusErrorCode().intValue());
                        String errName = basShuttleErr==null? "未知异常":basShuttleErr.getErrName();
                        BasShuttleErrLog basShuttleErrLog = new BasShuttleErrLog(
                                null,    // 编号
                                wrkMast.getWrkNo(),    // 工作号
                                now,    // 发生时间
                                null,    // 结束时间
                                wrkMast.getWrkSts(),    // 工作状态
                                wrkMast.getIoType(),    // 入出库类型
                                shuttleSlave.getId(),    // 四向穿梭车
                                null,    // plc
                                wrkMast.getLocNo(),    // 目标库位
                                wrkMast.getStaNo(),    // 目标站
                                wrkMast.getSourceStaNo(),    // 源站
                                wrkMast.getSourceLocNo(),    // 源库位
                                wrkMast.getBarcode(),    // 条码
                                (int) shuttleProtocol.getStatusErrorCode(),    // 异常码
                                errName,    // 异常
                                1,    // 异常情况
                                now,    // 添加时间
                                null,    // 添加人员
                                now,    // 修改时间
                                null,    // 修改人员
                                "任务中异常"    // 备注
                        );
                        if (!basShuttleErrLogService.insert(basShuttleErrLog)) {
                            News.error("四向穿梭车plc异常记录失败 ===>> [id:{}] [error:{}]", shuttleSlave.getId(), errName);
                        }
                    }
                } else {
                    // 异常修复
                    if (shuttleProtocol.getStatusErrorCode() == null || shuttleProtocol.getStatusErrorCode() == 0) {
                        latest.setEndTime(now);
                        latest.setUpdateTime(now);
                        latest.setStatus(2);
                        if (!basShuttleErrLogService.updateById(latest)) {
                            News.error("四向穿梭车plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", shuttleSlave.getId(), latest.getId());
                        }
                    }
                }
            }else {
                //无任务
                BasShuttleErrLog latest = basShuttleErrLogService.findLatest(shuttleSlave.getId());
                // 有异常
                if (shuttleProtocol.getStatusErrorCode() != null && shuttleProtocol.getStatusErrorCode() > 0) {
                    // 记录新异常
                    if (latest == null || (latest.getErrCode() != shuttleProtocol.getStatusErrorCode().intValue())) {
                        BasShuttleErr basShuttleErr = basShuttleErrService.queryByCode(shuttleProtocol.getStatusErrorCode().intValue());
                        String errName = basShuttleErr==null? "未知异常":basShuttleErr.getErrName();
                        BasShuttleErrLog basShuttleErrLog = new BasShuttleErrLog(
                                null,    // 编号
                                null,    // 工作号
                                now,    // 发生时间
                                null,    // 结束时间
                                null,    // 工作状态
                                null,    // 入出库类型
                                shuttleSlave.getId(),    // 四向穿梭车
                                null,    // plc
                                null,    // 目标库位
                                null,    // 目标站
                                null,    // 源站
                                null,    // 源库位
                                null,    // 条码
                                (int)shuttleProtocol.getStatusErrorCode(),    // 异常码
                                errName,    // 异常
                                1,    // 异常情况
                                now,    // 添加时间
                                null,    // 添加人员
                                now,    // 修改时间
                                null,    // 修改人员
                                "无任务异常"    // 备注
                        );
                        if (!basShuttleErrLogService.insert(basShuttleErrLog)) {
                            News.error("四向穿梭车plc异常记录失败 ===>> [id:{}] [error:{}]", shuttleSlave.getId(), errName);
                        }
                    }
                    // 无异常
                } else {
                    // 异常修复
                    if (latest != null && latest.getStatus() == 1) {
                        latest.setEndTime(now);
                        latest.setUpdateTime(now);
                        latest.setStatus(2);
                        if (!basShuttleErrLogService.updateById(latest)) {
                            News.error("四向穿梭车plc异常记录修复失败 ===>> [id:{}] [errLogId:{}]", shuttleSlave.getId(), latest.getId());
                        }
                    }
                }
            }
        }
    }
 
    // -------------------------------------------------------------------------------
 
    /**
     * 空栈板初始化入库,叉车入库站放货
     */
    public void storeEmptyPlt(){
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历空板入库口
            for (DevpSlave.Sta emptyInSta : devp.getEmptyInSta()) {
                // 获取空板入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(emptyInSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                // 站点条件判断
                if (staProtocol.isAutoing()
                        && staProtocol.isLoading()
                        && staProtocol.isInEnable()
                        && staProtocol.isEmptyMk()
                        && (staProtocol.getWorkNo() == 0 || (staProtocol.getWorkNo() >= 9990 || staProtocol.getWorkNo() <= 9999))
                        && staProtocol.isPakMk()) {
 
                    // 获取条码扫描仪信息
                    String barcode = null;
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, emptyInSta.getBarcode());
                    if (barcodeThread != null) {
                        String barcode0 = barcodeThread.getBarcode();
                        if(!Cools.isEmpty(barcode0)) {
//                            News.info("{}号条码扫描器检测条码信息:{}", emptyInSta.getBarcode(), barcode0);
                            if(!"NG".endsWith(barcode0) && !"NoRead".equals(barcode0) && !"empty".equals(barcode0)) {
                                barcode = barcode0;
                            }
                        }
                    }
 
                    LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, emptyInSta.getLed());
 
                    try {
                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
 
                        SearchLocParam param = new SearchLocParam();
                        param.setIoType(10);
                        if (!Cools.isEmpty(barcode)) {
                            param.setBarcode(barcode);
                        }
                        param.setSourceStaNo(emptyInSta.getStaNo());
//                        param.setLocType1(locTypeDto.getLocType1());
                        String response = new HttpHandler.Builder()
                                .setUri(wmsUrl)
                                .setPath("/rpc/pakin/loc/v1")
                                .setJson(JSON.toJSONString(param))
                                .build()
                                .doPost();
                        JSONObject jsonObject = JSON.parseObject(response);
                        Integer code = jsonObject.getInteger("code");
                        if (code.equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
 
                            // 更新站点信息 且 下发plc命令
                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
                            staProtocol.setStaNo(dto.getStaNo().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            if (!result) {
                                throw new CoolException("更新plc站点信息失败");
                            }
                        } else {
                            if (ledThread != null) {
                                String errorMsg = jsonObject.getString("msg");
                                if (!Cools.isEmpty(errorMsg)) {
                                    MessageQueue.offer(SlaveType.Led, emptyInSta.getLed(), new Task(3, errorMsg));
                                }
                            }
                            News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
 
                }
 
 
            }
        }
    }
 
    /**
     * 出库  ===>> 工作档信息写入led显示器
     */
    public void ledExecute() {
        for (LedSlave led : slaveProperties.getLed()) {
            // 获取输送线plc线程
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
            // 命令集合
            List<LedCommand> commands = new ArrayList<>();
            // 工作档集合
            List<WrkMast> wrkMasts = new ArrayList<>();
            for (Integer staNo : led.getStaArr()) {
                // 获取叉车站点
                StaProtocol staProtocol = devpThread.getStation().get(staNo);
                if (null == staProtocol || null == staProtocol.getWorkNo() || 0 == staProtocol.getWorkNo() || !staProtocol.isLoading()) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                // 获取工作档数据
                WrkMast wrkMast = wrkMastMapper.selectById(staProtocol.getWorkNo());
                if (null == wrkMast || wrkMast.getWrkSts() < 14 || wrkMast.getIoType() < 100) { continue; }
                wrkMasts.add(wrkMast);
                // 组装命令
                LedCommand ledCommand = new LedCommand();
                ledCommand.setWorkNo(wrkMast.getWrkNo());
                ledCommand.setIoType(wrkMast.getIoType());
                // 出库模式
                switch (wrkMast.getIoType()) {
                    case 101:
                        ledCommand.setTitle("全板出库");
                        break;
                    case 103:
                        ledCommand.setTitle("拣料出库");
                        break;
                    case 104:
                        ledCommand.setTitle("并板出库");
                        break;
                    case 107:
                        ledCommand.setTitle("盘点出库");
                        break;
                    case 110:
                        ledCommand.setTitle("空板出库");
                        ledCommand.setEmptyMk(true);
                        break;
                    default:
                        News.error("任务入出库类型错误!!![工作号:{}] [入出库类型:{}]", wrkMast.getWrkNo(), wrkMast.getIoType());
                        break;
                }
                ledCommand.setSourceLocNo(wrkMast.getSourceLocNo());
                ledCommand.setLocNo(wrkMast.getLocNo());
                ledCommand.setStaNo(wrkMast.getStaNo());
                if (wrkMast.getIoType() != 110 && wrkMast.getIoType() != 10) {
                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkMast.getWrkNo());
                    wrkDetls.forEach(wrkDetl -> ledCommand.getMatDtos().add(new MatDto(wrkDetl.getMatnr(), wrkDetl.getMaktx(), wrkDetl.getAnfme(),wrkDetl.getSpecs())));
                }
                commands.add(ledCommand);
            }
            Set<Integer> workNos = wrkMasts.stream().map(WrkMast::getWrkNo).collect(Collectors.toSet());
            // 获取LED线程
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
            // 相同工作号集合则过滤
            if (CollectionUtils.equals(ledThread.getWorkNos(), workNos)) {
                continue;
            }
            // 命令下发 -------------------------------------------------------------------------------
            if (!commands.isEmpty()) {
                if (led.getId()>3){
                    if (!MessageQueue.offer(SlaveType.Led, led.getId()-3, new Task(1, commands))) {
                        log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId()-3, led.getIp(), led.getPort());
                        continue;
                    }else {
                        ledThread.setLedMk(false);
                    }
                }else {
                    if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(1, commands))) {
                        log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                        continue;
                    }else {
                        ledThread.setLedMk(false);
                    }
                }
            }
 
            try {
                // 修改主档led标记
                for (WrkMast wrkMast : wrkMasts) {
                    wrkMast.setOveMk("Y");
                    wrkMast.setModiTime(new Date());
                    if (wrkMastMapper.updateById(wrkMast) == 0) {
                        throw new CoolException("更新工作档失败");
                    }
                }
 
                // 更新线程当前工作号集合
                ledThread.setWorkNos(workNos);
 
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
 
        }
    }
 
    /**
     * 其他  ===>> LED显示器复位,显示默认信息
     */
    public void ledReset() {
        for (LedSlave led : slaveProperties.getLed()) {
 
            // 获取输送线plc线程
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
            // 命令集合
            boolean reset = true;
            for (Integer staNo : led.getStaArr()) {
                // 获取叉车站点
                StaProtocol staProtocol = devpThread.getStation().get(staNo);
                if (staProtocol == null) {
                    continue;
                }
                if (staProtocol.getWorkNo() != 0 && staProtocol.isLoading()) {
                    reset = false;
                    break;
                }
            }
            // 获取led线程
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
            // led显示默认内容
            if (reset && !ledThread.isLedMk()) {
                ledThread.setLedMk(true);
                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(4, new ArrayList<>()))) {
                    News.error(" - {}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                } else {
 
                }
            }
        }
        for (LedSlave led : slaveProperties.getLed()) {
            // 获取输送线plc线程
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, led.getDevpPlcId());
            // 命令集合
            boolean reset = true;
            for (Integer staNo : led.getStaArr()) {
                // 获取叉车站点
                StaProtocol staProtocol = devpThread.getStation().get(staNo);
                if (staProtocol == null) { continue; }
                if (staProtocol.getWorkNo() != 0) {
                    reset = false;
                    break;
                }
            }
            // 获取led线程
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, led.getId());
            // led显示默认内容
            if (reset && !ledThread.isLedMk()) {
                ledThread.setLedMk(true);
                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(2, new ArrayList<>()))) {
                    News.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                }
            }
        }
    }
 
    /**
     * 四向穿梭车电量检测 ===>> 发起充电
     */
    public synchronized void loopShuttleCharge() {
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            //获取四向穿梭车线程
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
 
            //判断当前小车是否满足需要充电要求
            if (!shuttleProtocol.isRequireCharge()) {
                continue;
            }
 
            WrkCharge wrkCharge = wrkChargeService.selectWorking(shuttleProtocol.getShuttleNo().intValue());
            if (wrkCharge != null) {//已有充电任务
                continue;
            }
 
            ShuttleChargeType shuttleCharge = null;
            for (ShuttleChargeType chargeType : ShuttleChargeType.values()) {
                if (wrkChargeService.selectWorkingOfCharge(chargeType.id) == null) {
                    shuttleCharge = chargeType;
                    break;
                }
            }
 
            if (shuttleCharge == null) {
                continue;
            }
 
            String chargeLocNo = shuttleCharge.locNo;
            wrkCharge = new WrkCharge();
            wrkCharge.setShuttleNo(shuttle.getId());
            wrkCharge.setCharge(shuttleCharge.id);
            wrkCharge.setWrkNo(commonService.getChargeWorkNo(4));
            wrkCharge.setWrkSts(51L);   // 21.准备充电
            wrkCharge.setIoPri((double) 10);
            wrkCharge.setLocNo(chargeLocNo);
            wrkCharge.setMemo("charge");
            wrkCharge.setAppeTime(new Date());
            if (!wrkChargeService.insert(wrkCharge)) {
                News.error("保存{}号四向穿梭车充电任务失败!!!", shuttle.getId());
                continue;
            }
 
            shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);//充电中
            News.info("保存{}号四向穿梭车充电任务成功!!!", shuttle.getId());
        }
    }
 
    /**
     * 执行四向穿梭车充电任务
     */
    public synchronized void executeShuttleCharge() {
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            WrkCharge wrkCharge = wrkChargeService.selectWorking(shuttle.getId());
            if (wrkCharge == null) {
                continue;
            }
 
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkCharge.getShuttleNo());
            if (shuttleThread == null) {
                continue;
            }
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
 
            //获取提升机
            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1);
            if (liftThread == null) {
                continue;
            }
            LiftProtocol liftProtocol = liftThread.getLiftProtocol();
            if (liftProtocol == null) {
                continue;
            }
            //充电库位号
            String chargeLocNo = wrkCharge.getLocNo();
            //充电库位层高
            Integer chargeLocNoLev = Utils.getLev(chargeLocNo);
 
            if (wrkCharge.getWrkSts() == 51) {
                //当前穿梭车库位号
                String currentLocNo = shuttleProtocol.getCurrentLocNo();
                if (currentLocNo == null) {
                    continue;
                }
                //小车当前层高
                Integer currentLev = Utils.getLev(currentLocNo);
 
                if (currentLev == chargeLocNoLev) {
                    //同一层无需经过提升机
                    //直接计算车到充电库位
 
                    ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
 
                    //获取小车到充电库位路径指令
                    List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, chargeLocNo, NavigationMapType.NONE.id, assignCommand, shuttleThread);
                    if (commands == null) {
                        continue;//未找到路径
                    }
                    //进行充电中
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);
 
                    assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                    assignCommand.setTaskMode(ShuttleTaskModeType.CHARGE.id.shortValue());//充电
                    assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());
                    assignCommand.setCharge(true);//充电任务
 
                    //创建充电指令
                    ShuttleCommand command = shuttleThread.getChargeSwitchCommand((short) 1);//开始充电
                    commands.add(command);
 
                    //指令集分配
                    assignCommand.setCommands(commands);
 
                    wrkCharge.setWrkSts(56L);//充电中状态
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        //下发任务
                        MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                    }
                }else {
                    //不同层,调度小车到充电桩目标层
 
                    ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
 
                    //获取小车当前楼层的站点号
                    String liftSiteLocNo = Utils.levToOutInStaLocNo(currentLev);
 
                    //小车移动到提升机口站点,计算路径
                    List<ShuttleCommand> commands = this.shuttleAssignCommand(currentLocNo, liftSiteLocNo, NavigationMapType.NONE.id, assignCommand, shuttleThread);
                    if (commands == null) {
                        continue;//未找到路径
                    }
 
                    //获取当前小车所在楼层的站点信息
                    BasDevp basDevp = basDevpService.queryByLocNo(liftSiteLocNo);
                    if (basDevp == null) {
                        continue;//找不到站点信息
                    }
                    Short basDevpQrCode = Short.parseShort(basDevp.getQrCodeValue());//站点二维码
 
                    //增加移动进提升机命令
                    ShuttleCommand moveCommand = shuttleThread.getMoveCommand(basDevpQrCode, liftProtocol.getBarcode(), 1600, ShuttleRunDirection.TOP.id, basDevpQrCode, 1600, 500);
                    commands.add(moveCommand);
 
                    //分配目标库位
                    shuttleProtocol.setLocNo(chargeLocNo);
                    assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                    assignCommand.setTaskMode(ShuttleTaskModeType.CHARGE.id.shortValue());//充电
                    assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());
                    assignCommand.setCharge(true);//充电任务
                    //目标库位
                    assignCommand.setLocNo(chargeLocNo);
                    //源库位
                    assignCommand.setSourceLocNo(currentLocNo);
                    assignCommand.setCommands(commands);
                    wrkCharge.setWrkSts(52L);//小车迁移状态
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        //下发任务
                        MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                    }
                }
 
            }else if(wrkCharge.getWrkSts() == 53){
                //小车已经达到提升机内
 
                //判断提升机是否处于空闲状态
                if (!liftProtocol.isIdle(wrkCharge.getWrkNo().shortValue())) {
                    continue;
                }
 
                //判断小车是否在提升机内
                if (shuttleProtocol.getCurrentCode().intValue() != liftProtocol.getBarcode().intValue()) {
                    //小车不在提升机内
                    continue;
                }
 
                //给提升机分配任务
                liftProtocol.setTaskNo(wrkCharge.getWrkNo().shortValue());//设置任务号
                liftProtocol.setShuttleNo(wrkCharge.getShuttleNo().shortValue());//设置四向穿梭车号
                liftProtocol.setProtocolStatus(LiftProtocolStatusType.WORKING);//设置提升机状态为工作中
 
                //命令list
                ArrayList<LiftCommand> commands = new ArrayList<>();
 
                //提升机前往目标楼层
                //获取充电库位目标楼层命令
                LiftCommand command1 = liftThread.getLiftUpDownCommand(liftProtocol.getLiftNo(), liftProtocol.getTaskNo(), chargeLocNoLev >= 2 ? chargeLocNoLev + 1 : chargeLocNoLev);
                commands.add(command1);//将命令添加进list
 
                wrkCharge.setWrkSts(54L);//提升机搬运中
                //所需命令组合完毕,更新数据库,提交到线程去工作
                LiftAssignCommand assignCommand = new LiftAssignCommand();
                assignCommand.setCommands(commands);
                assignCommand.setLiftNo(liftProtocol.getLiftNo());
                assignCommand.setTaskNo(liftProtocol.getTaskNo());
                if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                    //下发任务
                    MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand));
                }
 
            }else if(wrkCharge.getWrkSts() == 55){//55.提升机迁移小车完成
                //直接计算车到充电库位
 
                ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
 
                Short liftLev = liftProtocol.getLev();
                if (liftLev == null) {
                    continue;
                }
                //判断提升机楼层是否到达目标楼层
                if (liftLev.intValue() != chargeLocNoLev) {
                    continue;//没有到达目标楼层
                }
 
                //此时车在提升机内部,下达一步指令让车移动到提升机口
                Integer staNo = Utils.levToOutInStaNo(liftLev >= 2 ? liftLev + 1 : liftLev);//站点号
                BasDevp basDevp = basDevpService.selectById(staNo);
                if (basDevp == null) {
                    continue;//站点不存在
                }
 
                //获取提升机口到充电库位路径指令
                List<ShuttleCommand> commands = this.shuttleAssignCommand(basDevp.getLocNo(), chargeLocNo, NavigationMapType.NONE.id, assignCommand, shuttleThread);
                if (commands == null) {
                    continue;//未找到路径
                }
 
                short startCode = liftProtocol.getBarcode();//提升机内部二维码
                Short distCode = Short.parseShort(basDevp.getQrCodeValue());//提升机口站点二维码
                Short runDirection = ShuttleRunDirection.BOTTOM.id;//运行方向
                //获取命令
                ShuttleCommand moveCommand = shuttleThread.getMoveCommand(startCode, distCode, 1600, runDirection, startCode, 1600, 500);
                commands.add(0, moveCommand);//将该指令添加到队头
 
                //进行充电中
                shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);
 
                assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                assignCommand.setTaskMode(ShuttleTaskModeType.CHARGE.id.shortValue());//充电
                assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());
                assignCommand.setCharge(true);//充电任务
 
                //创建充电指令
                ShuttleCommand command = shuttleThread.getChargeSwitchCommand((short) 1);//开始充电
                commands.add(command);
 
                //指令集分配
                assignCommand.setCommands(commands);
 
                wrkCharge.setWrkSts(56L);//充电中状态
                if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                    //下发任务
                    MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                }
            }else if (wrkCharge.getWrkSts() == 57) {//57.小车到达充电桩
                //充电中
                //判断小车是否充满电量,满电1000或电压54V以上
                if (shuttleProtocol.getBatteryPower() == null || shuttleProtocol.getCurrentVoltage() == null) {
                    continue;
                }
                if (shuttleProtocol.getBatteryPower() >= 1000 && shuttleProtocol.getCurrentVoltage() >= 54000) {
                    //充满,断开充电
//                    List<ShuttleCommand> commands = new ArrayList<>();
//                    ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
//                    assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
//                    assignCommand.setTaskMode((short) 0);
//                    assignCommand.setTaskNo(wrkCharge.getWrkNo().shortValue());
//                    assignCommand.setCharge(true);
//
//                    //创建充电指令
//                    ShuttleCommand command = shuttleThread.getChargeSwitchCommand((short) 2);//断开充电
//                    commands.add(command);
//
//                    //指令集分配
//                    assignCommand.setCommands(commands);
//
//                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING_WAITING);
 
                    //将小车移动到空闲的巷道
                    ShuttleAssignCommand assignCommand = Utils.searchEmptyGroupToMoveShuttle(Utils.getLev(shuttleProtocol.getLocNo()), shuttleProtocol.getShuttleNo().intValue(), shuttleThread, null);
                    if (assignCommand == null) {
                        continue;
                    }
 
                    wrkCharge.setWrkSts(60L);//60.充电任务完成
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                        //下发任务
                        MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                    }
                }
 
                if (shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.CHARGING_WAITING.id) {
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING);
                }
            }
        }
 
    }
 
 
    /**
     * 出入库模式切换
     */
    public synchronized void outAndIn() {
        try {
            // 根据输送线plc遍历
            for (DevpSlave devp : slaveProperties.getDevp()) {
                for (DevpSlave.Sta inSta : devp.getInSta()) {
                    Thread.sleep(500);
                    boolean a=true,b=true;
                    List<WrkMast> wrkMasts = wrkMastMapper.selectAll(inSta.getStaNo());
                    if (Cools.isEmpty(wrkMasts)){
                        b=false;
                    }else {
                        for (WrkMast wrkMast:wrkMasts){
                            if (wrkMast.getSourceStaNo() > wrkMast.getStaNo()){
                                a=false;
                                break;
                            }
                        }
                    }
                    switch (inSta.getStaNo()) {
                        case 102://1F
                            if (a && b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 11));
                            }else if (b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 12));
                            }else {
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 10));
                            }
                            break;
                        case 202://2F
                            if (a && b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 21));
                            }else if (b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 22));
                            }else {
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 20));
                            }
                            break;
                        case 302://3F
                            if (a && b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 31));
                            }else if (b){
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 32));
                            }else {
                                MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, 30));
                            }
                            break;
                    }
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
}