#
Junjie
2024-04-07 07f9e200470b261ebf7e5e9b06cf7fa38770db6e
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
package com.zy.asrs.wcs.rcs.thread.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.common.utils.HttpHandler;
import com.zy.asrs.framework.common.DateUtils;
import com.zy.asrs.framework.common.SpringUtils;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.core.entity.BasShuttle;
import com.zy.asrs.wcs.core.entity.Loc;
import com.zy.asrs.wcs.core.model.NavigateNode;
import com.zy.asrs.wcs.core.model.command.ShuttleCommand;
import com.zy.asrs.wcs.core.model.enums.ShuttleCommandModeType;
import com.zy.asrs.wcs.core.model.enums.ShuttleRunDirection;
import com.zy.asrs.wcs.core.service.BasShuttleService;
import com.zy.asrs.wcs.core.service.LocService;
import com.zy.asrs.wcs.core.utils.NavigateUtils;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.OutputQueue;
import com.zy.asrs.wcs.rcs.entity.DeviceDataLog;
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.DeviceDataLogService;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.rcs.entity.Device;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Slf4j
@SuppressWarnings("all")
public class SurayShuttleThread implements ShuttleThread {
 
    private static final String API_URL = "http://127.0.0.1:8082";
 
    private Device device;
    private RedisUtil redisUtil;
    private ShuttleProtocol shuttleProtocol;
 
    public SurayShuttleThread(Device device,RedisUtil redisUtil) {
        this.device = device;
        this.redisUtil = redisUtil;
    }
 
    @Override
    public void run() {
        News.info("{}号四向车线程启动", device.getDeviceNo());
        this.connect();
        while (true) {
            try {
                read();
                Thread.sleep(500);
            } catch (Exception e) {
                log.error("ShuttleThread Fail", e);
            }
        }
    }
 
