| | |
| | | |
| | | import com.zy.asrs.utils.Utils; |
| | | import com.zy.common.model.NavigateNode; |
| | | import com.zy.common.model.NyShuttleOperaResult; |
| | | import com.zy.common.utils.*; |
| | | import com.zy.core.News; |
| | | import com.zy.core.ThreadHandler; |
| | |
| | | import com.zy.core.cache.OutputQueue; |
| | | import com.zy.core.cache.SlaveConnection; |
| | | import com.zy.core.enums.*; |
| | | import com.zy.core.model.LiftSlave; |
| | | 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.command.ShuttleRedisCommand; |
| | | import com.zy.core.model.command.*; |
| | | import com.zy.core.model.protocol.LiftProtocol; |
| | | import com.zy.core.model.protocol.NyShuttleProtocol; |
| | | import com.zy.core.properties.SlaveProperties; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.io.IOException; |
| | | import java.net.Socket; |
| | | import java.text.MessageFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | |
| | | private ShuttleSlave slave; |
| | | private NyShuttleProtocol shuttleProtocol; |
| | | private RedisUtil redisUtil; |
| | | private Socket socket; |
| | | |
| | | public NyShuttleThread(ShuttleSlave slave,RedisUtil redisUtil) { |
| | | this.slave = slave; |
| | |
| | | |
| | | private void read() { |
| | | try { |
| | | if (this.socket == null || this.socket.isClosed()) { |
| | | //链接断开重新链接 |
| | | this.connect(); |
| | | } |
| | | readStatus(); |
| | | //四向穿梭车空闲、有任务、标记为true、存在任务指令,需要执行任务的下一条指令 |
| | | if (shuttleProtocol.getFree() == ShuttleStatusType.IDLE.id |
| | |
| | | |
| | | //----------读取四向穿梭车状态----------- |
| | | NyShuttleHttpCommand readStatusCommand = NyHttpUtils.getReadStatusCommand(slave.getId()); |
| | | JSONObject jsonObject = NyHttpUtils.requestCommand(readStatusCommand); |
| | | JSONObject jsonObject = NyHttpUtils.requestCommand(socket, 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())); |
| | | shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.OFFLINE); |
| | | OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车Socket状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | }else { |
| | | //手动状态/自动状态 |
| | | shuttleProtocol.setWorkingMode(jsonObject.getInteger("workingMode")); |
| | |
| | | errCode.add(Integer.parseInt(o.toString())); |
| | | } |
| | | //故障码 |
| | | shuttleProtocol.setErrCode(errCode); |
| | | shuttleProtocol.setErrCode(errCode.get(0)); |
| | | //总里程数 |
| | | shuttleProtocol.setStatusSum(jsonObject.getObject("statusSum", NyShuttleProtocol.StatusSumClass.class)); |
| | | //非自动状态时间计时 |
| | |
| | | //小车处于运行中,将标记置为true |
| | | if (shuttleProtocol.getFree() == 0) { |
| | | shuttleProtocol.setPakMk(true); |
| | | }else { |
| | | //小车处于等待确认且空闲状态,如有工作号则清空路径 |
| | | if (shuttleProtocol.getTaskNo() != 0 && shuttleProtocol.getProtocolStatusType() == ShuttleProtocolStatusType.IDLE) { |
| | | Object o = redisUtil.get("shuttle_wrk_no_" + shuttleProtocol.getTaskNo()); |
| | | if (o != null) { |
| | | ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class); |
| | | ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand(); |
| | | NavigateMapData navigateMapData = new NavigateMapData(Utils.getLev(shuttleProtocol.getCurrentLocNo())); |
| | | navigateMapData.writeNavigateNodeToRedisMap(assignCommand.getNodes(), false);//解锁路径 |
| | | |
| | | //删除redis |
| | | redisUtil.del("shuttle_wrk_no_" + redisCommand.getWrkNo()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | //将四向穿梭车状态保存至数据库 |
| | |
| | | } |
| | | } 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())); |
| | | OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车Socket状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | try { |
| | | this.socket.close(); |
| | | this.socket = null; |
| | | Thread.sleep(1000); |
| | | this.connect(); |
| | | } catch (IOException | InterruptedException exception) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | try { |
| | | Socket socket = new Socket(slave.getIp(),slave.getPort()); |
| | | socket.setSoTimeout(60000); |
| | | socket.setKeepAlive(true); |
| | | this.socket = socket; |
| | | log.info(MessageFormat.format("【{0}】四向穿梭车Socket链接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | } catch (IOException e) { |
| | | OutputQueue.SHUTTLE.offer(MessageFormat.format("【{0}】四向穿梭车Socket链接失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | //发出请求 |
| | | JSONObject result = NyHttpUtils.requestCommand(command); |
| | | JSONObject result = null; |
| | | try { |
| | | result = NyHttpUtils.requestCommand(socket, command); |
| | | } catch (IOException e) { |
| | | try { |
| | | this.socket.close(); |
| | | this.socket = null; |
| | | Thread.sleep(1000); |
| | | this.connect(); |
| | | } catch (IOException exception) { |
| | | exception.printStackTrace(); |
| | | } catch (InterruptedException ex) { |
| | | throw new RuntimeException(ex); |
| | | } |
| | | } |
| | | if (result == null) { |
| | | return false;//请求失败 |
| | | } |
| | |
| | | return false; |
| | | } |
| | | |
| | | WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class); |
| | | WrkMastMapper wrkMastMapper = SpringUtils.getBean(WrkMastMapper.class); |
| | | |
| | | Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo); |
| | | if (o == null) { |
| | | return false; |
| | | } |
| | | |
| | | ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class); |
| | | List<NyShuttleHttpCommand> commands = redisCommand.getAssignCommand().getCommands(); |
| | | ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand(); |
| | | List<NyShuttleHttpCommand> commands = redisCommand.getAssignCommand().getCommands(); |
| | | //当前步序 |
| | | int commandStep = redisCommand.getCommandStep(); |
| | | if (commands.size() == 0) { |
| | |
| | | return false;//小车状态忙,禁止执行命令 |
| | | } |
| | | |
| | | //检测小车是否要进提升机,如需要进提升机则调度提升机 |
| | | if (!checkLiftStation(wrkNo)) { |
| | | // //检测小车是否要进提升机,如需要进提升机则调度提升机 |
| | | // if (!checkLiftStation(wrkNo)) { |
| | | // return false; |
| | | // } |
| | | |
| | | //检测穿梭车是否在提升机内 |
| | | if (!checkShuttleInTheLift(wrkNo)) { |
| | | return false; |
| | | } |
| | | |
| | | //检查路径是否可行走(检查路径锁定状态,检测路径是否有其他小车) |
| | | //检测当前行走路径,和下一步路径 |
| | | boolean checkPathIsAvailable = NavigateUtils.checkPathIsAvailable(command.getNodes(), shuttleProtocol.getShuttleNo().intValue(), Utils.getLev(shuttleProtocol.getCurrentLocNo())); |
| | | boolean checkPathIsAvailable2 = NavigateUtils.checkPathIsAvailable(nextNodes, shuttleProtocol.getShuttleNo().intValue(), Utils.getLev(shuttleProtocol.getCurrentLocNo())); |
| | | if (!checkPathIsAvailable && !checkPathIsAvailable2) { |
| | | //检测路径是否可行走 |
| | | if (!checkPath(command.getNodes(), nextNodes, redisCommand)) { |
| | | return false; |
| | | } |
| | | |
| | | //锁定路径,锁定当前路径和下一步路径 |
| | | List<NavigateNode> nodes = command.getNodes(); |
| | | nodes.addAll(nextNodes); |
| | | if (nextNodes != null) { |
| | | nodes.addAll(nextNodes); |
| | | } |
| | | navigateMapData.writeNavigateNodeToRedisMap(nodes, true);//所使用的路径进行锁定禁用 |
| | | |
| | | //可执行命令 |
| | |
| | | null, |
| | | null, |
| | | JSON.toJSONString(command), |
| | | null |
| | | null, |
| | | JSON.toJSONString(shuttleProtocol) |
| | | ); |
| | | shuttleOptService.insert(opt); |
| | | } |
| | |
| | | }else { |
| | | //已执行完成 |
| | | |
| | | //删除redis |
| | | redisUtil.del("shuttle_wrk_no_" + redisCommand.getWrkNo()); |
| | | //最后一段命令为移动命令,则暂缓删除redis等待清除路径时一次性删除 |
| | | //最后一段命令为不是移动命令,则删除redis |
| | | if (!command.getRequest().getBody().get("requestType").equals("move")) { |
| | | //删除redis |
| | | redisUtil.del("shuttle_wrk_no_" + redisCommand.getWrkNo()); |
| | | } |
| | | |
| | | if (!assignCommand.getCharge()) { |
| | | //对主线程抛出等待确认状态waiting |
| | |
| | | if (o == null) { |
| | | return false; |
| | | } |
| | | |
| | | WrkMastMapper wrkMastMapper = SpringUtils.getBean(WrkMastMapper.class); |
| | | |
| | | ShuttleRedisCommand redisCommand = JSON.parseObject(o.toString(), ShuttleRedisCommand.class); |
| | | //当前步序 |
| | |
| | | return true;//提升机达到小车楼层,放行 |
| | | } |
| | | |
| | | //搜索是否有其他任务占用了提升机,如果占用提升机的任务和当前任务相同,则运行执行 |
| | | WrkMast wrkMast1 = wrkMastMapper.selectLiftWrkMast(liftProtocol.getLiftNo().intValue()); |
| | | if (wrkMast1 != null && wrkMast1.getWrkNo() != wrkNo.intValue()) { |
| | | return false; |
| | | } |
| | | |
| | | //提升机未到达小车楼层,呼叫提升机 |
| | | //获取提升机命令 |
| | | NyLiftCommand liftCommand = NyLiftUtils.getLiftCommand(liftProtocol.getLiftNo().intValue(), NyLiftTaskModelType.MOVE_CAR.id, null, basDevp.getDevNo(), wrkNo.intValue()); |
| | | ArrayList<NyLiftCommand> liftCommands = new ArrayList<>(); |
| | | liftCommands.add(liftCommand); |
| | | |
| | | //提交到线程去工作 |
| | | LiftAssignCommand assignCommand = new LiftAssignCommand(); |
| | | assignCommand.setCommands(liftCommands); |
| | | assignCommand.setLiftNo(liftProtocol.getLiftNo()); |
| | | assignCommand.setTaskNo(wrkNo); |
| | | assignCommand.setTaskMode(NyLiftTaskModelType.MOVE_CAR.id.shortValue()); |
| | | |
| | | //下发任务 |
| | | MessageQueue.offer(SlaveType.Lift, liftProtocol.getLiftNo().intValue(), new Task(3, assignCommand)); |
| | | |
| | | return false;//默认不放行 |
| | | } |
| | | |
| | | /** |
| | | * 检测穿梭车是否在提升机内 |
| | | * 如穿梭车在提升机内,必须等待提升机空闲才可执行穿梭车命令 |
| | | */ |
| | | private boolean checkShuttleInTheLift(Short wrkNo) { |
| | | //读取redis数据 |
| | | if (wrkNo == null) { |
| | | return false; |
| | | } |
| | | |
| | | Object o = redisUtil.get("shuttle_wrk_no_" + wrkNo); |
| | | if (o == null) { |
| | | return false; |
| | | } |
| | | |
| | | SlaveProperties slaveProperties = SpringUtils.getBean(SlaveProperties.class); |
| | | BasLiftService liftService = SpringUtils.getBean(BasLiftService.class); |
| | | for (LiftSlave liftSlave : slaveProperties.getLift()) { |
| | | BasLift basLift = liftService.selectById(liftSlave.getId()); |
| | | if (basLift == null) { |
| | | continue; |
| | | } |
| | | Integer liftX = basLift.getPoint$().getX(); |
| | | Integer liftY = basLift.getPoint$().getY(); |
| | | if (liftX.equals(shuttleProtocol.getPoint().getX()) && liftY.equals(shuttleProtocol.getPoint().getY())) { |
| | | //小车在提升机内 |
| | | //判断提升机是否空闲 |
| | | LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, liftSlave.getId()); |
| | | if (liftThread == null) { |
| | | return false; |
| | | } |
| | | LiftProtocol liftProtocol = liftThread.getLiftProtocol(); |
| | | if (liftProtocol == null) { |
| | | return false; |
| | | } |
| | | if (liftProtocol.isIdle()) { |
| | | //提升机处于空闲,放行 |
| | | return true; |
| | | } |
| | | }else { |
| | | return true;//不在提升机内,放行 |
| | | } |
| | | } |
| | | return false;//默认不放行 |
| | | } |
| | | |
| | | /** |
| | | * 检测路径是否可行走 |
| | | * 如果路径为目标库位,但不可行走,系统将尝试重新计算路径 |
| | | */ |
| | | private boolean checkPath(List<NavigateNode> currentNodes, List<NavigateNode> nextNodes, ShuttleRedisCommand redisCommand) { |
| | | //检测路径是否可行走(检查路径锁定状态,检测路径是否有其他小车) |
| | | //检测当前行走路径,和下一步路径 |
| | | boolean checkPathIsAvailable = NavigateUtils.checkPathIsAvailable(currentNodes, shuttleProtocol.getShuttleNo().intValue(), Utils.getLev(shuttleProtocol.getCurrentLocNo())); |
| | | if (nextNodes == null) { |
| | | if (checkPathIsAvailable) { |
| | | return true;//可行走 |
| | | } |
| | | }else { |
| | | boolean checkPathIsAvailable2 = NavigateUtils.checkPathIsAvailable(nextNodes, shuttleProtocol.getShuttleNo().intValue(), Utils.getLev(shuttleProtocol.getCurrentLocNo())); |
| | | if (checkPathIsAvailable && checkPathIsAvailable2) { |
| | | return true;//可行走 |
| | | } |
| | | } |
| | | |
| | | ShuttleAssignCommand assignCommand = redisCommand.getAssignCommand(); |
| | | NavigateNode currentTarget = currentNodes.get(currentNodes.size() - 1); |
| | | String currentLocNo = NavigatePositionConvert.nodeToLocNo(currentTarget); |
| | | NavigateNode nextTarget = nextNodes.get(nextNodes.size() - 1); |
| | | String nextLocNo = NavigatePositionConvert.nodeToLocNo(nextTarget); |
| | | if (assignCommand.getLocNo().equals(currentLocNo) || assignCommand.getLocNo().equals(nextLocNo)) { |
| | | //当前路径最后一个节点是目标库位,进行路径检测,如果不可行走,重新计算路径 |
| | | //不可行走,重新计算路径 |
| | | NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleProtocol.getShuttleNo().intValue(), shuttleProtocol.getTaskNo(), shuttleProtocol.getCurrentLocNo(), assignCommand.getLocNo()); |
| | | if (result == null) { |
| | | return false;//路径计算失败,不可行走 |
| | | } |
| | | |
| | | List<NyShuttleHttpCommand> newCommands = result.getCommands();//新路径 |
| | | |
| | | //当前步序 |
| | | int commandStep = redisCommand.getCommandStep(); |
| | | List<NyShuttleHttpCommand> commands = assignCommand.getCommands(); |
| | | |
| | | commands.remove(commandStep);//移除当前步序指令 |
| | | if (assignCommand.getLocNo().equals(currentLocNo)) { |
| | | //当前路径,需要再多移除下一步指令 |
| | | commands.remove(commandStep + 1); |
| | | } |
| | | |
| | | //将新路径添加进指令集合 |
| | | commands.addAll(commandStep, newCommands); |
| | | assignCommand.setCommands(commands); |
| | | redisCommand.setAssignCommand(assignCommand); |
| | | //任务数据保存到redis |
| | | redisUtil.set("shuttle_wrk_no_" + redisCommand.getWrkNo(), JSON.toJSONString(redisCommand)); |
| | | return false;//当前不可行走,等待下一次执行走新路径 |
| | | } |
| | | |
| | | return false;//不可行走 |
| | | } |
| | | |
| | | } |