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; } //***************设备层通讯-不同厂商设备通讯方案不一致*************** //请求登录 private String requestLoginToken() { try { HashMap 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 headers = new HashMap<>(); headers.put("Authorization", "Bearer " + loginToken); HashMap 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; } }