| New file |
| | |
| | | package com.zy.core.thread.impl; |
| | | |
| | | import HslCommunication.Core.Types.OperateResult; |
| | | import HslCommunication.Core.Types.OperateResultExOne; |
| | | import HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.zy.common.SpringUtils; |
| | | import com.zy.common.utils.DateUtils; |
| | | import com.zy.common.utils.RedisUtil; |
| | | import com.zy.core.News; |
| | | import com.zy.core.cache.OutputQueue; |
| | | import com.zy.core.enums.*; |
| | | import com.zy.core.model.DeviceCommandMsgModel; |
| | | import com.zy.core.model.DeviceMsgModel; |
| | | import com.zy.core.properties.DeviceConfig; |
| | | import com.zy.core.thread.ForkLiftThread; |
| | | import com.zy.core.utils.DeviceMsgUtils; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.text.MessageFormat; |
| | | import java.util.*; |
| | | |
| | | @Slf4j |
| | | @SuppressWarnings("all") |
| | | public class ZyForkLiftThread implements ForkLiftThread { |
| | | |
| | | private DeviceConfig deviceConfig; |
| | | private RedisUtil redisUtil; |
| | | private SiemensS7Net siemensS7Net; |
| | | private boolean connect = false; |
| | | |
| | | public ZyForkLiftThread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | | this.redisUtil = redisUtil; |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | if (deviceConfig.getFake()) { |
| | | this.connect = true; |
| | | OutputQueue.FORKLIFT.offer(MessageFormat.format( "【{0}】货叉提升机连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0)); |
| | | News.info("【{}】货叉提升机连接成功 ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()); |
| | | return true; |
| | | } |
| | | |
| | | boolean result = false; |
| | | siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, deviceConfig.getIp()); |
| | | siemensS7Net.setRack(Integer.valueOf(0).byteValue()); |
| | | siemensS7Net.setSlot(Integer.valueOf(0).byteValue()); |
| | | OperateResult connect = siemensS7Net.ConnectServer(); |
| | | if(connect.IsSuccess){ |
| | | result = true; |
| | | OutputQueue.FORKLIFT.offer(MessageFormat.format( "【{0}】货叉提升机连接成功 ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0)); |
| | | News.info("【{}】货叉提升机连接成功 ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()); |
| | | } else { |
| | | OutputQueue.FORKLIFT.offer(MessageFormat.format( "【{0}】货叉提升机连接失败!!! ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort(), 0, 0)); |
| | | News.error("【{}】货叉提升机连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", DateUtils.convert(new Date()), deviceConfig.getDeviceNo(), deviceConfig.getIp(), deviceConfig.getPort()); |
| | | } |
| | | // siemensS7Net.ConnectClose(); |
| | | this.connect = result; |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public void close() { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | News.info("{}号货叉提升机线程启动", deviceConfig.getDeviceNo()); |
| | | this.connect(); |
| | | while (true) { |
| | | try { |
| | | DeviceMsgUtils deviceMsgUtils = null; |
| | | try { |
| | | deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class); |
| | | }catch (Exception e){} |
| | | if (deviceMsgUtils == null) { |
| | | continue; |
| | | } |
| | | DeviceCommandMsgModel deviceCommandMsg = deviceMsgUtils.getDeviceCommandMsg(SlaveType.ForkLift, deviceConfig.getDeviceNo()); |
| | | if (deviceCommandMsg == null) { |
| | | continue; |
| | | } |
| | | executeCommand(deviceCommandMsg, deviceMsgUtils); |
| | | |
| | | Thread.sleep(200); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void executeCommand(DeviceCommandMsgModel deviceCommandMsg, DeviceMsgUtils deviceMsgUtils) { |
| | | if (deviceConfig.getFake()) { |
| | | redisUtil.set(RedisKeyType.FAKE_DEVICE_FORK_LIFT_COMMAND_MSG_KEY.key + deviceConfig.getDeviceNo(), deviceCommandMsg, 60 * 60); |
| | | return; |
| | | } |
| | | |
| | | String command = JSON.toJSONString(deviceCommandMsg.getCommand()); |
| | | JSONObject commandObj = JSON.parseObject(command); |
| | | |
| | | HashMap<String, Object> resultData = new HashMap<>(); |
| | | resultData.put("result", "false"); |
| | | |
| | | String deviceMsgType = "command"; |
| | | OperateResult result = null; |
| | | OperateResult result2 = null; |
| | | if (commandObj.getInteger("mode") == 3) { |
| | | //取放货 |
| | | short[] array = new short[4]; |
| | | array[0] = commandObj.getShort("taskNO");//任务号 |
| | | array[1] = commandObj.getShort("mode");//任务模式 |
| | | array[2] = commandObj.getShort("pick");//取货数据 |
| | | array[3] = commandObj.getShort("put");//放货数据 |
| | | resultData.put("commandData", array); |
| | | |
| | | result = siemensS7Net.Write("DB103.0", array); |
| | | if (result.IsSuccess) { |
| | | result2 = siemensS7Net.Write("DB103.8", (short) 1); |
| | | if (result2.IsSuccess) { |
| | | resultData.put("result", "success"); |
| | | } |
| | | } |
| | | } else if (commandObj.getInteger("mode") == 4) { |
| | | //小车换层 |
| | | short[] array = new short[4]; |
| | | array[0] = commandObj.getShort("taskNO");//任务号 |
| | | array[1] = commandObj.getShort("mode");//任务模式 |
| | | array[2] = commandObj.getShort("pick");//取货数据 |
| | | array[3] = commandObj.getShort("put");//放货数据 |
| | | resultData.put("commandData", array); |
| | | |
| | | result = siemensS7Net.Write("DB103.0", array); |
| | | if (result.IsSuccess) { |
| | | result2 = siemensS7Net.Write("DB103.8", (short) 1); |
| | | if (result2.IsSuccess) { |
| | | resultData.put("result", "success"); |
| | | } |
| | | } |
| | | } else if (commandObj.getInteger("mode") == 5) { |
| | | //提升机移动 |
| | | short[] array = new short[4]; |
| | | array[0] = commandObj.getShort("taskNO");//任务号 |
| | | array[1] = commandObj.getShort("mode");//任务模式 |
| | | array[2] = commandObj.getShort("pick");//取货数据 |
| | | array[3] = commandObj.getShort("put");//放货数据 |
| | | resultData.put("commandData", array); |
| | | |
| | | result = siemensS7Net.Write("DB103.0", array); |
| | | if (result.IsSuccess) { |
| | | result2 = siemensS7Net.Write("DB103.8", (short) 1); |
| | | if (result2.IsSuccess) { |
| | | resultData.put("result", "success"); |
| | | } |
| | | } |
| | | } else if (commandObj.getInteger("mode") == 9996) { |
| | | //复位 |
| | | short[] array = new short[1]; |
| | | array[0] = 1; |
| | | resultData.put("commandData", array); |
| | | |
| | | result = siemensS7Net.Write("DB103.10", array); |
| | | if (result.IsSuccess) { |
| | | resultData.put("result", "success"); |
| | | } |
| | | } else if (commandObj.getInteger("mode") == 9997) { |
| | | //切换入库模式 |
| | | short[] array = new short[1]; |
| | | array[0] = 1; |
| | | resultData.put("commandData", array); |
| | | |
| | | result = siemensS7Net.Write("DB103.12", array); |
| | | if (result.IsSuccess) { |
| | | resultData.put("result", "success"); |
| | | } |
| | | } else if (commandObj.getInteger("mode") == 9998) { |
| | | //切换出库模式 |
| | | short[] array = new short[1]; |
| | | array[0] = 2; |
| | | resultData.put("commandData", array); |
| | | |
| | | result = siemensS7Net.Write("DB103.12", array); |
| | | if (result.IsSuccess) { |
| | | resultData.put("result", "success"); |
| | | } |
| | | } else if (commandObj.getInteger("mode") == 9999) { |
| | | //读取状态 |
| | | JSONObject device = new JSONObject(); |
| | | |
| | | OperateResultExOne<byte[]> readResult1 = siemensS7Net.Read("DB101.0", (short) 18); |
| | | if (readResult1.IsSuccess) { |
| | | //模式 |
| | | device.put("model", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 0)); |
| | | //PLC任务号 |
| | | device.put("wrkNo", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 2)); |
| | | //任务状态 |
| | | device.put("protocolStatus", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 4)); |
| | | //任务模式 |
| | | device.put("taskMode", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 6)); |
| | | //取货数据 |
| | | device.put("pick", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 8)); |
| | | //放货数据 |
| | | device.put("put", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 10)); |
| | | //出入库模式 |
| | | device.put("iOMode", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 12)); |
| | | //故障码 |
| | | device.put("errorCode", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 14)); |
| | | //层 |
| | | device.put("lev", (int) siemensS7Net.getByteTransform().TransInt16(readResult1.Content, 16)); |
| | | |
| | | resultData.put("deviceStatus", device); |
| | | } |
| | | |
| | | //站点个数 |
| | | int staCount = 4; |
| | | //读取托盘数据 |
| | | List<Integer> trayList = new ArrayList<>(); |
| | | OperateResultExOne<byte[]> readResult2 = siemensS7Net.Read("DB102.0", (short) (staCount * 2)); |
| | | if(readResult2.IsSuccess) { |
| | | for (int i = 0; i < staCount; i++) { |
| | | short val = siemensS7Net.getByteTransform().TransInt16(readResult2.Content, i * 2); |
| | | trayList.add((int) val); |
| | | } |
| | | } |
| | | |
| | | //读取小车数据 |
| | | OperateResultExOne<byte[]> readResult3 = siemensS7Net.Read("DB102.50", (short) (staCount * 2)); |
| | | List<Integer> carList = new ArrayList<>(); |
| | | if(readResult3.IsSuccess) { |
| | | for (int i = 0; i < staCount; i++) { |
| | | short val = siemensS7Net.getByteTransform().TransInt16(readResult3.Content, i * 2); |
| | | carList.add((int) val); |
| | | } |
| | | } |
| | | |
| | | device.put("trayList", trayList); |
| | | device.put("carList", carList); |
| | | |
| | | deviceMsgType = "status"; |
| | | } |
| | | |
| | | resultData.put("commandResult1", JSON.toJSONString(result)); |
| | | resultData.put("commandResult2", JSON.toJSONString(result2)); |
| | | |
| | | if (deviceMsgType.equals("command")) { |
| | | log.info("收到Rcs ForkLift Command Data: {}", JSON.toJSONString(deviceCommandMsg)); |
| | | } |
| | | DeviceMsgModel deviceMsgModel = new DeviceMsgModel(); |
| | | deviceMsgModel.setDeviceId(deviceConfig.getDeviceNo()); |
| | | deviceMsgModel.setDeviceMsgType(deviceMsgType); |
| | | deviceMsgModel.setDeviceMsg(JSON.toJSONString(resultData)); |
| | | deviceMsgModel.setDeviceOriginMsg(JSON.toJSONString(resultData)); |
| | | deviceMsgModel.setResultKey(deviceCommandMsg.getResultKey()); |
| | | deviceMsgUtils.sendDeviceMsg(SlaveType.ForkLift, deviceConfig.getDeviceNo(), deviceMsgModel); |
| | | } |
| | | |
| | | @Override |
| | | public DeviceConfig getDeviceConfig() { |
| | | return this.deviceConfig; |
| | | } |
| | | |
| | | /** |
| | | * 扩展字段 |
| | | */ |
| | | @Data |
| | | private class InnerForkLiftExtend { |
| | | |
| | | } |
| | | } |