package com.zy.core.thread; import com.alibaba.fastjson.JSONObject; import com.core.common.DateUtils; import com.core.common.SpringUtils; import com.zy.asrs.entity.*; import com.zy.asrs.service.*; import com.zy.common.utils.*; import com.zy.core.News; import com.zy.core.ThreadHandler; import com.zy.core.cache.MessageQueue; import com.zy.core.cache.OutputQueue; import com.zy.core.enums.*; import com.zy.core.model.ShuttleSlave; import com.zy.core.model.Task; import com.zy.core.model.command.NyShuttleHttpCommand; import com.zy.core.model.command.ShuttleAssignCommand; import com.zy.core.model.command.ShuttleCommand; import com.zy.core.model.protocol.NyShuttleProtocol; import lombok.Data; import lombok.extern.slf4j.Slf4j; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Date; /** * 牛眼四向穿梭车线程 */ @Data @Slf4j public class NyShuttleThread implements Runnable, ThreadHandler { private ShuttleSlave slave; private NyShuttleProtocol shuttleProtocol; private RedisUtil redisUtil; public NyShuttleThread(ShuttleSlave slave,RedisUtil redisUtil) { this.slave = slave; this.redisUtil = redisUtil; } @Override public void run() { this.connect(); while (true) { try { int step = 1; Task task = MessageQueue.poll(SlaveType.Shuttle, slave.getId()); if (task != null) { step = task.getStep(); } switch (step) { // 读数据 case 1: read(); break; // 写入数据 case 2: write((ShuttleCommand) task.getData()); break; //下发任务 case 3: assignWork((ShuttleAssignCommand) task.getData()); break; default: break; } Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } } } private void read() { try { readStatus(); //四向穿梭车空闲、有任务、标记为true、存在任务指令,需要执行任务的下一条指令 if (shuttleProtocol.getFree() == ShuttleStatusType.IDLE.id && shuttleProtocol.getTaskNo() != 0 && shuttleProtocol.getPakMk()) { //执行下一步指令 executeWork(shuttleProtocol.getTaskNo()); } } catch (Exception e) { e.printStackTrace(); OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); } } private void readStatus() { try { if (null == shuttleProtocol) { shuttleProtocol = new NyShuttleProtocol(); shuttleProtocol.setShuttleNo(slave.getId().shortValue()); shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE); } //----------读取四向穿梭车状态----------- NyShuttleHttpCommand readStatusCommand = NyHttpUtils.getReadStatusCommand(slave.getId()); JSONObject jsonObject = NyHttpUtils.requestCommand(readStatusCommand); if (jsonObject == null) { OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); }else { //手动状态/自动状态 shuttleProtocol.setWorkingMode(jsonObject.getInteger("workingMode")); //允许状态 0:运行中1:空闲 shuttleProtocol.setFree(jsonObject.getInteger("free")); //当前速度 shuttleProtocol.setSpeed(jsonObject.getInteger("speed")); //负载状态 shuttleProtocol.setLoadState(jsonObject.getInteger("loadState")); //管制状态 shuttleProtocol.setSuspendState(jsonObject.getInteger("suspendState")); //顶升位置 shuttleProtocol.setLiftPosition(jsonObject.getInteger("liftPosition")); //运行方向 shuttleProtocol.setRunDir(jsonObject.getInteger("runDir")); //运行方向 shuttleProtocol.setRunDir2(jsonObject.getInteger("runDir2")); //充电状态 shuttleProtocol.setChargState(jsonObject.getInteger("chargState")); //电池电量 shuttleProtocol.setPowerPercent(jsonObject.getInteger("powerPercent")); //最高电芯电压(mV) shuttleProtocol.setMaxCellVoltage(jsonObject.getInteger("maxCellVoltage")); //最低电芯电压(mV) shuttleProtocol.setMinCellVoltage(jsonObject.getInteger("minCellVoltage")); //电池电压 shuttleProtocol.setVoltage(jsonObject.getInteger("voltage")); //充放电循环次数 shuttleProtocol.setChargeCycleTimes(jsonObject.getInteger("chargeCycleTimes")); //剩余电量 shuttleProtocol.setSurplusQuantity(jsonObject.getInteger("surplusQuantity")); //总电量 shuttleProtocol.setCountQuantity(jsonObject.getInteger("countQuantity")); //实际库位xyz shuttleProtocol.setPoint(jsonObject.getObject("point", NyShuttleProtocol.NyShuttlePointClass.class)); //实际坐标xyz shuttleProtocol.setCoord(jsonObject.getObject("coord", NyShuttleProtocol.NyShuttlePointClass.class)); //任务目的库位 shuttleProtocol.setTask(jsonObject.getObject("task", NyShuttleProtocol.TaskClass.class)); //任务状态 shuttleProtocol.setTaskState(jsonObject.getInteger("taskState")); //故障状态 shuttleProtocol.setErrState(jsonObject.getInteger("errState")); ArrayList errCode = new ArrayList<>(); for (Object o : jsonObject.getJSONArray("errCode")) { errCode.add(Integer.parseInt(o.toString())); } //故障码 shuttleProtocol.setErrCode(errCode); //总里程数 shuttleProtocol.setStatusSum(jsonObject.getObject("statusSum", NyShuttleProtocol.StatusSumClass.class)); //非自动状态时间计时 shuttleProtocol.setErrTime(jsonObject.getInteger("errTime")); //小车处于运行中,将标记置为true if (shuttleProtocol.getFree() == 0) { shuttleProtocol.setPakMk(true); } //将四向穿梭车状态保存至数据库 BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class); BasShuttle basShuttle = shuttleService.selectById(slave.getId()); if (basShuttle == null) { basShuttle = new BasShuttle(); //四向穿梭车号 basShuttle.setShuttleNo(slave.getId()); shuttleService.insert(basShuttle); } //工作模式 basShuttle.setWorkingMode(shuttleProtocol.getWorkingMode()); //运行状态 basShuttle.setFree(shuttleProtocol.getFree()); //当前速度 basShuttle.setSpeed(shuttleProtocol.getSpeed()); //负载状态 basShuttle.setLoadState(shuttleProtocol.getLoadState()); //管制状态 basShuttle.setSuspendState(shuttleProtocol.getSuspendState()); //顶升位置 basShuttle.setLiftPosition(shuttleProtocol.getLiftPosition()); //运行方向 basShuttle.setRunDir(shuttleProtocol.getRunDir()); //运行方向 basShuttle.setRunDir2(shuttleProtocol.getRunDir2()); //充电状态 basShuttle.setChargState(shuttleProtocol.getChargState()); //电池电量 basShuttle.setPowerPercent(shuttleProtocol.getPowerPercent()); //最高电芯电压 basShuttle.setMaxCellVoltage(shuttleProtocol.getMaxCellVoltage()); //电池电压 basShuttle.setVoltage(shuttleProtocol.getVoltage()); //充放电循环次数 basShuttle.setChargeCycleTimes(shuttleProtocol.getChargeCycleTimes()); //剩余电量 basShuttle.setSurplusQuantity(shuttleProtocol.getSurplusQuantity()); //总电量 basShuttle.setCountQuantity(shuttleProtocol.getCountQuantity()); //实际库位 basShuttle.setPoint(JSONObject.toJSONString(shuttleProtocol.getPoint())); //实际坐标 basShuttle.setCoord(JSONObject.toJSONString(shuttleProtocol.getCoord())); //任务目的库位 basShuttle.setTask(JSONObject.toJSONString(shuttleProtocol.getTask())); //任务状态 basShuttle.setTaskState(shuttleProtocol.getTaskState()); //故障状态 basShuttle.setErrState(shuttleProtocol.getErrState()); //总里程数 basShuttle.setStatusSum(JSONObject.toJSONString(shuttleProtocol.getStatusSum())); //非自动状态时间计时 basShuttle.setErrTime(shuttleProtocol.getErrTime()); //任务号 basShuttle.setWrkNo(shuttleProtocol.getTaskNo().intValue()); //修改时间 basShuttle.setUpdateTime(new Date()); //作业标记 basShuttle.setPakMk(shuttleProtocol.getPakMk()); if (shuttleService.updateById(basShuttle)) { OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); // log.info(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); } } } catch (Exception e) { e.printStackTrace(); OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); } } @Override public boolean connect() { return true; } @Override public void close() { } private boolean write(ShuttleCommand command){ if (null == command) { News.error("四向穿梭车写入命令为空"); return false; } BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class); if (shuttleService == null) { News.error("系统错误"); return false; } BasShuttle basShuttle = shuttleService.selectById(slave.getId().shortValue()); if (basShuttle == null) { News.error("四向穿梭车不存在"); return false; } // command.setShuttleNo(slave.getId().shortValue()); // short[] array = getCommandArr(command);//获取命令报文 // OperateResult result = modbusTcpNet.Write("0", array); // if (result != null && result.IsSuccess) { // News.info("四向穿梭车命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); // OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); // // try { // Thread.sleep(3000);//命令下发后休眠 // } catch (InterruptedException e) { // throw new RuntimeException(e); // } // // for (int i = 0; i < 5; i++) { // if (command.getCommandWord().intValue() == 5 || command.getCommandWord().intValue() == 6) { // break;//充电开关和系统复位不需要重发机制 // } // readStatus();//重新读取状态 // if (shuttleProtocol.getBusyStatusType().equals(ShuttleStatusType.BUSY)) { // break; // } // // //判断是否运行中,如不运行,重新下发命令 // result = modbusTcpNet.Write("0", array); // News.info("四向穿梭车命令下发[id:{}] >>>>> {},次数:{}", slave.getId(), JSON.toJSON(command), i); // OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2},次数:{}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command), i)); // try { // Thread.sleep(300);//命令下发后休眠 // } catch (InterruptedException e) { // throw new RuntimeException(e); // } // } // // return true; // } else { // OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】写入四向穿梭车plc数据失败 ===>> [id:{1}] [ip:{2}] [port:{3}],次数:{}", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); // News.error("写入四向穿梭车plc数据失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); // return false; // } return false; } //分配任务 private void assignWork(ShuttleAssignCommand assignCommand) { // ShuttleRedisCommand redisCommand = new ShuttleRedisCommand(); // // if (!assignCommand.getAuto()) { // List allNode = new ArrayList<>(); // List commands = new ArrayList<>(); // LocMastService locMastService = SpringUtils.getBean(LocMastService.class); // BasShuttleService shuttleService = SpringUtils.getBean(BasShuttleService.class); // NavigateMapData navigateMapData; // // //获取小车移动速度 // BasShuttle basShuttle = shuttleService.selectById(slave.getId()); // Integer runSpeed = 1000; // if (basShuttle != null) { // Integer runSpeed1 = basShuttle.getRunSpeed(); // if (runSpeed1 != null) { // runSpeed = runSpeed1; // } // } // // LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1); // LiftProtocol liftProtocol = liftThread.getLiftProtocol(); // // switch (assignCommand.getTaskMode()) { // case 1://入库 // case 2://出库 // //小车移动到提升机口,计算路径 // //计算小车起点到中点所需命令 // LocMast currentLocMast = locMastService.queryByQrCode(shuttleProtocol.getCurrentCode().toString()); // List firstMastResult = NavigateUtils.calc(currentLocMast.getLocNo(), assignCommand.getSourceLocNo(), NavigationMapType.NORMAL.id, Utils.getShuttlePoints(assignCommand.getShuttleNo().intValue(), Utils.getLev(currentLocMast.getLocNo())));//小车到中点,处于无货状态,使用正常通道地图 // boolean checkResult = Utils.checkShuttlePath(firstMastResult, shuttleProtocol.getShuttleNo().intValue()); // if (firstMastResult != null && checkResult) { // allNode.addAll(firstMastResult);//将节点进行保存 // //获取分段路径 // ArrayList> data = NavigateUtils.getSectionPath(firstMastResult); // //将每一段路径分成command指令 // for (ArrayList nodes : data) { // //开始路径 // NavigateNode startPath = nodes.get(0); // //中间路径 // NavigateNode middlePath = nodes.get(nodes.size() - 2); // //目标路径 // NavigateNode endPath = nodes.get(nodes.size() - 1); // Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离 // Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离 // // //正常移动命令 // Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码 // Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//目标二维码 // Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码 // commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed)); // } // // //托盘顶升 // commands.add(getPalletCommand((short) 1)); // }else { // //没有计算到路径,可能存在小车位置就是起点位置 // if (currentLocMast.getLocNo().equals(assignCommand.getSourceLocNo())) { // //小车位置就是起点位置,无需移动,直接顶升 // //托盘顶升 // commands.add(getPalletCommand((short) 1)); // } // } // // //计算中点到终点路径 // List secMastResult = NavigateUtils.calc(assignCommand.getSourceLocNo(), assignCommand.getLocNo(), NavigationMapType.DFX.id, Utils.getShuttlePoints(assignCommand.getShuttleNo().intValue(), Utils.getLev(assignCommand.getSourceLocNo())));//小车从中点到终点,处于有货状态,使用DFX地图 // boolean checkResult2 = Utils.checkShuttlePath(secMastResult, shuttleProtocol.getShuttleNo().intValue()); // if (secMastResult != null && checkResult2) { // allNode.addAll(secMastResult);//将节点进行保存 // //获取分段路径 // ArrayList> data = NavigateUtils.getSectionPath(secMastResult); // //将每一段路径分成command指令 // for (ArrayList nodes : data) { // //开始路径 // NavigateNode startPath = nodes.get(0); // //中间路径 // NavigateNode middlePath = nodes.get(nodes.size() - 2); // //目标路径 // NavigateNode endPath = nodes.get(nodes.size() - 1); // Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离 // Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离 // // //正常移动命令 // Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码 // Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码 // Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码 // commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed)); // } // // //托盘下降 // commands.add(getPalletCommand((short) 2)); // } // // if (firstMastResult == null || secMastResult == null) { // throw new CoolException(MessageFormat.format( "四向穿梭车出入库路径搜索失败 ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort())); // } // // navigateMapData = new NavigateMapData(Utils.getLev(currentLocMast.getLocNo())); // //所使用的路径进行锁定禁用 // navigateMapData.writeNavigateNodeToRedisMap(firstMastResult, true);////所使用的路径进行锁定禁用 // navigateMapData.writeNavigateNodeToRedisMap(secMastResult, true);////所使用的路径进行锁定禁用 // break; // case 3://托盘顶升 // case 4://托盘下降 // commands.add(getPalletCommand(assignCommand.getTaskMode() == 3 ? (short) 1 : (short) 2)); // break; // case 5://强制左移 // commands.add(getForceMoveCommand((short) 2)); // break; // case 6://强制右移 // commands.add(getForceMoveCommand((short) 1)); // break; // case 7://强制上移 // commands.add(getForceMoveCommand((short) 3)); // break; // case 8://强制下移 // commands.add(getForceMoveCommand((short) 4)); // break; // case 9://状态复位 // ShuttleCommand reset = getResetCommand(); // commands.add(reset); // break; // case 10://正方向(右)寻库位 // commands.add(getFindLocCommand((short) 1)); // break; // case 11://负方向(左)寻库位 // commands.add(getFindLocCommand((short) 2)); // break; // case 12://向正方向(前)寻库位 // commands.add(getFindLocCommand((short) 4)); // break; // case 13://向负方向(后)寻库位 // commands.add(getFindLocCommand((short) 3)); // break; // case 14://移动到目标库位 // String startQr = shuttleProtocol.getCurrentCode().toString();//起始位置 // //如果穿梭车在提升机内,移动时需要先下发出提升机命令 // if (liftProtocol.getBarcode().intValue() == shuttleProtocol.getCurrentCode().intValue()) { // //穿梭车出提升机 // Short liftArrival = liftProtocol.getPositionArrivalFeedback();//提升机位置反馈 // String liftSiteLocNo = Utils.liftArrivalToOutInStaLocNo(liftArrival); // LocMast locMast1 = locMastService.selectById(liftSiteLocNo); // ShuttleCommand moveCommand = getMoveCommand(liftProtocol.getBarcode(), Short.parseShort(locMast1.getQrCodeValue()), 1600, ShuttleRunDirection.BOTTOM.id, null, null, runSpeed); // commands.add(moveCommand); // // //起始位置修改为提升机口站点位置 // startQr = locMast1.getQrCodeValue(); // } // // LocMast locMast = locMastService.queryByQrCode(startQr); // List result = NavigateUtils.calc(locMast.getLocNo(), assignCommand.getLocNo(), NavigationMapType.NONE.id, Utils.getShuttlePoints(assignCommand.getShuttleNo().intValue(), Utils.getLev(locMast.getLocNo())));//手动命令-移动命令,使用无过滤地图 // boolean checkResult3 = Utils.checkShuttlePath(result, shuttleProtocol.getShuttleNo().intValue()); // if (result != null && checkResult3) { // //所使用的路径进行锁定禁用 // navigateMapData = new NavigateMapData(Utils.getLev(locMast.getLocNo())); // navigateMapData.writeNavigateNodeToRedisMap(result, true);////所使用的路径进行锁定禁用 // // allNode.addAll(result);//将节点进行保存 // //获取分段路径 // ArrayList> data = NavigateUtils.getSectionPath(result); // //将每一段路径分成command指令 // for (ArrayList nodes : data) { // //开始路径 // NavigateNode startPath = nodes.get(0); // //中间路径 // NavigateNode middlePath = nodes.get(nodes.size() - 2); // //目标路径 // NavigateNode endPath = nodes.get(nodes.size() - 1); // Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离 // Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离 // Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码 // Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码 // Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码 // //正常移动命令 // commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed)); // } // } // break; // case 15://充电开关 // commands.add(getChargeSwitchCommand()); // break; // case 16://移动到提升机 // LocMast locMast1 = locMastService.queryByQrCode(shuttleProtocol.getCurrentCode().toString()); // int lev = Utils.getLev(locMast1.getLocNo());//穿梭车当前高度 // String liftSiteLocNo = Utils.levToOutInStaLocNo(lev);//当前楼层站点库位号 // LocMast liftSitelocMast = locMastService.selectById(liftSiteLocNo); // List result1 = NavigateUtils.calc(locMast1.getLocNo(), liftSiteLocNo, NavigationMapType.NONE.id, Utils.getShuttlePoints(assignCommand.getShuttleNo().intValue(), Utils.getLev(locMast1.getLocNo())));//移动到提升机,使用无过滤地图 // boolean checkResult4 = Utils.checkShuttlePath(result1, shuttleProtocol.getShuttleNo().intValue()); // Short endStartCode = null; // if (result1 != null && checkResult4) { // //所使用的路径进行锁定禁用 // navigateMapData = new NavigateMapData(Utils.getLev(locMast1.getLocNo())); // navigateMapData.writeNavigateNodeToRedisMap(result1, true);////所使用的路径进行锁定禁用 // // allNode.addAll(result1);//将节点进行保存 // //获取分段路径 // ArrayList> data = NavigateUtils.getSectionPath(result1); // //将每一段路径分成command指令 // for (ArrayList nodes : data) { // //开始路径 // NavigateNode startPath = nodes.get(0); // //中间路径 // NavigateNode middlePath = nodes.get(nodes.size() - 2); // //目标路径 // NavigateNode endPath = nodes.get(nodes.size() - 1); // Integer allDistance = NavigateUtils.getCurrentPathAllDistance(nodes);//计算当前路径行走总距离 // Integer middleToDistDistance = NavigateUtils.getMiddleToDistDistance(nodes, middlePath);//计算中间点到目标点行走距离 // Short startCode = NavigatePositionConvert.xyToPosition(startPath.getX(), startPath.getY(), startPath.getZ());//开始二维码 // Short middleCode = NavigatePositionConvert.xyToPosition(middlePath.getX(), middlePath.getY(), middlePath.getZ());//中间二维码 // Short distCode = NavigatePositionConvert.xyToPosition(endPath.getX(), endPath.getY(), endPath.getZ());//目标二维码 // endStartCode = distCode; // //正常移动命令 // commands.add(getMoveCommand(startCode, distCode, allDistance, ShuttleRunDirection.get(startPath.getDirection()).id, middleCode, middleToDistDistance, runSpeed)); // } // } // // if (endStartCode == null && shuttleProtocol.getCurrentCode() == Short.parseShort(liftSitelocMast.getQrCodeValue())) { // //穿梭车已经在提升机站点口 // endStartCode = shuttleProtocol.getCurrentCode(); // } // // //增加移动进提升机命令 // ShuttleCommand moveCommand = getMoveCommand(endStartCode, liftProtocol.getBarcode(), 1600, ShuttleRunDirection.TOP.id, null, null, runSpeed); // commands.add(moveCommand); // break; // default: // } // assignCommand.setCommands(commands); // assignCommand.setNodes(allNode);//当前任务所占用的节点list // } // // redisCommand.setShuttleNo(assignCommand.getShuttleNo());//四向穿梭车号 // redisCommand.setWrkNo(assignCommand.getTaskNo());//工作号 // redisCommand.setCommandStep(0);//命令执行步序 // redisCommand.setAssignCommand(assignCommand);//命令 // redisCommand.setErrorCommands(new ArrayList());//发生错误时尝试执行的指令,优先级最高 // shuttleProtocol.setTaskNo(assignCommand.getTaskNo()); // shuttleProtocol.setAssignCommand(assignCommand); // shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WORKING); // //任务数据保存到redis // redisUtil.set("shuttle_wrk_no_" + assignCommand.getTaskNo(), JSON.toJSONString(redisCommand)); // //执行下发任务 // executeWork(assignCommand.getTaskNo()); } //执行下发的指令 private boolean executeWork(Integer wrkNo) { // //读取redis数据 // if (wrkNo == null) { // return false; // } // // WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class); // // Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo); // if (o == null) { // return false; // } // ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class); // // if (shuttleProtocol.getBusyStatus().intValue() == ShuttleStatusType.BUSY.id) { // return false;//小车状态忙 // } // // if (!checkLiftStation(wrkNo)) {//检测是否有提升机站点,有则调度提升机 // return false; // } // // LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, 1); // LiftProtocol liftProtocol = liftThread.getLiftProtocol(); // // List commands = redisCommand.getAssignCommand().getCommands(); // //当前步序 // int commandStep = redisCommand.getCommandStep(); // //path路径数目 // int size = commands.size(); // ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand(); // // if (commandStep != 0) { // //判断上一条指令是否完成 // ShuttleCommand command = commands.get(commandStep - 1); // if (command.getCommandWord().intValue() == 1) { // //移动命令 // if (command.getDistCodeNum().intValue() == shuttleProtocol.getCurrentCode().intValue()) { // //上一条指令的目标位置和当前小车位置相同,则认定上一条任务完成 // command.setComplete(true); // // //上一条指令起点是提升机二维码,则清零提升机任务号 // if (command.getStartCodeNum().intValue() == liftProtocol.getBarcode().intValue()) { // //判断提升机是否处于空闲 // if (liftProtocol.isIdleNoTask() && liftProtocol.getTaskNo().intValue() == redisCommand.getWrkNo().intValue()) { // liftProtocol.setTaskNo((short) 0);//清空任务号 // WrkMast wrkMast = wrkMastService.selectById(wrkNo); // if (wrkMast != null) { // wrkMast.setLiftNo(null);//解锁提升机 // wrkMastService.updateById(wrkMast); // } // } // } // // //入库命令,当小车取完货后,需要将提升机释放 // if (assignCommand.getTaskMode().intValue() == ShuttleTaskModeType.PAK_IN.id) { // //判断上一条指令的起点是否为输送站点且目标点不是提升机内部二维码 // Short startCodeNum = command.getStartCodeNum(); // BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); // BasDevp basDevp = basDevpService.queryByQrCode(startCodeNum.intValue());//目标站点 // if (basDevp != null && command.getDistCodeNum().intValue() != liftProtocol.getBarcode().intValue()) { // //上一条指令的起点为输送站点且目标点不是提升机内部二维码 // //此时小车应该已经离开输送站点,判断提升机是否空闲且有工作号 // if (liftProtocol.isIdleNoTask() && liftProtocol.getTaskNo().intValue() == redisCommand.getWrkNo().intValue()) { // liftProtocol.setTaskNo((short) 0);//清空任务号 // WrkMast wrkMast = wrkMastService.selectById(wrkNo); // if (wrkMast != null) { // wrkMast.setLiftNo(null);//解锁提升机 // wrkMastService.updateById(wrkMast); // } // } // } // } // // } // } else if (command.getCommandWord().intValue() == 2) { // //托盘顶升命令 // if (command.getPalletLift().intValue() == 1) { // //顶升 // //判断是否顶升到位 // if (shuttleProtocol.getPlcOutputLift()) { // //自动模式 // if (assignCommand.getAuto() && shuttleProtocol.getPlcInputStatus().intValue() == 1) { // //顶升到位,且托盘雷达有物,认定任务完成 // command.setComplete(true); // }else { // //手动模式,不判断托盘雷达 // //顶升到位,认定任务完成 // command.setComplete(true); // } // } // }else { // //下降 // //判断是否下降到位,判断托盘雷达是否无物 // if (!shuttleProtocol.getPlcOutputLift() && !shuttleProtocol.getPlcOutputTransfer()) { // //自动模式 // if (assignCommand.getAuto() && shuttleProtocol.getPlcInputStatus().intValue() == 0) { // //下降到位,且托盘雷达无物,认定任务完成 // command.setComplete(true); // }else { // //手动模式,不判断托盘雷达 // //下降到位,且托盘雷达无物,认定任务完成 // command.setComplete(true); // } // } // } // } else if (command.getCommandWord().intValue() == 5) { // //充电命令 // //判断小车充电开关 // if (shuttleProtocol.getPlcOutputCharge()) { // //正常充电,认定任务完成 // command.setComplete(true); // } // } // //任务数据保存到redis // redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand)); // // if (!command.getComplete()) { // //上一条任务未完成,禁止下发命令 // return false; // } // } // // if (commands.size() == 0) { // return false; // } // // //取出命令 // ShuttleCommand command = commands.get(commandStep); // // if (assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_IN.id.shortValue() // || assignCommand.getTaskMode() == ShuttleTaskModeType.PAK_OUT.id.shortValue() // ) { // //小车失去坐标,禁止下发命令 // if (shuttleProtocol.getCurrentCode() == 0) { // return false; // } // } // // // //判断小车当前二维码是否为提升机二维码 // if (shuttleProtocol.getCurrentCode().intValue() == liftProtocol.getBarcode().intValue()) { // //小车当前命令起始位置就是提升机二维码,说明小车需要向提升机外移动,则需要判断状态是否满足 // if (command.getStartCodeNum().intValue() == liftProtocol.getBarcode().intValue()){ // //提升机是否空闲,提升机是否到达目标楼层,目标楼层是否给出提升机到位信号位 // if (!liftProtocol.isIdleNoTask()) { // return false;//提升机忙,禁止下发命令 // } // if (liftProtocol.getTaskNo().intValue() != 0 && liftProtocol.getTaskNo().intValue() != wrkNo) { // //提升机工作号和当前工作不相同,禁止下发命令 // return false; // } // // Short distCodeNum = command.getDistCodeNum();//目标二维码 // BasDevpService basDevpService = SpringUtils.getBean(BasDevpService.class); // BasDevp basDevp = basDevpService.queryByQrCode(distCodeNum.intValue());//目标站点 // if (basDevp == null) { // return false;//找不到目标站,禁止下发命令 // } // // int lev = Utils.getLev(basDevp.getLocNo());//目标二维码所在楼层 // int liftLev = liftProtocol.getLev().intValue();//提升机所在楼层 // if (liftLev != lev) { // return false;//提升机不在目标楼层,禁止下发命令 // } // // //获取目标站信息 // SiemensDevpThread devpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1); // StaProtocol staProtocol = devpThread.getStation().get(basDevp.getDevNo()); // if (staProtocol == null) { // return false;//站点信息不存在,禁止下发命令 // } // if (!staProtocol.isLiftArrival()) { // return false;//站点提升机到位信号false,禁止下发命令 // } // // //条件满足,占用提升机 // liftProtocol.setTaskNo(wrkNo); // } // } // // //下发命令 // if (!write(command)) { // News.error("四向穿梭车命令下发失败,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command)); // return false; // } else { // News.info("四向穿梭车命令下发成功,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command)); // // //将标记置为false(防止重发) // shuttleProtocol.setPakMk(false); // // //保存数据到数据库做流水 // BasShuttleOptService shuttleOptService = SpringUtils.getBean(BasShuttleOptService.class); // if (shuttleOptService != null) { // short[] commandArr = getCommandArr(command);//获取命令报文 // BasShuttleOpt opt = new BasShuttleOpt( // assignCommand.getTaskNo().intValue(), // assignCommand.getShuttleNo().intValue(), // new Date(), // ShuttleTaskModeType.get(assignCommand.getTaskMode()).desc, // assignCommand.getSourceLocNo(), // assignCommand.getLocNo(), // null, // null, // null, // JSON.toJSONString(command), // JSON.toJSONString(commandArr) // ); // shuttleOptService.insert(opt); // } // // //判断数据是否执行完成 // if (commandStep < size - 1) { // //更新redis数据 // //步序增加 // commandStep++; // redisCommand.setCommandStep(commandStep); // //任务数据保存到redis // redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand)); // }else { // //已执行完成 // // if (redisCommand.getLiftSecurityMk()) { // //曾锁定过提升机,需要进行解锁 // if (liftProtocol != null) { // liftProtocol.setSecurityMk(false); // } // } // // String locNo = shuttleProtocol.getLocNo() == null ? shuttleProtocol.getSourceLocNo() : shuttleProtocol.getLocNo(); // if (locNo != null) { // //解除锁定的库位路径 // NavigateMapData navigateMapData = new NavigateMapData(Utils.getLev(locNo)); // navigateMapData.writeNavigateNodeToRedisMap(redisCommand.getAssignCommand().getNodes(), false); // } // // //删除redis // redisUtil.del("shuttle_wrk_no_" + redisCommand.getWrkNo()); // // if (!assignCommand.getAuto()) { // //手动模式不抛出等待状态,直接复位 // //设置四向穿梭车为空闲状态 // shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE); // //任务号清零 // shuttleProtocol.setTaskNo((short) 0); // //标记复位 // shuttleProtocol.setPakMk(true); // News.info("四向穿梭车手动任务执行完成,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command)); // }else { // if (!assignCommand.getCharge()) { // //对主线程抛出等待确认状态waiting // shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.WAITING); // }else { // shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.CHARGING_WAITING); // } // News.info("四向穿梭车任务执行下发完成等待执行结束,穿梭车号={},任务数据={}", shuttleProtocol.getShuttleNo(), JSON.toJSON(command)); // } // // } // // } return true; } }