@@ -10,7 +10,6 @@ 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;
|
||||
@@ -181,22 +180,22 @@ public class PreDetectionController extends BaseController {
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@GetMapping("/startFreqConverter")
|
||||
@ApiOperation("开启变频器测试")
|
||||
public HttpResult<String> startFreqConverter(@RequestParam("loginName") String loginName, @RequestParam("converterId") String converterId, @RequestParam("monitorId") String monitorId) {
|
||||
public HttpResult<String> startFreqConverter(@RequestParam("converterId") String converterId, @RequestParam("monitorId") String monitorId) {
|
||||
String methodDescribe = getMethodDescribe("startFreqConverter");
|
||||
LogUtil.njcnDebug(log, "{}", methodDescribe);
|
||||
|
||||
preDetectionService.startFreqConverter(loginName, converterId, monitorId);
|
||||
preDetectionService.startFreqConverter(converterId, monitorId);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@GetMapping("/stopFreqConverter")
|
||||
@ApiOperation("关闭变频器测试")
|
||||
public HttpResult<String> stopFreqConverter(@RequestParam("loginName") String loginName) {
|
||||
public HttpResult<String> stopFreqConverter(@RequestParam("converterId") String converterId, @RequestParam("monitorId") String monitorId) {
|
||||
String methodDescribe = getMethodDescribe("stopFreqConverter");
|
||||
LogUtil.njcnDebug(log, "{}", methodDescribe);
|
||||
|
||||
preDetectionService.stopFreqConverter(loginName + CnSocketUtil.FREQ_CONVERTER_TAG, loginName + CnSocketUtil.DEV_TAG);
|
||||
preDetectionService.stopFreqConverter(converterId, monitorId);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,18 +43,18 @@ public class SocketFreqConverterDevService {
|
||||
/**
|
||||
* 连接设备Socket
|
||||
*
|
||||
* @param devTag 设备Channel唯一标识符
|
||||
* @param monitorId 监测点Id
|
||||
*/
|
||||
public void connectSocket(String devTag) {
|
||||
if (SocketManager.isChannelActive(devTag)) {
|
||||
public void connectSocket(String monitorId) {
|
||||
if (SocketManager.isChannelActive(monitorId)) {
|
||||
return;
|
||||
}
|
||||
String ip = socketConnectionConfig.getDevice().getIp();
|
||||
Integer port = socketConnectionConfig.getDevice().getPort();
|
||||
|
||||
NettyFreqConverterDevClientHandler handler = new NettyFreqConverterDevClientHandler(devTag, this);
|
||||
NettyFreqConverterDevClientHandler handler = new NettyFreqConverterDevClientHandler(monitorId, this);
|
||||
CompletableFuture.runAsync(() -> {
|
||||
NettyClient.commonConnect(ip, port, devTag, handler);
|
||||
NettyClient.commonConnect(ip, port, monitorId, handler);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ public class SocketFreqConverterDevService {
|
||||
/**
|
||||
* 连接设备
|
||||
*/
|
||||
public void connectionDev(String devTag, String converterId, String monitorId) {
|
||||
public void connectionDev(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(devTag, JSON.toJSONString(socketMsg));
|
||||
SocketManager.sendMsg(monitorId, JSON.toJSONString(socketMsg));
|
||||
FormalTestManager.freqConverterDevStep = SourceOperateCodeEnum.YJC_SBTXJY;
|
||||
}
|
||||
|
||||
public void handleRead(String devTag, String msg) {
|
||||
public void handleRead(String monitorId, String msg) {
|
||||
SocketDataMsg socketDataMsg = MsgUtil.socketDataMsg(msg);
|
||||
|
||||
switch (FormalTestManager.freqConverterDevStep) {
|
||||
case YJC_SBTXJY:
|
||||
handleYjcSbtxjy(devTag, socketDataMsg);
|
||||
handleYjcSbtxjy(monitorId, socketDataMsg);
|
||||
break;
|
||||
case FORMAL_REAL:
|
||||
handleFormalReal(devTag, socketDataMsg);
|
||||
handleFormalReal(monitorId, socketDataMsg);
|
||||
break;
|
||||
case QUITE:
|
||||
handleQuit(devTag, socketDataMsg);
|
||||
handleQuit(monitorId, socketDataMsg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleYjcSbtxjy(String devTag, SocketDataMsg socketDataMsg) {
|
||||
private void handleYjcSbtxjy(String monitorId, SocketDataMsg socketDataMsg) {
|
||||
SourceResponseCodeEnum responseCodeEnum = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode());
|
||||
switch (Objects.requireNonNull(responseCodeEnum)) {
|
||||
case SUCCESS:
|
||||
this.sendGetDipDataMsg(devTag);
|
||||
this.sendGetDipDataMsg(monitorId);
|
||||
FormalTestManager.freqConverterDevStep = SourceOperateCodeEnum.FORMAL_REAL;
|
||||
break;
|
||||
default:
|
||||
log.info("设备响应异常,devTag={}, operateCode={}, code={}, data={}", devTag, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData());
|
||||
log.info("设备响应异常,monitorId={}, operateCode={}, code={}, data={}", monitorId, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleFormalReal(String devTag, SocketDataMsg socketDataMsg) {
|
||||
private void handleFormalReal(String monitorId, 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("设备响应异常,devTag={}, operateCode={}, code={}, data={}", devTag, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData());
|
||||
log.info("设备响应异常,monitorId={}, operateCode={}, code={}, data={}", monitorId, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleQuit(String devTag, SocketDataMsg socketDataMsg) {
|
||||
private void handleQuit(String monitorId, SocketDataMsg socketDataMsg) {
|
||||
SourceResponseCodeEnum responseCodeEnum = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode());
|
||||
|
||||
switch (responseCodeEnum) {
|
||||
case SUCCESS:
|
||||
cleanup(devTag, true);
|
||||
cleanup(monitorId, true);
|
||||
break;
|
||||
default:
|
||||
log.warn("设备关闭响应失败,devTag={}, operateCode={}, code={}, data={}", devTag, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData());
|
||||
log.warn("设备关闭响应失败,monitorId={}, operateCode={}, code={}, data={}", monitorId, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void stopTest(String converterTag, String devTag) {
|
||||
public void stopTest(String converterId, String monitorId) {
|
||||
FormalTestManager.freqConverterDevStep = SourceOperateCodeEnum.QUITE;
|
||||
sendQuitMsg(devTag, SourceOperateCodeEnum.QUIT_INIT_03);
|
||||
sendQuitMsg(monitorId, SourceOperateCodeEnum.QUIT_INIT_03);
|
||||
}
|
||||
|
||||
|
||||
@@ -205,11 +205,12 @@ public class SocketFreqConverterDevService {
|
||||
SocketManager.sendMsg(monitorId, JSON.toJSONString(socketMsg));
|
||||
}
|
||||
|
||||
private void sendQuitMsg(String devTag, SourceOperateCodeEnum operateCodeEnum) {
|
||||
private void sendQuitMsg(String monitorId, SourceOperateCodeEnum operateCodeEnum) {
|
||||
SocketMsg<String> socketMsg = new SocketMsg<>();
|
||||
socketMsg.setRequestId(SourceOperateCodeEnum.QUITE.getValue());
|
||||
socketMsg.setOperateCode(operateCodeEnum.getValue());
|
||||
SocketManager.sendMsg(devTag, JSON.toJSONString(socketMsg));
|
||||
SocketManager.sendMsg(monitorId, JSON.toJSONString(socketMsg));
|
||||
log.info("已向设备发送关闭指令,monitorId={}, operateCode={}", monitorId, operateCodeEnum.getValue());
|
||||
}
|
||||
|
||||
private void saveDipData(DevData devData) {
|
||||
@@ -256,12 +257,12 @@ public class SocketFreqConverterDevService {
|
||||
return listDTO.getT();
|
||||
}
|
||||
|
||||
public void cleanup(String devTag, boolean removeSocket) {
|
||||
public void cleanup(String monitorId, boolean removeSocket) {
|
||||
FormalTestManager.freqConverterDevStep = null;
|
||||
if (removeSocket) {
|
||||
SocketManager.removeUser(devTag);
|
||||
SocketManager.removeUser(monitorId);
|
||||
} else {
|
||||
SocketManager.clearUser(devTag);
|
||||
SocketManager.clearUser(monitorId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,19 +45,19 @@ public class SocketFreqConverterService {
|
||||
/**
|
||||
* 连接变频器Socket
|
||||
*
|
||||
* @param converterChannelTag 变频器Channel唯一标识符
|
||||
* @param converterId 变频器id
|
||||
*/
|
||||
public void connectSocket(String converterChannelTag) {
|
||||
if (SocketManager.isChannelActive(converterChannelTag)) {
|
||||
public void connectSocket(String converterId) {
|
||||
if (SocketManager.isChannelActive(converterId)) {
|
||||
return;
|
||||
}
|
||||
String ip = socketConnectionConfig.getFreqConverter().getIp();
|
||||
Integer port = socketConnectionConfig.getFreqConverter().getPort();
|
||||
|
||||
NettyFreqConverterClientHandler handler = new NettyFreqConverterClientHandler(converterChannelTag, this);
|
||||
NettyFreqConverterClientHandler handler = new NettyFreqConverterClientHandler(converterId, this);
|
||||
|
||||
CompletableFuture.runAsync(() -> {
|
||||
NettyClient.commonConnect(ip, port, converterChannelTag, handler);
|
||||
NettyClient.commonConnect(ip, port, converterId, handler);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public class SocketFreqConverterService {
|
||||
/**
|
||||
* 连接变频器
|
||||
*/
|
||||
public void connectionFreqConverter(String freqConverterTag, String converterId, String monitorId) {
|
||||
public void connectionFreqConverter(String converterId, String monitorId) {
|
||||
this.init(converterId, monitorId);
|
||||
|
||||
SocketMsg<Map<String, Object>> socketMsg = new SocketMsg<>();
|
||||
@@ -90,39 +90,39 @@ public class SocketFreqConverterService {
|
||||
map.put("timeoutMs", freqConverterConfig.getTimeoutMs());
|
||||
socketMsg.setData(map);
|
||||
|
||||
SocketManager.sendMsg(freqConverterTag, JSON.toJSONString(socketMsg));
|
||||
SocketManager.sendMsg(converterId, JSON.toJSONString(socketMsg));
|
||||
FormalTestManager.freqConverterStep = SourceOperateCodeEnum.CMD_INIT_SERIAL;
|
||||
}
|
||||
|
||||
public void handleRead(String converterChannelTag, String msg) {
|
||||
public void handleRead(String converterId, String msg) {
|
||||
FreqConverterRespDTO respDTO = JSON.parseObject(msg, FreqConverterRespDTO.class);
|
||||
|
||||
switch (FormalTestManager.freqConverterStep) {
|
||||
case CMD_PING:
|
||||
handlePing(converterChannelTag, respDTO);
|
||||
handlePing(converterId, respDTO);
|
||||
break;
|
||||
case CMD_INIT_SERIAL:
|
||||
handleInitSerial(converterChannelTag, respDTO);
|
||||
handleInitSerial(converterId, respDTO);
|
||||
break;
|
||||
case CMD_GET_SERIAL_CONFIG:
|
||||
handleGetSerialConfig(converterChannelTag, respDTO);
|
||||
handleGetSerialConfig(converterId, respDTO);
|
||||
break;
|
||||
case CMD_GET_DEVICE_STATUS:
|
||||
handleGetDeviceStatus(converterChannelTag, respDTO);
|
||||
handleGetDeviceStatus(converterId, respDTO);
|
||||
break;
|
||||
case CMD_CLOSE_SERIAL:
|
||||
handleCloseSerial(converterChannelTag, respDTO);
|
||||
handleCloseSerial(converterId, respDTO);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void stopTest(String converterTag, String devTag) {
|
||||
public void stopTest(String converterId, String monitorId) {
|
||||
FormalTestManager.freqConverterStep = SourceOperateCodeEnum.CMD_CLOSE_SERIAL;
|
||||
this.sendClose(converterTag);
|
||||
this.sendClose(converterId);
|
||||
}
|
||||
|
||||
|
||||
private void handlePing(String converterChannelTag, FreqConverterRespDTO respDTO) {
|
||||
private void handlePing(String converterId, 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 converterChannelTag, FreqConverterRespDTO respDTO) {
|
||||
private void handleInitSerial(String converterId, FreqConverterRespDTO respDTO) {
|
||||
if (respDTO.getCode() == 0 && respDTO.getSuccess()) {
|
||||
|
||||
FormalTestManager.freqConverterStep = SourceOperateCodeEnum.CMD_GET_DEVICE_STATUS;
|
||||
@@ -138,22 +138,23 @@ public class SocketFreqConverterService {
|
||||
if (Objects.isNull(FormalTestManager.scheduler)) {
|
||||
FormalTestManager.scheduler = Executors.newScheduledThreadPool(1);
|
||||
FormalTestManager.scheduledFuture = FormalTestManager.scheduler.scheduleAtFixedRate(() -> {
|
||||
this.sendGetDeviceStatusMsg(converterChannelTag);
|
||||
this.sendGetDeviceStatusMsg(converterId);
|
||||
}, 0l, 200l, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleGetSerialConfig(String converterChannelTag, FreqConverterRespDTO respDTO) {
|
||||
private void handleGetSerialConfig(String converterId, FreqConverterRespDTO respDTO) {
|
||||
}
|
||||
|
||||
private void handleGetDeviceStatus(String converterChannelTag, FreqConverterRespDTO respDTO) {
|
||||
private void handleGetDeviceStatus(String converterId, 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;
|
||||
@@ -163,9 +164,9 @@ public class SocketFreqConverterService {
|
||||
freqConverterService.saveFreqConverterStatus(freqConverterStatus);
|
||||
}
|
||||
|
||||
private void handleCloseSerial(String converterChannelTag, FreqConverterRespDTO respDTO) {
|
||||
private void handleCloseSerial(String converterId, FreqConverterRespDTO respDTO) {
|
||||
if (respDTO.getCode() == 0 && respDTO.getSuccess()) {
|
||||
cleanup(converterChannelTag, true);
|
||||
cleanup(converterId, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +182,7 @@ public class SocketFreqConverterService {
|
||||
SocketManager.sendMsg(converterId, JSON.toJSONString(socketMsg));
|
||||
}
|
||||
|
||||
private void sendClose(String converterTag) {
|
||||
private void sendClose(String converterId) {
|
||||
SocketMsg<Map<String, Object>> socketMsg = new SocketMsg<>();
|
||||
socketMsg.setOperateCode(SourceOperateCodeEnum.CMD_CLOSE_SERIAL.getValue());
|
||||
String requestId = IdUtil.fastSimpleUUID();
|
||||
@@ -189,18 +190,18 @@ public class SocketFreqConverterService {
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
socketMsg.setData(map);
|
||||
SocketManager.sendMsg(converterTag, JSON.toJSONString(socketMsg));
|
||||
SocketManager.sendMsg(converterId, JSON.toJSONString(socketMsg));
|
||||
}
|
||||
|
||||
public void cleanup(String converterChannelTag, boolean removeSocket) {
|
||||
public void cleanup(String converterId, boolean removeSocket) {
|
||||
clearScheduleTask();
|
||||
FormalTestManager.freqConverterStep = null;
|
||||
FormalTestManager.stopFlag = false;
|
||||
FormalTestManager.pairsIpMap.clear();
|
||||
FormalTestManager.pairsIpMap.remove(converterId);
|
||||
if (removeSocket) {
|
||||
SocketManager.removeUser(converterChannelTag);
|
||||
SocketManager.removeUser(converterId);
|
||||
} else {
|
||||
SocketManager.clearUser(converterChannelTag);
|
||||
SocketManager.clearUser(converterId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@ 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;
|
||||
|
||||
|
||||
/**
|
||||
@@ -52,6 +56,7 @@ public interface PreDetectionService {
|
||||
void closeTestSimulate(SimulateDetectionParam param);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param param
|
||||
*/
|
||||
void startContrastTest(ContrastDetectionParam param);
|
||||
@@ -65,7 +70,7 @@ public interface PreDetectionService {
|
||||
|
||||
void startCoefficient();
|
||||
|
||||
void startFreqConverter(String name, String converterId, String monitorId);
|
||||
void startFreqConverter(String converterId, String monitorId);
|
||||
|
||||
void stopFreqConverter(String converterId, String monitorId);
|
||||
void stopFreqConverter(String converterId,String monitorId);
|
||||
}
|
||||
|
||||
@@ -394,16 +394,14 @@ public class PreDetectionServiceImpl implements PreDetectionService {
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
public void startFreqConverter(String converterId, String monitorId) {
|
||||
socketFreqConverterService.connectSocket(converterId);
|
||||
socketFreqConverterDevService.connectSocket(monitorId);
|
||||
|
||||
while (true) {
|
||||
if (SocketManager.isChannelActive(freqConverterTag) && SocketManager.isChannelActive(devTag)) {
|
||||
socketFreqConverterService.connectionFreqConverter(freqConverterTag, converterId, monitorId);
|
||||
socketFreqConverterDevService.connectionDev(devTag, converterId, monitorId);
|
||||
if (SocketManager.isChannelActive(converterId) && SocketManager.isChannelActive(monitorId)) {
|
||||
socketFreqConverterService.connectionFreqConverter(converterId, monitorId);
|
||||
socketFreqConverterDevService.connectionDev(converterId, monitorId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -411,9 +409,9 @@ public class PreDetectionServiceImpl implements PreDetectionService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopFreqConverter(String converterTag, String devTag) {
|
||||
socketFreqConverterService.stopTest(converterTag, devTag);
|
||||
socketFreqConverterDevService.stopTest(converterTag, converterTag);
|
||||
public void stopFreqConverter(String converterId, String monitorId) {
|
||||
socketFreqConverterService.stopTest(converterId, monitorId);
|
||||
socketFreqConverterDevService.stopTest(converterId, monitorId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,8 +14,6 @@ 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";
|
||||
|
||||
@@ -59,15 +59,15 @@ public class NettyClient {
|
||||
/**
|
||||
* 静态方法:智能连接变频器设备(兼容性包装)
|
||||
*
|
||||
* @param ip IP地址
|
||||
* @param port 端口号
|
||||
* @param ChannelId Channel唯一标识
|
||||
* @param handler 变频器处理器
|
||||
* @param ip IP地址
|
||||
* @param port 端口号
|
||||
* @param id 唯一标识id
|
||||
* @param handler 变频器处理器
|
||||
*/
|
||||
public static void commonConnect(String ip, Integer port, String ChannelId,
|
||||
public static void commonConnect(String ip, Integer port, String id,
|
||||
SimpleChannelInboundHandler handler) {
|
||||
if (instance != null) {
|
||||
instance.executeCommonConnect(ip, port, ChannelId, handler);
|
||||
instance.executeCommonConnect(ip, port, id, handler);
|
||||
} else {
|
||||
log.error("NettyClient未初始化,无法创建连接");
|
||||
}
|
||||
@@ -76,36 +76,36 @@ public class NettyClient {
|
||||
/**
|
||||
* 执行变频器Socket连接建立流程
|
||||
*
|
||||
* @param ip 目标服务器IP地址
|
||||
* @param port 目标服务器端口号
|
||||
* @param ChannelId Channel唯一标识id
|
||||
* @param handler 变频器业务处理器
|
||||
* @param ip 目标服务器IP地址
|
||||
* @param port 目标服务器端口号
|
||||
* @param id 唯一标识id
|
||||
* @param handler 变频器业务处理器
|
||||
*/
|
||||
private static void executeCommonConnect(String ip, Integer port,
|
||||
String ChannelId,
|
||||
String id,
|
||||
SimpleChannelInboundHandler handler) {
|
||||
NioEventLoopGroup group = createEventLoopGroup();
|
||||
|
||||
try {
|
||||
Bootstrap bootstrap = configureBootstrap(group);
|
||||
ChannelInitializer<NioSocketChannel> initializer = createCommonChannelInitializer(ChannelId, handler);
|
||||
ChannelInitializer<NioSocketChannel> initializer = createCommonChannelInitializer(id, handler);
|
||||
bootstrap.handler(initializer);
|
||||
ChannelFuture channelFuture = bootstrap.connect(ip, port).sync();
|
||||
handleCommonConnectionResult(channelFuture, ChannelId, handler, group);
|
||||
handleCommonConnectionResult(channelFuture, id, handler, group);
|
||||
} catch (Exception e) {
|
||||
handleCommonConnectionException(e, ChannelId, handler, group);
|
||||
handleCommonConnectionException(e, id, handler, group);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建通用通道初始化器
|
||||
*
|
||||
* @param channelId Channel唯一标识id
|
||||
* @param handler 通用业务处理器
|
||||
* @param id 唯一标识id
|
||||
* @param handler 通用业务处理器
|
||||
* @return ChannelInitializer 通道初始化器
|
||||
*/
|
||||
private static ChannelInitializer<NioSocketChannel> createCommonChannelInitializer(
|
||||
String channelId, SimpleChannelInboundHandler handler) {
|
||||
String id, SimpleChannelInboundHandler handler) {
|
||||
return new ChannelInitializer<NioSocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(NioSocketChannel ch) {
|
||||
@@ -123,23 +123,23 @@ public class NettyClient {
|
||||
* 处理通用连接结果
|
||||
*
|
||||
* @param channelFuture 连接Future对象
|
||||
* @param channelId Channel唯一标识符
|
||||
* @param id id
|
||||
* @param handler 通用业务处理器
|
||||
* @param group 事件循环组
|
||||
*/
|
||||
private static void handleCommonConnectionResult(ChannelFuture channelFuture,
|
||||
String channelId,
|
||||
String id,
|
||||
SimpleChannelInboundHandler handler,
|
||||
NioEventLoopGroup group) {
|
||||
channelFuture.addListener((ChannelFutureListener) ch -> {
|
||||
if (!ch.isSuccess()) {
|
||||
log.error("连接Socket失败,channelId={}", channelId);
|
||||
log.error("连接Socket失败,id={}", id);
|
||||
group.shutdownGracefully();
|
||||
} else {
|
||||
log.info("连接Socket成功,channel={}, channelId={}",
|
||||
channelId, channelFuture.channel().id());
|
||||
SocketManager.addGroup(channelId, group);
|
||||
SocketManager.addUser(channelId, channelFuture.channel());
|
||||
log.info("连接Socket成功,id={}, channelId={}",
|
||||
id, channelFuture.channel().id());
|
||||
SocketManager.addGroup(id, group);
|
||||
SocketManager.addUser(id, channelFuture.channel());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -147,15 +147,15 @@ public class NettyClient {
|
||||
/**
|
||||
* 处理通用连接异常
|
||||
*
|
||||
* @param e 异常对象
|
||||
* @param channelId Channel唯一标识id
|
||||
* @param handler 通用业务处理器
|
||||
* @param group 事件循环组
|
||||
* @param e 异常对象
|
||||
* @param id 唯一标识id
|
||||
* @param handler 通用业务处理器
|
||||
* @param group 事件循环组
|
||||
*/
|
||||
private static void handleCommonConnectionException(Exception e, String channelId,
|
||||
private static void handleCommonConnectionException(Exception e, String id,
|
||||
SimpleChannelInboundHandler handler,
|
||||
NioEventLoopGroup group) {
|
||||
log.error("连接Socket服务端发生异常,channelId={}, error={}", channelId, e.getMessage(), e);
|
||||
log.error("连接Socket服务端发生异常,id={}, error={}", id, e.getMessage(), e);
|
||||
group.shutdownGracefully();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
|
||||
/**
|
||||
* 变频器Channel唯一标识符
|
||||
* 变频器ID
|
||||
*/
|
||||
private final String converterChannelTag;
|
||||
private final String converterId;
|
||||
|
||||
|
||||
/**
|
||||
@@ -44,20 +44,20 @@ public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @param converterChannelTag 变频器Chanel唯一标识符
|
||||
* @param converterId 变频器ID
|
||||
* @param socketFreqConverterService 变频器Socket响应服务
|
||||
*/
|
||||
public NettyFreqConverterClientHandler(String converterChannelTag, SocketFreqConverterService socketFreqConverterService) {
|
||||
this.converterChannelTag = converterChannelTag;
|
||||
public NettyFreqConverterClientHandler(String converterId, SocketFreqConverterService socketFreqConverterService) {
|
||||
this.converterId = converterId;
|
||||
this.socketFreqConverterService = socketFreqConverterService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
log.info("变频器连接已建立,converterChannelTag={}, channelId={}", converterChannelTag, ctx.channel().id());
|
||||
log.info("变频器连接已建立,converterId={}, channelId={}", converterId, ctx.channel().id());
|
||||
|
||||
// 注册Channel到SocketManager
|
||||
SocketManager.addUser(converterChannelTag, ctx.channel());
|
||||
SocketManager.addUser(converterId, 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("收到空消息,忽略,converterChannelTag={}", converterChannelTag);
|
||||
log.debug("收到空消息,忽略,converterId={}", converterId);
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("收到变频器消息,converterChannelTag={}, msg={}", converterChannelTag, msg);
|
||||
log.info("收到变频器消息,converterId={}, msg={}", converterId, msg);
|
||||
|
||||
// 处理状态数据
|
||||
socketFreqConverterService.handleRead(converterChannelTag, msg);
|
||||
socketFreqConverterService.handleRead(converterId, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
log.warn("变频器连接已断开,converterChannelTag={}", converterChannelTag);
|
||||
log.warn("变频器连接已断开,converterId={}", converterId);
|
||||
|
||||
// 移除Channel
|
||||
socketFreqConverterService.cleanup(converterChannelTag, false);
|
||||
socketFreqConverterService.cleanup(converterId, false);
|
||||
|
||||
// 尝试重连
|
||||
if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
|
||||
reconnectAttempts++;
|
||||
log.info("尝试重连变频器,converterChannelTag={}, 第{}次重连", converterChannelTag, reconnectAttempts);
|
||||
log.info("尝试重连变频器,converterId={}, 第{}次重连", converterId, reconnectAttempts);
|
||||
|
||||
ctx.executor().schedule(() -> {
|
||||
try {
|
||||
// 获取连接信息并重连
|
||||
// 注意:这里需要根据实际情况实现重连逻辑
|
||||
log.info("变频器重连逻辑待实现,converterChannelTag={}", converterChannelTag);
|
||||
log.info("变频器重连逻辑待实现,converterId={}", converterId);
|
||||
} catch (Exception e) {
|
||||
log.error("变频器重连失败,converterChannelTag={}, error={}", converterChannelTag, e.getMessage(), e);
|
||||
log.error("变频器重连失败,converterId={}, error={}", converterId, e.getMessage(), e);
|
||||
}
|
||||
}, RECONNECT_INTERVAL_MS, java.util.concurrent.TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
log.error("变频器重连失败,已达到最大重连次数,converterChannelTag={}", converterChannelTag);
|
||||
log.error("变频器重连失败,已达到最大重连次数,converterId={}", converterId);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
@@ -105,7 +105,7 @@ public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
log.error("变频器连接发生异常,converterChannelTag={}, error={}", converterChannelTag, cause.getMessage(), cause);
|
||||
log.error("变频器连接发生异常,converterId={}, error={}", converterId, 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("变频器连接读空闲,converterChannelTag={}", converterChannelTag);
|
||||
log.warn("变频器连接读空闲,converterId={}", converterId);
|
||||
// 可以选择发送心跳或关闭连接
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,42 +15,42 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public class NettyFreqConverterDevClientHandler extends SimpleChannelInboundHandler<String> {
|
||||
|
||||
private final String devChannelTag;
|
||||
private final String monitorId;
|
||||
private final SocketFreqConverterDevService socketFreqConverterDevService;
|
||||
|
||||
public NettyFreqConverterDevClientHandler(String devChannelTag, SocketFreqConverterDevService socketFreqConverterDevService) {
|
||||
this.devChannelTag = devChannelTag;
|
||||
public NettyFreqConverterDevClientHandler(String monitorId, SocketFreqConverterDevService socketFreqConverterDevService) {
|
||||
this.monitorId = monitorId;
|
||||
this.socketFreqConverterDevService = socketFreqConverterDevService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
log.info("设备连接已建立,devChannelTag={}, channelId={}", devChannelTag, ctx.channel().id());
|
||||
SocketManager.addUser(devChannelTag, ctx.channel());
|
||||
log.info("设备连接已建立,monitorId={}, channelId={}", monitorId, ctx.channel().id());
|
||||
SocketManager.addUser(monitorId, ctx.channel());
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
|
||||
if (StrUtil.isBlank(msg)) {
|
||||
log.debug("收到空消息,忽略,devChannelTag={}", devChannelTag);
|
||||
log.debug("收到空消息,忽略,monitorId={}", monitorId);
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("收到设备消息,devChannelTag={}, msg={}", devChannelTag, msg);
|
||||
socketFreqConverterDevService.handleRead(devChannelTag, msg);
|
||||
log.info("收到设备消息,monitorId={}, msg={}", monitorId, msg);
|
||||
socketFreqConverterDevService.handleRead(monitorId, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
log.warn("设备连接已断开,devChannelTag={}", devChannelTag);
|
||||
socketFreqConverterDevService.cleanup(devChannelTag, false);
|
||||
log.warn("设备连接已断开,monitorId={}", monitorId);
|
||||
socketFreqConverterDevService.cleanup(monitorId, false);
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
log.error("设备连接发生异常,devChannelTag={}, error={}", devChannelTag, cause.getMessage(), cause);
|
||||
log.error("设备连接发生异常,monitorId={}, error={}", monitorId, 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("设备连接读空闲,devChannelTag={}", devChannelTag);
|
||||
log.warn("设备连接读空闲,monitorId={}", monitorId);
|
||||
}
|
||||
}
|
||||
super.userEventTriggered(ctx, evt);
|
||||
|
||||
@@ -25,6 +25,11 @@ public class FreqConverterStatus extends BaseEntity implements Serializable {
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 变频器ID(用于区分不同变频器)
|
||||
*/
|
||||
private String converterId;
|
||||
|
||||
private Integer slaveAddress;
|
||||
|
||||
private Integer statusWord1;
|
||||
|
||||
@@ -31,6 +31,21 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
public class FreqConverterServiceImpl extends ServiceImpl<FreqConverterStatusMapper, FreqConverterStatus> 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) {
|
||||
@@ -39,7 +54,7 @@ public class FreqConverterServiceImpl extends ServiceImpl<FreqConverterStatusMap
|
||||
|
||||
@Override
|
||||
public List<FreqConverterStatus> listStatusHistory(String converterId) {
|
||||
return this.lambdaQuery().list();
|
||||
return this.lambdaQuery().eq(FreqConverterStatus::getConverterId, converterId).list();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user