diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketFreqConverterDevService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketFreqConverterDevService.java index 889274b7..4d68f613 100644 --- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketFreqConverterDevService.java +++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketFreqConverterDevService.java @@ -349,8 +349,28 @@ public class SocketFreqConverterDevService { } public void cleanup(String devTag) { + String currentUserId = this.userId; FormalTestManager.freqConverterDevStep = null; FormalTestManager.isRemoveSocket = true; SocketManager.removeUser(devTag); + clearStateIfStopped(currentUserId); + this.userId = null; + this.monitorId = null; + } + + /** + * 如果设备已停止,则清除共享的运行时状态 + * @param currentUserId 当前用户ID + */ + private void clearStateIfStopped(String currentUserId) { + if (StrUtil.isBlank(currentUserId)) { + FormalTestManager.clearFreqConverterRuntimeState(); + return; + } + String freqConverterTag = currentUserId + CnSocketUtil.FREQ_CONVERTER_TAG; + // 避免过早把 freqConverterTableSuffix 等全局值清掉,造成变频器无法使用全局变量 + if (!SocketManager.isChannelActive(freqConverterTag)) { + FormalTestManager.clearFreqConverterRuntimeState(); + } } } diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketFreqConverterService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketFreqConverterService.java index dc356f89..ff5c65dd 100644 --- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketFreqConverterService.java +++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketFreqConverterService.java @@ -9,6 +9,7 @@ import com.njcn.gather.detection.pojo.dto.FreqConverterRespDTO; import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum; import com.njcn.gather.detection.pojo.vo.SocketDataMsg; import com.njcn.gather.detection.pojo.vo.SocketMsg; +import com.njcn.gather.detection.util.socket.CnSocketUtil; import com.njcn.gather.detection.util.socket.FormalTestManager; import com.njcn.gather.detection.util.socket.SocketManager; import com.njcn.gather.detection.util.socket.cilent.NettyClient; @@ -168,11 +169,15 @@ public class SocketFreqConverterService { } public void cleanup(String converterChannelTag) { + String currentUserId = this.userId; clearScheduleTask(); FormalTestManager.freqConverterStep = null; // FormalTestManager.stopFlag = false; FormalTestManager.isRemoveSocket = true; SocketManager.removeUser(converterChannelTag); + updateCurrentTestStatus(); + clearStateIfStopped(currentUserId); + this.userId = null; } private void handleInitSerial(String converterChannelTag, FreqConverterRespDTO respDTO) { @@ -253,6 +258,28 @@ public class SocketFreqConverterService { } } + private void updateCurrentTestStatus() { + if (StrUtil.isNotBlank(FormalTestManager.currentFreqConverterId)) { + pqFreqConverterConfigService.updateTestStatus(FormalTestManager.currentFreqConverterId, 1); + } + } + + /** + * 如果变频器已停止,则清除共享的运行时状态 + * @param currentUserId 当前用户ID + */ + private void clearStateIfStopped(String currentUserId) { + if (StrUtil.isBlank(currentUserId)) { + FormalTestManager.clearFreqConverterRuntimeState(); + return; + } + String devTag = currentUserId + CnSocketUtil.DEV_TAG; + // 避免过早把 freqConverterTableSuffix 等全局值清掉,造成设备无法使用全局变量 + if (!SocketManager.isChannelActive(devTag)) { + FormalTestManager.clearFreqConverterRuntimeState(); + } + } + private void consumePendingDipTasks(FreqConverterStatus freqConverterStatus) { if (FormalTestManager.pendingDipTaskMap.isEmpty()) { return; diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/FormalTestManager.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/FormalTestManager.java index 3595d324..e773b619 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/FormalTestManager.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/FormalTestManager.java @@ -226,6 +226,17 @@ public class FormalTestManager { */ public static boolean isXu; + /** + * 清理变频器耐受实验运行态数据 + */ + public static void clearFreqConverterRuntimeState() { + freqConverterStep = null; + freqConverterDevStep = null; + currentFreqConverterId = null; + freqConverterTableSuffix = null; + pendingDipTaskMap.clear(); + } + @Data public static class PendingDipTask { private PqDipData pqDipData;