|  |  |  | 
|---|
|  |  |  | private SteProtocol steProtocol; | 
|---|
|  |  |  | private short heartBeatVal = 1; | 
|---|
|  |  |  | private boolean resetFlag = false; | 
|---|
|  |  |  | private boolean connectSte = false; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public SteThread(SteSlave slave) { | 
|---|
|  |  |  | this.slave = slave; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @SuppressWarnings("InfiniteLoopStatement") | 
|---|
|  |  |  | public void run() { | 
|---|
|  |  |  | this.connect(); | 
|---|
|  |  |  | public void run() {        connectSte = this.connect(); | 
|---|
|  |  |  | while(!connectSte){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | connectSte = this.connect(); | 
|---|
|  |  |  | Thread.sleep(100); | 
|---|
|  |  |  | } catch (Exception e){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 启动线程自动重连 | 
|---|
|  |  |  | new Thread(this::steConnect).start(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 启动读数据线程 | 
|---|
|  |  |  | new Thread(this::readStatusSte).start(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 启动任务下发线程 | 
|---|
|  |  |  | new Thread(this::taskIssued).start(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 任务下发 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void taskIssued() { | 
|---|
|  |  |  | while (true) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(!connectSte){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(1000L); | 
|---|
|  |  |  | } catch (Exception e){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int step = 1; | 
|---|
|  |  |  | Task task = MessageQueue.poll(SlaveType.Ste, slave.getId()); | 
|---|
|  |  |  | if (task != null) { | 
|---|
|  |  |  | 
|---|
|  |  |  | switch (step) { | 
|---|
|  |  |  | // 读数据 | 
|---|
|  |  |  | case 1: | 
|---|
|  |  |  | readStatus(); | 
|---|
|  |  |  | //                        readStatus(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | // 写入数据 | 
|---|
|  |  |  | case 2: | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 心跳 | 
|---|
|  |  |  | //                heartbeat(); | 
|---|
|  |  |  | Thread.sleep(500); | 
|---|
|  |  |  | Thread.sleep(50); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | log.error("STE写线程异常"+e.getMessage()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void steConnect() { | 
|---|
|  |  |  | while (true) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(1000); | 
|---|
|  |  |  | if(!connectSte){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | connectSte = this.connect(); | 
|---|
|  |  |  | Thread.sleep(100); | 
|---|
|  |  |  | } catch (Exception e){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("ste连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|---|
|  |  |  | initSte(); | 
|---|
|  |  |  | //                e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void readStatusSte() { | 
|---|
|  |  |  | while (true) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(!connectSte){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(1000L); | 
|---|
|  |  |  | } catch (Exception e){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | initSte(); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Thread.sleep(50); | 
|---|
|  |  |  | //                System.out.println("读线程"+ slave.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | readStatus(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("ste数据读取线程异常!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | initSte(); | 
|---|
|  |  |  | //                e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void readStatus(){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) 22); | 
|---|
|  |  |  | OperateResultExOne<byte[]> result = siemensS7Net.Read("DB101.0", (short) 28); | 
|---|
|  |  |  | if (result.IsSuccess) { | 
|---|
|  |  |  | if (null == steProtocol) { | 
|---|
|  |  |  | steProtocol = new SteProtocol(); | 
|---|
|  |  |  | 
|---|
|  |  |  | steProtocol.setRow(siemensS7Net.getByteTransform().TransInt16(result.Content, 14)); | 
|---|
|  |  |  | steProtocol.setBay(siemensS7Net.getByteTransform().TransInt16(result.Content, 16)); | 
|---|
|  |  |  | steProtocol.setLev(siemensS7Net.getByteTransform().TransInt16(result.Content, 18)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                steProtocol.setReady(siemensS7Net.getByteTransform().TransInt16(result.Content, 20)); | 
|---|
|  |  |  | boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, 20, 1); | 
|---|
|  |  |  | steProtocol.setReady(status[0]?(short)1:(short)0); | 
|---|
|  |  |  | steProtocol.setLoca(siemensS7Net.getByteTransform().TransInt16(result.Content, 20)); | 
|---|
|  |  |  | steProtocol.setLocaStart(siemensS7Net.getByteTransform().TransInt16(result.Content, 22)); | 
|---|
|  |  |  | steProtocol.setLocaEnd(siemensS7Net.getByteTransform().TransInt16(result.Content, 24)); | 
|---|
|  |  |  | boolean[] status = siemensS7Net.getByteTransform().TransBool(result.Content, 26, 1); | 
|---|
|  |  |  | //                steProtocol.setReady(status[0]?(short)1:(short)0); | 
|---|
|  |  |  | //                steProtocol.setReady(status[1]?(short)1:(short)0); | 
|---|
|  |  |  | //                steProtocol.setReady(status[2]?(short)1:(short)0); | 
|---|
|  |  |  | //                steProtocol.setLoad(status[3]?(short)1:(short)0); | 
|---|
|  |  |  | //                steProtocol.setReady(status[4]?(short)1:(short)0); | 
|---|
|  |  |  | steProtocol.setChargeStatus(status[5]?(short)1:(short)0); | 
|---|
|  |  |  | steProtocol.setTrack(status[6]?(short)1:(short)0); | 
|---|
|  |  |  | steProtocol.setReady(status[3]?(short)1:(short)0); | 
|---|
|  |  |  | //                steProtocol.setReady(status[3]?(short)1:(short)0); | 
|---|
|  |  |  | //                steProtocol.setTrack(status[6]?(short)1:(short)0); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | SteLocaType locaType = SteLocaType.NONE; | 
|---|
|  |  |  | boolean[] status2 = siemensS7Net.getByteTransform().TransBool(result.Content, 21, 1); | 
|---|
|  |  |  | if (steProtocol.getRow().intValue() >= 6) { | 
|---|
|  |  |  | if (status[7]){//A点 | 
|---|
|  |  |  | locaType = SteLocaType.A; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (status2[1]){//A待机点 | 
|---|
|  |  |  | if (locaType.equals(SteLocaType.NONE)) { | 
|---|
|  |  |  | locaType = SteLocaType.A_WAITING; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | if (status2[0]){//B点 | 
|---|
|  |  |  | if (locaType.equals(SteLocaType.NONE)) { | 
|---|
|  |  |  | locaType = SteLocaType.B; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (status2[2]){//B待机点 | 
|---|
|  |  |  | if (locaType.equals(SteLocaType.NONE)) { | 
|---|
|  |  |  | locaType = SteLocaType.B_WAITING; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | boolean[] status2 = siemensS7Net.getByteTransform().TransBool(result.Content, 27, 1); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | steProtocol.setLoca(locaType.id.shortValue()); | 
|---|
|  |  |  | steProtocol.setReady(status2[3]?(short)1:(short)0); | 
|---|
|  |  |  | steProtocol.setLoad(status2[4]?(short)1:(short)0); | 
|---|
|  |  |  | steProtocol.setChargeStatus(status2[0]?(short)1:(short)0); | 
|---|
|  |  |  | steProtocol.setTrack(status2[1]?(short)1:(short)0); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                steProtocol.setLoad(status2[6]?(short)1:(short)0); | 
|---|
|  |  |  | steProtocol.setLoad(status2[7]?(short)1:(short)0); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | OutputQueue.STE.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 复位信号 | 
|---|
|  |  |  | if (steProtocol.getWaiting()) { | 
|---|
|  |  |  | if (resetFlag) { | 
|---|
|  |  |  | SteCommand steCommand = new SteCommand(); | 
|---|
|  |  |  | steCommand.setComplete(true); | 
|---|
|  |  |  | if (write(steCommand) && confirmPos()) { | 
|---|
|  |  |  | resetFlag = false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //                // 复位信号 | 
|---|
|  |  |  | //                if (steProtocol.getWaiting()) { | 
|---|
|  |  |  | //                    if (resetFlag) { | 
|---|
|  |  |  | //                        SteCommand steCommand = new SteCommand(); | 
|---|
|  |  |  | //                        steCommand.setComplete(true); | 
|---|
|  |  |  | //                        if (write(steCommand) && confirmPos()) { | 
|---|
|  |  |  | //                            resetFlag = false; | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 根据实时信息更新数据库 | 
|---|
|  |  |  | BasSteService service = SpringUtils.getBean(BasSteService.class); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | command.setSteNo(slave.getId()); | 
|---|
|  |  |  | OperateResult result = null; | 
|---|
|  |  |  | News.error("穿梭车写入命令"+JSON.toJSONString(command)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 开始任务 | 
|---|
|  |  |  | if (!command.getComplete()) { | 
|---|
|  |  |  | //组织任务前,先清空写任务确认位,以及任务完成确认位 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.0", (short) 0);//设备号 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.2", (short) 0);//任务号 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.12.0", false);//任务开始确认位 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.12.1", false);//任务完成确认 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.12", (short) 0);//起始点位 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.14", (short) 0);//目的点位 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.16.0", false);//任务开始确认位 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.16.1", false);//任务完成确认位 | 
|---|
|  |  |  | //            siemensS7Net.Write("DB100.18", (short) 0);//硫化罐号 | 
|---|
|  |  |  | // 1.任务号 | 
|---|
|  |  |  | OperateResult result0 = siemensS7Net.Write("DB100.2", command.getTaskNo().shortValue()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (command.getTaskMode() != 0) { | 
|---|
|  |  |  | // 2.作业 | 
|---|
|  |  |  | OperateResult result1 = siemensS7Net.Write("DB100.4", command.getTaskMode()); | 
|---|
|  |  |  | OperateResult result2 = siemensS7Net.Write("DB100.12", (short) command.getStartLoad().shortValue());//起始点位 | 
|---|
|  |  |  | OperateResult result3 = siemensS7Net.Write("DB100.14", (short) command.getEndLoad().shortValue());//目的点位 | 
|---|
|  |  |  | OperateResult result4 = siemensS7Net.Write("DB100.18", (short) command.getJarNo());//目的点位 | 
|---|
|  |  |  | // 3.确认开始任务 | 
|---|
|  |  |  | if (result0.IsSuccess && result1.IsSuccess) { | 
|---|
|  |  |  | result = siemensS7Net.Write("DB100.12.0", true); | 
|---|
|  |  |  | if (result0.IsSuccess && result1.IsSuccess && result2.IsSuccess && result3.IsSuccess && result4.IsSuccess) { | 
|---|
|  |  |  | result = siemensS7Net.Write("DB100.16.0", true); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(300); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 任务完成 | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | siemensS7Net.Write("DB100.2", (short) 0);//任务号 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.4.0", (short) 0);//任务类型 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.12.0", false);//任务开始确认位 | 
|---|
|  |  |  | //            siemensS7Net.Write("DB100.2", (short) 0);//任务号 | 
|---|
|  |  |  | //            siemensS7Net.Write("DB100.4.0", (short) 0);//任务类型 | 
|---|
|  |  |  | siemensS7Net.Write("DB100.16.0", false);//任务开始确认位 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | result = siemensS7Net.Write("DB100.12.1", true);//任务完成确认 | 
|---|
|  |  |  | result = siemensS7Net.Write("DB100.16.1", true);//任务完成确认 | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | 
|---|
|  |  |  | } catch (Exception ignore) {} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (result != null && result.IsSuccess) { | 
|---|
|  |  |  | // 维护数据库排列层 | 
|---|
|  |  |  | if (!steProtocol.getWaiting()) { | 
|---|
|  |  |  | if (!Cools.isEmpty(command.getRow(), command.getBay(), command.getLev())) { | 
|---|
|  |  |  | this.modifyPos(command.getRow().intValue(), command.getBay().intValue(), command.getLev().intValue()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | News.info("穿梭车命令下发[id:{}] >>>>> {}", slave.getId(), JSON.toJSON(command)); | 
|---|
|  |  |  | OutputQueue.STE.offer(MessageFormat.format("【{0}】[id:{1}] >>>>> 命令下发: {2}", DateUtils.convert(new Date()), slave.getId(), JSON.toJSON(command))); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | 
|---|
|  |  |  | OutputQueue.STE.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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void modifyPos(Integer row, Integer bay, Integer lev) { | 
|---|
|  |  |  | BasSteService service = SpringUtils.getBean(BasSteService.class); | 
|---|
|  |  |  | if (!service.updatePos(this.slave.getId(), row, bay, lev)) { | 
|---|
|  |  |  | News.error("更新{}号穿梭车定位失败 ===>> 排:【{}】, 列:【{}】,层:【{}】", this.slave.getId(), row, bay, lev); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | /******************************************************************************************/ | 
|---|
|  |  |  | /**************************************** 测试专用 *****************************************/ | 
|---|
|  |  |  | /*****************************************************************************************/ | 
|---|
|  |  |  | public static void main(String[] args) throws InterruptedException { | 
|---|
|  |  |  | SteSlave slave = new SteSlave(); | 
|---|
|  |  |  | slave.setId(1); | 
|---|
|  |  |  | slave.setIp("192.168.2.1"); | 
|---|
|  |  |  | slave.setPort(502); | 
|---|
|  |  |  | SteThread thread = new SteThread(slave); | 
|---|
|  |  |  | thread.connect(); | 
|---|
|  |  |  | thread.readStatus(); | 
|---|
|  |  |  | System.out.println(JSON.toJSONString(thread.steProtocol)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 穿梭车运行禁止 | 
|---|
|  |  |  | SteCommand command = new SteCommand(); | 
|---|
|  |  |  | command.setRun((short)0); | 
|---|
|  |  |  | thread.write(command); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //    public static void main(String[] args) throws InterruptedException { | 
|---|
|  |  |  | //        SteSlave slave = new SteSlave(); | 
|---|
|  |  |  | //        slave.setId(1); | 
|---|
|  |  |  | //        slave.setIp("192.168.2.1"); | 
|---|
|  |  |  | //        slave.setPort(502); | 
|---|
|  |  |  | //        SteThread thread = new SteThread(slave); | 
|---|
|  |  |  | //        thread.connect(); | 
|---|
|  |  |  | //        thread.readStatus(); | 
|---|
|  |  |  | //        System.out.println(JSON.toJSONString(thread.steProtocol)); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        // 穿梭车运行禁止 | 
|---|
|  |  |  | //        SteCommand command = new SteCommand(); | 
|---|
|  |  |  | ////        command.setRun((short)0); | 
|---|
|  |  |  | //        thread.write(command); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|