| | |
| | | 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)); |
| | | //非自动状态时间计时 |
| | |
| | | } |
| | | } 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;//小车状态忙,禁止执行命令 |
| | | } |
| | | |
| | | //检测小车是否要进提升机,如需要进提升机则调度提升机 |
| | | if (!checkLiftStation(wrkNo)) { |
| | | return false; |
| | | } |
| | | // //检测小车是否要进提升机,如需要进提升机则调度提升机 |
| | | // if (!checkLiftStation(wrkNo)) { |
| | | // return false; |
| | | // } |
| | | |
| | | //检测穿梭车是否在提升机内 |
| | | if (!checkShuttleInTheLift(wrkNo)) { |
| | |
| | | null, |
| | | null, |
| | | JSON.toJSONString(command), |
| | | null |
| | | null, |
| | | JSON.toJSONString(shuttleProtocol) |
| | | ); |
| | | shuttleOptService.insert(opt); |
| | | } |