From 5ef79791cf93200c938b09dbd2461a7775391825 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期日, 05 四月 2026 23:59:17 +0800
Subject: [PATCH] #线程日志
---
src/main/java/com/zy/core/network/ZyStationConnectDriver.java | 61 ++++++++++++++++++++++++++----
1 files changed, 52 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/zy/core/network/ZyStationConnectDriver.java b/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
index 2bc600c..e2fac59 100644
--- a/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
+++ b/src/main/java/com/zy/core/network/ZyStationConnectDriver.java
@@ -9,7 +9,6 @@
import com.zy.core.network.api.ZyStationConnectApi;
import com.zy.core.network.entity.ZyStationStatusEntity;
import java.util.List;
-import com.zy.core.network.fake.ZyStationFakeConnect;
import com.zy.core.network.fake.ZyStationFakeSegConnect;
import com.zy.core.network.fake.ZyStationV4FakeSegConnect;
import com.zy.core.network.real.ZyStationRealConnect;
@@ -28,9 +27,10 @@
@Slf4j
public class ZyStationConnectDriver implements ThreadHandler {
- private static final ZyStationFakeConnect zyStationFakeConnect = new ZyStationFakeConnect();
private static final ZyStationFakeSegConnect zyStationFakeSegConnect = new ZyStationFakeSegConnect();
private static final ZyStationV4FakeSegConnect zyStationV4FakeSegConnect = new ZyStationV4FakeSegConnect();
+ private static final long SEND_LOCK_WARN_MS = 3_000L;
+ private static final long SEND_COST_WARN_MS = 5_000L;
private volatile boolean connected = false;
private volatile boolean connecting = false;
@@ -38,6 +38,7 @@
private RedisUtil redisUtil;
private volatile ZyStationConnectApi zyStationConnectApi;
private volatile boolean closed = false;
+ private volatile boolean fakeConfigUnsupported = false;
private ScheduledExecutorService executor;
private final Object connectLock = new Object();
@@ -57,6 +58,9 @@
if (closed) {
return false;
}
+ if (fakeConfigUnsupported) {
+ return false;
+ }
if (connected && zyStationConnectApi != null) {
return true;
}
@@ -67,7 +71,8 @@
if (deviceConfig.getFake() == 0) {
if ("ZyStationV3Thread".equals(deviceConfig.getThreadImpl())) {
connectApi = new ZyStationV3RealConnect(deviceConfig, redisUtil);
- } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())) {
+ } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())
+ || "ZyStationV5Thread".equals(deviceConfig.getThreadImpl())) {
connectApi = new ZyStationV4RealConnect(deviceConfig, redisUtil);
} else {
connectApi = new ZyStationRealConnect(deviceConfig, redisUtil);
@@ -76,12 +81,16 @@
if ("ZyStationV3Thread".equals(deviceConfig.getThreadImpl())) {
zyStationFakeSegConnect.addFakeConnect(deviceConfig, redisUtil);
connectApi = zyStationFakeSegConnect;
- } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())) {
+ } else if ("ZyStationV4Thread".equals(deviceConfig.getThreadImpl())
+ || "ZyStationV5Thread".equals(deviceConfig.getThreadImpl())) {
zyStationV4FakeSegConnect.addFakeConnect(deviceConfig, redisUtil);
connectApi = zyStationV4FakeSegConnect;
} else {
- zyStationFakeConnect.addFakeConnect(deviceConfig, redisUtil);
- connectApi = zyStationFakeConnect;
+ fakeConfigUnsupported = true;
+ zyStationConnectApi = null;
+ log.error("鏃х増杈撻�佺珯 fake 宸茬Щ闄わ紝deviceNo={}, threadImpl={}, 璇峰垏鎹㈠埌 ZyStationV3Thread銆乑yStationV4Thread 鎴� ZyStationV5Thread",
+ deviceConfig.getDeviceNo(), deviceConfig.getThreadImpl());
+ return false;
}
}
@@ -153,9 +162,12 @@
if (!connected || connecting || connectApi == null) {
return new CommandResponse(false, "璁惧鏈繛鎺ワ紝鍛戒护涓嬪彂澶辫触");
}
+ long lockWaitStart = System.currentTimeMillis();
+ int waitRounds = 0;
while (true) {
Object lock = redisUtil.get(RedisKeyType.STATION_EXECUTE_COMMAND_LOCK.key);
if(lock != null) {
+ waitRounds++;
try {
Thread.sleep(500);
}catch (Exception e) {
@@ -166,9 +178,31 @@
break;
}
}
- CommandResponse commandResponse = connectApi.sendCommand(deviceConfig.getDeviceNo(), command);
- redisUtil.del(RedisKeyType.STATION_EXECUTE_COMMAND_LOCK.key);
- return commandResponse;
+ long lockWaitCost = System.currentTimeMillis() - lockWaitStart;
+ if (lockWaitCost >= SEND_LOCK_WARN_MS) {
+ log.warn("杈撻�佸懡浠ょ瓑寰呭叏灞�鍙戦�侀攣瓒呮椂锛宒eviceNo={}, taskNo={}, stationId={}, targetStaNo={}, waitMs={}, waitRounds={}",
+ deviceConfig == null ? null : deviceConfig.getDeviceNo(),
+ command == null ? null : command.getTaskNo(),
+ command == null ? null : command.getStationId(),
+ command == null ? null : command.getTargetStaNo(),
+ lockWaitCost,
+ waitRounds);
+ }
+ long sendStart = System.currentTimeMillis();
+ try {
+ return connectApi.sendCommand(deviceConfig.getDeviceNo(), command);
+ } finally {
+ redisUtil.del(RedisKeyType.STATION_EXECUTE_COMMAND_LOCK.key);
+ long sendCostMs = System.currentTimeMillis() - sendStart;
+ if (sendCostMs >= SEND_COST_WARN_MS) {
+ log.warn("杈撻�佸懡浠ゅ簳灞傚彂閫佽�楁椂杩囬暱锛宒eviceNo={}, taskNo={}, stationId={}, targetStaNo={}, sendCostMs={}",
+ deviceConfig == null ? null : deviceConfig.getDeviceNo(),
+ command == null ? null : command.getTaskNo(),
+ command == null ? null : command.getStationId(),
+ command == null ? null : command.getTargetStaNo(),
+ sendCostMs);
+ }
+ }
}
public CommandResponse sendOriginCommand(String address, short[] data) {
@@ -179,6 +213,15 @@
return connectApi.sendOriginCommand(address, data);
}
+ public boolean clearTaskBufferSlot(Integer stationId, Integer slotIdx) {
+ ZyStationConnectApi connectApi = zyStationConnectApi;
+ if (!connected || connecting || connectApi == null) {
+ return false;
+ }
+ CommandResponse response = connectApi.clearTaskBufferSlot(deviceConfig.getDeviceNo(), stationId, slotIdx);
+ return response != null && Boolean.TRUE.equals(response.getResult());
+ }
+
public byte[] readOriginCommand(String address, int length) {
ZyStationConnectApi connectApi = zyStationConnectApi;
if (!connected || connecting || connectApi == null) {
--
Gitblit v1.9.1