From ef1cd5823fe7cf724e1de875a1a5737a44f160f1 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期四, 15 一月 2026 13:11:12 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java | 605 +++++++++++++++++++++++++-----------------------------
1 files changed, 285 insertions(+), 320 deletions(-)
diff --git a/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java b/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
index 1687d61..a493189 100644
--- a/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
+++ b/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
@@ -3,8 +3,6 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.asrs.entity.DeviceConfig;
-import com.zy.asrs.utils.Utils;
-import com.zy.common.model.NavigateNode;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.enums.RedisKeyType;
@@ -24,12 +22,14 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
import java.util.Map;
-import java.util.function.Supplier;
+import java.util.Arrays;
public class ZyStationFakeSegConnect implements ZyStationConnectApi {
- private static int LOCK_STATION = 0;
+ // 绔欑偣绾ч攣锛氭瘡涓珯鐐圭嫭绔嬩竴鎶婇攣锛屾彁鍗囧苟鍙戞�ц兘
+ private final Map<Integer, ReentrantLock> stationLocks = new ConcurrentHashMap<>();
private HashMap<Integer, List<ZyStationStatusEntity>> deviceStatusMap = new HashMap<>();
private HashMap<Integer, DeviceConfig> deviceConfigMap = new HashMap<>();
private RedisUtil redisUtil;
@@ -60,8 +60,7 @@
&& statusEntity.isLoading()
&& statusEntity.getTaskNo() > 0
&& !statusEntity.isRunBlock()
- && !statusEntity.getStationId().equals(statusEntity.getTargetStaNo())
- ) {
+ && !statusEntity.getStationId().equals(statusEntity.getTargetStaNo())) {
BlockingQueue<StationCommand> commands = taskQueues.get(statusEntity.getTaskNo());
if (commands == null) {
statusEntity.setRunBlock(true);
@@ -76,7 +75,7 @@
}
}
});
- checkThread.start();
+ // checkThread.start();
return true;
}
@@ -94,7 +93,8 @@
}
DeviceConfig deviceConfig = deviceConfigMap.get(deviceNo);
if (statusList.isEmpty()) {
- List<ZyStationStatusEntity> init = JSON.parseArray(deviceConfig.getFakeInitStatus(), ZyStationStatusEntity.class);
+ List<ZyStationStatusEntity> init = JSON.parseArray(deviceConfig.getFakeInitStatus(),
+ ZyStationStatusEntity.class);
if (init != null) {
statusList.addAll(init);
for (ZyStationStatusEntity status : statusList) {
@@ -125,13 +125,16 @@
// 澶勭悊闈炵Щ鍔ㄥ懡浠�
if (command.getCommandType() != StationCommandType.MOVE) {
handleCommand(deviceNo, command);
- }else {
+ } else {
+ // 灏嗙Щ鍔ㄥ懡浠よ拷鍔犲埌浠诲姟闃熷垪锛堟敮鎸佸垎娈典笅鍙戯級
taskQueues.computeIfAbsent(taskNo, k -> new LinkedBlockingQueue<>()).offer(command);
taskLastUpdateTime.put(taskNo, System.currentTimeMillis());
+ // 鍙湁浠诲姟鏈惎鍔ㄦ椂鎵嶅惎鍔ㄦ墽琛屽櫒锛屽悗缁垎娈靛懡浠や粎杩藉姞鍒伴槦鍒�
if (taskRunning.putIfAbsent(taskNo, true) == null) {
executor.submit(() -> runTaskLoop(deviceNo, taskNo));
}
+ // 鍚庣画鍒嗘鍛戒护涓嶅啀杩斿洖閿欒锛屾甯歌拷鍔犲埌闃熷垪
}
return new CommandResponse(true, "鍛戒护宸插彈鐞嗭紙寮傛鎵ц锛�");
@@ -139,138 +142,171 @@
private void runTaskLoop(Integer deviceNo, Integer taskNo) {
try {
- // 鐢ㄤ簬瀛樺偍褰撳墠浠诲姟寰呮墽琛岀殑瀹屾暣璺緞闃熷垪
- BlockingQueue<Integer> pathQueue = new LinkedBlockingQueue<>();
- // 褰撳墠鏄惁姝e湪鎵ц绉诲姩
- boolean isMoving = false;
- // 褰撳墠绉诲姩鍒扮殑璺緞绱㈠紩
- int currentPathIndex = 0;
- // 瀹屾暣璺緞璁板綍
- List<Integer> fullPath = new ArrayList<>();
-
- StationCommand initialCommand = null;
+ // 寰呮墽琛岀殑璺緞闃熷垪锛堝瓨鍌ㄧ珯鐐笽D搴忓垪锛�
+ LinkedBlockingQueue<Integer> pendingPathQueue = new LinkedBlockingQueue<>();
+ // 褰撳墠鎵�鍦ㄧ珯鐐笽D
+ Integer currentStationId = null;
+ // 鏈�缁堢洰鏍囩珯鐐笽D
Integer finalTargetStationId = null;
+ // 鏄惁闇�瑕佺敓鎴愭潯鐮�
boolean generateBarcode = false;
+ // 鏄惁宸插垵濮嬪寲璧风偣
+ boolean initialized = false;
+ // 涓婁竴姝ユ墽琛屾椂闂达紙鐢ㄤ簬鍫靛妫�娴嬶級
long stepExecuteTime = System.currentTimeMillis();
while (true) {
+ if (Thread.currentThread().isInterrupted()) {
+ break;
+ }
+
BlockingQueue<StationCommand> commandQueue = taskQueues.get(taskNo);
if (commandQueue == null) {
break;
}
- // 灏濊瘯鑾峰彇鏂板懡浠わ紝濡傛灉娌℃湁鏂板懡浠ゅ垯缁х画鎵ц鐜版湁璺緞
+ // 灏濊瘯鑾峰彇鏂扮殑鍒嗘鍛戒护
StationCommand command = commandQueue.poll(100, TimeUnit.MILLISECONDS);
if (command != null) {
- stepExecuteTime = System.currentTimeMillis();
taskLastUpdateTime.put(taskNo, System.currentTimeMillis());
-
- if (initialCommand == null) {
- initialCommand = command;
+
+ // 棣栨鎺ユ敹鍛戒护鏃跺垵濮嬪寲
+ if (finalTargetStationId == null) {
finalTargetStationId = command.getTargetStaNo();
if (checkTaskNoInArea(taskNo)) {
generateBarcode = true;
}
}
+ // 灏嗘柊璺緞杩藉姞鍒板緟鎵ц闃熷垪
List<Integer> newPath = command.getNavigatePath();
if (newPath != null && !newPath.isEmpty()) {
- // 濡傛灉鏄涓�娈佃矾寰�
- if (fullPath.isEmpty()) {
- fullPath.addAll(newPath);
- for (Integer stationId : newPath) {
- pathQueue.offer(stationId);
- }
- } else {
- // 杩藉姞璺緞锛岄渶瑕佸幓閲嶈鎺ョ偣
- Integer lastStationId = fullPath.get(fullPath.size() - 1);
- int startIndex = 0;
- if (!newPath.isEmpty() && newPath.get(0).equals(lastStationId)) {
- startIndex = 1;
- }
- for (int i = startIndex; i < newPath.size(); i++) {
- Integer stationId = newPath.get(i);
- fullPath.add(stationId);
- pathQueue.offer(stationId);
- }
+ // 鑾峰彇闃熷垪涓渶鍚庝竴涓珯鐐癸紙鐢ㄤ簬琛旀帴鐐瑰幓閲嶏級
+ Integer lastInQueue = getLastInQueue(pendingPathQueue);
+ if (lastInQueue == null) {
+ lastInQueue = currentStationId;
}
+
+ int startIndex = 0;
+ // 濡傛灉鏂拌矾寰勭殑璧风偣涓庡綋鍓嶄綅缃垨闃熷垪鏈熬閲嶅锛屽垯璺宠繃
+ if (lastInQueue != null && !newPath.isEmpty() && newPath.get(0).equals(lastInQueue)) {
+ startIndex = 1;
+ }
+
+ for (int i = startIndex; i < newPath.size(); i++) {
+ pendingPathQueue.offer(newPath.get(i));
+ }
+
+ News.info("[WCS Debug] 浠诲姟{}杩藉姞璺緞娈�: {} -> 闃熷垪澶у皬: {}", taskNo, newPath, pendingPathQueue.size());
}
}
// 鎵ц绉诲姩閫昏緫
- if (!pathQueue.isEmpty()) {
- // 濡傛灉鍒氬紑濮嬶紝鍏堝垵濮嬪寲褰撳墠浣嶇疆
- if (currentPathIndex == 0 && !fullPath.isEmpty()) {
- Integer startStationId = fullPath.get(0);
- Integer deviceId = getDeviceNoByStationId(startStationId);
- if (deviceId != null) {
- initStationMove(taskNo, startStationId, deviceId, taskNo, finalTargetStationId, true, null);
- }
- }
+ if (!pendingPathQueue.isEmpty()) {
+ Integer nextStationId = pendingPathQueue.peek();
- // 鍙栧嚭涓嬩竴涓洰鏍囩偣
- Integer nextStationId = pathQueue.peek(); // 杩欓噷鐨勯�昏緫闇�瑕佹敼涓洪�愪釜琛岃蛋
- // 瀹為檯琛岃蛋閫昏緫搴旇鏄湪杩欓噷娑堣垂 pathQueue
- // 涓轰簡绠�鍖栵紝鎴戜滑灏� pathQueue 杞负 stationMoveByPathIds 鐨勯�昏緫锛屼絾杩欓噷闇�瑕佹敼閫犳垚姝ヨ繘寮�
-
- // 閲嶆柊璁捐锛歳unTaskLoop 璐熻矗涓嶆柇娑堣垂 pathQueue 骞舵墽琛屽崟姝ョЩ鍔�
- if (nextStationId != null) {
- Integer currentStationId = fullPath.get(currentPathIndex);
- if (currentStationId.equals(nextStationId)) {
- pathQueue.poll(); // 绉婚櫎宸插埌杈剧殑鐐�
+ // 濡傛灉灏氭湭鍒濆鍖栬捣鐐�
+ if (!initialized && currentStationId == null) {
+ // 浼樺厛鏌ユ壘鎵樼洏褰撳墠瀹為檯浣嶇疆锛堟敮鎸佸牭濉炲悗閲嶈矾鐢卞満鏅級
+ Integer actualCurrentStationId = findCurrentStationIdByTask(taskNo);
+ if (actualCurrentStationId != null) {
+ // 鎵惧埌浜嗗綋鍓嶆墭鐩樹綅缃紝浣跨敤瀹為檯浣嶇疆浣滀负璧风偣
+ currentStationId = actualCurrentStationId;
+ initialized = true;
+
+ // 娓呴櫎璇ョ珯鐐圭殑 runBlock 鏍囪锛堝牭濉炴仮澶嶏級
+ Integer deviceId = getDeviceNoByStationId(currentStationId);
+ if (deviceId != null) {
+ clearRunBlock(currentStationId, deviceId);
+ }
+
+ // 濡傛灉璺緞璧风偣涓庡綋鍓嶄綅缃浉鍚岋紝绉婚櫎璧风偣閬垮厤閲嶅
+ if (nextStationId.equals(currentStationId)) {
+ pendingPathQueue.poll();
+ }
+
+ stepExecuteTime = System.currentTimeMillis();
+ News.info("[WCS Debug] 浠诲姟{}鎭㈠鎵ц锛屽綋鍓嶄綅缃�: {}", taskNo, currentStationId);
continue;
}
-
- // 鎵ц浠� currentStationId 鍒� nextStationId 鐨勭Щ鍔�
- Integer currentDeviceNo = getDeviceNoByStationId(currentStationId);
- Integer nextDeviceNo = getDeviceNoByStationId(nextStationId);
-
- if (currentDeviceNo != null && nextDeviceNo != null) {
- boolean moveSuccess = stationMoveToNext(taskNo, currentStationId, currentDeviceNo, nextStationId, nextDeviceNo, taskNo, finalTargetStationId);
- if (moveSuccess) {
- currentPathIndex++;
- pathQueue.poll();
- stepExecuteTime = System.currentTimeMillis();
- sleep(1000); // 妯℃嫙鑰楁椂
- } else {
- if (!checkTaskNoInArea(taskNo)) {
- boolean fakeAllowCheckBlock = true;
- Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
- if (systemConfigMapObj != null) {
- HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
- if (systemConfigMap.get("fakeAllowCheckBlock") != null && !systemConfigMap.get("fakeAllowCheckBlock").equals("Y")) {
- fakeAllowCheckBlock = false;
- }
- }
- if (fakeAllowCheckBlock && System.currentTimeMillis() - stepExecuteTime > 1000 * 10) {
- //璁ゅ畾鍫靛
- boolean result = runBlockStation(taskNo, currentStationId, currentDeviceNo, taskNo, currentStationId);
- if(result) {
- break;
- }
+ // 鏈壘鍒板綋鍓嶄綅缃紙棣栨鎵ц锛夛紝棣栦釜绔欑偣灏辨槸璧风偣
+ currentStationId = nextStationId;
+ Integer deviceId = getDeviceNoByStationId(currentStationId);
+ if (deviceId != null) {
+ boolean result = initStationMove(taskNo, currentStationId, deviceId, taskNo,
+ finalTargetStationId, true, null);
+ if (result) {
+ initialized = true;
+ pendingPathQueue.poll(); // 绉婚櫎璧风偣
+ stepExecuteTime = System.currentTimeMillis();
+ News.info("[WCS Debug] 浠诲姟{}鍒濆鍖栬捣鐐�: {}", taskNo, currentStationId);
+ }
+ }
+ sleep(500);
+ continue;
+ }
+
+ // 鎵ц浠庡綋鍓嶇珯鐐瑰埌涓嬩竴绔欑偣鐨勭Щ鍔�
+ Integer currentDeviceNo = getDeviceNoByStationId(currentStationId);
+ Integer nextDeviceNo = getDeviceNoByStationId(nextStationId);
+
+ if (currentDeviceNo != null && nextDeviceNo != null) {
+ boolean moveSuccess = stationMoveToNext(taskNo, currentStationId, currentDeviceNo,
+ nextStationId, nextDeviceNo, taskNo, finalTargetStationId);
+ if (moveSuccess) {
+ currentStationId = nextStationId;
+ pendingPathQueue.poll();
+ stepExecuteTime = System.currentTimeMillis();
+ News.info("[WCS Debug] 浠诲姟{}绉诲姩鍒扮珯鐐�: {}, 鍓╀綑闃熷垪: {}", taskNo, currentStationId,
+ pendingPathQueue.size());
+ sleep(1000); // 妯℃嫙绉诲姩鑰楁椂
+ } else {
+ // 绉诲姩澶辫触锛屾鏌ユ槸鍚﹀牭濉�
+ if (!checkTaskNoInArea(taskNo)) {
+ boolean fakeAllowCheckBlock = getFakeAllowCheckBlock();
+
+ if (fakeAllowCheckBlock && System.currentTimeMillis() - stepExecuteTime > 10000) {
+ // 璁ゅ畾鍫靛
+ boolean result = runBlockStation(taskNo, currentStationId, currentDeviceNo, taskNo,
+ currentStationId);
+ if (result) {
+ News.info("[WCS Debug] 浠诲姟{}鍦ㄧ珯鐐箋}琚爣璁颁负鍫靛", taskNo, currentStationId);
+ pendingPathQueue.clear();
+ break;
}
}
- sleep(1000); // 澶辫触閲嶈瘯绛夊緟
}
- } else {
- pathQueue.poll(); // 鏃犳硶鎵ц锛岃烦杩�
+ sleep(500); // 澶辫触閲嶈瘯绛夊緟
}
+ } else {
+ // 鏃犳硶鑾峰彇璁惧鍙凤紝璺宠繃璇ョ珯鐐�
+ pendingPathQueue.poll();
}
} else {
- // 闃熷垪涓虹┖锛屾鏌ユ槸鍚﹁秴鏃�
- Long lastTime = taskLastUpdateTime.get(taskNo);
- if (lastTime != null && System.currentTimeMillis() - lastTime > 30000) {
- // 瀹屾垚浠诲姟鍚庢竻鐞�
- if (fullPath.size() > 0 && generateBarcode) {
- Integer lastStation = fullPath.get(fullPath.size()-1);
- Integer lastDevice = getDeviceNoByStationId(lastStation);
- if (lastDevice != null) {
- generateStationBarcode(taskNo, finalTargetStationId, lastDevice);
- }
+ // 璺緞闃熷垪涓虹┖锛岀瓑寰呮柊鐨勫垎娈靛懡浠�
+ if (currentStationId != null && finalTargetStationId != null
+ && currentStationId.equals(finalTargetStationId)) {
+ // 宸插埌杈炬渶缁堢洰鏍囷紝姝e父缁撴潫
+ if (generateBarcode) {
+ Integer targetDeviceNo = getDeviceNoByStationId(finalTargetStationId);
+ if (targetDeviceNo != null) {
+ generateStationBarcode(taskNo, finalTargetStationId, targetDeviceNo);
+ News.info("[WCS Debug] 浠诲姟{}鍒拌揪鐩爣{}骞剁敓鎴愭潯鐮�", taskNo, finalTargetStationId);
+ }
}
break;
}
+
+ // 鏈埌杈炬渶缁堢洰鏍囷紝绛夊緟鏂扮殑鍒嗘鍛戒护
+ Long lastTime = taskLastUpdateTime.get(taskNo);
+ if (lastTime != null && System.currentTimeMillis() - lastTime > 30000) {
+ // 瓒呮椂锛�30绉掑唴娌℃湁鏀跺埌鏂板垎娈靛懡浠�
+ News.info("[WCS Debug] 浠诲姟{}绛夊緟鍒嗘瓒呮椂锛屽綋鍓嶄綅缃�: {}, 鐩爣: {}", taskNo, currentStationId,
+ finalTargetStationId);
+ break;
+ }
+ // 缁х画绛夊緟鏂板垎娈靛懡浠わ紙涓嶅仛浠讳綍浜嬫儏锛屼笅涓�杞惊鐜細灏濊瘯鑾峰彇鏂板懡浠わ級
}
}
} catch (InterruptedException e) {
@@ -279,7 +315,35 @@
taskQueues.remove(taskNo);
taskLastUpdateTime.remove(taskNo);
taskRunning.remove(taskNo);
+ News.info("[WCS Debug] 浠诲姟{}鎵ц缁撴潫骞舵竻鐞嗚祫婧�", taskNo);
}
+ }
+
+ /**
+ * 鑾峰彇闃熷垪涓渶鍚庝竴涓厓绱狅紙涓嶇Щ闄わ級
+ */
+ private Integer getLastInQueue(LinkedBlockingQueue<Integer> queue) {
+ Integer last = null;
+ for (Integer item : queue) {
+ last = item;
+ }
+ return last;
+ }
+
+ /**
+ * 鑾峰彇鏄惁鍏佽妫�鏌ュ牭濉炵殑閰嶇疆
+ */
+ private boolean getFakeAllowCheckBlock() {
+ boolean fakeAllowCheckBlock = true;
+ Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
+ if (systemConfigMapObj != null) {
+ HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
+ String value = systemConfigMap.get("fakeAllowCheckBlock");
+ if (value != null && !value.equals("Y")) {
+ fakeAllowCheckBlock = false;
+ }
+ }
+ return fakeAllowCheckBlock;
}
@Override
@@ -310,7 +374,7 @@
if (command.getCommandType() == StationCommandType.WRITE_INFO) {
if (taskNo == 9998 && targetStationId == 0) {
- //鐢熸垚鍑哄簱绔欑偣浠跨湡鏁版嵁
+ // 鐢熸垚鍑哄簱绔欑偣浠跨湡鏁版嵁
generateFakeOutStationData(deviceNo, stationId);
return;
}
@@ -319,12 +383,7 @@
if (taskNo > 0 && taskNo != 9999 && taskNo != 9998 && stationId == targetStationId) {
generateStationData(deviceNo, taskNo, stationId, targetStationId);
}
-
- List<Integer> navigatePath = command.getNavigatePath();
- if (navigatePath != null && !navigatePath.isEmpty()) {
- segmentedPathCommand(command, generateBarcode);
- return;
- }
+ // 娉ㄦ剰锛歁OVE 绫诲瀷鐨勫懡浠ょ幇宸插湪 sendCommand 涓鐞嗭紝handleCommand 浠呭鐞嗛潪 MOVE 鍛戒护
}
private void generateFakeOutStationData(Integer deviceNo, Integer stationId) {
@@ -369,32 +428,7 @@
}
}
- private void segmentedPathCommand(StationCommand command, boolean generateBarcode) {
- Integer taskNo = command.getTaskNo();
- Integer finalTargetStationId = command.getTargetStaNo();
- List<Integer> path = command.getNavigatePath();
- if (path == null || path.isEmpty()) {
- return;
- }
-
- Integer currentStationId = findCurrentStationIdByTask(taskNo);
- int startIdx = 0;
- if (currentStationId != null) {
- int idx = path.indexOf(currentStationId);
- if (idx >= 0) {
- startIdx = idx;
- }
- }
-
- Integer segmentTargetStationId = path.get(path.size() - 1);
- int endIdx = path.size() - 1;
-
- boolean isFinalSegment = segmentTargetStationId.equals(finalTargetStationId);
- boolean appendMode = currentStationId != null && path.indexOf(currentStationId) >= 0;
- boolean generateBarcodeFinal = generateBarcode && isFinalSegment;
-
- stationMoveByPathIds(path, taskNo, finalTargetStationId, false, generateBarcodeFinal, startIdx, endIdx, appendMode);
- }
+ // segmentedPathCommand 鏂规硶宸插垹闄わ紝鍔熻兘宸叉暣鍚堝埌 runTaskLoop
private Integer getDeviceNoByStationId(Integer stationId) {
for (Integer devNo : deviceStatusMap.keySet()) {
@@ -426,116 +460,7 @@
return null;
}
- private boolean stationMoveByPathIds(List<Integer> stationPath, Integer taskNo, Integer targetStationId, boolean clearData, boolean generateBarcode, int startIdx, int endIdx, boolean appendMode) {
- Integer lastStationId = null;
- Integer targetStationDeviceNo = getDeviceNoByStationId(targetStationId);
- long executeTime = System.currentTimeMillis();
- int i = Math.max(0, startIdx);
- while (i < stationPath.size() && i <= endIdx) {
- if (Thread.currentThread().isInterrupted()) {
- return false;
- }
- Integer currentStationId = stationPath.get(i);
- Integer currentStationDeviceNo = getDeviceNoByStationId(currentStationId);
- if (currentStationDeviceNo == null) {
- return false;
- }
-
- Integer nextStationId = null;
- Integer nextStationDeviceNo = null;
- try {
- nextStationId = stationPath.get(i + 1);
- nextStationDeviceNo = getDeviceNoByStationId(nextStationId);
- } catch (Exception ignore) {}
-
- if (!checkTaskNoInArea(taskNo)) {
- boolean fakeAllowCheckBlock = true;
- Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
- if (systemConfigMapObj != null) {
- HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
- if (!systemConfigMap.get("fakeAllowCheckBlock").equals("Y")) {
- fakeAllowCheckBlock = false;
- }
- }
-
- if (fakeAllowCheckBlock && System.currentTimeMillis() - executeTime > 1000 * 10) {
- boolean result = runBlockStation(taskNo, currentStationId, currentStationDeviceNo, taskNo, currentStationId);
- if(!result) {
- continue;
- }
- return false;
- }
- }
-
- if (i == startIdx && !appendMode) {
- boolean result = initStationMove(taskNo, currentStationId, currentStationDeviceNo, taskNo, targetStationId, true, null);
- if (!result) {
- continue;
- }
- sleep(1000);
- if (Thread.currentThread().isInterrupted()) {
- return false;
- }
- }
-
- if (nextStationId != null && nextStationDeviceNo != null) {
- boolean result = stationMoveToNext(taskNo, currentStationId, currentStationDeviceNo, nextStationId, nextStationDeviceNo, taskNo, targetStationId);
- if (!result) {
- continue;
- }
- lastStationId = currentStationId;
- }
-
- if (currentStationId.equals(targetStationId)) {
- break;
- }
-
- i++;
- executeTime = System.currentTimeMillis();
- sleep(1000);
- if (Thread.currentThread().isInterrupted()) {
- return false;
- }
- }
-
- if (generateBarcode) {
- if (lastStationId != null && targetStationDeviceNo != null) {
- while (true) {
- if (Thread.currentThread().isInterrupted()) {
- break;
- }
- boolean result = generateStationBarcode(taskNo, targetStationId, targetStationDeviceNo);
- sleep(1000);
- if (!result) {
- continue;
- }
- break;
- }
- }
- }
-
- if (clearData) {
- sleep(10000);
- if (Thread.currentThread().isInterrupted()) {
- return true;
- }
- if (lastStationId != null && targetStationDeviceNo != null) {
- while (true) {
- if (Thread.currentThread().isInterrupted()) {
- break;
- }
- boolean result = clearStation(targetStationDeviceNo, taskNo, targetStationId);
- sleep(1000);
- if (!result) {
- continue;
- }
- break;
- }
- }
- }
-
- return true;
- }
+ // stationMoveByPathIds 鏂规硶宸插垹闄わ紝鍔熻兘宸叉暣鍚堝埌 runTaskLoop
private void sleep(long ms) {
try {
@@ -545,32 +470,40 @@
}
}
- public synchronized boolean setLockStation(Integer uuid) {
- if (LOCK_STATION == 0) {
- LOCK_STATION = uuid;
- return true;
- }else {
- if(LOCK_STATION == uuid) {
- return true;
- }
- }
- return false;
+ /**
+ * 鑾峰彇绔欑偣閿侊紝濡傛灉涓嶅瓨鍦ㄥ垯鍒涘缓
+ */
+ private ReentrantLock getStationLock(Integer stationId) {
+ return stationLocks.computeIfAbsent(stationId, k -> new ReentrantLock());
}
- public synchronized boolean releaseLockStation(Integer uuid) {
- if (LOCK_STATION != uuid) {
- return false;
+ /**
+ * 鎸夐『搴忛攣瀹氬涓珯鐐癸紙閬垮厤姝婚攣锛�
+ */
+ private void lockStations(Integer... stationIds) {
+ Integer[] sorted = Arrays.copyOf(stationIds, stationIds.length);
+ Arrays.sort(sorted);
+ for (Integer stationId : sorted) {
+ getStationLock(stationId).lock();
}
-
- LOCK_STATION = 0;
- return true;
}
- public synchronized boolean updateStationData(Integer lockTaskNo, Integer stationId, Integer deviceNo, Integer taskNo, Integer targetStaNo, Boolean isLoading, String barcode, Boolean runBlock) {
- if (LOCK_STATION != lockTaskNo) {
- return false;
+ /**
+ * 鎸夐�嗗簭瑙i攣澶氫釜绔欑偣
+ */
+ private void unlockStations(Integer... stationIds) {
+ Integer[] sorted = Arrays.copyOf(stationIds, stationIds.length);
+ Arrays.sort(sorted);
+ for (int i = sorted.length - 1; i >= 0; i--) {
+ getStationLock(sorted[i]).unlock();
}
+ }
+ /**
+ * 鏇存柊绔欑偣鏁版嵁锛堣皟鐢ㄥ墠蹇呴』宸叉寔鏈夎绔欑偣鐨勯攣锛�
+ */
+ private boolean updateStationDataInternal(Integer stationId, Integer deviceNo, Integer taskNo, Integer targetStaNo,
+ Boolean isLoading, String barcode, Boolean runBlock) {
List<ZyStationStatusEntity> statusList = deviceStatusMap.get(deviceNo);
if (statusList == null) {
return false;
@@ -605,8 +538,13 @@
return true;
}
- public synchronized boolean initStationMove(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo, Integer taskNo, Integer targetStationId, Boolean isLoading, String barcode) {
- boolean executeResult = lockExecute(lockTaskNo, () -> {
+ /**
+ * 鍒濆鍖栫珯鐐圭Щ鍔紙浣跨敤绔欑偣绾ч攣锛�
+ */
+ public boolean initStationMove(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo,
+ Integer taskNo, Integer targetStationId, Boolean isLoading, String barcode) {
+ lockStations(currentStationId);
+ try {
List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentStationDeviceNo);
if (statusList == null) {
return false;
@@ -625,25 +563,28 @@
}
}
- boolean result = updateStationData(lockTaskNo, currentStationId, currentStationDeviceNo, taskNo, targetStationId, isLoading, barcode, false);
- if (!result) {
- return false;
- }
- return true;
- });
-
- return executeResult;
+ return updateStationDataInternal(currentStationId, currentStationDeviceNo, taskNo, targetStationId,
+ isLoading, barcode, false);
+ } finally {
+ unlockStations(currentStationId);
+ }
}
- public synchronized boolean stationMoveToNext(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo, Integer nextStationId, Integer nextStationDeviceNo, Integer taskNo, Integer targetStaNo) {
- boolean executeResult = lockExecute(lockTaskNo, () -> {
+ /**
+ * 绔欑偣绉诲姩鍒颁笅涓�涓綅缃紙浣跨敤绔欑偣绾ч攣锛屾寜ID椤哄簭鑾峰彇閿侀伩鍏嶆閿侊級
+ */
+ public boolean stationMoveToNext(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo,
+ Integer nextStationId, Integer nextStationDeviceNo, Integer taskNo, Integer targetStaNo) {
+ // 鍚屾椂閿佸畾褰撳墠绔欑偣鍜屼笅涓�涓珯鐐癸紙鎸塈D椤哄簭锛岄伩鍏嶆閿侊級
+ lockStations(currentStationId, nextStationId);
+ try {
List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentStationDeviceNo);
if (statusList == null) {
return false;
}
List<ZyStationStatusEntity> nextStatusList = deviceStatusMap.get(nextStationDeviceNo);
- if (statusList == null) {
+ if (nextStatusList == null) {
return false;
}
@@ -661,23 +602,31 @@
return false;
}
- boolean result = updateStationData(lockTaskNo, nextStationId, nextStationDeviceNo, taskNo, targetStaNo, true, null, false);
+ boolean result = updateStationDataInternal(nextStationId, nextStationDeviceNo, taskNo, targetStaNo, true,
+ null, false);
if (!result) {
return false;
}
- boolean result2 = updateStationData(lockTaskNo, currentStationId, currentStationDeviceNo, 0, 0, false, "", false);
+ boolean result2 = updateStationDataInternal(currentStationId, currentStationDeviceNo, 0, 0, false, "",
+ false);
if (!result2) {
return false;
}
return true;
- });
- return executeResult;
+ } finally {
+ unlockStations(currentStationId, nextStationId);
+ }
}
- public synchronized boolean generateStationBarcode(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo) {
- boolean executeResult = lockExecute(lockTaskNo, () -> {
+ /**
+ * 鐢熸垚绔欑偣鏉$爜锛堜娇鐢ㄧ珯鐐圭骇閿侊級
+ */
+ public boolean generateStationBarcode(Integer lockTaskNo, Integer currentStationId,
+ Integer currentStationDeviceNo) {
+ lockStations(currentStationId);
+ try {
List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentStationDeviceNo);
if (statusList == null) {
return false;
@@ -693,20 +642,21 @@
Random random = new Random();
String barcodeTime = String.valueOf(System.currentTimeMillis());
- String barcode = String.valueOf(random.nextInt(10)) + String.valueOf(random.nextInt(10)) + barcodeTime.substring(7);
+ String barcode = String.valueOf(random.nextInt(10)) + String.valueOf(random.nextInt(10))
+ + barcodeTime.substring(7);
- boolean result = updateStationData(lockTaskNo, currentStationId, currentStationDeviceNo, null, null, null, barcode, null);
- if (!result) {
- return false;
- }
- return true;
- });
-
- return executeResult;
+ return updateStationDataInternal(currentStationId, currentStationDeviceNo, null, null, null, barcode, null);
+ } finally {
+ unlockStations(currentStationId);
+ }
}
- public synchronized boolean clearStation(Integer deviceNo, Integer lockTaskNo, Integer currentStationId) {
- boolean executeResult = lockExecute(lockTaskNo, () -> {
+ /**
+ * 娓呴櫎绔欑偣鏁版嵁锛堜娇鐢ㄧ珯鐐圭骇閿侊級
+ */
+ public boolean clearStation(Integer deviceNo, Integer lockTaskNo, Integer currentStationId) {
+ lockStations(currentStationId);
+ try {
List<ZyStationStatusEntity> statusList = deviceStatusMap.get(deviceNo);
if (statusList == null) {
return false;
@@ -719,18 +669,19 @@
return false;
}
- boolean result = updateStationData(deviceNo, lockTaskNo, currentStationId, 0, 0, false, "", false);
- if (!result) {
- return false;
- }
- return true;
- });
-
- return executeResult;
+ return updateStationDataInternal(currentStationId, deviceNo, 0, 0, false, "", false);
+ } finally {
+ unlockStations(currentStationId);
+ }
}
- public synchronized boolean runBlockStation(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo, Integer taskNo, Integer blockStationId) {
- boolean executeResult = lockExecute(lockTaskNo, () -> {
+ /**
+ * 鏍囪绔欑偣鍫靛锛堜娇鐢ㄧ珯鐐圭骇閿侊級
+ */
+ public boolean runBlockStation(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo,
+ Integer taskNo, Integer blockStationId) {
+ lockStations(currentStationId);
+ try {
List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentStationDeviceNo);
if (statusList == null) {
return false;
@@ -743,24 +694,38 @@
return false;
}
- boolean result = updateStationData(lockTaskNo, currentStationId, currentStationDeviceNo, taskNo, blockStationId, true, "", true);
- if (!result) {
- return false;
- }
- return true;
- });
-
- return executeResult;
+ return updateStationDataInternal(currentStationId, currentStationDeviceNo, taskNo, blockStationId, true, "",
+ true);
+ } finally {
+ unlockStations(currentStationId);
+ }
}
- public boolean lockExecute(Integer taskNo, Supplier<Boolean> function) {
- if (!setLockStation(taskNo)) {
- return false;
- }
+ /**
+ * 娓呴櫎绔欑偣鍫靛鏍囪锛堝牭濉炴仮澶嶆椂浣跨敤锛�
+ */
+ public void clearRunBlock(Integer stationId, Integer deviceNo) {
+ lockStations(stationId);
+ try {
+ List<ZyStationStatusEntity> statusList = deviceStatusMap.get(deviceNo);
+ if (statusList == null) {
+ return;
+ }
- boolean result = function.get();
- releaseLockStation(taskNo);
- return result;
+ ZyStationStatusEntity currentStatus = statusList.stream()
+ .filter(item -> item.getStationId().equals(stationId)).findFirst().orElse(null);
+
+ if (currentStatus == null) {
+ return;
+ }
+
+ if (currentStatus.isRunBlock()) {
+ currentStatus.setRunBlock(false);
+ News.info("[WCS Debug] 绔欑偣{}鍫靛鏍囪宸叉竻闄�", stationId);
+ }
+ } finally {
+ unlockStations(stationId);
+ }
}
private boolean checkTaskNoInArea(Integer taskNo) {
@@ -773,7 +738,7 @@
Integer start = data.getInteger("start");
Integer end = data.getInteger("end");
- if(taskNo >= start && taskNo <= end) {
+ if (taskNo >= start && taskNo <= end) {
return true;
}
--
Gitblit v1.9.1