zjj
7 天以前 314729da5f6f84a6112344a2210aadfeeb2bac0c
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
package com.zy.asrs.wcs.core.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.common.SnowflakeIdWorker;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.core.entity.*;
import com.zy.asrs.wcs.core.kernel.AnalyzeService;
import com.zy.asrs.wcs.core.model.NavigateNode;
import com.zy.asrs.wcs.core.model.enums.*;
import com.zy.asrs.wcs.core.service.*;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.entity.Device;
import com.zy.asrs.wcs.rcs.model.enums.ShuttleProtocolStatusType;
import com.zy.asrs.wcs.rcs.model.enums.SlaveType;
import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
import com.zy.asrs.wcs.rcs.service.DeviceService;
import com.zy.asrs.wcs.rcs.thread.LiftThread;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import com.zy.asrs.wcs.system.entity.Dict;
import com.zy.asrs.wcs.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * Created by vincent on 2023/10/12
 */
@Service
public class ShuttleDispatcher {
 
    private static final Integer INF = 9999999;
    private static final Integer WEIGHT = 1000000;
 
    @Autowired
    private TaskService taskService;
    @Autowired
    private LiftDispatcher liftDispatcher;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private AnalyzeService analyzeService;
    @Autowired
    private MotionService motionService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private BasShuttleService basShuttleService;
    @Autowired
    private TaskCtgService taskCtgService;
    @Autowired
    private ShuttleStandbyService shuttleStandbyService;
    @Autowired
    private DictService dictService;
 
    public synchronized ShuttleThread searchIdleShuttle(Task task) {
        String locNo = taskService.judgeInbound(task) ? task.getDestLoc() : task.getOriginLoc();
        ShuttleThread resThread = null;
        Integer finalDistance = ShuttleDispatcher.INF;
 
        //检测目标楼层车数量是否小于允许的最大数量
        boolean checkDispatchMaxNum = checkDispatchMaxNum(Utils.getLev(locNo), task.getHostId());
 
        List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
                .eq(Device::getDeviceType, DeviceCtgType.SHUTTLE.val())
                .eq(Device::getHostId, task.getHostId())
                .eq(Device::getStatus, 1));
 
        for (Device device : list) {
            if (taskService.hasBusyOutboundByShuttle(Integer.parseInt(device.getDeviceNo()))) {
                continue;
            }
            //获取四向穿梭车线程
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
            ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
            if (shuttleProtocol == null || shuttleProtocol.getShuttleNo() == null) {
                continue;
            }
 
            if (!shuttleThread.isIdle()) {
                continue;
            }
 
            BasShuttle basShuttle = basShuttleService.getOne(new LambdaQueryWrapper<BasShuttle>()
                    .eq(BasShuttle::getShuttleNo, device.getDeviceNo())
                    .eq(BasShuttle::getHostId, device.getHostId()));
            if (basShuttle == null) {
                continue;//小车基础数据不存在
            }
 
            if (!Cools.isEmpty(basShuttle.getDisableLev())) {
                List<Integer> disableLev = JSON.parseArray(basShuttle.getDisableLev(), Integer.class);
                //检查小车是否禁用该楼层
                if (disableLev.contains(Utils.getLev(locNo))) {
                    continue;//小车禁用该楼层跳过该车
                }
            }
 
            //检测是否存在充电任务
            Task taskCharge = taskService.selectChargeWorking(Integer.valueOf(device.getDeviceNo()));
            if (taskCharge != null) {
                continue;
            }
 
            // 有没有被其他任务调度
            int currentLev = Utils.getLev(shuttleProtocol.getCurrentLocNo());//小车当前层高
            String currentLocNo = shuttleProtocol.getCurrentLocNo();//小车当前库位号
 
            if (currentLocNo.equals(locNo)) {
                resThread = shuttleThread;
                break;
            }
 
            String targetLocNo = null;//默认到提升机待机位
            // 同楼层直接计算到目标库位
            if (currentLev == Utils.getLev(locNo)) {
                targetLocNo = locNo;
            }else {
                if (!checkDispatchMaxNum) {
                    News.info("{}任务,{}层,已经达到当前楼层调度车辆最大值", task.getTaskNo(), Utils.getLev(locNo));
                    continue;
                }
 
                //获取距离目标位置最近的空闲可换层提升机
                LiftThread liftThread = liftDispatcher.searchIdleLift(locNo, task.getHostId(), true);
                if (liftThread == null) {
                    continue;
                }
                Device recentTransferLift = liftThread.getDevice();
 
                //获取小车楼层提升机待机位
                ShuttleStandby shuttleStandby = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
                        .eq(ShuttleStandby::getDeviceId, recentTransferLift.getId())
                        .eq(ShuttleStandby::getDeviceLev, currentLev)
                        .eq(ShuttleStandby::getStatus, 1));
                targetLocNo = shuttleStandby.getDeviceLoc();
            }
 
