New file |
| | |
| | | package com.zy.core.thread; |
| | | |
| | | import HslCommunication.Core.Types.OperateResult; |
| | | import HslCommunication.Core.Types.OperateResultExOne; |
| | | import HslCommunication.Profinet.Melsec.MelsecMcNet; |
| | | import com.core.common.DateUtils; |
| | | import com.core.common.SpringUtils; |
| | | import com.core.exception.CoolException; |
| | | import com.zy.asrs.entity.BasCrnp; |
| | | import com.zy.asrs.entity.BasExt; |
| | | import com.zy.asrs.service.BasCrnpService; |
| | | import com.zy.asrs.service.BasExtService; |
| | | import com.zy.core.ThreadHandler; |
| | | import com.zy.core.cache.MessageQueue; |
| | | import com.zy.core.cache.OutputQueue; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.model.ExtSlave; |
| | | import com.zy.core.model.Task; |
| | | import com.zy.core.model.protocol.ExtProtocol; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * 机械臂线程 //Extraman==>机械臂 |
| | | * Created by vincent on 2020/8/4 |
| | | */ |
| | | @Data |
| | | @Slf4j |
| | | public class MelsecExtThread implements Runnable, ThreadHandler { |
| | | |
| | | private MelsecMcNet melsecMcNet; |
| | | private ExtSlave slave; |
| | | private ExtProtocol extProtocol; |
| | | private short heartBeatVal = 1; |
| | | private boolean resetFlag = false; |
| | | |
| | | public boolean isRunning = true; |
| | | |
| | | public MelsecExtThread(ExtSlave slave) { |
| | | this.slave = slave; |
| | | } |
| | | |
| | | @Override |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | | public void run() { |
| | | System.out.println("线程启动"); |
| | | System.out.println("extProtocol:"+extProtocol); |
| | | this.connect(); |
| | | // try { |
| | | // Thread.sleep(2000); |
| | | // } catch (InterruptedException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | while (isRunning) { |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Ext, slave.getId()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | | switch (step) { |
| | | // 读数据 |
| | | case 1: |
| | | readStatus(); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | // 心跳 |
| | | // heartbeat(); |
| | | Thread.sleep(500); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 初始化机械臂状态 |
| | | */ |
| | | private void initExt() { |
| | | if (null == extProtocol) { |
| | | extProtocol = new ExtProtocol(); |
| | | } |
| | | extProtocol.setTake(false); |
| | | extProtocol.setPut(false); |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | boolean result = false; |
| | | melsecMcNet = new MelsecMcNet(slave.getIp(), slave.getPort()); |
| | | OperateResult connect = melsecMcNet.ConnectServer(); |
| | | if(connect.IsSuccess){ |
| | | result = true; |
| | | OutputQueue.Ext.offer(MessageFormat.format( "【{0}】机械臂plc连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | log.info("MelsecExt"+" - 1"+" - 机械臂plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } else { |
| | | OutputQueue.Ext.offer(MessageFormat.format("【{0}】机械臂plc连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] ", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | log.error("MelsecExt"+" - 2"+" - 机械臂plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | initExt(); |
| | | } |
| | | // melsecMcNet.ConnectClose(); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 读取状态 |
| | | */ |
| | | private void readStatus(){ |
| | | try { |
| | | OperateResultExOne<byte[]> result = melsecMcNet.Read("D1035", (short) 56); |
| | | if (result.IsSuccess) { |
| | | if (null == extProtocol) { |
| | | extProtocol = new ExtProtocol(); |
| | | extProtocol.setExtNo(slave.getId()); |
| | | } |
| | | extProtocol.setTake(melsecMcNet.getByteTransform().TransBool(result.Content, 0)); |
| | | extProtocol.setPut(melsecMcNet.getByteTransform().TransBool(result.Content, 0)); |
| | | // extProtocol.setMode(melsecMcNet.getByteTransform().TransInt16(result.Content, 0)); |
| | | // extProtocol.setTaskNo(melsecMcNet.getByteTransform().TransInt16(result.Content, 2)); |
| | | |
| | | |
| | | // 根据实时信息更新数据库 |
| | | BasExtService extService = SpringUtils.getBean(BasExtService.class); |
| | | BasExt basExt = new BasExt(); |
| | | basExt.setExtNo(slave.getId()); |
| | | basExt.setExtTask(extProtocol.isTake()?"Y":"N"); |
| | | basExt.setExtPut(extProtocol.isPut()?"Y":"N"); |
| | | if (!extService.updateById(basExt)){ |
| | | log.error("MelsecExt"+" - 4"+" - 机械臂plc数据库更新失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | }else { |
| | | OutputQueue.Ext.offer(MessageFormat.format("【{0}】[id:{1}] <<<<< 实时数据更新成功",DateUtils.convert(new Date()), slave.getId())); |
| | | } |
| | | } else { |
| | | BasExtService extService = SpringUtils.getBean(BasExtService.class); |
| | | BasExt basExt = new BasExt(); |
| | | basExt.setExtNo(slave.getId()); |
| | | basExt.setExtTask("N"); |
| | | basExt.setExtPut("N"); |
| | | extService.updateById(basExt); |
| | | OutputQueue.Ext.offer(MessageFormat.format("【{0}】{1}机械臂plc状态信息失败",DateUtils.convert(new Date()), slave.getId())); |
| | | throw new CoolException(MessageFormat.format( "机械臂plc状态信息失败 ===>> [id:{0}] [ip:{1}] [port:{2}]", slave.getId(), slave.getIp(), slave.getPort())); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | OutputQueue.Ext.offer(MessageFormat.format("【{0}】读取机械臂plc状态信息失败 ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort())); |
| | | log.error("MelsecExt"+" - 5"+" - 读取机械臂plc状态信息失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | initExt(); |
| | | } finally { |
| | | // sign = System.currentTimeMillis(); |
| | | } |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | melsecMcNet.ConnectClose(); |
| | | } |
| | | |
| | | /** |
| | | * 心跳 |
| | | */ |
| | | private void heartbeat(){ |
| | | if (heartBeatVal >= 30000) { |
| | | heartBeatVal = -30000; |
| | | } else { |
| | | heartBeatVal =(short) (heartBeatVal+1); |
| | | } |
| | | OperateResult write = melsecMcNet.Write("D1011", heartBeatVal); |
| | | if (!write.IsSuccess) { |
| | | log.error("MelsecExt"+" - 9"+" - 机械臂plc心跳通讯失败 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } |
| | | } |
| | | |
| | | |
| | | /******************************************************************************************/ |
| | | /**************************************** 测试专用 *****************************************/ |
| | | /*****************************************************************************************/ |
| | | public static void main(String[] args) throws InterruptedException { |
| | | |
| | | } |
| | | |
| | | // 提供一个方法来停止线程 |
| | | public void requestStop() { |
| | | isRunning = false; |
| | | } |
| | | |
| | | // 提供一个方法来重启线程 |
| | | public Thread restartThread() { |
| | | isRunning = true; |
| | | Thread newThread = new Thread(this); |
| | | newThread.start(); |
| | | return newThread; |
| | | } |
| | | |
| | | } |