自动化立体仓库 - WCS系统
#
Junjie
2025-04-10 1d624dfdb040825ffecba6c792c480be662b2dbc
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
package com.zy.core.action;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.asrs.entity.BasShuttleOpt;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasShuttleOptService;
import com.zy.asrs.service.BasShuttleService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.utils.Utils;
import com.zy.common.ExecuteSupport;
import com.zy.common.model.NavigateNode;
import com.zy.common.utils.NavigateMapUtils;
import com.zy.common.utils.RedisUtil;
import com.zy.common.utils.ShuttleOperaUtils;
import com.zy.core.News;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.dispatcher.ShuttleDispatchUtils;
import com.zy.core.enums.*;
import com.zy.core.model.CommandResponse;
import com.zy.core.model.command.ShuttleAssignCommand;
import com.zy.core.model.command.ShuttleCommand;
import com.zy.core.model.command.ShuttleRedisCommand;
import com.zy.core.model.protocol.ShuttleProtocol;
import com.zy.core.thread.ShuttleThread;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Component
public class ShuttleAction {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private NavigateMapUtils navigateMapUtils;
    @Autowired
    private BasShuttleService basShuttleService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private ConfigService configService;
    @Autowired
    private BasShuttleOptService basShuttleOptService;
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private ShuttleDispatchUtils shuttleDispatchUtils;
 
