From 6182cc11c3e93610df7fda87611d2ca807c6354c Mon Sep 17 00:00:00 2001
From: qlsxk <qlsxk@qq.com>
Date: 星期四, 16 十月 2025 14:28:30 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java | 518 +++++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 395 insertions(+), 123 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
index 7504b2d..2a3b70d 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyForkLiftThread.java
@@ -2,13 +2,15 @@
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.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;
@@ -17,20 +19,18 @@
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.action.ForkLiftAction;
-import com.zy.core.action.ShuttleAction;
import com.zy.core.cache.OutputQueue;
-import com.zy.core.enums.ForkLiftProtocolStatusType;
-import com.zy.core.enums.ForkLiftTaskModeType;
-import com.zy.core.enums.RedisKeyType;
-import com.zy.core.model.CommandResponse;
-import com.zy.core.model.ForkLiftSlave;
+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.*;
@@ -38,44 +38,31 @@
@SuppressWarnings("all")
public class ZyForkLiftThread implements ForkLiftThread {
- private ForkLiftSlave slave;
+ private DeviceConfig device;
private ForkLiftProtocol forkLiftProtocol;
private RedisUtil redisUtil;
- private SiemensS7Net siemensS7Net;
private List<ForkLiftStaProtocol> forkLiftStaProtocols = new ArrayList<>();
+ private List<DeviceMsgModel> readResultList = new ArrayList<>();
+ private List<DeviceMsgModel> resultList = new ArrayList<>();
- public ZyForkLiftThread(ForkLiftSlave slave, RedisUtil redisUtil) {
- this.slave = slave;
+ public ZyForkLiftThread(DeviceConfig device, List<LiftStation> stationList, RedisUtil redisUtil) {
+ this.device = device;
this.redisUtil = redisUtil;
//鍒濆鍖栫珯鐐�
- for (ForkLiftSlave.Sta sta : this.slave.getSta()) {
+ for (LiftStation station : stationList) {
ForkLiftStaProtocol forkLiftStaProtocol = new ForkLiftStaProtocol();
- forkLiftStaProtocol.setStaNo(sta.getStaNo());//绔欑偣鍙�
- forkLiftStaProtocol.setLev(sta.getLev());//绔欑偣妤煎眰
- String locNo = Utils.getLocNo(this.slave.getStaRow(), this.slave.getStaBay(), sta.getLev());
+ forkLiftStaProtocol.setSiteId(station.getSiteId());//绔欑偣鍙�
+ forkLiftStaProtocol.setLev(station.getLev());//绔欑偣妤煎眰
+ String locNo = Utils.getLocNo(station.getRow(), station.getBay(), station.getLev());
forkLiftStaProtocol.setLocNo(locNo);//绔欑偣搴撲綅鍙�
- forkLiftStaProtocol.setLiftNo(sta.getLiftNo());//鎻愬崌鏈哄彿
+ forkLiftStaProtocol.setLiftNo(station.getLiftNo());//鎻愬崌鏈哄彿
forkLiftStaProtocols.add(forkLiftStaProtocol);
}
}
@Override
public boolean connect() {
- boolean result = false;
- siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp());
- siemensS7Net.setRack(slave.getRack().byteValue());
- siemensS7Net.setSlot(slave.getSlot().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()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
- News.info("璐у弶鎻愬崌鏈鸿繛鎺ユ垚鍔� ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
- } else {
- OutputQueue.FORKLIFT.offer(MessageFormat.format( "銆恵0}銆戣揣鍙夋彁鍗囨満杩炴帴澶辫触锛侊紒锛� ===>> [id:{1}] [ip:{2}] [port:{3}] [rack:{4}] [slot:{5}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort(), slave.getRack(), slave.getSlot()));
- News.error("璐у弶鎻愬崌鏈鸿繛鎺ュけ璐ワ紒锛侊紒 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort());
- }
- // siemensS7Net.ConnectClose();
- return result;
+ return true;
}
@Override
@@ -85,12 +72,25 @@
@Override
public void run() {
- News.info("{}鍙疯揣鍙夋彁鍗囨満绾跨▼鍚姩", slave.getId());
+ 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 {
- read();
- Thread.sleep(200);
execute();
} catch (Exception e) {
e.printStackTrace();
@@ -99,12 +99,15 @@
}
private void execute() {
- ForkLiftAction forkLiftAction = SpringUtils.getBean(ForkLiftAction.class);
+ 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 + slave.getId());
+ Object object = redisUtil.get(RedisKeyType.FORK_LIFT_FLAG.key + device.getDeviceNo());
if (object == null) {
return;
}
@@ -112,7 +115,7 @@
Integer taskNo = Integer.valueOf(String.valueOf(object));
if (taskNo != 0) {
//瀛樺湪浠诲姟闇�瑕佹墽琛�
- boolean result = forkLiftAction.executeWork(slave.getId(), taskNo);
+ boolean result = forkLiftAction.executeWork(device.getDeviceNo(), taskNo);
}
}
@@ -127,47 +130,87 @@
forkLiftProtocol.setPakMk(true);
}
} catch (Exception e) {
- OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆戣鍙栨彁鍗囨満鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+ 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 {
//鑾峰彇鎻愬崌鏈烘暟鎹�
- OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB101.0", (short) 16);
- if (result1.IsSuccess) {
- if (null == forkLiftProtocol) {
- forkLiftProtocol = new ForkLiftProtocol();
- forkLiftProtocol.setLiftNo(slave.getId());
- forkLiftProtocol.setProtocolStatus(ForkLiftProtocolStatusType.NONE);
- InnerForkLiftExtend innerForkLiftExtend = new InnerForkLiftExtend();
- }
+ DeviceMsgUtils deviceMsgUtils = null;
+ try {
+ deviceMsgUtils = SpringUtils.getBean(DeviceMsgUtils.class);
+ }catch (Exception e){
- //----------璇诲彇鎻愬崌鏈虹姸鎬�-----------
- //妯″紡
- forkLiftProtocol.setModel((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 0));
- //PLC浠诲姟鍙�
- forkLiftProtocol.setWrkNo((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 2));
- //浠诲姟鐘舵��
- forkLiftProtocol.setProtocolStatus((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 4));
- //浠诲姟妯″紡
- forkLiftProtocol.setTaskMode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 6));
- //鍙栬揣鏁版嵁
- forkLiftProtocol.setPick((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 8));
- //鏀捐揣鏁版嵁
- forkLiftProtocol.setPut((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 10));
- //鍑哄叆搴撴ā寮�
- forkLiftProtocol.setIOMode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 12));
- //鏁呴殰鐮�
- forkLiftProtocol.setErrorCode((int) siemensS7Net.getByteTransform().TransInt16(result1.Content, 14));
+ }
+ if(deviceMsgUtils == null){
+ return;
+ }
- //************琛ュ厖鎵╁睍瀛楁*************
- InnerForkLiftExtend forkLiftExtend = (InnerForkLiftExtend) forkLiftProtocol.getExtend();
- forkLiftProtocol.setExtend(forkLiftExtend);
+ ForkLiftCommand readStatusCommand = getReadStatusCommand();
+ //鎸囦护瓒呰繃2鏉★紝涓嶅啀涓嬪彂浠诲姟鐘舵�佽姹�
+ TreeSet<String> deviceCommandMsgListKey = deviceMsgUtils.getDeviceCommandMsgListKey(SlaveType.ForkLift, device.getDeviceNo());
+ if (deviceCommandMsgListKey.size() < 2) {
+ requestCommand(readStatusCommand);//璇锋眰鐘舵��
+ }
- }else {
- OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆憑1}璇诲彇璐у弶鎻愬崌鏈虹姸鎬佷俊鎭け璐�", DateUtils.convert(new Date()), slave.getId()));
+ 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) {
@@ -175,7 +218,7 @@
//淇濆瓨鏁版嵁璁板綍
DeviceDataLogService deviceDataLogService = SpringUtils.getBean(DeviceDataLogService.class);
DeviceDataLog deviceDataLog = new DeviceDataLog();
- deviceDataLog.setOriginData(Base64.getEncoder().encodeToString(result1.Content));
+ deviceDataLog.setOriginData(JSON.toJSONString(data));
deviceDataLog.setWcsData(JSON.toJSONString(forkLiftProtocol));
deviceDataLog.setType("forkLift");
deviceDataLog.setDeviceNo(forkLiftProtocol.getLiftNo());
@@ -189,7 +232,7 @@
//灏嗘彁鍗囨満鐘舵�佷繚瀛樿嚦鏁版嵁搴�
BasLiftService basLiftService = SpringUtils.getBean(BasLiftService.class);
BasLift basLift = basLiftService.selectOne(new EntityWrapper<BasLift>()
- .eq("lift_no", slave.getId()));
+ .eq("lift_no", device.getDeviceNo()));
if (basLift == null) {
basLift = new BasLift();
//鎻愬崌鏈哄彿
@@ -208,7 +251,8 @@
}
} catch (Exception e) {
- OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆戣鍙栬揣鍙夋彁鍗囨満鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), slave.getId(), slave.getIp(), slave.getPort()));
+ e.printStackTrace();
+ OutputQueue.FORKLIFT.offer(MessageFormat.format("銆恵0}銆戣鍙栬揣鍙夋彁鍗囨満鐘舵�佷俊鎭け璐� ===>> [id:{1}] [ip:{2}] [port:{3}]", DateUtils.convert(new Date()), device.getDeviceNo(), device.getIp(), device.getPort()));
}
}
@@ -233,50 +277,133 @@
@Override
public CommandResponse pickAndPut(ForkLiftCommand command) {
CommandResponse response = new CommandResponse(false);
-
- short[] array = new short[4];
- array[0] = command.getTaskNo();//浠诲姟鍙�
- array[1] = command.getMode();//浠诲姟妯″紡
- array[2] = command.getPick();//鍙栬揣鏁版嵁
- array[3] = command.getPut();//鏀捐揣鏁版嵁
- OperateResult result = siemensS7Net.Write("DB103.0", array);
- if (result.IsSuccess) {
- OperateResult result2 = siemensS7Net.Write("DB103.16", command.getConfirm());
- if (result2.IsSuccess) {
- response.setResult(true);
+ 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;
}
- return response;
}
@Override
public CommandResponse shuttleSwitch(ForkLiftCommand command) {
CommandResponse response = new CommandResponse(false);
-
- short[] array = new short[4];
- array[0] = command.getTaskNo();//浠诲姟鍙�
- array[1] = command.getMode();//浠诲姟妯″紡
- array[2] = command.getPick();//鍙栬揣鏁版嵁
- array[3] = command.getPut();//鏀捐揣鏁版嵁
- OperateResult result = siemensS7Net.Write("DB103.0", array);
- if (result.IsSuccess) {
- OperateResult result2 = siemensS7Net.Write("DB103.8", command.getConfirm());
- if (result2.IsSuccess) {
- response.setResult(true);
+ 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;
}
- 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);
- OperateResult result = siemensS7Net.Write("DB103.10", (short) 1);
- if (result.IsSuccess) {
- News.info("璐у弶鎻愬崌鏈虹‘璁ゅ懡浠や笅鍙戞垚鍔燂紝鎻愬崌鏈哄彿={}", forkLiftProtocol.getLiftNo());
+ 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;
}
- return response;
}
@Override
@@ -284,6 +411,7 @@
if (this.forkLiftProtocol.getTaskNo() == null
|| this.forkLiftProtocol.getProtocolStatus() == null
|| this.forkLiftProtocol.getModel() == null
+ || this.forkLiftProtocol.getErrorCode() == null
) {
return false;
}
@@ -291,7 +419,9 @@
boolean res = this.forkLiftProtocol.getProtocolStatus() == ForkLiftProtocolStatusType.IDLE.id
&& this.forkLiftProtocol.getWrkNo() == 0
&& this.forkLiftProtocol.getTaskNo() == 0
- && this.forkLiftProtocol.getModel() == 2;
+ && this.forkLiftProtocol.getModel() == 2
+ && this.forkLiftProtocol.getErrorCode() == 0
+ ;
return res;
}
@@ -312,13 +442,16 @@
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.getModel() == 2
+ && this.forkLiftProtocol.getErrorCode() == 0
+ ;
return res;
}
@@ -339,33 +472,172 @@
}
@Override
- public List<ForkLiftCommand> getPickAndPutCommand(Integer taskNo, Integer pick, Integer put) {
- List<ForkLiftCommand> commands = new ArrayList<>();
- ForkLiftCommand command = new ForkLiftCommand();
- command.setLiftNo(slave.getId());
- command.setTaskNo(taskNo.shortValue());
- command.setMode(ForkLiftTaskModeType.PICK_PUT.id.shortValue());
- command.setPick(pick.shortValue());
- command.setPut(put.shortValue());
- command.setConfirm((short) 1);
+ public ForkLiftCommand getPickAndPutCommand(Integer taskNo, Integer pick, Integer put) {
+ Integer realPick = pick % 1000;
+ Integer realPut = put % 1000;
- commands.add(command);
- return commands;
+ 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 List<ForkLiftCommand> getShuttleSwitchCommand(Integer taskNo, Integer originLev, Integer targetLev) {
- List<ForkLiftCommand> commands = new ArrayList<>();
- ForkLiftCommand command = new ForkLiftCommand();
- command.setLiftNo(slave.getId());
- command.setTaskNo(taskNo.shortValue());
- command.setMode(ForkLiftTaskModeType.SHUTTLE_SWITCH.id.shortValue());
- command.setPick(originLev.shortValue());
- command.setPut(targetLev.shortValue());
- command.setConfirm((short) 1);
+ public ForkLiftCommand getShuttleSwitchCommand(Integer taskNo, Integer pick, Integer put) {
+ Integer realPick = pick % 1000;
+ Integer realPut = put % 1000;
- commands.add(command);
- return commands;
+ 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();
+ }
}
/**
--
Gitblit v1.9.1