            //当前穿梭车线程到当前车子所在楼层的提升机口距离
            List<NavigateNode> currentShuttlePath = NavigateUtils.calc(
                    currentLocNo
                    , targetLocNo
                    , NavigationMapType.NORMAL.id
                    , Utils.getShuttlePoints(Integer.parseInt(shuttleThread.getDevice().getDeviceNo()), currentLev)
            );//搜索空闲穿梭车,使用正常通道地图
            if (currentShuttlePath == null) {
                continue;
            }
 
            Integer currDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
 
            // 不同楼层权重
            if (currentLev != Utils.getLev(locNo)) {
                currDistance += WEIGHT;
            }
 
            // 挂载任务权重
            List<Task> tasks = taskService.selectWorkingByShuttle(Integer.valueOf(device.getDeviceNo()), null);
            if (!Cools.isEmpty(tasks)) {
                currDistance += tasks.size() * WEIGHT;
            }
 
            if (currDistance < finalDistance) {
                finalDistance = currDistance;
                resThread = shuttleThread;
            }
        }
 
        return resThread;
    }
 
    //生成迁移任务
    public synchronized Task generateMoveTask(Device device, String locNo) {
        // 已有迁移任务
        if (taskService.selectMoveWorking(Integer.valueOf(device.getDeviceNo())) != null) {
            return null;
        }
 
        //获取迁移任务类型
        TaskCtg taskCtg = taskCtgService.getOne(new LambdaQueryWrapper<TaskCtg>()
                .eq(TaskCtg::getFlag, String.valueOf(TaskCtgType.MOVE))
                .eq(TaskCtg::getStatus, 1));
        if (taskCtg == null) {
            return null;
        }
 
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
        if (shuttleThread == null) {
            return null;
        }
 
        Task task = new Task();
        task.setUuid(String.valueOf(snowflakeIdWorker.nextId()));
        task.setTaskNo(String.valueOf(Utils.getTaskNo("MOVE")));
        task.setTaskSts(TaskStsType.NEW_MOVE.sts);
        task.setTaskCtg(taskCtg.getId());
        task.setPriority(10);
        task.setOriginSite(null);
        task.setOriginLoc(null);
        task.setDestSite(null);
        task.setDestLoc(locNo); // 迁移位置
        task.setIoTime(new Date());
        task.setStartTime(new Date());
        task.setHostId(device.getHostId());
        task.setStatus(1);
        task.setShuttleNo(Integer.valueOf(device.getDeviceNo()));
 
        // generate motion list
        List<Motion> motionList = analyzeService.generateShuttleMoveMotion(task);
        if (Cools.isEmpty(motionList)) {
            News.error("保存{}号四向穿梭车迁移任务失败!!!", device.getDeviceNo());
            return null;
        }
        motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), device.getHostId());
 
        task.setTaskSts(TaskStsType.ANALYZE_MOVE.sts);
 
        if (!taskService.save(task)) {
            News.error("保存{}号四向穿梭车迁移任务失败!!!", device.getDeviceNo());
            return null;
        }
 
        return task;
    }
 
    //生成手动取放货任务
    public synchronized Task generateManuaTakeMoveTask(Device device, String sourceLocNo, String locNo) {
        // 已有手动任务
        if (taskService.selectManualWorking(Integer.valueOf(device.getDeviceNo())) != null) {
            return null;
        }
 
        //获取手动任务类型
        TaskCtg taskCtg = taskCtgService.getOne(new LambdaQueryWrapper<TaskCtg>()
                .eq(TaskCtg::getFlag, String.valueOf(TaskCtgType.MANUAL))
                .eq(TaskCtg::getStatus, 1));
        if (taskCtg == null) {
            return null;
        }
 
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
        if (shuttleThread == null) {
            return null;
        }
 
        Task task = new Task();
        task.setUuid(String.valueOf(snowflakeIdWorker.nextId()));
        task.setTaskNo(String.valueOf(Utils.getTaskNo("MANUAL")));
        task.setTaskSts(TaskStsType.NEW_MANUAL.sts);
        task.setTaskCtg(taskCtg.getId());
        task.setPriority(10);
        task.setOriginSite(null);
        task.setOriginLoc(sourceLocNo);
        task.setDestSite("takeMove");
        task.setDestLoc(locNo);
        task.setIoTime(new Date());
        task.setStartTime(new Date());
        task.setStatus(1);
        task.setMemo("manual");
        task.setShuttleNo(Integer.valueOf(device.getDeviceNo()));
        task.setRecordLoc("Y");//记录库存信息
        task.setHostId(device.getHostId());
 
        // generate motion list
        List<Motion> motionList = analyzeService.generateShuttleManualMotion(task);
        if (Cools.isEmpty(motionList)) {
            News.error("保存{}号四向穿梭车手动任务失败!!!", device.getDeviceNo());
            return null;
        }
        motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), device.getHostId());
 
        task.setTaskSts(TaskStsType.ANALYZE_MANUAL.sts);
 
        if (!taskService.save(task)) {
            News.error("保存{}号四向穿梭车手动任务失败!!!", device.getDeviceNo());
            return null;
        }
 
        return task;
    }
 
    /**
     * 搜索避让库位,通过小车号和目标库位
     */
    public String searchStandByLocNo(Integer shuttleNo, Long hostId, String locNo) {
        BasShuttle basShuttle = basShuttleService.getOne(new LambdaQueryWrapper<BasShuttle>()
                .eq(BasShuttle::getShuttleNo, shuttleNo)
                .eq(BasShuttle::getHostId, hostId));
        if (basShuttle == null) {
            throw new CoolException("小车基础数据不存在");
        }
 
        String idleLoc = basShuttle.getIdleLoc();
        if (Cools.isEmpty(idleLoc)) {
            throw new CoolException("小车避让数据不存在");
        }
 
        int lev = Utils.getLev(locNo);//当前楼层
        List<String> standbyLoc = JSON.parseArray(idleLoc, String.class);
        if (standbyLoc.isEmpty()) {
            throw new CoolException("避让数据异常");
        }
        //获取当前层避让位置
        List<String> currentLevStandByLoc = new ArrayList<>();
        for (String loc : standbyLoc) {
            if (Utils.getLev(loc) == lev) {
                currentLevStandByLoc.add(loc);
            }
        }
        if (currentLevStandByLoc.isEmpty()) {
            throw new CoolException("当前层无避让位置");
        }
 
        Integer finalDistance = ShuttleDispatcher.INF;
        String recentLoc = null;
        for (String loc : currentLevStandByLoc) {
            //当前穿梭车到避让位计算
            List<NavigateNode> currentShuttlePath = NavigateUtils.calc(
                    locNo
                    , loc
                    , NavigationMapType.NORMAL.id
                    , Utils.getShuttlePoints(shuttleNo, lev)
            );//使用正常通道地图
            if (currentShuttlePath == null) {
                continue;
            }
 
            Integer currDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
            if (currDistance < finalDistance) {
                finalDistance = currDistance;
                recentLoc = loc;
            }
        }
 
        if (recentLoc == null) {
            throw new CoolException("搜索避让位置失败");
        }
 
        return recentLoc;
    }
 
    /**
     * 搜索可用库位,通过小车号和目标库位
     */
    public String searchAvailableLocNo(Integer shuttleNo, Long hostId, String currentLocNo, List<String> locNos) {
        BasShuttle basShuttle = basShuttleService.getOne(new LambdaQueryWrapper<BasShuttle>()
                .eq(BasShuttle::getShuttleNo, shuttleNo)
                .eq(BasShuttle::getHostId, hostId));
        if (basShuttle == null) {
            throw new CoolException("小车基础数据不存在");
        }
 
        if (locNos.isEmpty()) {
            throw new CoolException("当前层无避让位置");
        }
 
        int lev = Utils.getLev(currentLocNo);
 
        Integer finalDistance = ShuttleDispatcher.INF;
        String recentLoc = null;
        for (String loc : locNos) {
            //当前穿梭车到避让位计算
            List<NavigateNode> currentShuttlePath = NavigateUtils.calc(
                    currentLocNo
                    , loc
                    , NavigationMapType.NORMAL.id
                    , Utils.getShuttlePoints(shuttleNo, lev)
            );//使用正常通道地图
            if (currentShuttlePath == null) {
                continue;
            }
 
            Integer currDistance = NavigateUtils.getOriginPathAllDistance(currentShuttlePath);//计算当前路径行走总距离
            if (currDistance < finalDistance) {
                finalDistance = currDistance;
                recentLoc = loc;
            }
        }
 
        if (recentLoc == null) {
            throw new CoolException("搜索避让位置失败");
        }
 
        return recentLoc;
    }
 
    /**
     * 检测目标楼层车数量是否小于允许的最大数量
     * true: 小于最大数量  false: 大于或等于最大数量
     */
    public boolean checkDispatchMaxNum(Integer lev, Long hostId) {
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getFlag, "dispatchShuttleMaxNum"));
        if (dict == null) {
            return false;
        }
 
        int levCount = 0;//目标楼层车辆数量
        List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
                .eq(Device::getDeviceType, DeviceCtgType.SHUTTLE.val())
                .eq(Device::getHostId, hostId)
                .eq(Device::getStatus, 1));
 
        for (Device device : list) {
            //获取四向穿梭车线程
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, device.getId().intValue());
            if (shuttleThread == null) {
                continue;
            }
 
            ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
            if (shuttleProtocol == null) {
                continue;
            }
            if (shuttleProtocol.getProtocolStatusType() == ShuttleProtocolStatusType.OFFLINE){
                continue;
            }
 
            if (Utils.getLev(shuttleProtocol.getCurrentLocNo()) == lev) {
                if (shuttleProtocol.getHasCharge()) {
                    continue;//充电中
                }
 
                levCount++;//目标楼层有车,数量增加
            }
        }
 
        //搜索是否存在前往目标楼层的小车移动工作档
        for (Task task : taskService.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskSts, TaskStsType.NEW_MOVE.sts, TaskStsType.ANALYZE_MOVE.sts, TaskStsType.EXECUTE_MOVE.sts, TaskStsType.COMPLETE_MOVE.sts))) {
            if (task.getOriginLoc() == null || task.getDestLoc() == null) {
                continue;
            }
 
            int sourceLev = Utils.getLev(task.getOriginLoc());//工作档源楼层
            int targetLev = Utils.getLev(task.getDestLoc());//工作档目标楼层
            if (sourceLev == targetLev) {
                continue;//工作档楼层和目标楼层相同,跳过
            }
 
            if (targetLev == lev) {
                levCount++;//工作档目标楼层和实际楼层相同,数量增加
                continue;
            }
 
        }
 
        //搜索是否存在前往目标楼层的小车工作档
        for (Task task : taskService.list(new LambdaQueryWrapper<Task>()
                .in(Task::getTaskSts, TaskStsType.NEW_INBOUND.sts, TaskStsType.ANALYZE_INBOUND.sts, TaskStsType.EXECUTE_INBOUND.sts, TaskStsType.COMPLETE_INBOUND.sts
                        , TaskStsType.NEW_OUTBOUND.sts, TaskStsType.ANALYZE_OUTBOUND.sts, TaskStsType.EXECUTE_OUTBOUND.sts, TaskStsType.COMPLETE_OUTBOUND.sts))) {
 
            List<Motion> motions = motionService.list(new LambdaQueryWrapper<Motion>()
                    .eq(Motion::getTaskNo, task.getTaskNo())
                    .in(Motion::getMotionCtg, MotionCtgType.SHUTTLE_MOVE
                            , MotionCtgType.SHUTTLE_MOVE_LIFT_PALLET
                            , MotionCtgType.SHUTTLE_MOVE_DOWN_PALLET
                            , MotionCtgType.SHUTTLE_MOVE_FROM_LIFT
                            , MotionCtgType.SHUTTLE_MOVE_TO_LIFT
                            , MotionCtgType.SHUTTLE_MOVE_FROM_CONVEYOR
                            , MotionCtgType.SHUTTLE_MOVE_TO_CONVEYOR
                            , MotionCtgType.SHUTTLE_MOVE_FROM_LIFT_TO_CONVEYOR
                    ));
 
            boolean isUpdateLev = false;
            for (Motion motion : motions) {
                if (motion.getOrigin() == null || motion.getTarget() == null) {
                    continue;
                }
 
                int sourceLev = Utils.getLev(motion.getOrigin());//动作源楼层
                int targetLev = Utils.getLev(motion.getTarget());//动作目标楼层
                if (sourceLev != targetLev) {
                    isUpdateLev = true;
                    break;
                }
            }
 
            if(isUpdateLev) {
                levCount++;//工作档属于跨层任务,小车归属于目标楼层
                continue;
            }
 
        }
 
        return levCount < Integer.parseInt(dict.getValue());
    }
    //分析出库路径待机库位
    public String analyzeOutPathWaitLoc(String startLoc, String targetLoc, Device shuttleDevice) {
        //计算路径并分解成两段动作
        List<NavigateNode> nodeList = NavigateUtils.calc(startLoc, targetLoc, NavigationMapType.DFX.id, Utils.getShuttlePoints(Integer.parseInt(shuttleDevice.getDeviceNo()), Utils.getLev(startLoc)));
        if (nodeList == null) {
            News.error("{} dash {} can't find navigate path!", startLoc, targetLoc);
            return null;
        }
        //获取分段路径
        ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(nodeList);
        if (data.size() <= 1) {
            return null;//两点之间只有一段路径
        }
 
        //取出倒数第二段路径
        ArrayList<NavigateNode> navigateNodes = data.get(data.size() - 2);
        NavigateNode startNode = navigateNodes.get(0);
        String lastPathStartLoc = Utils.getLocNo(startNode.getX(), startNode.getY(), startNode.getZ());
        return lastPathStartLoc;
    }
 
}