From ef757c52eab6783b2ea7826f5e87ae7ba0a7753c Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Fri, 10 Apr 2026 15:13:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PreDetectionController.java | 9 +-- .../SocketFreqConverterDevService.java | 53 ++++++++-------- .../handler/SocketFreqConverterService.java | 57 +++++++++--------- .../service/PreDetectionService.java | 9 +-- .../service/impl/PreDetectionServiceImpl.java | 20 ++++--- .../detection/util/socket/CnSocketUtil.java | 2 + .../util/socket/cilent/NettyClient.java | 60 +++++++++---------- .../NettyFreqConverterClientHandler.java | 36 +++++------ .../NettyFreqConverterDevClientHandler.java | 24 ++++---- .../pojo/po/FreqConverterStatus.java | 5 -- .../impl/FreqConverterServiceImpl.java | 17 +----- 11 files changed, 135 insertions(+), 157 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/detection/controller/PreDetectionController.java b/detection/src/main/java/com/njcn/gather/detection/controller/PreDetectionController.java index 39117106..4cd63a77 100644 --- a/detection/src/main/java/com/njcn/gather/detection/controller/PreDetectionController.java +++ b/detection/src/main/java/com/njcn/gather/detection/controller/PreDetectionController.java @@ -10,6 +10,7 @@ import com.njcn.gather.detection.pojo.param.ContrastDetectionParam; import com.njcn.gather.detection.pojo.param.PreDetectionParam; import com.njcn.gather.detection.pojo.param.SimulateDetectionParam; import com.njcn.gather.detection.service.PreDetectionService; +import com.njcn.gather.detection.util.socket.CnSocketUtil; import com.njcn.web.controller.BaseController; import com.njcn.web.utils.HttpResultUtil; import io.swagger.annotations.Api; @@ -180,22 +181,22 @@ public class PreDetectionController extends BaseController { @OperateInfo(info = LogEnum.SYSTEM_COMMON) @GetMapping("/startFreqConverter") @ApiOperation("开启变频器测试") - public HttpResult startFreqConverter(@RequestParam("converterId") String converterId, @RequestParam("monitorId") String monitorId) { + public HttpResult startFreqConverter(@RequestParam("loginName") String loginName, @RequestParam("converterId") String converterId, @RequestParam("monitorId") String monitorId) { String methodDescribe = getMethodDescribe("startFreqConverter"); LogUtil.njcnDebug(log, "{}", methodDescribe); - preDetectionService.startFreqConverter(converterId, monitorId); + preDetectionService.startFreqConverter(loginName, converterId, monitorId); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); } @OperateInfo(info = LogEnum.SYSTEM_COMMON) @GetMapping("/stopFreqConverter") @ApiOperation("关闭变频器测试") - public HttpResult stopFreqConverter(@RequestParam("converterId") String converterId, @RequestParam("monitorId") String monitorId) { + public HttpResult stopFreqConverter(@RequestParam("loginName") String loginName) { String methodDescribe = getMethodDescribe("stopFreqConverter"); LogUtil.njcnDebug(log, "{}", methodDescribe); - preDetectionService.stopFreqConverter(converterId, monitorId); + preDetectionService.stopFreqConverter(loginName + CnSocketUtil.FREQ_CONVERTER_TAG, loginName + CnSocketUtil.DEV_TAG); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); } } 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 19caeb6f..9765264b 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 @@ -43,18 +43,18 @@ public class SocketFreqConverterDevService { /** * 连接设备Socket * - * @param monitorId 监测点Id + * @param devTag 设备Channel唯一标识符 */ - public void connectSocket(String monitorId) { - if (SocketManager.isChannelActive(monitorId)) { + public void connectSocket(String devTag) { + if (SocketManager.isChannelActive(devTag)) { return; } String ip = socketConnectionConfig.getDevice().getIp(); Integer port = socketConnectionConfig.getDevice().getPort(); - NettyFreqConverterDevClientHandler handler = new NettyFreqConverterDevClientHandler(monitorId, this); + NettyFreqConverterDevClientHandler handler = new NettyFreqConverterDevClientHandler(devTag, this); CompletableFuture.runAsync(() -> { - NettyClient.commonConnect(ip, port, monitorId, handler); + NettyClient.commonConnect(ip, port, devTag, handler); }); } @@ -66,7 +66,7 @@ public class SocketFreqConverterDevService { /** * 连接设备 */ - public void connectionDev(String converterId, String monitorId) { + public void connectionDev(String devTag, String converterId, String monitorId) { this.init(converterId, monitorId); String payload = buildSingleMonitorPayload(monitorId); @@ -78,40 +78,40 @@ public class SocketFreqConverterDevService { socketMsg.setRequestId(SourceOperateCodeEnum.YJC_SBTXJY.getValue()); socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_03.getValue()); socketMsg.setData(payload); - SocketManager.sendMsg(monitorId, JSON.toJSONString(socketMsg)); + SocketManager.sendMsg(devTag, JSON.toJSONString(socketMsg)); FormalTestManager.freqConverterDevStep = SourceOperateCodeEnum.YJC_SBTXJY; } - public void handleRead(String monitorId, String msg) { + public void handleRead(String devTag, String msg) { SocketDataMsg socketDataMsg = MsgUtil.socketDataMsg(msg); switch (FormalTestManager.freqConverterDevStep) { case YJC_SBTXJY: - handleYjcSbtxjy(monitorId, socketDataMsg); + handleYjcSbtxjy(devTag, socketDataMsg); break; case FORMAL_REAL: - handleFormalReal(monitorId, socketDataMsg); + handleFormalReal(devTag, socketDataMsg); break; case QUITE: - handleQuit(monitorId, socketDataMsg); + handleQuit(devTag, socketDataMsg); break; } } - private void handleYjcSbtxjy(String monitorId, SocketDataMsg socketDataMsg) { + private void handleYjcSbtxjy(String devTag, SocketDataMsg socketDataMsg) { SourceResponseCodeEnum responseCodeEnum = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode()); switch (Objects.requireNonNull(responseCodeEnum)) { case SUCCESS: - this.sendGetDipDataMsg(monitorId); + this.sendGetDipDataMsg(devTag); FormalTestManager.freqConverterDevStep = SourceOperateCodeEnum.FORMAL_REAL; break; default: - log.info("设备响应异常,monitorId={}, operateCode={}, code={}, data={}", monitorId, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData()); + log.info("设备响应异常,devTag={}, operateCode={}, code={}, data={}", devTag, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData()); break; } } - private void handleFormalReal(String monitorId, SocketDataMsg socketDataMsg) { + private void handleFormalReal(String devTag, SocketDataMsg socketDataMsg) { SourceResponseCodeEnum responseCodeEnum = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode()); switch (responseCodeEnum) { @@ -131,27 +131,27 @@ public class SocketFreqConverterDevService { case DATA_RESOLVE: case NO_INIT_DEV: default: - log.info("设备响应异常,monitorId={}, operateCode={}, code={}, data={}", monitorId, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData()); + log.info("设备响应异常,devTag={}, operateCode={}, code={}, data={}", devTag, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData()); break; } } - private void handleQuit(String monitorId, SocketDataMsg socketDataMsg) { + private void handleQuit(String devTag, SocketDataMsg socketDataMsg) { SourceResponseCodeEnum responseCodeEnum = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode()); switch (responseCodeEnum) { case SUCCESS: - cleanup(monitorId, true); + cleanup(devTag, true); break; default: - log.warn("设备关闭响应失败,monitorId={}, operateCode={}, code={}, data={}", monitorId, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData()); + log.warn("设备关闭响应失败,devTag={}, operateCode={}, code={}, data={}", devTag, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData()); break; } } - public void stopTest(String converterId, String monitorId) { + public void stopTest(String converterTag, String devTag) { FormalTestManager.freqConverterDevStep = SourceOperateCodeEnum.QUITE; - sendQuitMsg(monitorId, SourceOperateCodeEnum.QUIT_INIT_03); + sendQuitMsg(devTag, SourceOperateCodeEnum.QUIT_INIT_03); } @@ -205,12 +205,11 @@ public class SocketFreqConverterDevService { SocketManager.sendMsg(monitorId, JSON.toJSONString(socketMsg)); } - private void sendQuitMsg(String monitorId, SourceOperateCodeEnum operateCodeEnum) { + private void sendQuitMsg(String devTag, SourceOperateCodeEnum operateCodeEnum) { SocketMsg socketMsg = new SocketMsg<>(); socketMsg.setRequestId(SourceOperateCodeEnum.QUITE.getValue()); socketMsg.setOperateCode(operateCodeEnum.getValue()); - SocketManager.sendMsg(monitorId, JSON.toJSONString(socketMsg)); - log.info("已向设备发送关闭指令,monitorId={}, operateCode={}", monitorId, operateCodeEnum.getValue()); + SocketManager.sendMsg(devTag, JSON.toJSONString(socketMsg)); } private void saveDipData(DevData devData) { @@ -257,12 +256,12 @@ public class SocketFreqConverterDevService { return listDTO.getT(); } - public void cleanup(String monitorId, boolean removeSocket) { + public void cleanup(String devTag, boolean removeSocket) { FormalTestManager.freqConverterDevStep = null; if (removeSocket) { - SocketManager.removeUser(monitorId); + SocketManager.removeUser(devTag); } else { - SocketManager.clearUser(monitorId); + SocketManager.clearUser(devTag); } } } 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 b91da325..e6370e1f 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 @@ -45,19 +45,19 @@ public class SocketFreqConverterService { /** * 连接变频器Socket * - * @param converterId 变频器id + * @param converterChannelTag 变频器Channel唯一标识符 */ - public void connectSocket(String converterId) { - if (SocketManager.isChannelActive(converterId)) { + public void connectSocket(String converterChannelTag) { + if (SocketManager.isChannelActive(converterChannelTag)) { return; } String ip = socketConnectionConfig.getFreqConverter().getIp(); Integer port = socketConnectionConfig.getFreqConverter().getPort(); - NettyFreqConverterClientHandler handler = new NettyFreqConverterClientHandler(converterId, this); + NettyFreqConverterClientHandler handler = new NettyFreqConverterClientHandler(converterChannelTag, this); CompletableFuture.runAsync(() -> { - NettyClient.commonConnect(ip, port, converterId, handler); + NettyClient.commonConnect(ip, port, converterChannelTag, handler); }); } @@ -70,7 +70,7 @@ public class SocketFreqConverterService { /** * 连接变频器 */ - public void connectionFreqConverter(String converterId, String monitorId) { + public void connectionFreqConverter(String freqConverterTag, String converterId, String monitorId) { this.init(converterId, monitorId); SocketMsg> socketMsg = new SocketMsg<>(); @@ -90,39 +90,39 @@ public class SocketFreqConverterService { map.put("timeoutMs", freqConverterConfig.getTimeoutMs()); socketMsg.setData(map); - SocketManager.sendMsg(converterId, JSON.toJSONString(socketMsg)); + SocketManager.sendMsg(freqConverterTag, JSON.toJSONString(socketMsg)); FormalTestManager.freqConverterStep = SourceOperateCodeEnum.CMD_INIT_SERIAL; } - public void handleRead(String converterId, String msg) { + public void handleRead(String converterChannelTag, String msg) { FreqConverterRespDTO respDTO = JSON.parseObject(msg, FreqConverterRespDTO.class); switch (FormalTestManager.freqConverterStep) { case CMD_PING: - handlePing(converterId, respDTO); + handlePing(converterChannelTag, respDTO); break; case CMD_INIT_SERIAL: - handleInitSerial(converterId, respDTO); + handleInitSerial(converterChannelTag, respDTO); break; case CMD_GET_SERIAL_CONFIG: - handleGetSerialConfig(converterId, respDTO); + handleGetSerialConfig(converterChannelTag, respDTO); break; case CMD_GET_DEVICE_STATUS: - handleGetDeviceStatus(converterId, respDTO); + handleGetDeviceStatus(converterChannelTag, respDTO); break; case CMD_CLOSE_SERIAL: - handleCloseSerial(converterId, respDTO); + handleCloseSerial(converterChannelTag, respDTO); break; } } - public void stopTest(String converterId, String monitorId) { + public void stopTest(String converterTag, String devTag) { FormalTestManager.freqConverterStep = SourceOperateCodeEnum.CMD_CLOSE_SERIAL; - this.sendClose(converterId); + this.sendClose(converterTag); } - private void handlePing(String converterId, FreqConverterRespDTO respDTO) { + private void handlePing(String converterChannelTag, FreqConverterRespDTO respDTO) { if (respDTO.getCode() == 0 && respDTO.getSuccess() && respDTO.getMessage().equals("pong")) { return; } else { @@ -130,7 +130,7 @@ public class SocketFreqConverterService { } } - private void handleInitSerial(String converterId, FreqConverterRespDTO respDTO) { + private void handleInitSerial(String converterChannelTag, FreqConverterRespDTO respDTO) { if (respDTO.getCode() == 0 && respDTO.getSuccess()) { FormalTestManager.freqConverterStep = SourceOperateCodeEnum.CMD_GET_DEVICE_STATUS; @@ -138,23 +138,22 @@ public class SocketFreqConverterService { if (Objects.isNull(FormalTestManager.scheduler)) { FormalTestManager.scheduler = Executors.newScheduledThreadPool(1); FormalTestManager.scheduledFuture = FormalTestManager.scheduler.scheduleAtFixedRate(() -> { - this.sendGetDeviceStatusMsg(converterId); + this.sendGetDeviceStatusMsg(converterChannelTag); }, 0l, 200l, TimeUnit.MILLISECONDS); } } } - private void handleGetSerialConfig(String converterId, FreqConverterRespDTO respDTO) { + private void handleGetSerialConfig(String converterChannelTag, FreqConverterRespDTO respDTO) { } - private void handleGetDeviceStatus(String converterId, FreqConverterRespDTO respDTO) { + private void handleGetDeviceStatus(String converterChannelTag, FreqConverterRespDTO respDTO) { JSONObject obj = JSONUtil.parseObj(respDTO.getData().toString()); String timestamp = (String) obj.get("Timestamp"); timestamp = timestamp.replace("+08:00", StrUtil.EMPTY); obj.set("Timestamp", timestamp); FreqConverterStatus freqConverterStatus = JSON.parseObject(obj.toString(), FreqConverterStatus.class); - freqConverterStatus.setConverterId(converterId); // 变频器故障中,移除这段时期内的设备数据 if (freqConverterStatus.getStatusWord1() == 4) { FormalTestManager.stopFlag = true; @@ -164,9 +163,9 @@ public class SocketFreqConverterService { freqConverterService.saveFreqConverterStatus(freqConverterStatus); } - private void handleCloseSerial(String converterId, FreqConverterRespDTO respDTO) { + private void handleCloseSerial(String converterChannelTag, FreqConverterRespDTO respDTO) { if (respDTO.getCode() == 0 && respDTO.getSuccess()) { - cleanup(converterId, true); + cleanup(converterChannelTag, true); } } @@ -182,7 +181,7 @@ public class SocketFreqConverterService { SocketManager.sendMsg(converterId, JSON.toJSONString(socketMsg)); } - private void sendClose(String converterId) { + private void sendClose(String converterTag) { SocketMsg> socketMsg = new SocketMsg<>(); socketMsg.setOperateCode(SourceOperateCodeEnum.CMD_CLOSE_SERIAL.getValue()); String requestId = IdUtil.fastSimpleUUID(); @@ -190,18 +189,18 @@ public class SocketFreqConverterService { Map map = new HashMap<>(); socketMsg.setData(map); - SocketManager.sendMsg(converterId, JSON.toJSONString(socketMsg)); + SocketManager.sendMsg(converterTag, JSON.toJSONString(socketMsg)); } - public void cleanup(String converterId, boolean removeSocket) { + public void cleanup(String converterChannelTag, boolean removeSocket) { clearScheduleTask(); FormalTestManager.freqConverterStep = null; FormalTestManager.stopFlag = false; - FormalTestManager.pairsIpMap.remove(converterId); + FormalTestManager.pairsIpMap.clear(); if (removeSocket) { - SocketManager.removeUser(converterId); + SocketManager.removeUser(converterChannelTag); } else { - SocketManager.clearUser(converterId); + SocketManager.clearUser(converterChannelTag); } } diff --git a/detection/src/main/java/com/njcn/gather/detection/service/PreDetectionService.java b/detection/src/main/java/com/njcn/gather/detection/service/PreDetectionService.java index 269fa9a2..e37bed66 100644 --- a/detection/src/main/java/com/njcn/gather/detection/service/PreDetectionService.java +++ b/detection/src/main/java/com/njcn/gather/detection/service/PreDetectionService.java @@ -3,10 +3,6 @@ package com.njcn.gather.detection.service; import com.njcn.gather.detection.pojo.param.ContrastDetectionParam; import com.njcn.gather.detection.pojo.param.PreDetectionParam; import com.njcn.gather.detection.pojo.param.SimulateDetectionParam; -import org.springframework.web.bind.annotation.RequestParam; - -import java.util.List; -import java.util.Map; /** @@ -56,7 +52,6 @@ public interface PreDetectionService { void closeTestSimulate(SimulateDetectionParam param); /** - * * @param param */ void startContrastTest(ContrastDetectionParam param); @@ -70,7 +65,7 @@ public interface PreDetectionService { void startCoefficient(); - void startFreqConverter(String converterId, String monitorId); + void startFreqConverter(String name, String converterId, String monitorId); - void stopFreqConverter(String converterId,String monitorId); + void stopFreqConverter(String converterId, String monitorId); } diff --git a/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java b/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java index b654a15b..8a5f90b5 100644 --- a/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java @@ -394,14 +394,16 @@ public class PreDetectionServiceImpl implements PreDetectionService { } @Override - public void startFreqConverter(String converterId, String monitorId) { - socketFreqConverterService.connectSocket(converterId); - socketFreqConverterDevService.connectSocket(monitorId); + public void startFreqConverter(String loginName, String converterId, String monitorId) { + String freqConverterTag = loginName + CnSocketUtil.FREQ_CONVERTER_TAG; + String devTag = loginName + CnSocketUtil.DEV_TAG; + socketFreqConverterService.connectSocket(freqConverterTag); + socketFreqConverterDevService.connectSocket(devTag); while (true) { - if (SocketManager.isChannelActive(converterId) && SocketManager.isChannelActive(monitorId)) { - socketFreqConverterService.connectionFreqConverter(converterId, monitorId); - socketFreqConverterDevService.connectionDev(converterId, monitorId); + if (SocketManager.isChannelActive(freqConverterTag) && SocketManager.isChannelActive(devTag)) { + socketFreqConverterService.connectionFreqConverter(freqConverterTag, converterId, monitorId); + socketFreqConverterDevService.connectionDev(devTag, converterId, monitorId); break; } } @@ -409,9 +411,9 @@ public class PreDetectionServiceImpl implements PreDetectionService { } @Override - public void stopFreqConverter(String converterId, String monitorId) { - socketFreqConverterService.stopTest(converterId, monitorId); - socketFreqConverterDevService.stopTest(converterId, monitorId); + public void stopFreqConverter(String converterTag, String devTag) { + socketFreqConverterService.stopTest(converterTag, devTag); + socketFreqConverterDevService.stopTest(converterTag, converterTag); } /** diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/CnSocketUtil.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/CnSocketUtil.java index 090a7da6..ff66e737 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/CnSocketUtil.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/CnSocketUtil.java @@ -14,6 +14,8 @@ import com.njcn.gather.detection.util.socket.websocket.WebServiceManager; */ public class CnSocketUtil { + public final static String FREQ_CONVERTER_TAG="_FreqConverter"; + public final static String DEV_TAG = "_Dev"; public final static String CONTRAST_DEV_TAG = "_Contrast_Dev"; diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java index 9c3c4d70..b421deb8 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java @@ -59,15 +59,15 @@ public class NettyClient { /** * 静态方法:智能连接变频器设备(兼容性包装) * - * @param ip IP地址 - * @param port 端口号 - * @param id 唯一标识id - * @param handler 变频器处理器 + * @param ip IP地址 + * @param port 端口号 + * @param ChannelId Channel唯一标识 + * @param handler 变频器处理器 */ - public static void commonConnect(String ip, Integer port, String id, + public static void commonConnect(String ip, Integer port, String ChannelId, SimpleChannelInboundHandler handler) { if (instance != null) { - instance.executeCommonConnect(ip, port, id, handler); + instance.executeCommonConnect(ip, port, ChannelId, handler); } else { log.error("NettyClient未初始化,无法创建连接"); } @@ -76,36 +76,36 @@ public class NettyClient { /** * 执行变频器Socket连接建立流程 * - * @param ip 目标服务器IP地址 - * @param port 目标服务器端口号 - * @param id 唯一标识id - * @param handler 变频器业务处理器 + * @param ip 目标服务器IP地址 + * @param port 目标服务器端口号 + * @param ChannelId Channel唯一标识id + * @param handler 变频器业务处理器 */ private static void executeCommonConnect(String ip, Integer port, - String id, + String ChannelId, SimpleChannelInboundHandler handler) { NioEventLoopGroup group = createEventLoopGroup(); try { Bootstrap bootstrap = configureBootstrap(group); - ChannelInitializer initializer = createCommonChannelInitializer(id, handler); + ChannelInitializer initializer = createCommonChannelInitializer(ChannelId, handler); bootstrap.handler(initializer); ChannelFuture channelFuture = bootstrap.connect(ip, port).sync(); - handleCommonConnectionResult(channelFuture, id, handler, group); + handleCommonConnectionResult(channelFuture, ChannelId, handler, group); } catch (Exception e) { - handleCommonConnectionException(e, id, handler, group); + handleCommonConnectionException(e, ChannelId, handler, group); } } /** * 创建通用通道初始化器 * - * @param id 唯一标识id - * @param handler 通用业务处理器 + * @param channelId Channel唯一标识id + * @param handler 通用业务处理器 * @return ChannelInitializer 通道初始化器 */ private static ChannelInitializer createCommonChannelInitializer( - String id, SimpleChannelInboundHandler handler) { + String channelId, SimpleChannelInboundHandler handler) { return new ChannelInitializer() { @Override protected void initChannel(NioSocketChannel ch) { @@ -123,23 +123,23 @@ public class NettyClient { * 处理通用连接结果 * * @param channelFuture 连接Future对象 - * @param id id + * @param channelId Channel唯一标识符 * @param handler 通用业务处理器 * @param group 事件循环组 */ private static void handleCommonConnectionResult(ChannelFuture channelFuture, - String id, + String channelId, SimpleChannelInboundHandler handler, NioEventLoopGroup group) { channelFuture.addListener((ChannelFutureListener) ch -> { if (!ch.isSuccess()) { - log.error("连接Socket失败,id={}", id); + log.error("连接Socket失败,channelId={}", channelId); group.shutdownGracefully(); } else { - log.info("连接Socket成功,id={}, channelId={}", - id, channelFuture.channel().id()); - SocketManager.addGroup(id, group); - SocketManager.addUser(id, channelFuture.channel()); + log.info("连接Socket成功,channel={}, channelId={}", + channelId, channelFuture.channel().id()); + SocketManager.addGroup(channelId, group); + SocketManager.addUser(channelId, channelFuture.channel()); } }); } @@ -147,15 +147,15 @@ public class NettyClient { /** * 处理通用连接异常 * - * @param e 异常对象 - * @param id 唯一标识id - * @param handler 通用业务处理器 - * @param group 事件循环组 + * @param e 异常对象 + * @param channelId Channel唯一标识id + * @param handler 通用业务处理器 + * @param group 事件循环组 */ - private static void handleCommonConnectionException(Exception e, String id, + private static void handleCommonConnectionException(Exception e, String channelId, SimpleChannelInboundHandler handler, NioEventLoopGroup group) { - log.error("连接Socket服务端发生异常,id={}, error={}", id, e.getMessage(), e); + log.error("连接Socket服务端发生异常,channelId={}, error={}", channelId, e.getMessage(), e); group.shutdownGracefully(); } diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyFreqConverterClientHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyFreqConverterClientHandler.java index ec1857eb..d12119f0 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyFreqConverterClientHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyFreqConverterClientHandler.java @@ -16,9 +16,9 @@ import lombok.extern.slf4j.Slf4j; public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler { /** - * 变频器ID + * 变频器Channel唯一标识符 */ - private final String converterId; + private final String converterChannelTag; /** @@ -44,20 +44,20 @@ public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler /** * 构造方法 * - * @param converterId 变频器ID + * @param converterChannelTag 变频器Chanel唯一标识符 * @param socketFreqConverterService 变频器Socket响应服务 */ - public NettyFreqConverterClientHandler(String converterId, SocketFreqConverterService socketFreqConverterService) { - this.converterId = converterId; + public NettyFreqConverterClientHandler(String converterChannelTag, SocketFreqConverterService socketFreqConverterService) { + this.converterChannelTag = converterChannelTag; this.socketFreqConverterService = socketFreqConverterService; } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - log.info("变频器连接已建立,converterId={}, channelId={}", converterId, ctx.channel().id()); + log.info("变频器连接已建立,converterChannelTag={}, channelId={}", converterChannelTag, ctx.channel().id()); // 注册Channel到SocketManager - SocketManager.addUser(converterId, ctx.channel()); + SocketManager.addUser(converterChannelTag, ctx.channel()); super.channelActive(ctx); } @@ -65,39 +65,39 @@ public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { if (StrUtil.isBlank(msg)) { - log.debug("收到空消息,忽略,converterId={}", converterId); + log.debug("收到空消息,忽略,converterChannelTag={}", converterChannelTag); return; } - log.info("收到变频器消息,converterId={}, msg={}", converterId, msg); + log.info("收到变频器消息,converterChannelTag={}, msg={}", converterChannelTag, msg); // 处理状态数据 - socketFreqConverterService.handleRead(converterId, msg); + socketFreqConverterService.handleRead(converterChannelTag, msg); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - log.warn("变频器连接已断开,converterId={}", converterId); + log.warn("变频器连接已断开,converterChannelTag={}", converterChannelTag); // 移除Channel - socketFreqConverterService.cleanup(converterId, false); + socketFreqConverterService.cleanup(converterChannelTag, false); // 尝试重连 if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) { reconnectAttempts++; - log.info("尝试重连变频器,converterId={}, 第{}次重连", converterId, reconnectAttempts); + log.info("尝试重连变频器,converterChannelTag={}, 第{}次重连", converterChannelTag, reconnectAttempts); ctx.executor().schedule(() -> { try { // 获取连接信息并重连 // 注意:这里需要根据实际情况实现重连逻辑 - log.info("变频器重连逻辑待实现,converterId={}", converterId); + log.info("变频器重连逻辑待实现,converterChannelTag={}", converterChannelTag); } catch (Exception e) { - log.error("变频器重连失败,converterId={}, error={}", converterId, e.getMessage(), e); + log.error("变频器重连失败,converterChannelTag={}, error={}", converterChannelTag, e.getMessage(), e); } }, RECONNECT_INTERVAL_MS, java.util.concurrent.TimeUnit.MILLISECONDS); } else { - log.error("变频器重连失败,已达到最大重连次数,converterId={}", converterId); + log.error("变频器重连失败,已达到最大重连次数,converterChannelTag={}", converterChannelTag); } super.channelInactive(ctx); @@ -105,7 +105,7 @@ public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - log.error("变频器连接发生异常,converterId={}, error={}", converterId, cause.getMessage(), cause); + log.error("变频器连接发生异常,converterChannelTag={}, error={}", converterChannelTag, cause.getMessage(), cause); ctx.close(); } @@ -114,7 +114,7 @@ public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.READER_IDLE) { - log.warn("变频器连接读空闲,converterId={}", converterId); + log.warn("变频器连接读空闲,converterChannelTag={}", converterChannelTag); // 可以选择发送心跳或关闭连接 } } diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyFreqConverterDevClientHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyFreqConverterDevClientHandler.java index 7c2a1d1a..a77fddd9 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyFreqConverterDevClientHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyFreqConverterDevClientHandler.java @@ -15,42 +15,42 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class NettyFreqConverterDevClientHandler extends SimpleChannelInboundHandler { - private final String monitorId; + private final String devChannelTag; private final SocketFreqConverterDevService socketFreqConverterDevService; - public NettyFreqConverterDevClientHandler(String monitorId, SocketFreqConverterDevService socketFreqConverterDevService) { - this.monitorId = monitorId; + public NettyFreqConverterDevClientHandler(String devChannelTag, SocketFreqConverterDevService socketFreqConverterDevService) { + this.devChannelTag = devChannelTag; this.socketFreqConverterDevService = socketFreqConverterDevService; } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { - log.info("设备连接已建立,monitorId={}, channelId={}", monitorId, ctx.channel().id()); - SocketManager.addUser(monitorId, ctx.channel()); + log.info("设备连接已建立,devChannelTag={}, channelId={}", devChannelTag, ctx.channel().id()); + SocketManager.addUser(devChannelTag, ctx.channel()); super.channelActive(ctx); } @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { if (StrUtil.isBlank(msg)) { - log.debug("收到空消息,忽略,monitorId={}", monitorId); + log.debug("收到空消息,忽略,devChannelTag={}", devChannelTag); return; } - log.info("收到设备消息,monitorId={}, msg={}", monitorId, msg); - socketFreqConverterDevService.handleRead(monitorId, msg); + log.info("收到设备消息,devChannelTag={}, msg={}", devChannelTag, msg); + socketFreqConverterDevService.handleRead(devChannelTag, msg); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { - log.warn("设备连接已断开,monitorId={}", monitorId); - socketFreqConverterDevService.cleanup(monitorId, false); + log.warn("设备连接已断开,devChannelTag={}", devChannelTag); + socketFreqConverterDevService.cleanup(devChannelTag, false); super.channelInactive(ctx); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - log.error("设备连接发生异常,monitorId={}, error={}", monitorId, cause.getMessage(), cause); + log.error("设备连接发生异常,devChannelTag={}, error={}", devChannelTag, cause.getMessage(), cause); ctx.close(); } @@ -59,7 +59,7 @@ public class NettyFreqConverterDevClientHandler extends SimpleChannelInboundHand if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.READER_IDLE) { - log.warn("设备连接读空闲,monitorId={}", monitorId); + log.warn("设备连接读空闲,devChannelTag={}", devChannelTag); } } super.userEventTriggered(ctx, evt); diff --git a/detection/src/main/java/com/njcn/gather/freqConverter/pojo/po/FreqConverterStatus.java b/detection/src/main/java/com/njcn/gather/freqConverter/pojo/po/FreqConverterStatus.java index ebcb19ed..f66a7c91 100644 --- a/detection/src/main/java/com/njcn/gather/freqConverter/pojo/po/FreqConverterStatus.java +++ b/detection/src/main/java/com/njcn/gather/freqConverter/pojo/po/FreqConverterStatus.java @@ -25,11 +25,6 @@ public class FreqConverterStatus extends BaseEntity implements Serializable { */ private String id; - /** - * 变频器ID(用于区分不同变频器) - */ - private String converterId; - private Integer slaveAddress; private Integer statusWord1; diff --git a/detection/src/main/java/com/njcn/gather/freqConverter/service/impl/FreqConverterServiceImpl.java b/detection/src/main/java/com/njcn/gather/freqConverter/service/impl/FreqConverterServiceImpl.java index 1cfa7a5f..b3c156bf 100644 --- a/detection/src/main/java/com/njcn/gather/freqConverter/service/impl/FreqConverterServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/freqConverter/service/impl/FreqConverterServiceImpl.java @@ -31,21 +31,6 @@ import java.util.stream.Collectors; @Service public class FreqConverterServiceImpl extends ServiceImpl implements IFreqConverterService { - /** - * 默认最大数据量阈值 - */ - private static final int DEFAULT_MAX_DATA_COUNT = 10000; - - /** - * 默认数据过期天数 - */ - private static final int DEFAULT_EXPIRE_DAYS = 30; - - /** - * 默认每个变频器保留的最新数据条数 - */ - private static final int DEFAULT_KEEP_LATEST_COUNT = 100; - @Override @Transactional(rollbackFor = Exception.class) public boolean saveFreqConverterStatus(FreqConverterStatus status) { @@ -54,7 +39,7 @@ public class FreqConverterServiceImpl extends ServiceImpl listStatusHistory(String converterId) { - return this.lambdaQuery().eq(FreqConverterStatus::getConverterId, converterId).list(); + return this.lambdaQuery().list(); } @Override