    public synchronized boolean assignWork(Integer shuttleNo, ShuttleAssignCommand assignCommand) {
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
        if (shuttleThread == null) {
            return false;
        }
 
        ShuttleRedisCommand redisCommand = new ShuttleRedisCommand();
 
        redisCommand.setShuttleNo(assignCommand.getShuttleNo());//四向穿梭车号
        redisCommand.setWrkNo(assignCommand.getTaskNo());//工作号
        redisCommand.setCommandStep(0);//命令执行步序
        redisCommand.setAssignCommand(assignCommand);//命令
        //任务数据保存到redis
        if (redisUtil.set(RedisKeyType.SHUTTLE_WORK_FLAG.key + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand, SerializerFeature.DisableCircularReferenceDetect))) {
            shuttleThread.setSyncTaskNo(assignCommand.getTaskNo());
            return true;
        }
        return false;
    }
 
    public synchronized boolean executeWork(Integer shuttleNo, Integer taskNo) {
        Object obj = redisUtil.get(RedisKeyType.SHUTTLE_WORK_FLAG.key + taskNo);
        if (obj == null) {
            return false;
        }
 
        ShuttleRedisCommand redisCommand = null;
        try {
            redisCommand = objectMapper.readValue(String.valueOf(obj), ShuttleRedisCommand.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
 
        if (redisCommand == null) {
            return false;
        }
 
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
        if (shuttleThread == null) {
            return false;
        }
 
        ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
        if (shuttleProtocol == null) {
            return false;
        }
 
        //判断工作号是否相同
        if (!shuttleProtocol.getTaskNo().equals(taskNo)) {
            return false;
        }
 
        List<ShuttleCommand> commands = redisCommand.getAssignCommand().getCommands();
        if (commands.isEmpty()) {
            return false;
        }
 
        ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand();
        int commandStep = redisCommand.getCommandStep();
 
        Integer mode = -1;
        if(commandStep < commands.size()) {
            //取出命令
            ShuttleCommand currentCommand = commands.get(commandStep);
            mode = currentCommand.getMode();
        }
 
        //判断设备是否空闲
        Integer finalMode = mode;
        if (!shuttleThread.isDeviceIdle(new ExecuteSupport() {
            @Override
            public Boolean judgement() {
                if (ShuttleCommandModeType.CHARGE_CLOSE.id.equals(finalMode)) {//关闭充电
                    return false;//不需要判断状态
                }
                return true;//需要判断状态
            }
        })) {
            return false;
        }
 
        // 完结上一条命令
        boolean updateCommand = false;
        if (commandStep != 0) {
            ShuttleCommand command = commands.get(commandStep - 1);
            if (command.getMode() == ShuttleCommandModeType.MOVE.id) {
                // 正常移动
                if (command.getTargetLocNo().equals(shuttleProtocol.getCurrentLocNo())) {
                    command.setComplete(true);
                    updateCommand = true;
                    //解锁锁定路径,上一条路径
                    List<NavigateNode> nodes = null;
                    try {
                        String nodesStr = objectMapper.writeValueAsString(command.getNodes());
                        nodes = objectMapper.readValue(nodesStr, new TypeReference<List<NavigateNode>>() {
                        });
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
 
                    if (nodes != null) {
                        NavigateNode targetNode = assignCommand.getNodes().get(assignCommand.getNodes().size() - 1);//最终节点
                        NavigateNode node = nodes.get(nodes.size() - 1);
                        if (!(targetNode.getX() == node.getX() && targetNode.getY() == node.getY())) {
                            nodes.remove(nodes.size() - 1);//剔除尾节点
                        }
                        boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(shuttleProtocol.getCurrentLocNo()), shuttleProtocol.getShuttleNo(), nodes, false);//解锁路径
                        if (!result) {
                            return false;//解锁失败
                        }
                    }
                }
            } else if (command.getMode() == ShuttleCommandModeType.PALLET_LIFT.id) {
                // 托盘顶升
                //判断是否顶升到位
                if (shuttleProtocol.getHasLift()) {
                    command.setComplete(true);
                    updateCommand = true;
//                    //判断是否有物
//                    if (shuttleProtocol.getHasPallet()) {
//                        command.setComplete(true);
//                    }
                }
            } else if (command.getMode() == ShuttleCommandModeType.PALLET_DOWN.id) {
                // 托盘下降命令
                // 判断是否下降到位
                if (!shuttleProtocol.getHasLift()) {
                    command.setComplete(true);
                    updateCommand = true;
                }
            } else if (command.getMode() == ShuttleCommandModeType.CHARGE_OPEN.id) {
                // 充电开
                //判断小车充电状态
                if (shuttleProtocol.getHasCharge()) {
                    command.setComplete(true);
                    updateCommand = true;
                }
            }else {
                command.setComplete(true);//其他命令默认认为完成
                updateCommand = true;
            }
 
            if(updateCommand) {
                // 更新redis数据
                redisUtil.set(RedisKeyType.SHUTTLE_WORK_FLAG.key + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand, SerializerFeature.DisableCircularReferenceDetect));
            }
 
            if (!command.getComplete()) {
                return false;
            }
 
            //判断是否为最后一条命令且命令执行完成,抛出等待确认状态
            ShuttleCommand endCommand = commands.get(commands.size() - 1);
            if (endCommand.getComplete()) {
                News.info("四向穿梭车任务执行下发完成等待执行结束,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(commands));
 
                // 系统任务
                if (assignCommand.getAuto()) {
                    if (!assignCommand.getCharge()) {
                        if (command.getMode() == ShuttleCommandModeType.UPDATE_LOCATION.id) {
                            //更新坐标任务无需抛出等待确认
                            shuttleThread.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                            shuttleThread.setSyncTaskNo(0);
                        }else {
                            //对主线程抛出等待确认状态waiting
                            shuttleThread.setProtocolStatus(ShuttleProtocolStatusType.WAITING);
                        }
                    }else {
                        shuttleThread.setProtocolStatus(ShuttleProtocolStatusType.CHARGING_WAITING);
                    }
                    News.info("四向穿梭车任务执行下发完成等待执行结束,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
 
                    // 手动任务
                } else {
                    //手动模式不抛出等待状态,直接复位空闲状态
                    shuttleThread.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                    //任务号清零
                    shuttleThread.setSyncTaskNo(0);
                    //标记复位
                    shuttleThread.setPakMk(true);
                    News.info("四向穿梭车手动任务执行完成,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
                }
 
                //删除redis
                redisUtil.del(RedisKeyType.SHUTTLE_WORK_FLAG.key + redisCommand.getWrkNo());
                return false;//禁止再下发命令
            }
        }
 
        //取出命令
        ShuttleCommand command = commands.get(commandStep);
 
        // 下发命令
        CommandResponse response = write(command, shuttleNo);
 
        //保存命令日志
        BasShuttleOpt basShuttleOpt = new BasShuttleOpt();
        basShuttleOpt.setWrkNo(redisCommand.getWrkNo());
        basShuttleOpt.setShuttleNo(shuttleNo);
 
        basShuttleOpt.setMode(ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc);
        basShuttleOpt.setSourceLocNo(assignCommand.getSourceLocNo());
        basShuttleOpt.setDistLocNo(assignCommand.getLocNo());
        basShuttleOpt.setCommand(JSON.toJSONString(command));
        basShuttleOpt.setSystemStatus(JSON.toJSONString(shuttleProtocol));
        basShuttleOpt.setDeviceWrk(String.valueOf(command.getTaskNo()));
        basShuttleOpt.setResponse(response.getMessage());//获取响应
        basShuttleOpt.setSendTime(new Date());//指令下发时间
        basShuttleOpt.setSend(response.getResult() ? 1 : 0);
        //保存命令流水
        basShuttleOptService.insert(basShuttleOpt);
 
        if (!response.getResult()) {
            News.error("四向穿梭车命令下发失败,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command));
            return false;
        }
 
        shuttleThread.setProtocolStatus(ShuttleProtocolStatusType.WORKING);
        commandStep++;
        //更新redis数据
        redisCommand.setCommandStep(commandStep);
        //任务数据保存到redis
        redisUtil.set(RedisKeyType.SHUTTLE_WORK_FLAG.key + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand));
        return true;
    }
 
    //演示模式
    public synchronized void demo(Integer shuttleNo) {
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
        if (shuttleThread == null) {
            return;
        }
 
        ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
        if (shuttleProtocol == null) {
            return;
        }
 
        if (!shuttleProtocol.getDemo()) {
            return;
        }
 
        WrkMast moveWorking = wrkMastService.selectShuttleHasMoveWorking(shuttleNo);
        if (moveWorking != null) {
            return;
        }
 
        WrkMast shuttleWorking = wrkMastService.selectShuttleWorking(shuttleNo);
        if(shuttleWorking != null) {
            return;
        }
 
        WrkMast chargeWorking = wrkMastService.selectChargeWorking(shuttleNo);
        if (chargeWorking != null) {
            return;
        }
 
        if (shuttleThread.isRequireCharge()) {
            return;
        }
 
        if (!shuttleThread.isIdle()) {
            return;
        }
 
        boolean demoSwitchLev = false;
        Config demoSwitchLevConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "demoSwitchLev"));
        if (demoSwitchLevConfig != null) {
            if (demoSwitchLevConfig.getValue().equals("Y")) {
                demoSwitchLev = true;
            }
        }
 
        Config demoRunLevConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "demoRunLev"));
        if (demoRunLevConfig == null) {
            return;
        }
        List<Integer> levList = JSON.parseArray(demoRunLevConfig.getValue(), Integer.class);
 
        if (!demoSwitchLev) {
            String currentLocNo = shuttleProtocol.getCurrentLocNo();
            levList = new ArrayList<>();
            levList.add(Utils.getLev(currentLocNo));
        }
 
        LocMast targetLoc = null;
        EntityWrapper<LocMast> wrapper = new EntityWrapper<>();
        wrapper.in("lev1", levList);
        wrapper.eq("loc_sts", "O");
        wrapper.last("ORDER BY RAND() LIMIT 1");
        for (int i = 0; i < 3; i++) {
            LocMast locMast = locMastService.selectOne(wrapper);
            if(locMast == null) {
                continue;
            }
 
            ArrayList<String> locs = new ArrayList<>();
            locs.add(locMast.getLocNo());
            Integer shuttle = Utils.checkGroupLocHasShuttle(locs);
            if(shuttle != null) {
                continue;
            }
 
            targetLoc = locMast;
            break;
        }
 
        if(targetLoc == null) {
            return;
        }
 
        shuttleDispatchUtils.dispatchShuttle(null, targetLoc.getLocNo(), shuttleNo);
    }
 
