From f653130e5df936041f7a5ae005e10c91415b64b1 Mon Sep 17 00:00:00 2001 From: Junjie <fallin.jie@qq.com> Date: 星期四, 10 七月 2025 15:54:33 +0800 Subject: [PATCH] # --- src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java | 650 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 650 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java b/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java new file mode 100644 index 0000000..939c7cb --- /dev/null +++ b/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java @@ -0,0 +1,650 @@ +package com.zy.core.thread.impl; + +import HslCommunication.Core.Types.OperateResult; +import HslCommunication.Core.Types.OperateResultExOne; +import HslCommunication.Profinet.Siemens.SiemensS7Net; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.mapper.EntityWrapper; +import com.core.common.DateUtils; +import com.core.common.SpringUtils; +import com.zy.asrs.entity.BasLift; +import com.zy.asrs.entity.DeviceConfig; +import com.zy.asrs.entity.DeviceDataLog; +import com.zy.asrs.service.BasLiftService; +import com.zy.asrs.service.DeviceDataLogService; +import com.zy.asrs.utils.Utils; +import com.zy.common.ExecuteSupport; +import com.zy.common.utils.RedisUtil; +import com.zy.core.News; +import com.zy.core.action.ForkLiftAction; +import com.zy.core.cache.OutputQueue; +import com.zy.core.enums.*; +import com.zy.core.model.*; +import com.zy.core.model.command.ForkLiftCommand; +import com.zy.core.model.protocol.ForkLiftProtocol; +import com.zy.core.model.protocol.ForkLiftStaProtocol; +import com.zy.core.thread.ForkLiftThread; +import com.zy.core.utils.DeviceMsgUtils; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.*; + +@Slf4j +@SuppressWarnings("all") +public class ZyForkLiftThread implements ForkLiftThread { + + private DeviceConfig device; + private ForkLiftProtocol forkLiftProtocol; + private RedisUtil redisUtil; + private List<ForkLiftStaProtocol> forkLiftStaProtocols = new ArrayList<>(); + private List<DeviceMsgModel> readResultList = new ArrayList<>(); + private List<DeviceMsgModel> resultList = new ArrayList<>(); + + public ZyForkLiftThread(DeviceConfig device, List<LiftStation> stationList, RedisUtil redisUtil) { + this.device = device; + this.redisUtil = redisUtil; + //鍒濆鍖栫珯鐐� + for (LiftStation station : stationList) { + ForkLiftStaProtocol forkLiftStaProtocol = new ForkLiftStaProtocol(); + forkLiftStaProtocol.setStaNo(station.getStaNo());//绔欑偣鍙� + forkLiftStaProtocol.setLev(station.getLev());//绔欑偣妤煎眰 + String locNo = Utils.getLocNo(station.getRow(), station.getBay(), station.getLev()); + forkLiftStaProtocol.setLocNo(locNo);//绔欑偣搴撲綅鍙� + forkLiftStaProtocol.setLiftNo(station.getLiftNo());//鎻愬崌鏈哄彿 + forkLiftStaProtocols.add(forkLiftStaProtocol); + } + } + + @Override + public boolean connect() { + return true; + } + + @Override + public void close() { + + } + + @Override + public void run() { + News.info("{}鍙疯揣鍙夋彁鍗囨満绾跨▼鍚姩", device.getDeviceNo()); + + this.connect(); + //璁惧璇诲彇 + Thread readThread = new Thread(() -> { + while (true) { + try { + listenMessageFromRedis(); + read(); + Thread.sleep(100); + } catch (Exception e) { + log.error("ForkliftThread Fail", e); + } + } + }); + readThread.start(); + + while (true) { + try { + execute(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private void execute() { + ForkLiftAction forkLiftAction = null; + try { + forkLiftAction = SpringUtils.getBean(ForkLiftAction.class); + }catch (Exception e){} + if (forkLiftAction == null) { + return; + } + + Object object = redisUtil.get(RedisKeyType.FORK_LIFT_FLAG.key + device.getDeviceNo()); + if (object == null) { + return; + } + + Integer taskNo = Integer.valueOf(String.valueOf(object)); + if (taskNo != 0) { + //瀛樺湪浠诲姟闇�瑕佹墽琛� + boolean result = forkLiftAction.executeWork(device.getDeviceNo(), taskNo); + } + } + + private void read() { + try { + readStatus(); + + //鎻愬崌鏈哄浜庤繍琛岀姸鎬侊紝灏嗘爣璁扮疆涓簍rue + if (forkLiftProtocol.getProtocolStatusType().equals(ForkLiftProtocolStatusType.PICK_UP) + || forkLiftProtocol.getProtocolStatusType().equals(ForkLiftProtocolStatusType.PUT_DOWN) + || forkLiftProtocol.getProtocolStatusType().equals(ForkLiftProtocolStatusType.WAITING)) { + forkLiftProtocol.setPakMk(true); + } + } catch (Exception e) { + OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆戣鍙栨彁鍗囨満鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getDeviceNo(), device.getIp(), device.getPort())); + } + } + + private void readStatus() { + try { + //鑾峰彇鎻愬崌鏈烘暟鎹� + + DeviceMsgUtils deviceMsgUtils = null; + try { + deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class); + }catch (Exception e){ + + } + if(deviceMsgUtils == null){ + return; + } + + ForkLiftCommand readStatusCommand = getReadStatusCommand(); + //鎸囦护瓒呰繃2鏉★紝涓嶅啀涓嬪彂浠诲姟鐘舵�佽姹� + TreeSet<String> deviceCommandMsgListKey = deviceMsgUtils.getDeviceCommandMsgListKey(SlaveType.ForkLift, device.getDeviceNo()); + if (deviceCommandMsgListKey.size() < 2) { + requestCommand(readStatusCommand);//璇锋眰鐘舵�� + } + + if (this.readResultList.isEmpty()) { + return; + } + + DeviceMsgModel deviceMsgModel = this.readResultList.get(0); + this.readResultList.remove(0); + JSONObject deviceMsg = JSON.parseObject(JSON.toJSONString(deviceMsgModel.getDeviceMsg())); + if (!deviceMsg.getString("result").equals("success")) { + return; + } + JSONObject data = deviceMsg.getJSONObject("deviceStatus"); + + if (null == forkLiftProtocol) { + forkLiftProtocol = new ForkLiftProtocol(); + forkLiftProtocol.setLiftNo(device.getDeviceNo()); + forkLiftProtocol.setProtocolStatus(ForkLiftProtocolStatusType.NONE); + + InnerForkLiftExtend innerForkLiftExtend = new InnerForkLiftExtend(); + } + + //----------璇诲彇鎻愬崌鏈虹姸鎬�----------- + //妯″紡 + forkLiftProtocol.setModel(data.getInteger("model")); + //PLC浠诲姟鍙� + forkLiftProtocol.setWrkNo(data.getInteger("wrkNo")); + //浠诲姟鐘舵�� + forkLiftProtocol.setProtocolStatus(data.getInteger("protocolStatus")); + //浠诲姟妯″紡 + forkLiftProtocol.setTaskMode(data.getInteger("taskMode")); + //鍙栬揣鏁版嵁 + forkLiftProtocol.setPick(data.getInteger("pick")); + //鏀捐揣鏁版嵁 + forkLiftProtocol.setPut(data.getInteger("put")); + //鍑哄叆搴撴ā寮� + forkLiftProtocol.setIOMode(data.getInteger("iOMode")); + //鏁呴殰鐮� + forkLiftProtocol.setErrorCode(data.getInteger("errorCode")); + //褰撳墠灞� + forkLiftProtocol.setLev(data.getInteger("lev")); + + //************琛ュ厖鎵╁睍瀛楁************* + InnerForkLiftExtend forkLiftExtend = (InnerForkLiftExtend) forkLiftProtocol.getExtend(); + forkLiftProtocol.setExtend(forkLiftExtend); + + JSONArray trayList = data.getJSONArray("trayList"); + for (int i = 0; i < trayList.size(); i++) { + int hasTray = (int) trayList.get(i); + ForkLiftStaProtocol forkLiftStaProtocol = forkLiftStaProtocols.get(i); + forkLiftStaProtocol.setHasTray(hasTray == 1); + } + + JSONArray carList = data.getJSONArray("carList"); + for (int i = 0; i < carList.size(); i++) { + int hasCar = (int) carList.get(i); + ForkLiftStaProtocol forkLiftStaProtocol = forkLiftStaProtocols.get(i); + forkLiftStaProtocol.setHasCar(hasCar == 1); + } + + if (System.currentTimeMillis() - forkLiftProtocol.getDeviceDataLog() > 1000 * 5) { + //閲囬泦鏃堕棿瓒呰繃5s锛屼繚瀛樹竴娆℃暟鎹褰� + //淇濆瓨鏁版嵁璁板綍 + DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class); + DeviceDataLog deviceDataLog = new DeviceDataLog(); + deviceDataLog.setOriginData(JSON.toJSONString(data)); + deviceDataLog.setWcsData(JSON.toJSONString(forkLiftProtocol)); + deviceDataLog.setType("forkLift"); + deviceDataLog.setDeviceNo(forkLiftProtocol.getLiftNo()); + deviceDataLog.setCreateTime(new Date()); + deviceDataLogService.insert(deviceDataLog); + + //鏇存柊閲囬泦鏃堕棿 + forkLiftProtocol.setDeviceDataLog(System.currentTimeMillis()); + } + + //灏嗘彁鍗囨満鐘舵�佷繚瀛樿嚦鏁版嵁搴� + BasLiftService basLiftService = SpringUtils.getBean(BasLiftService.class); + BasLift basLift = basLiftService.selectOne(new EntityWrapper<BasLift>() + .eq("lift_no", device.getDeviceNo())); + if (basLift == null) { + basLift = new BasLift(); + //鎻愬崌鏈哄彿 + basLift.setLiftNo(forkLiftProtocol.getLiftNo()); + basLift.setStatus(1); + basLiftService.insert(basLift); + } + //浠诲姟鍙� + basLift.setWrkNo(forkLiftProtocol.getTaskNo()); + //淇敼鏃堕棿 + basLift.setUpdateTime(new Date()); + //璁惧鐘舵�� + basLift.setDeviceStatus(JSON.toJSONString(forkLiftProtocol)); + if (basLiftService.updateById(basLift)) { + OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆慬id:{1}] <<<<< 瀹炴椂鏁版嵁鏇存柊鎴愬姛",DateUtils.convert(new Date()), forkLiftProtocol.getLiftNo())); + } + + } catch (Exception e) { + e.printStackTrace(); + OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆戣鍙栬揣鍙夋彁鍗囨満鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getDeviceNo(), device.getIp(), device.getPort())); + } + } + + @Override + public ForkLiftProtocol getStatus(boolean clone) { + if (this.forkLiftProtocol == null) { + return null; + } + return clone ? this.forkLiftProtocol.clone() : this.forkLiftProtocol; + } + + @Override + public List<ForkLiftStaProtocol> getForkLiftStaProtocols() { + return this.forkLiftStaProtocols; + } + + @Override + public ForkLiftProtocol getStatus() { + return getStatus(true); + } + + @Override + public CommandResponse pickAndPut(ForkLiftCommand command) { + CommandResponse response = new CommandResponse(false); + try { + //鍙戝嚭璇锋眰 + String resultKey = requestCommand(command); + //鏌ヨ璇锋眰缁撴灉 + JSONObject result = queryCommandStatus(resultKey); + if (result == null) { + return response;//璇锋眰澶辫触 + } + if(!result.getString("result").equals("success")) { + return response;//璇锋眰澶辫触 + } + + this.forkLiftProtocol.setSendTime(System.currentTimeMillis());//鎸囦护涓嬪彂鏃堕棿 + response.setMessage(JSON.toJSONString(result)); + response.setResult(true); + return response; + } catch (Exception e) { + e.printStackTrace(); + response.setMessage(e.getMessage()); + return response; + } + } + + @Override + public CommandResponse shuttleSwitch(ForkLiftCommand command) { + CommandResponse response = new CommandResponse(false); + try { + //鍙戝嚭璇锋眰 + String resultKey = requestCommand(command); + //鏌ヨ璇锋眰缁撴灉 + JSONObject result = queryCommandStatus(resultKey); + if (result == null) { + return response;//璇锋眰澶辫触 + } + if(!result.getString("result").equals("success")) { + return response;//璇锋眰澶辫触 + } + + this.forkLiftProtocol.setSendTime(System.currentTimeMillis());//鎸囦护涓嬪彂鏃堕棿 + response.setMessage(JSON.toJSONString(result)); + response.setResult(true); + return response; + } catch (Exception e) { + e.printStackTrace(); + response.setMessage(e.getMessage()); + return response; + } + } + + @Override + public CommandResponse move(ForkLiftCommand command) { + CommandResponse response = new CommandResponse(false); + try { + //鍙戝嚭璇锋眰 + String resultKey = requestCommand(command); + //鏌ヨ璇锋眰缁撴灉 + JSONObject result = queryCommandStatus(resultKey); + if (result == null) { + return response;//璇锋眰澶辫触 + } + if(!result.getString("result").equals("success")) { + return response;//璇锋眰澶辫触 + } + + this.forkLiftProtocol.setSendTime(System.currentTimeMillis());//鎸囦护涓嬪彂鏃堕棿 + response.setMessage(JSON.toJSONString(result)); + response.setResult(true); + return response; + } catch (Exception e) { + e.printStackTrace(); + response.setMessage(e.getMessage()); + return response; + } + } + + @Override + public CommandResponse switchIOMode(ForkLiftCommand command) { + CommandResponse response = new CommandResponse(false); + try { + //鍙戝嚭璇锋眰 + String resultKey = requestCommand(command); + //鏌ヨ璇锋眰缁撴灉 + JSONObject result = queryCommandStatus(resultKey); + if (result == null) { + return response;//璇锋眰澶辫触 + } + if(!result.getString("result").equals("success")) { + return response;//璇锋眰澶辫触 + } + + this.forkLiftProtocol.setSendTime(System.currentTimeMillis());//鎸囦护涓嬪彂鏃堕棿 + response.setMessage(JSON.toJSONString(result)); + response.setResult(true); + return response; + } catch (Exception e) { + e.printStackTrace(); + response.setMessage(e.getMessage()); + return response; + } + } + + @Override + public CommandResponse reset() { + CommandResponse response = new CommandResponse(false); + try { + ForkLiftCommand resetCommand = getResetCommand(9999); + + //鍙戝嚭璇锋眰 + String resultKey = requestCommand(resetCommand); + //鏌ヨ璇锋眰缁撴灉 + JSONObject result = queryCommandStatus(resultKey); + if (result == null) { + return response;//璇锋眰澶辫触 + } + if(!result.getString("result").equals("success")) { + return response;//璇锋眰澶辫触 + } + + this.forkLiftProtocol.setSendTime(System.currentTimeMillis());//鎸囦护涓嬪彂鏃堕棿 + response.setMessage(JSON.toJSONString(result)); + response.setResult(true); + return response; + } catch (Exception e) { + e.printStackTrace(); + response.setMessage(e.getMessage()); + return response; + } + } + + @Override + public boolean isIdle() { + if (this.forkLiftProtocol.getTaskNo() == null + || this.forkLiftProtocol.getProtocolStatus() == null + || this.forkLiftProtocol.getModel() == null + || this.forkLiftProtocol.getErrorCode() == null + ) { + return false; + } + + boolean res = this.forkLiftProtocol.getProtocolStatus() == ForkLiftProtocolStatusType.IDLE.id + && this.forkLiftProtocol.getWrkNo() == 0 + && this.forkLiftProtocol.getTaskNo() == 0 + && this.forkLiftProtocol.getModel() == 2 + && this.forkLiftProtocol.getErrorCode() == 0 + ; + return res; + } + + @Override + public boolean isDeviceIdle() { + return isDeviceIdle(null); + } + + @Override + public boolean isDeviceIdle(ExecuteSupport support) { + if (null != support) { + Boolean judgement = support.judgement(); + if (judgement != null && !judgement) { + return true; + } + } + + if (this.forkLiftProtocol.getTaskNo() == null + || this.forkLiftProtocol.getProtocolStatus() == null + || this.forkLiftProtocol.getModel() == null + || this.forkLiftProtocol.getErrorCode() == null + ) { + return false; + } + + boolean res = this.forkLiftProtocol.getProtocolStatus() == ForkLiftProtocolStatusType.IDLE.id + && this.forkLiftProtocol.getWrkNo() == 0 + && this.forkLiftProtocol.getModel() == 2 + && this.forkLiftProtocol.getErrorCode() == 0 + ; + return res; + } + + @Override + public boolean setProtocolStatus(ForkLiftProtocolStatusType status) { + return false; + } + + @Override + public boolean setSyncTaskNo(Integer taskNo) { + this.forkLiftProtocol.setSyncTaskNo(taskNo); + return true; + } + + @Override + public int generateDeviceTaskNo(int taskNo, ForkLiftTaskModeType type) { + return taskNo; + } + + @Override + public ForkLiftCommand getPickAndPutCommand(Integer taskNo, Integer pick, Integer put) { + Integer realPick = pick % 1000; + Integer realPut = put % 1000; + + ForkLiftCommand command = new ForkLiftCommand(); + command.setLiftNo(device.getDeviceNo()); + command.setTaskNo(taskNo); + command.setMode(ForkLiftTaskModeType.PICK_PUT.id); + command.setPick(realPick); + command.setPut(realPut); + command.setConfirm(1); + + return command; + } + + @Override + public ForkLiftCommand getShuttleSwitchCommand(Integer taskNo, Integer pick, Integer put) { + Integer realPick = pick % 1000; + Integer realPut = put % 1000; + + ForkLiftCommand command = new ForkLiftCommand(); + command.setLiftNo(device.getDeviceNo()); + command.setTaskNo(taskNo); + command.setMode(ForkLiftTaskModeType.SHUTTLE_SWITCH.id); + command.setPick(realPick); + command.setPut(realPut); + command.setConfirm(1); + + return command; + } + + @Override + public ForkLiftCommand getMoveCommand(Integer taskNo, Integer pick, Integer put) { + Integer realPick = pick % 1000; + Integer realPut = put % 1000; + + ForkLiftCommand command = new ForkLiftCommand(); + command.setLiftNo(device.getDeviceNo()); + command.setTaskNo(taskNo); + command.setMode(ForkLiftTaskModeType.MOVE.id); + command.setPick(realPick); + command.setPut(realPut); + command.setConfirm(1); + + return command; + } + + @Override + public ForkLiftCommand getSwitchIOCommand(Integer taskNo, ForkLiftIoModeType mode) { + ForkLiftCommand command = new ForkLiftCommand(); + command.setLiftNo(device.getDeviceNo()); + if (mode.equals(ForkLiftIoModeType.IN)) { + command.setMode(ForkLiftTaskModeType.SWITCH_IN.id); + } else { + command.setMode(ForkLiftTaskModeType.SWITCH_OUt.id); + } + return command; + } + + @Override + public ForkLiftCommand getResetCommand(Integer taskNo) { + ForkLiftCommand command = new ForkLiftCommand(); + command.setLiftNo(device.getDeviceNo()); + command.setMode(ForkLiftTaskModeType.RESET.id); + return command; + } + + //鑾峰彇璇荤姸鎬佷俊鎭懡浠� + private ForkLiftCommand getReadStatusCommand() { + ForkLiftCommand command = new ForkLiftCommand(); + command.setLiftNo(device.getDeviceNo()); + command.setMode(ForkLiftTaskModeType.READ_STATUS.id); + return command; + } + + //鍙戝嚭璇锋眰 + private String requestCommand(ForkLiftCommand command) throws IOException { + try { + DeviceMsgUtils deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class); + if (deviceMsgUtils == null) { + return null; + } + + //鍘嬬缉鏁版嵁鍖� + JSONObject data = JSON.parseObject(JSON.toJSONString(command)); + + DeviceCommandMsgModel commandMsgModel = new DeviceCommandMsgModel(); + commandMsgModel.setDeviceId(device.getDeviceNo()); + commandMsgModel.setDeviceType(String.valueOf(SlaveType.ForkLift)); + commandMsgModel.setCommand(data); + String key = deviceMsgUtils.sendDeviceCommand(SlaveType.ForkLift, device.getDeviceNo(), commandMsgModel); + return key; + }catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + private JSONObject queryCommandStatus(String resultKey) { + // 鑾峰彇鏈嶅姟鍣ㄥ搷搴� + // 灏濊瘯50娆� + JSONObject result = null; + for (int i = 0; i < 50; i++) { + result = getRequestBody(resultKey); + if (result == null) { + try { + Thread.sleep(500); + } catch (Exception e) { + e.printStackTrace(); + } + }else { + break; + } + } + return result; + } + + public JSONObject getRequestBody(String resultKey) { + try { + // 鑾峰彇鏈嶅姟鍣ㄥ搷搴� + JSONObject result = null; + int idx = -1; + for (int i = 0; i < resultList.size(); i++) { + DeviceMsgModel deviceMsgModel = resultList.get(i); + if(deviceMsgModel.getResultKey().equals(resultKey)){ + idx = i; + result = JSON.parseObject(JSON.toJSONString(deviceMsgModel.getDeviceMsg())); + break; + } + } + + if (result == null) { + return null;//鏃犲搷搴旂粨鏋� + } + + resultList.remove(idx); + return result; + } catch (Exception e) { + return null; + } + } + + private void listenMessageFromRedis() { + try { + DeviceMsgUtils deviceMsgUtils = null; + try { + deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class); + }catch (Exception e){ + + } + if (deviceMsgUtils == null) { + return; + } + DeviceMsgModel deviceMsg = deviceMsgUtils.getDeviceMsg(SlaveType.ForkLift, device.getDeviceNo()); + if(deviceMsg == null){ + return; + } + + if (deviceMsg.getDeviceMsgType().equals("status")) { + readResultList.add(deviceMsg); + }else { + resultList.add(deviceMsg); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 鎵╁睍瀛楁 + */ + @Data + private class InnerForkLiftExtend { + + } +} -- Gitblit v1.9.1