#
Junjie
2024-03-26 161b7184288c47d8446fdc21786821d489be736a
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
package com.zy.asrs.wcs.rcs.thread.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.asrs.common.utils.HttpHandler;
import com.zy.asrs.framework.common.DateUtils;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.OutputQueue;
import com.zy.asrs.wcs.rcs.model.enums.ShuttleProtocolStatusType;
import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol;
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.extern.slf4j.Slf4j;
 
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
 
@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(device.getDeviceNo());
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                    shuttleProtocol.setDevice(device);
                }
 
                //----------读取四向穿梭车状态-----------
                //小车忙状态位
                shuttleProtocol.setDeviceStatus(data.getInteger("deviceStatus"));
                //当前二维码
                shuttleProtocol.setCurrentCode(data.getString("deviceLocation") == null ? "0" : data.getString("deviceLocation"));
                //电池电量
                shuttleProtocol.setBatteryPower(data.getString("battery") == null ? "0%" : data.getString("battery"));
 
 
                //是否顶升
                shuttleProtocol.setHasLift(data.getInteger("palletStatus") == 1 ? true : false);
                //行驶方向
                shuttleProtocol.setRunDirection(data.getString("direction") == null ? "none" : data.getString("direction"));
 
                ///读取四向穿梭车状态-end
 
//                //小车处于忙碌状态,将标记置为true
//                if (shuttleProtocol.getDeviceStatusType() == ShuttleDeviceStatusType.BUSY) {
//                    shuttleProtocol.setPakMk(true);
//                }
//
//                if (shuttleProtocol.getProtocolStatusType() == null && shuttleProtocol.getDeviceStatus().intValue() == ShuttleDeviceStatusType.IDLE.id) {
//                    //小车空闲状态、小车任务状态为未知,认定曾离线过,需要复位成空闲
//                    shuttleProtocol.setProtocolStatusType(ShuttleProtocolStatusType.IDLE);
//                }
 
//                if (System.currentTimeMillis() - shuttleProtocol.getDeviceDataLog() > 1000 * 5) {
//                    //采集时间超过5s,保存一次数据记录
//                    //保存数据记录
//                    DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class);
//                    DeviceDataLog deviceDataLog = new DeviceDataLog();
//                    deviceDataLog.setOriginData(Base64.getEncoder().encodeToString(result.Content));
//                    deviceDataLog.setWcsData(JSON.toJSONString(shuttleProtocol));
//                    deviceDataLog.setType("shuttle");
//                    deviceDataLog.setDeviceNo(shuttleProtocol.getShuttleNo().intValue());
//                    deviceDataLog.setCreateTime(new Date());
//                    deviceDataLogService.insert(deviceDataLog);
//
//                    //更新采集时间
//                    shuttleProtocol.setDeviceDataLog(System.currentTimeMillis());
//                }
            } 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 synchronized boolean movePath() {
        return false;
    }
 
    @Override
    public synchronized boolean move() {
        return false;
    }
 
    @Override
    public synchronized boolean lift() {
        return false;
    }
 
    //***************设备层通讯-不同厂商设备通讯方案不一致***************
 
    //请求登录
    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;
    }
}