//    //跑库程序
//    public synchronized void moveLoc(Integer shuttleNo) {
//        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
//        if (shuttleThread == null) {
//            return;
//        }
//        ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(false);
//        if (shuttleProtocol == null) {
//            return;
//        }
//
//        //小车开启跑库模式
//        if (!shuttleProtocol.getMoveLoc()) {
//            return;
//        }
//
//        //小车空闲
//        if (!shuttleThread.isIdle()) {
//            return;
//        }
//
//        int lev = Utils.getLev(shuttleProtocol.getCurrentLocNo());//小车当前楼层
//
//        if (shuttleProtocol.getMoveType() == 0) {//跑轨道
//            //根据地图方向决定跑x或y
//            Config config = configService.selectOne(new EntityWrapper<Config>()
//                    .eq("code", "direction_map")
//                    .eq("status", 1));
//            if (config == null) {
//                //跑库结束
//                shuttleProtocol.setMoveLoc(false);
//                shuttleProtocol.setMoveType(0);
//                shuttleProtocol.setXStart(0);
//                shuttleProtocol.setXTarget(0);
//                shuttleProtocol.setXCurrent(0);
//                shuttleProtocol.setYStart(0);
//                shuttleProtocol.setYTarget(0);
//                shuttleProtocol.setYCurrent(0);
//                return;
//            }
//            String direction = config.getValue();
//
//            if (direction.equals("y")) {//跑x轴方向,跑完x轴再切换y轴
//                ArrayList<String> locs = new ArrayList<>();
//                for (int i = shuttleProtocol.getXCurrent(); i <= shuttleProtocol.getXTarget(); i++) {
//                    String locNo = Utils.getLocNo(i, shuttleProtocol.getYCurrent(), lev);
//                    locs.add(locNo);
//                }
//
//                List<LocMast> locList = locMastService.selectList(new EntityWrapper<LocMast>()
//                        .eq("loc_sts", LocStsType.O.toString())
//                        .in("loc_no", locs));
//                if (locList.isEmpty()) {
//                    //空库位
//                    shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
//                    return;
//                }
//
//                LocMast start = locList.get(0);
//                LocMast target = locList.get(locList.size() - 1);
//                //判断小车是否在起点位置
//                if (!shuttleProtocol.getCurrentLocNo().equals(start.getLocNo())) {//不在起点位置,调度去起点位置
//                    shuttleDispatcher.generateMoveTask(device, start.getLocNo());
//                }else {
//                    //在起点位置,调度去目标位置
//                    Task task = shuttleDispatcher.generateMoveTask(device, target.getLocNo());
//                    if (task != null) {
//                        shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);//切换y轴
//                    }
//
//                    if(shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
//                        //y轴也跑完了,结束跑库
//                        shuttleProtocol.setMoveLoc(false);
//                        shuttleProtocol.setMoveType(0);
//                        shuttleProtocol.setXStart(0);
//                        shuttleProtocol.setXTarget(0);
//                        shuttleProtocol.setXCurrent(0);
//                        shuttleProtocol.setYStart(0);
//                        shuttleProtocol.setYTarget(0);
//                        shuttleProtocol.setYCurrent(0);
//                        return;
//                    }
//                }
//            }else {//跑y轴方向,跑完y轴再切换x轴
//                ArrayList<String> locs = new ArrayList<>();
//                for (int i = shuttleProtocol.getYCurrent(); i <= shuttleProtocol.getYTarget(); i++) {
//                    String locNo = Utils.getLocNo(shuttleProtocol.getXCurrent(), i, lev);
//                    locs.add(locNo);
//                }
//
//                List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
//                        .eq(Loc::getLocSts, LocStsType.O.val())
//                        .in(Loc::getLocNo, locs));
//                if (locList.isEmpty()) {
//                    //空库位
//                    shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
//                    return;
//                }
//
//                Loc start = locList.get(0);
//                Loc target = locList.get(locList.size() - 1);
//                //判断小车是否在起点位置
//                if (!shuttleProtocol.getCurrentLocNo().equals(start.getLocNo())) {//不在起点位置,调度去起点位置
//                    shuttleDispatcher.generateMoveTask(device, start.getLocNo());
//                }else {
//                    //在起点位置,调度去目标位置
//                    Task task = shuttleDispatcher.generateMoveTask(device, target.getLocNo());
//                    if (task != null) {
//                        shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);//切换x轴
//                    }
//
//                    if(shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
//                        //y轴也跑完了,结束跑库
//                        shuttleProtocol.setMoveLoc(false);
//                        shuttleProtocol.setMoveType(0);
//                        shuttleProtocol.setXStart(0);
//                        shuttleProtocol.setXTarget(0);
//                        shuttleProtocol.setXCurrent(0);
//                        shuttleProtocol.setYStart(0);
//                        shuttleProtocol.setYTarget(0);
//                        shuttleProtocol.setYCurrent(0);
//                        return;
//                    }
//                }
//            }
//
//        } else if (shuttleProtocol.getMoveType() == 1) {//跑库位
//            //根据地图方向决定跑x或y
//            Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>()
//                    .eq(Dict::getFlag, "direction_map")
//                    .eq(Dict::getStatus, 1));
//            if (dict == null) {
//                //跑库结束
//                shuttleProtocol.setMoveLoc(false);
//                shuttleProtocol.setMoveType(0);
//                shuttleProtocol.setXStart(0);
//                shuttleProtocol.setXTarget(0);
//                shuttleProtocol.setXCurrent(0);
//                shuttleProtocol.setYStart(0);
//                shuttleProtocol.setYTarget(0);
//                shuttleProtocol.setYCurrent(0);
//                return;
//            }
//            String direction = dict.getValue();
//
//            if (direction.equals("y")) {//跑x轴方向,跑完x轴再切换y轴
//                Integer xCurrent = shuttleProtocol.getXCurrent();
//
//                //获取待跑库位号
//                String locNo = Utils.getLocNo(xCurrent, shuttleProtocol.getYCurrent(), lev);
//                Loc target = locService.getOne(new LambdaQueryWrapper<Loc>()
//                        .eq(Loc::getLocNo, locNo)
//                        .eq(Loc::getLocSts, LocStsType.O.val())
//                        .eq(Loc::getHostId, device.getHostId()));
//                if (target == null || shuttleProtocol.getCurrentLocNo().equals(locNo)) {//库位不存在或小车已在当前位置
//                    shuttleProtocol.setXCurrent(xCurrent + 1);
//                    if (shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
//                        //x轴跑完,切换y轴
//                        shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());
//                        shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
//
//                        if(shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
//                            //y轴也跑完了,结束跑库
//                            shuttleProtocol.setMoveLoc(false);
//                            shuttleProtocol.setMoveType(0);
//                            shuttleProtocol.setXStart(0);
//                            shuttleProtocol.setXTarget(0);
//                            shuttleProtocol.setXCurrent(0);
//                            shuttleProtocol.setYStart(0);
//                            shuttleProtocol.setYTarget(0);
//                            shuttleProtocol.setYCurrent(0);
//                            return;
//                        }
//                    }
//                    return;
//                }
//
//                //调度去库位
//                Task task = shuttleDispatcher.generateMoveTask(device, locNo);
//                if (task == null) {
//                    return;//调度失败
//                }
//
//                shuttleProtocol.setXCurrent(xCurrent + 1);
//                if (shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
//                    //x轴跑完,切换y轴
//                    shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());
//                    shuttleProtocol.setYCurrent(shuttleProtocol.getYCurrent() + 1);
//
//                    if(shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
//                        //y轴也跑完了,结束跑库
//                        shuttleProtocol.setMoveLoc(false);
//                        shuttleProtocol.setMoveType(0);
//                        shuttleProtocol.setXStart(0);
//                        shuttleProtocol.setXTarget(0);
//                        shuttleProtocol.setXCurrent(0);
//                        shuttleProtocol.setYStart(0);
//                        shuttleProtocol.setYTarget(0);
//                        shuttleProtocol.setYCurrent(0);
//                        return;
//                    }
//                }
//
//            }else {//跑y轴方向,跑完y轴再切换x轴
//                Integer yCurrent = shuttleProtocol.getYCurrent();
//
//                //获取待跑库位号
//                String locNo = Utils.getLocNo(shuttleProtocol.getXCurrent(), yCurrent, lev);
//                Loc target = locService.getOne(new LambdaQueryWrapper<Loc>()
//                        .eq(Loc::getLocNo, locNo)
//                        .eq(Loc::getLocSts, LocStsType.O.val())
//                        .eq(Loc::getHostId, device.getHostId()));
//                if (target == null || shuttleProtocol.getCurrentLocNo().equals(locNo)) {//库位不存在或小车已在当前位置
//                    shuttleProtocol.setYCurrent(yCurrent + 1);
//                    if (shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
//                        //y轴跑完,切换x轴
//                        shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());
//                        shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
//
//                        if(shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
//                            //x轴也跑完了,结束跑库
//                            shuttleProtocol.setMoveLoc(false);
//                            shuttleProtocol.setMoveType(0);
//                            shuttleProtocol.setXStart(0);
//                            shuttleProtocol.setXTarget(0);
//                            shuttleProtocol.setXCurrent(0);
//                            shuttleProtocol.setYStart(0);
//                            shuttleProtocol.setYTarget(0);
//                            shuttleProtocol.setYCurrent(0);
//                            return;
//                        }
//                    }
//                    return;
//                }
//
//                //调度去库位
//                Task task = shuttleDispatcher.generateMoveTask(device, locNo);
//                if (task == null) {
//                    return;//调度失败
//                }
//
//                shuttleProtocol.setYCurrent(yCurrent + 1);
//                if (shuttleProtocol.getYCurrent() > shuttleProtocol.getYTarget()) {
//                    //y轴跑完,切换x轴
//                    shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());
//                    shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
//
//                    if(shuttleProtocol.getXCurrent() > shuttleProtocol.getXTarget()) {
//                        //x轴也跑完了,结束跑库
//                        shuttleProtocol.setMoveLoc(false);
//                        shuttleProtocol.setMoveType(0);
//                        shuttleProtocol.setXStart(0);
//                        shuttleProtocol.setXTarget(0);
//                        shuttleProtocol.setXCurrent(0);
//                        shuttleProtocol.setYStart(0);
//                        shuttleProtocol.setYTarget(0);
//                        shuttleProtocol.setYCurrent(0);
//                        return;
//                    }
//                }
//
//            }
//        } else if (shuttleProtocol.getMoveType() == 2) {//母轨道循环跑
//            Integer xCurrent = shuttleProtocol.getXCurrent();
//            Integer yCurrent = shuttleProtocol.getYCurrent();
//
//            String locNo = Utils.getLocNo(xCurrent, yCurrent, lev);
//            //调度去目标位置
//            if (shuttleProtocol.getCurrentLocNo().equals(locNo)) {
//                if (yCurrent.equals(shuttleProtocol.getYStart())) {
//                    shuttleProtocol.setYCurrent(shuttleProtocol.getYTarget());//小车和目标位置一致,切换库位
//                } else {
//                    shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());//小车和目标位置一致,切换库位
//                }
//            } else {
//                Task result = shuttleDispatcher.generateMoveTask(device, locNo);
//                if (result != null) {//调度成功
//                    if (yCurrent.equals(shuttleProtocol.getYStart())) {
//                        shuttleProtocol.setYCurrent(shuttleProtocol.getYTarget());//切换库位
//                    } else {
//                        shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());//切换库位
//                    }
//                }
//            }
//        } else if (shuttleProtocol.getMoveType() == 3) {//子轨道循环跑
//            Integer xCurrent = shuttleProtocol.getXCurrent();
//            Integer yCurrent = shuttleProtocol.getYCurrent();
//
//            String locNo = Utils.getLocNo(xCurrent, yCurrent, lev);
//            //调度去目标位置
//            if (shuttleProtocol.getCurrentLocNo().equals(locNo)) {
//                if (xCurrent.equals(shuttleProtocol.getXStart())) {
//                    shuttleProtocol.setXCurrent(shuttleProtocol.getXTarget());//小车和目标位置一致,切换库位
//                } else {
//                    shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());//小车和目标位置一致,切换库位
//                }
//            } else {
//                Task result = shuttleDispatcher.generateMoveTask(device, locNo);
//                if (result != null) {//调度成功
//                    if (xCurrent.equals(shuttleProtocol.getXStart())) {
//                        shuttleProtocol.setXCurrent(shuttleProtocol.getXTarget());//切换库位
//                    } else {
//                        shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());//切换库位
//                    }
//                }
//            }
//        } else if (shuttleProtocol.getMoveType() == 4) {//取放货
//            Integer xCurrent = shuttleProtocol.getXCurrent();
//            if (xCurrent > shuttleProtocol.getXTarget()) {//当X值大于X目标值
//                shuttleProtocol.setXCurrent(shuttleProtocol.getXStart());
//                shuttleProtocol.setYCurrent(shuttleProtocol.getYStart());
//                return;
//            }
//
//            //判断x轴货位是否放满
//            boolean flag = true;
//            for (Loc loc : locService.list(new LambdaQueryWrapper<Loc>()
//                    .eq(Loc::getHostId, device.getHostId())
//                    .eq(Loc::getRow, xCurrent)
//                    .ge(Loc::getBay, shuttleProtocol.getYStart())
//                    .le(Loc::getBay, shuttleProtocol.getYTarget()))) {
//                if (loc.getLocSts() != LocStsType.F.val()) {
//                    flag = false;//未满
//                    break;
//                }
//            }
//            if (flag) {
//                shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
//                return;
//            }
//
//            //搜索有货库位
//            List<Loc> list = locService.list(new LambdaQueryWrapper<Loc>()
//                    .eq(Loc::getLocSts, LocStsType.F.val())
//                    .eq(Loc::getHostId, device.getHostId())
//                    .notIn(Loc::getRow, xCurrent)
//                    .eq(Loc::getStatus, 1));
//            if (list.isEmpty()) {
//                return;
//            }
//
//            Loc start = list.get(0);
//
//            List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
//                    .eq(Loc::getHostId, device.getHostId())
//                    .eq(Loc::getStatus, 1)
//                    .eq(Loc::getLocSts, LocStsType.O.val())
//                    .eq(Loc::getRow, xCurrent)
//                    .orderByDesc(Loc::getBay)
//                    .orderByAsc(Loc::getRow));
//            if (locList.isEmpty()) {
//                return;
//            }
//
//            Loc target = locList.get(0);
//            if (target == null) {
//                return;
//            }
//
//            //调度去目标位置
//            if (!shuttleProtocol.getCurrentLocNo().equals(target.getLocNo())) {
//                Task task = shuttleDispatcher.generateManuaTakeMoveTask(device, start.getLocNo(), target.getLocNo());
////                if(task != null) {//调度成功
////                    shuttleProtocol.setXCurrent(shuttleProtocol.getXCurrent() + 1);
////                }
//            }
//        }
//    }
 
    private synchronized CommandResponse write(ShuttleCommand command, Integer shuttleNo) {
        CommandResponse response = new CommandResponse(false);
        if (null == command) {
            News.error("四向穿梭车写入命令为空");
            response.setMessage("四向穿梭车写入命令为空");
            return response;
        }
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
        if (command.getMode() == ShuttleCommandModeType.MOVE.id
                || command.getMode() == ShuttleCommandModeType.IN_LIFT.id
                || command.getMode() == ShuttleCommandModeType.OUT_LIFT.id) {//移动
            response = shuttleThread.move(command);
        } else if (command.getMode() == ShuttleCommandModeType.PALLET_LIFT.id
                || command.getMode() == ShuttleCommandModeType.PALLET_DOWN.id) {//顶升
            response = shuttleThread.lift(command);
        } else if (command.getMode() == ShuttleCommandModeType.CHARGE_OPEN.id
                || command.getMode() == ShuttleCommandModeType.CHARGE_CLOSE.id) {//充电
            response = shuttleThread.charge(command);
        } else if (command.getMode() == ShuttleCommandModeType.RESET.id) {//复位
            response = shuttleThread.reset(command);
        } else if (command.getMode() == ShuttleCommandModeType.UPDATE_LOCATION.id) {//更新坐标
            response = shuttleThread.updateLocation(command);
        }
 
        News.info("四向穿梭车命令下发,穿梭车号={},任务数据={}", shuttleNo, JSON.toJSON(command));
        return response;
    }
 
}