| | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.zy.asrs.entity.BasShuttle; |
| | | import com.zy.asrs.entity.DeviceConfig; |
| | | import com.zy.asrs.entity.DeviceDataLog; |
| | | import com.zy.asrs.entity.LocMast; |
| | | import com.zy.asrs.service.BasShuttleService; |
| | | import com.zy.asrs.service.DeviceDataLogService; |
| | | import com.zy.asrs.service.LocMastService; |
| | | import com.zy.asrs.utils.Utils; |
| | | import com.zy.common.ExecuteSupport; |
| | |
| | | import com.zy.core.News; |
| | | import com.zy.core.model.DeviceCommandMsgModel; |
| | | import com.zy.core.utils.DeviceMsgUtils; |
| | | import com.zy.core.action.ShuttleAction; |
| | | import com.zy.core.cache.OutputQueue; |
| | | import com.zy.core.enums.*; |
| | | import com.zy.core.model.CommandResponse; |
| | |
| | | private static final boolean DEBUG = false;//调试模式 |
| | | |
| | | private List<JSONObject> socketReadResults = new ArrayList<>(); |
| | | private List<JSONObject> socketResults = new ArrayList<>(); |
| | | private List<DeviceMsgModel> socketResults = new ArrayList<>(); |
| | | |
| | | //原始设备数据 |
| | | private Object originDeviceData; |
| | |
| | | listenInit();//监听初始化事件 |
| | | readStatus(); |
| | | Thread.sleep(100); |
| | | |
| | | // //小车空闲且有跑库程序 |
| | | // shuttleAction.moveLoc(deviceConfig.getDeviceNo()); |
| | | } catch (Exception e) { |
| | | log.error("ShuttleThread Fail", e); |
| | | } |
| | |
| | | }); |
| | | readThread.start(); |
| | | |
| | | //设备执行 |
| | | Thread executeThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | ShuttleAction shuttleAction = null; |
| | | try { |
| | | shuttleAction = SpringUtils.getBean(ShuttleAction.class); |
| | | }catch (Exception e){ |
| | | continue; |
| | | } |
| | | |
| | | if (shuttleAction == null) { |
| | | continue; |
| | | } |
| | | |
| | | Object object = redisUtil.get(RedisKeyType.SHUTTLE_FLAG.key + deviceConfig.getDeviceNo()); |
| | | if (object == null) { |
| | | continue; |
| | | } |
| | | |
| | | Integer taskNo = Integer.valueOf(String.valueOf(object)); |
| | | if (taskNo != 0) { |
| | | //存在任务需要执行 |
| | | boolean result = shuttleAction.executeWork(deviceConfig.getDeviceNo(), taskNo); |
| | | } |
| | | |
| | | // //小车空闲且有跑库程序 |
| | | // shuttleAction.moveLoc(deviceConfig.getDeviceNo()); |
| | | |
| | | //演示模式 |
| | | shuttleAction.demo(deviceConfig.getDeviceNo()); |
| | | |
| | | Thread.sleep(200); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }); |
| | | executeThread.start(); |
| | | |
| | | //其他任务 |
| | | Thread otherThread = new Thread(() -> { |
| | | while (true) { |
| | | try { |
| | | saveLog();//保存数据 |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }); |
| | | otherThread.start(); |
| | | } |
| | | |
| | | private void saveLog() { |
| | | if (shuttleProtocol == null) { |
| | | return; |
| | | } |
| | | |
| | | if (System.currentTimeMillis() - shuttleProtocol.getDeviceDataLog() > 1000 * 5) { |
| | | if (this.originDeviceData != null) { |
| | | //采集时间超过5s,保存一次数据记录 |
| | | //保存数据记录 |
| | | DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class); |
| | | if (deviceDataLogService == null) { |
| | | return; |
| | | } |
| | | DeviceDataLog deviceDataLog = new DeviceDataLog(); |
| | | deviceDataLog.setOriginData(JSON.toJSONString(this.originDeviceData)); |
| | | deviceDataLog.setWcsData(JSON.toJSONString(shuttleProtocol)); |
| | | deviceDataLog.setType(String.valueOf(SlaveType.Shuttle)); |
| | | deviceDataLog.setDeviceNo(deviceConfig.getDeviceNo()); |
| | | deviceDataLog.setCreateTime(new Date()); |
| | | deviceDataLogService.insert(deviceDataLog); |
| | | |
| | | //更新采集时间 |
| | | shuttleProtocol.setDeviceDataLog(System.currentTimeMillis()); |
| | | OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), deviceConfig.getDeviceNo())); |
| | | } |
| | | } |
| | | // //设备执行 |
| | | // Thread executeThread = new Thread(() -> { |
| | | // while (true) { |
| | | // try { |
| | | // ShuttleAction shuttleAction = null; |
| | | // try { |
| | | // shuttleAction = SpringUtils.getBean(ShuttleAction.class); |
| | | // }catch (Exception e){ |
| | | // continue; |
| | | // } |
| | | // |
| | | // if (shuttleAction == null) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // Object object = redisUtil.get(RedisKeyType.SHUTTLE_FLAG.key + deviceConfig.getDeviceNo()); |
| | | // if (object == null) { |
| | | // continue; |
| | | // } |
| | | // |
| | | // Integer taskNo = Integer.valueOf(String.valueOf(object)); |
| | | // if (taskNo != 0) { |
| | | // //存在任务需要执行 |
| | | // boolean result = shuttleAction.executeWork(deviceConfig.getDeviceNo(), taskNo); |
| | | // } |
| | | // Thread.sleep(100); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | // }); |
| | | // executeThread.start(); |
| | | } |
| | | |
| | | private void listenMessageFromRedis() { |
| | |
| | | data.put("originDeviceData", deviceMsg.getDeviceOriginMsg()); |
| | | socketReadResults.add(data); |
| | | } else { |
| | | socketResults.add(data);//添加数据 |
| | | socketResults.add(deviceMsg);//添加数据 |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public JSONObject getRequestBody(String type, String taskId) { |
| | | public JSONObject getRequestBody(String resultKey) { |
| | | try { |
| | | // 获取服务器响应 |
| | | JSONObject result = null; |
| | | if (type.equals("readState")) { |
| | | type = "state"; |
| | | } |
| | | |
| | | int idx = -1; |
| | | for (int i = 0; i < socketResults.size(); i++) { |
| | | JSONObject socketResult = socketResults.get(i); |
| | | if (!socketResult.get("msgType").equals("responseMsg")) {//不是响应内容 |
| | | continue; |
| | | } |
| | | |
| | | JSONObject resultResponse = JSON.parseObject(socketResult.get("response").toString()); |
| | | JSONObject resultBody = JSON.parseObject(resultResponse.get("body").toString()); |
| | | String responseType = resultBody.get("responseType").toString(); |
| | | if (DEBUG) { |
| | | result = socketResult; |
| | | DeviceMsgModel deviceMsgModel = socketResults.get(i); |
| | | if(deviceMsgModel.getResultKey().equals(resultKey)){ |
| | | idx = i; |
| | | result = JSON.parseObject(JSON.toJSONString(deviceMsgModel.getDeviceMsg())); |
| | | break; |
| | | } |
| | | |
| | | if (!responseType.equals(type)) { |
| | | continue;//响应类型与请求类型不一致,不在调试模式下 |
| | | } |
| | | |
| | | if (taskId != null) { |
| | | String responseTaskId = resultBody.get("taskId").toString(); |
| | | if (!responseTaskId.equals(taskId)) { |
| | | continue;//响应ID与请求ID不一致,不在调试模式下 |
| | | } |
| | | } |
| | | |
| | | result = socketResult; |
| | | break; |
| | | } |
| | | |
| | | if (result == null) { |
| | | return null;//无响应结果 |
| | | } |
| | | |
| | | socketResults.remove(idx); |
| | | return filterBodyData(result);//返回Body结果集 |
| | | } catch (Exception e) { |
| | | return null; |
| | |
| | | // 获取服务器响应 |
| | | JSONObject result = null; |
| | | int removeIdx = -1; |
| | | JSONObject socketResult = null; |
| | | for (int i = 0; i < socketResults.size(); i++) { |
| | | JSONObject socketResult = socketResults.get(i); |
| | | if (socketResult == null) { |
| | | DeviceMsgModel deviceMsgModel = socketResults.get(i); |
| | | if (!deviceMsgModel.getDeviceMsgType().equals("shuttleInit")) { |
| | | continue; |
| | | } |
| | | if (!socketResult.get("msgType").equals("requestMsg")) {//不是请求内容 |
| | | continue; |
| | | } |
| | | removeIdx = i; |
| | | socketResult = JSON.parseObject(JSON.toJSONString(deviceMsgModel.getDeviceMsg())); |
| | | break; |
| | | } |
| | | |
| | | JSONObject resultResponse = JSON.parseObject(socketResult.get("request").toString()); |
| | | JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString()); |
| | | JSONObject resultBody = JSON.parseObject(resultResponse.get("body").toString()); |
| | | String requestType = resultBody.getString("requestType"); |
| | | Integer requestId = resultHeader.getInteger("requestId"); |
| | | if (requestType.equals("init")) { |
| | | removeIdx = i;//此数据已经处理,从结果集中剔除 |
| | | socketResults.remove(removeIdx); |
| | | if (socketResult == null) { |
| | | return; |
| | | } |
| | | |
| | | Integer code = resultBody.getInteger("code"); |
| | | //小车复位请求 |
| | | ShuttleCommand initCommand = getInitCommand(requestId, code); |
| | | //发出请求 |
| | | NyShuttleHttpCommand httpCommand = JSON.parseObject(initCommand.getBody(), NyShuttleHttpCommand.class); |
| | | requestCommand(httpCommand); |
| | | JSONObject resultResponse = JSON.parseObject(socketResult.get("request").toString()); |
| | | JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString()); |
| | | JSONObject resultBody = JSON.parseObject(resultResponse.get("body").toString()); |
| | | String requestType = resultBody.getString("requestType"); |
| | | Integer requestId = resultHeader.getInteger("requestId"); |
| | | if (requestType.equals("init")) { |
| | | socketResults.remove(removeIdx); |
| | | |
| | | log.info(MessageFormat.format("【{0}】四向车复位上报 ===>> [code:{1}] [ip:{2}] [port:{3}]", deviceConfig.getDeviceNo(), code, deviceConfig.getIp(), deviceConfig.getPort())); |
| | | OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向车复位上报 ===>> [code:{1}] [ip:{2}] [port:{3}]", deviceConfig.getDeviceNo(), code, deviceConfig.getIp(), deviceConfig.getPort())); |
| | | break; |
| | | } |
| | | Integer code = resultBody.getInteger("code"); |
| | | //小车复位请求 |
| | | ShuttleCommand initCommand = getInitCommand(requestId, code); |
| | | //发出请求 |
| | | NyShuttleHttpCommand httpCommand = JSON.parseObject(initCommand.getBody(), NyShuttleHttpCommand.class); |
| | | requestCommand(httpCommand); |
| | | |
| | | log.info(MessageFormat.format("【{0}】四向车复位上报 ===>> [code:{1}] [ip:{2}] [port:{3}]", deviceConfig.getDeviceNo(), code, deviceConfig.getIp(), deviceConfig.getPort())); |
| | | OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向车复位上报 ===>> [code:{1}] [ip:{2}] [port:{3}]", deviceConfig.getDeviceNo(), code, deviceConfig.getIp(), deviceConfig.getPort())); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | try { |
| | | //发出请求 |
| | | NyShuttleHttpCommand httpCommand = JSON.parseObject(command.getBody(), NyShuttleHttpCommand.class); |
| | | JSONObject requestToken = requestCommand(httpCommand); |
| | | String resultKey = requestCommand(httpCommand); |
| | | //查询请求结果 |
| | | JSONObject result = queryCommandStatus(requestToken.getString("requestType"), requestToken.getString("taskId")); |
| | | JSONObject result = queryCommandStatus(resultKey); |
| | | if (result == null) { |
| | | return response;//请求失败 |
| | | } |
| | |
| | | try { |
| | | //发出请求 |
| | | NyShuttleHttpCommand httpCommand = JSON.parseObject(command.getBody(), NyShuttleHttpCommand.class); |
| | | JSONObject requestToken = requestCommand(httpCommand); |
| | | String resultKey = requestCommand(httpCommand); |
| | | //查询请求结果 |
| | | JSONObject result = queryCommandStatus(requestToken.getString("requestType"), requestToken.getString("taskId")); |
| | | JSONObject result = queryCommandStatus(resultKey); |
| | | if (result == null) { |
| | | return response;//请求失败 |
| | | } |
| | |
| | | try { |
| | | //发出请求 |
| | | NyShuttleHttpCommand httpCommand = JSON.parseObject(command.getBody(), NyShuttleHttpCommand.class); |
| | | JSONObject requestToken = requestCommand(httpCommand); |
| | | String resultKey = requestCommand(httpCommand); |
| | | //查询请求结果 |
| | | JSONObject result = queryCommandStatus(requestToken.getString("requestType"), requestToken.getString("taskId")); |
| | | JSONObject result = queryCommandStatus(resultKey); |
| | | if (result == null) { |
| | | return response;//请求失败 |
| | | } |
| | |
| | | try { |
| | | //发出请求 |
| | | NyShuttleHttpCommand httpCommand = JSON.parseObject(command.getBody(), NyShuttleHttpCommand.class); |
| | | JSONObject requestToken = requestCommand(httpCommand); |
| | | String resultKey = requestCommand(httpCommand); |
| | | //查询请求结果 |
| | | JSONObject result = queryCommandStatus(requestToken.getString("requestType"), requestToken.getString("taskId")); |
| | | JSONObject result = queryCommandStatus(resultKey); |
| | | if (result == null) { |
| | | return response;//请求失败 |
| | | } |
| | |
| | | //将路径锁与小车路径进行匹配 |
| | | ArrayList<NavigateNode> tmp = new ArrayList<>(); |
| | | //检测路径是否被锁定 |
| | | int[][] map = navigateMapData.getDataFromRedis(lev, NavigationMapType.DFX.id, null, null); |
| | | int[][] map = navigateMapData.getDataFromRedis(lev, NavigationMapType.getDfxWithDevice(), null, null); |
| | | for (NavigateNode node : path) { |
| | | if(map[node.getX()][node.getY()] == -999) { |
| | | tmp.add(node); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void updateDeviceDataLogTime(long time) { |
| | | shuttleProtocol.setDeviceDataLog(time); |
| | | } |
| | | |
| | | @Override |
| | | public JSONObject parseStatusToMsg(ShuttleProtocol shuttleProtocol) { |
| | | JSONObject result = new JSONObject(); |
| | | result.put("msgType", "responseMsg"); |
| | |
| | | int yp = Integer.parseInt(String.valueOf(nyNode.get("x"))); |
| | | int z = Integer.parseInt(String.valueOf(nyNode.get("z"))); |
| | | |
| | | List<List<MapNode>> mapData = navigateMapData.getJsonData(startNode.getZ(), NavigationMapType.NONE.id, null, null); |
| | | List<List<MapNode>> mapData = navigateMapData.getJsonData(startNode.getZ(), NavigationMapType.getMapTypes(NavigationMapType.NONE), null, null); |
| | | List<MapNode> mapNodes = mapData.get(node.getX()); |
| | | MapNode mapNode = mapNodes.get(node.getY()); |
| | | |
| | |
| | | throw new CoolException("地址码不存在"); |
| | | } |
| | | |
| | | List<List<MapNode>> mapData = navigateMapData.getJsonData(lev, NavigationMapType.NONE.id, null, null); |
| | | List<List<MapNode>> mapData = navigateMapData.getJsonData(lev, NavigationMapType.getMapTypes(NavigationMapType.NONE), null, null); |
| | | List<MapNode> mapNodes = mapData.get(locMast.getRow1()); |
| | | MapNode mapNode = mapNodes.get(locMast.getBay1()); |
| | | |
| | |
| | | } |
| | | |
| | | //发出请求 |
| | | private JSONObject requestCommand(NyShuttleHttpCommand httpCommand) throws IOException { |
| | | private String requestCommand(NyShuttleHttpCommand httpCommand) throws IOException { |
| | | try { |
| | | DeviceMsgUtils deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class); |
| | | if (deviceMsgUtils == null) { |
| | |
| | | commandMsgModel.setDeviceType(String.valueOf(SlaveType.Shuttle)); |
| | | commandMsgModel.setCommand(data); |
| | | String key = deviceMsgUtils.sendDeviceCommand(SlaveType.Shuttle, deviceConfig.getDeviceNo(), commandMsgModel); |
| | | |
| | | String requestType = null; |
| | | String taskId = null; |
| | | try { |
| | | requestType = httpCommand.getRequest().getBody().get("requestType").toString(); |
| | | taskId = httpCommand.getRequest().getBody().get("taskId").toString(); |
| | | } catch (Exception e) { |
| | | // return null; |
| | | //taskId可能取空,不报错,正常情况 |
| | | } |
| | | |
| | | JSONObject result = new JSONObject(); |
| | | result.put("requestType", requestType); |
| | | result.put("taskId", taskId); |
| | | return result; |
| | | return key; |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private JSONObject queryCommandStatus(String requestType, String taskId) { |
| | | private JSONObject queryCommandStatus(String resultKey) { |
| | | // 获取服务器响应 |
| | | // 尝试50次 |
| | | JSONObject result = null; |
| | | for (int i = 0; i < 50; i++) { |
| | | result = getRequestBody(requestType, taskId); |
| | | result = getRequestBody(resultKey); |
| | | if (result == null) { |
| | | try { |
| | | Thread.sleep(500); |
| | |
| | | return new int[]{data.getInteger("y"), data.getInteger("x"), z}; |
| | | } |
| | | |
| | | @Override |
| | | public Object getOriginDeviceData() { |
| | | return this.originDeviceData; |
| | | } |
| | | |
| | | @Data |
| | | private class InnerSuhttleExtend { |
| | | |