    private void read() {
        try {
            readStatus();
//            //四向穿梭车空闲、有任务、标记为true、存在任务指令,需要执行任务的下一条指令
//            if (shuttleProtocol.getTaskNo() != 0) {
//                Object obj = redisUtil.get(DeviceRedisConstant.SHUTTLE_WORK_FLAG + shuttleProtocol.getTaskNo());
//                if (null != obj) {
//                    ShuttleRedisCommand redisCommand = JSON.parseObject(obj.toString(), ShuttleRedisCommand.class);
//                    executeWork(redisCommand);
//                }
//            }
//
//            //小车空闲且有跑库程序
//            if (shuttleProtocol.isIdle() && shuttleProtocol.getMoveLoc()) {
//                moveLoc();
//            }
        } catch (Exception e) {
            log.error("ShuttleThread Fail!", e);
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】读取四向穿梭车状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getDeviceNo(), device.getIp(), device.getPort()));
        }
    }
 
    private void readStatus() {
        try {
            JSONObject data = requestDeviceStatus();
            if (data != null) {
                if (null == shuttleProtocol) {
                    shuttleProtocol = new ShuttleProtocol();
                    shuttleProtocol.setShuttleNo(Integer.valueOf(device.getDeviceNo()));
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                    shuttleProtocol.setDevice(device);
 
                    InnerSuhttleExtend extend = new InnerSuhttleExtend();
                    shuttleProtocol.setExtend(extend);
                }
 
                //----------读取四向穿梭车状态-----------
                //小车设备状态
                Integer deviceStatus = data.getInteger("deviceStatus");
                if (deviceStatus == 255) {
                    //离线
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.OFFLINE);
                }
                shuttleProtocol.setDeviceStatus(deviceStatus);
                //当前二维码
                shuttleProtocol.setCurrentCode(data.getString("groundCode") == null ? "0" : data.getString("groundCode"));
                //电池电量
                shuttleProtocol.setBatteryPower(data.getString("battery") == null ? "0%" : data.getString("battery"));
 
 
                //是否顶升
                shuttleProtocol.setHasLift(data.getInteger("palletStatus") == 1 ? true : false);
                //是否有托盘
                shuttleProtocol.setHasPallet(data.getInteger("hasPallet") == null ? true : data.getInteger("hasPallet") != 2 ? true : false);
                //行驶方向
                shuttleProtocol.setRunDirection(data.getString("direction") == null ? "none" : data.getString("direction"));
                //是否为充电状态
                shuttleProtocol.setHasCharge((deviceStatus == 5 || deviceStatus == 13) ? true : false);
 
                //*********读取扩展字段**********
                InnerSuhttleExtend extend = (InnerSuhttleExtend) shuttleProtocol.getExtend();
                extend.setMapVersion(data.getString("version"));//地图版本
                extend.setStatusDescription(data.getString("statusDescription"));//状态描述
                shuttleProtocol.setExtend(extend);//扩展字段
 
                ///读取四向穿梭车状态-end
 
                //小车处于忙碌状态,将标记置为true
                if (!shuttleProtocol.getIdle()) {
                    shuttleProtocol.setPakMk(true);
                }
 
                if (shuttleProtocol.getProtocolStatusType() == null && shuttleProtocol.getDeviceStatus().intValue() == 3) {
                    //小车空闲状态、小车任务状态为未知,认定曾离线过,需要复位成空闲
                    shuttleProtocol.setProtocolStatusType(ShuttleProtocolStatusType.IDLE);
                }
 
                if (System.currentTimeMillis() - shuttleProtocol.getDeviceDataLog() > 1000 * 5) {
                    //采集时间超过5s,保存一次数据记录
                    //保存数据记录
                    DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class);
                    DeviceDataLog deviceDataLog = new DeviceDataLog();
                    deviceDataLog.setOriginData(JSON.toJSONString(data));
                    deviceDataLog.setWcsData(JSON.toJSONString(shuttleProtocol));
                    deviceDataLog.setType(String.valueOf(SlaveType.Shuttle));
                    deviceDataLog.setDeviceNo(String.valueOf(shuttleProtocol.getShuttleNo()));
                    deviceDataLog.setCreateTime(new Date());
                    deviceDataLog.setHostId(device.getHostId());
                    deviceDataLogService.save(deviceDataLog);
 
                    //更新采集时间
                    shuttleProtocol.setDeviceDataLog(System.currentTimeMillis());
                }
 
                //将四向穿梭车状态保存至数据库
                BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class);
                BasShuttle basShuttle = shuttleService.getOne(new LambdaQueryWrapper<BasShuttle>()
                        .eq(BasShuttle::getShuttleNo, device.getDeviceNo())
                        .eq(BasShuttle::getHostId, device.getHostId()));
                if (basShuttle == null) {
                    basShuttle = new BasShuttle();
                    //四向穿梭车号
                    basShuttle.setShuttleNo(Integer.valueOf(device.getDeviceNo()));
                    basShuttle.setStatus(1);
                    basShuttle.setDeleted(0);
                    basShuttle.setHostId(device.getHostId());
                    shuttleService.save(basShuttle);
                }
                //任务号
                basShuttle.setTaskNo(shuttleProtocol.getTaskNo().intValue());
                //修改时间
                basShuttle.setUpdateTime(new Date());
                //设备状态
                basShuttle.setProtocol(JSON.toJSONString(shuttleProtocol));
                if (shuttleService.updateById(basShuttle)) {
                    OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), device.getDeviceNo()));
                }
            } else {
                OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】{1}读取四向穿梭车状态信息失败", DateUtils.convert(new Date()), device.getDeviceNo()));
                throw new CoolException(MessageFormat.format("读取四向穿梭车状态信息失败 ===>> [id:{0}] [ip:{1}] [port:{2}]", device.getDeviceNo(), device.getIp(), device.getPort()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】读取四向穿梭车状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getDeviceNo(), device.getIp(), device.getPort()));
        }
    }
 
    @Override
    public boolean connect() {
        return true;
    }
 
    @Override
    public void close() {
 
    }
 
    @Override
    public ShuttleProtocol getStatus() {
        return this.shuttleProtocol;
    }
 
    @Override
    public Device getDevice() {
        return this.device;
    }
 
    @Override
    public synchronized boolean movePath(List<NavigateNode> nodes, Integer taskNo) {
        try {
            String loginToken = requestLoginToken();
            if (loginToken == null) {
                return false;
            }
 
            HashMap<String, Object> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + loginToken);
 
            ArrayList<HashMap<String, Object>> modes = new ArrayList<>();
            //获取分段路径
            ArrayList<ArrayList<NavigateNode>> data = NavigateUtils.getSectionPath(nodes);
            for (ArrayList<NavigateNode> sectionNodes : data) {
                boolean flag = true;
                int oper;
                //开始路径
                NavigateNode startPath = nodes.get(0);
                if (ShuttleRunDirection.get(startPath.getDirection()) == ShuttleRunDirection.LEFT
                        || ShuttleRunDirection.get(startPath.getDirection()) == ShuttleRunDirection.RIGHT) {
                    //母轨方向
                    oper = 5;
                }else {
                    //子轨方向
                    oper = 6;
                }
 
                for (NavigateNode node : sectionNodes) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("nodexX", node.getX());
                    map.put("nodexY", node.getY());
                    map.put("nodexZ", node.getZ());
                    if (flag) {
                        map.put("oper", oper);
                        flag = false;
                    }
                    modes.add(map);
                }
            }
 
 
            HashMap<String, Object> param = new HashMap<>();
            param.put("messageName", "runRoute");
            param.put("msgTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            param.put("deviceNo", device.getDeviceNo());
            param.put("taskId", taskNo);
            param.put("nodeNum", nodes.size());
            param.put("modes", modes);
            String response = new HttpHandler.Builder()
                    .setUri(API_URL)
                    .setPath("/RDS/runRoute")
                    .setHeaders(headers)
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    @Override
    public synchronized boolean move(ShuttleCommand command) {
        try {
            String loginToken = requestLoginToken();
            if (loginToken == null) {
                return false;
            }
 
            HashMap<String, Object> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + loginToken);
 
            String response = new HttpHandler.Builder()
                    .setUri(API_URL)
                    .setPath("/RDS/runOrder")
                    .setHeaders(headers)
                    .setJson(command.getBody())
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    @Override
    public synchronized boolean lift(ShuttleCommand command) {
        try {
            String loginToken = requestLoginToken();
            if (loginToken == null) {
                return false;
            }
 
            HashMap<String, Object> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + loginToken);
 
            String response = new HttpHandler.Builder()
                    .setUri(API_URL)
                    .setPath("/RDS/actionOrder")
                    .setHeaders(headers)
                    .setJson(command.getBody())
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    @Override
    public synchronized boolean charge(ShuttleCommand command) {
        try {
            String loginToken = requestLoginToken();
            if (loginToken == null) {
                return false;
            }
 
            HashMap<String, Object> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + loginToken);
 
            String response = new HttpHandler.Builder()
                    .setUri(API_URL)
                    .setPath("/RDS/actionOrder")
                    .setHeaders(headers)
                    .setJson(command.getBody())
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    @Override
    public synchronized boolean reset(ShuttleCommand command) {
        return false;
    }
 
    @Override
    public boolean isIdle() {
        if (this.shuttleProtocol.getIdle() == null
                || this.shuttleProtocol.getPakMk() == null
                || this.shuttleProtocol.getErrorCode() == null
                || this.shuttleProtocol.getProtocolStatus() == null
        ) {
            return false;
        }
 
        boolean res = this.shuttleProtocol.getIdle()
                && this.shuttleProtocol.getPakMk()
                && this.shuttleProtocol.getErrorCode().equals("0")
                && this.shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.IDLE.id
                ;
        return res;
    }
 
    @Override
    public boolean isRequireCharge() {
        if (this.shuttleProtocol.getIdle() == null
                || this.shuttleProtocol.getPakMk() == null
                || this.shuttleProtocol.getErrorCode() == null
                || this.shuttleProtocol.getProtocolStatus() == null
        ) {
            return false;
        }
 
        boolean res = this.shuttleProtocol.getIdle()
                && this.shuttleProtocol.getPakMk()
                && this.shuttleProtocol.getErrorCode().equals("0")
                && this.shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.IDLE.id
                ;
        if (!res) {
            return res;
        } else {
            // 电量小于阈值需要进行充电
            try {
                BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class);
                if (shuttleService == null) {
                    return false;
                }
                BasShuttle basShuttle = shuttleService.getById(this.device.getDeviceNo());
                if (basShuttle == null) {
                    return false;
                }
                Integer chargeLine = basShuttle.getChargeLine();
                if (chargeLine == null) {
                    return false;
                }
                return Integer.valueOf(this.shuttleProtocol.getBatteryPower()) < chargeLine;
            } catch (Exception e) {
                News.error("fail", e);
                return false;
            }
        }
    }
 
    @Override
    public boolean isCharging() {
        if (this.shuttleProtocol.getDeviceStatus() == null) {
            return false;
        }
 
        if (this.shuttleProtocol.getDeviceStatus() == 5 || this.shuttleProtocol.getDeviceStatus() == 13) {
            //充电中和电池均衡 =》 充电
            return true;
        }
        return false;
    }
 
    @Override
    public boolean isChargingCompleted() {
        return false;
    }
 
    @Override
    public ShuttleCommand getMoveCommand(Integer taskNo, String startCodeNum, String distCodeNum, Integer allDistance, Integer runDirection, Integer runSpeed) {
        HashMap<String, Object> body = new HashMap<>();
        body.put("messageName", "runOrder");
        body.put("msgTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        body.put("deviceNo", Integer.parseInt(this.device.getDeviceNo()));
        body.put("taskId", taskNo);
 
        LocService locService = SpringUtils.getBean(LocService.class);
        Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>()
                .eq(Loc::getCode, distCodeNum)
                .eq(Loc::getHostId, this.device.getHostId()));
        if (loc == null) {
            throw new CoolException("库位信息不存在");
        }
        body.put("nodeX", loc.getRow());
        body.put("nodeY", loc.getBay());
        body.put("nodeZ", loc.getLev());
 
        ShuttleCommand command = new ShuttleCommand();
        command.setShuttleNo(Integer.parseInt(this.device.getDeviceNo()));
        command.setBody(JSON.toJSONString(body));
        command.setMode(ShuttleCommandModeType.MOVE.id);
        command.setTargetLocNo(loc.getLocNo());
        return command;
    }
 
    @Override
    public ShuttleCommand getLiftCommand(Integer taskNo, Boolean lift) {
        HashMap<String, Object> body = new HashMap<>();
        body.put("messageName", "actionOrder");
        body.put("msgTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        body.put("deviceNo", Integer.parseInt(this.device.getDeviceNo()));
        body.put("taskId", taskNo);
        body.put("action", lift ? 1 : 2);
 
        ShuttleCommand command = new ShuttleCommand();
        command.setShuttleNo(Integer.parseInt(this.device.getDeviceNo()));
        command.setBody(JSON.toJSONString(body));
        command.setMode(lift ? ShuttleCommandModeType.PALLET_LIFT.id : ShuttleCommandModeType.PALLET_DOWN.id);
        return command;
    }
 
    @Override
    public ShuttleCommand getChargeCommand(Integer taskNo, Boolean charge) {
        HashMap<String, Object> body = new HashMap<>();
        body.put("messageName", "runOrder");
        body.put("msgTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        body.put("deviceNo", Integer.parseInt(this.device.getDeviceNo()));
        body.put("taskId", taskNo);
        body.put("action", charge ? 3 : 4);
 
        ShuttleCommand command = new ShuttleCommand();
        command.setShuttleNo(Integer.parseInt(this.device.getDeviceNo()));
        command.setBody(JSON.toJSONString(body));
        command.setMode(charge ? ShuttleCommandModeType.CHARGE_OPEN.id : ShuttleCommandModeType.CHARGE_CLOSE.id);
        return command;
    }
 
    //***************设备层通讯-不同厂商设备通讯方案不一致***************
 
    //请求登录
    private String requestLoginToken() {
        try {
            HashMap<String, Object> param = new HashMap<>();
            param.put("username", "admin");
            param.put("password", "admin123");
            param.put("msgTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            String response = new HttpHandler.Builder()
                    .setUri(API_URL)
                    .setPath("/RDS/loginToken")
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {
                return jsonObject.getString("token");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    //获取设备状态
    private JSONObject requestDeviceStatus() {
        try {
            String loginToken = requestLoginToken();
            if (loginToken == null) {
                return null;
            }
 
            HashMap<String, Object> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + loginToken);
 
            HashMap<String, Object> param = new HashMap<>();
            param.put("messageName", "deviceRgvStatus");
            param.put("msgTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            param.put("deviceNo", device.getDeviceNo());
            String response = new HttpHandler.Builder()
                    .setUri(API_URL)
                    .setPath("/RDS/deviceRgvStatus")
                    .setHeaders(headers)
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {
                return jsonObject.getJSONArray("data").getJSONObject(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    @Data
    private class InnerSuhttleExtend {
 
        /**
         * 地图版本
         */
        private String mapVersion;
 
        /**
         * 状态描述
         */
        private String statusDescription;
 
    }
 
}