耐受结果、变频器重新连接Socket调整

This commit is contained in:
caozehui
2026-04-15 09:45:53 +08:00
parent 2293d81b71
commit c5e77ee9b1
14 changed files with 147 additions and 62 deletions

View File

@@ -181,22 +181,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("userId") String userId, @RequestParam("converterId") String converterId, @RequestParam("monitorId") String monitorId) {
String methodDescribe = getMethodDescribe("startFreqConverter");
LogUtil.njcnDebug(log, "{}", methodDescribe);
preDetectionService.startFreqConverter(loginName, converterId, monitorId);
preDetectionService.startFreqConverter(userId, 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("userId") String userId) {
String methodDescribe = getMethodDescribe("stopFreqConverter");
LogUtil.njcnDebug(log, "{}", methodDescribe);
preDetectionService.stopFreqConverter(loginName + CnSocketUtil.FREQ_CONVERTER_TAG, loginName + CnSocketUtil.DEV_TAG);
preDetectionService.stopFreqConverter(userId + CnSocketUtil.FREQ_CONVERTER_TAG, userId + CnSocketUtil.DEV_TAG);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
}
}

View File

@@ -19,11 +19,12 @@ import com.njcn.gather.detection.util.socket.SocketManager;
import com.njcn.gather.detection.util.socket.cilent.NettyClient;
import com.njcn.gather.detection.util.socket.cilent.NettyFreqConverterDevClientHandler;
import com.njcn.gather.detection.util.socket.config.SocketConnectionConfig;
import com.njcn.gather.detection.util.socket.websocket.WebServiceManager;
import com.njcn.gather.device.pojo.vo.PreDetection;
import com.njcn.gather.device.service.IPqDevService;
import com.njcn.gather.dip.pojo.po.PqDipData;
import com.njcn.gather.dip.service.IPqDipDataService;
import com.njcn.gather.freqConverter.service.IPqFreqConverterConfigService;
import com.njcn.gather.freqConverter.service.IPqFreqConverterTestResService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -41,8 +42,9 @@ public class SocketFreqConverterDevService {
private final SocketConnectionConfig socketConnectionConfig;
private final IPqDevService pqDevService;
private final IPqDipDataService pqDipDataService;
private final IPqFreqConverterConfigService pqFreqConverterConfigService;
private final IPqFreqConverterTestResService pqFreqConverterTestResService;
private String monitorId;
private String userId;
/**
* 连接设备Socket
@@ -62,18 +64,21 @@ public class SocketFreqConverterDevService {
});
}
private void init(String converterId, String monitorId) {
private void init(String userId, String converterId, String monitorId) {
FormalTestManager.freqConverterDevStep = null;
FormalTestManager.stopFlag = false;
FormalTestManager.isRemoveSocket = false;
pqDipDataService.clearAllData(FormalTestManager.freqConverterTableSuffix);
pqFreqConverterTestResService.clearAllData(FormalTestManager.freqConverterTableSuffix);
this.userId = userId;
this.monitorId = monitorId;
}
/**
* 连接设备
*/
public void connectionDev(String devTag, String converterId, String monitorId) {
this.init(converterId, monitorId);
public void connectionDev(String userId, String devTag, String converterId, String monitorId) {
this.init(userId, converterId, monitorId);
String payload = buildSingleMonitorPayload(monitorId);
if (StrUtil.isBlank(payload)) {
@@ -107,6 +112,11 @@ public class SocketFreqConverterDevService {
private void handleYjcSbtxjy(String devTag, SocketDataMsg socketDataMsg) {
SourceResponseCodeEnum responseCodeEnum = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode());
switch (Objects.requireNonNull(responseCodeEnum)) {
case UNPROCESSED_BUSINESS:
break;
case RE_OPERATE:
WebServiceManager.sendMsg(this.userId, JSON.toJSONString(socketDataMsg));
break;
case SUCCESS:
this.sendGetDipDataMsg(devTag);
FormalTestManager.freqConverterDevStep = SourceOperateCodeEnum.FORMAL_REAL;
@@ -147,7 +157,7 @@ public class SocketFreqConverterDevService {
switch (responseCodeEnum) {
case SUCCESS:
cleanup(devTag, true);
cleanup(devTag);
break;
default:
log.warn("设备关闭响应失败devTag={}, operateCode={}, code={}, data={}", devTag, socketDataMsg.getOperateCode(), socketDataMsg.getCode(), socketDataMsg.getData());
@@ -179,7 +189,7 @@ public class SocketFreqConverterDevService {
}
List<PreDetection.MonitorListDTO> matchedMonitorList = monitorList.stream()
.filter(item -> split[1].equals(item.getLineId()))
.filter(item -> split[1].equals(StrUtil.EMPTY + item.getLine()))
.collect(Collectors.toList());
if (CollUtil.isEmpty(matchedMonitorList)) {
return null;
@@ -264,12 +274,9 @@ public class SocketFreqConverterDevService {
return listDTO.getT();
}
public void cleanup(String devTag, boolean removeSocket) {
public void cleanup(String devTag) {
FormalTestManager.freqConverterDevStep = null;
if (removeSocket) {
SocketManager.removeUser(devTag);
} else {
SocketManager.clearUser(devTag);
}
FormalTestManager.isRemoveSocket = true;
SocketManager.removeUser(devTag);
}
}

View File

@@ -5,7 +5,6 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.njcn.db.mybatisplus.handler.DynamicTableNameHandler;
import com.njcn.gather.detection.pojo.dto.FreqConverterRespDTO;
import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum;
import com.njcn.gather.detection.pojo.vo.SocketMsg;
@@ -42,6 +41,7 @@ public class SocketFreqConverterService {
private final SocketConnectionConfig socketConnectionConfig;
private final IPqFreqConverterConfigService pqFreqConverterConfigService;
private String userId;
/**
* 连接变频器Socket
@@ -98,19 +98,21 @@ public class SocketFreqConverterService {
}
}
private void init(String converterId, String monitorId) {
private void init(String userId, String converterId, String monitorId) {
this.userId = userId;
FormalTestManager.freqConverterStep = null;
Integer suffix = pqFreqConverterConfigService.getSuffix(converterId);
freqConverterService.clearAllData(suffix);
FormalTestManager.freqConverterTableSuffix = suffix;
FormalTestManager.isRemoveSocket = false;
clearScheduleTask();
}
/**
* 连接变频器
*/
public void connectionFreqConverter(String freqConverterTag, String converterId, String monitorId) {
this.init(converterId, monitorId);
public void connectionFreqConverter(String userId, String freqConverterTag, String converterId, String monitorId) {
this.init(userId, converterId, monitorId);
SocketMsg<Map<String, Object>> socketMsg = new SocketMsg<>();
socketMsg.setOperateCode(SourceOperateCodeEnum.CMD_INIT_SERIAL.getValue());
@@ -154,19 +156,12 @@ public class SocketFreqConverterService {
this.sendClose(converterTag);
}
public void cleanup(String converterChannelTag, boolean removeSocket) {
public void cleanup(String converterChannelTag) {
clearScheduleTask();
FormalTestManager.freqConverterStep = null;
if (!removeSocket) {
FormalTestManager.stopFlag = true;
} else {
FormalTestManager.stopFlag = false;
}
if (removeSocket) {
SocketManager.removeUser(converterChannelTag);
} else {
SocketManager.clearUser(converterChannelTag);
}
FormalTestManager.stopFlag = false;
FormalTestManager.isRemoveSocket = true;
SocketManager.removeUser(converterChannelTag);
}
private void handleInitSerial(String converterChannelTag, FreqConverterRespDTO respDTO) {
@@ -177,7 +172,7 @@ public class SocketFreqConverterService {
FormalTestManager.scheduler = Executors.newScheduledThreadPool(1);
FormalTestManager.scheduledFuture = FormalTestManager.scheduler.scheduleAtFixedRate(() -> {
this.sendGetDeviceStatusMsg(converterChannelTag);
}, 0l, 200l, TimeUnit.MILLISECONDS);
}, 0l, 200l, TimeUnit.SECONDS);
}
} else {
log.error("变频器初始化串口失败converterChannelTag={}, converterId={}, converterTag={}, msg={}", converterChannelTag, converterChannelTag, converterChannelTag, respDTO.getMessage());
@@ -197,12 +192,12 @@ public class SocketFreqConverterService {
} else {
FormalTestManager.stopFlag = false;
}
freqConverterService.saveFreqConverterStatus(FormalTestManager.freqConverterTableSuffix,freqConverterStatus);
freqConverterService.saveFreqConverterStatus(FormalTestManager.freqConverterTableSuffix, freqConverterStatus);
}
private void handleCloseSerial(String converterChannelTag, FreqConverterRespDTO respDTO) {
if (respDTO.getCode() == 0 && respDTO.getSuccess()) {
cleanup(converterChannelTag, true);
cleanup(converterChannelTag);
} else {
this.sendClose(converterChannelTag);
}

View File

@@ -65,7 +65,7 @@ public interface PreDetectionService {
void startCoefficient();
void startFreqConverter(String name, String converterId, String monitorId);
void startFreqConverter(String userId, String converterId, String monitorId);
void stopFreqConverter(String converterId, String monitorId);
}

View File

@@ -394,19 +394,19 @@ 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;
public void startFreqConverter(String userId, String converterId, String monitorId) {
String freqConverterTag = userId + CnSocketUtil.FREQ_CONVERTER_TAG;
String devTag = userId + CnSocketUtil.DEV_TAG;
socketFreqConverterService.connectSocket(freqConverterTag);
//socketFreqConverterDevService.connectSocket(devTag);
socketFreqConverterDevService.connectSocket(devTag);
long startTime = System.currentTimeMillis();
long timeout = 3000; // 3秒超时时间
while (true) {
// if (SocketManager.isChannelActive(freqConverterTag) && SocketManager.isChannelActive(devTag)) {
if (SocketManager.isChannelActive(freqConverterTag)) {
socketFreqConverterService.connectionFreqConverter(freqConverterTag, converterId, monitorId);
//socketFreqConverterDevService.connectionDev(devTag, converterId, monitorId);
if (SocketManager.isChannelActive(freqConverterTag) && SocketManager.isChannelActive(devTag)) {
// if (SocketManager.isChannelActive(freqConverterTag)) {
socketFreqConverterService.connectionFreqConverter(userId, freqConverterTag, converterId, monitorId);
socketFreqConverterDevService.connectionDev(userId, devTag, converterId, monitorId);
break;
}
@@ -421,7 +421,7 @@ public class PreDetectionServiceImpl implements PreDetectionService {
@Override
public void stopFreqConverter(String converterTag, String devTag) {
socketFreqConverterService.stopTest(converterTag, devTag);
socketFreqConverterDevService.stopTest(converterTag, converterTag);
socketFreqConverterDevService.stopTest(converterTag, devTag);
}
/**

View File

@@ -81,17 +81,6 @@ public class SocketManager {
socketGroup.remove(userId);
}
public static void clearUser(String userId) {
Channel channel = socketSessions.remove(userId);
NioEventLoopGroup eventExecutors = socketGroup.remove(userId);
if (ObjectUtil.isNotNull(eventExecutors)) {
eventExecutors.shutdownGracefully();
}
if (ObjectUtil.isNotNull(channel)) {
log.info("{}__{}已清理客户端会话", userId, channel.id());
}
}
public static Channel getChannelByUserId(String userId) {
return socketSessions.get(userId);
}

View File

@@ -50,7 +50,7 @@ public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler
/**
* 构造方法
*
* @param converterChannelTag 变频器Chanel唯一标识符
* @param converterChannelTag 变频器Chanel唯一标识符
* @param socketFreqConverterService 变频器Socket响应服务
*/
public NettyFreqConverterClientHandler(String converterChannelTag, SocketFreqConverterService socketFreqConverterService) {
@@ -93,9 +93,9 @@ public class NettyFreqConverterClientHandler extends SimpleChannelInboundHandler
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.warn("变频器连接已断开converterChannelTag={}", converterChannelTag);
socketFreqConverterService.cleanup(converterChannelTag, false);
socketFreqConverterService.cleanup(converterChannelTag);
if (!isReconnecting && reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
if (!isReconnecting && reconnectAttempts < MAX_RECONNECT_ATTEMPTS && !FormalTestManager.isRemoveSocket) {
attemptReconnect(ctx);
} else if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
log.error("变频器重连失败,已达到最大重连次数{}次converterChannelTag={}", MAX_RECONNECT_ATTEMPTS, converterChannelTag);

View File

@@ -44,7 +44,7 @@ public class NettyFreqConverterDevClientHandler extends SimpleChannelInboundHand
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.warn("设备连接已断开devChannelTag={}", devChannelTag);
socketFreqConverterDevService.cleanup(devChannelTag, false);
socketFreqConverterDevService.cleanup(devChannelTag);
super.channelInactive(ctx);
}

View File

@@ -0,0 +1,11 @@
package com.njcn.gather.freqConverter.mapper;
import com.github.yulichang.base.MPJBaseMapper;
import com.njcn.gather.freqConverter.pojo.po.PqFreqConverterTestRes;
/**
* @author caozehui
* @data 2026-04-14
*/
public interface PqFreqConverterTestResMapper extends MPJBaseMapper<PqFreqConverterTestRes> {
}

View File

@@ -0,0 +1,32 @@
package com.njcn.gather.freqConverter.pojo.po;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @author caozehui
* @data 2026-04-14
*/
@Data
@TableName(value = "ad_harmonic_xx")
public class PqFreqConverterTestRes {
/**
* 主键ID
*/
private String id;
/**
* 装置暂降数据id
*/
private String dipId;
/**
* 变频器状态数据id
*/
private String statusId;
/**
* 0为不耐受1为耐受
*/
private Boolean tolerant;
}

View File

@@ -0,0 +1,18 @@
package com.njcn.gather.freqConverter.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.njcn.gather.freqConverter.pojo.po.PqFreqConverterTestRes;
/**
* @author caozehui
* @data 2026-04-14
*/
public interface IPqFreqConverterTestResService extends IService<PqFreqConverterTestRes> {
/**
* 清空所有数据
*
* @param suffix 表后缀
* @return
*/
void clearAllData(Integer suffix);
}

View File

@@ -72,6 +72,15 @@ public class PqFreqConverterConfigServiceImpl extends ServiceImpl<PqFreqConverte
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='电压暂降数据表';";
tableGenMapper.genTable(tableSql);
tableSql = "CREATE TABLE `pq_freq_converter_test_res_" + maxSuffix + "` (" +
" `id` char(32) NOT NULL COMMENT '主键ID'," +
" `dip_id` char(32) NOT NULL COMMENT '装置暂降数据id'," +
" `status_id` char(32) NOT NULL COMMENT '变频器状态数据id'," +
" `tolerant` tinyInt(1) NOT NULL COMMENT '0为不耐受1为耐受'," +
" PRIMARY KEY (`id`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='变频器耐受实验结果';";
tableGenMapper.genTable(tableSql);
PqFreqConverterConfig pqFreqConverterConfig = BeanUtil.copyProperties(param, PqFreqConverterConfig.class);
pqFreqConverterConfig.setSuffix(maxSuffix);
pqFreqConverterConfig.setState(DataStateEnum.ENABLE.getCode());
@@ -94,7 +103,9 @@ public class PqFreqConverterConfigServiceImpl extends ServiceImpl<PqFreqConverte
List<PqFreqConverterConfig> pqFreqConverterConfigs = this.listByIds(ids);
for (PqFreqConverterConfig pqFreqConverterConfig : pqFreqConverterConfigs) {
Integer suffix = pqFreqConverterConfig.getSuffix();
sql.append("pq_freq_converter_status_" + suffix + ",").append("pq_dip_data_" + suffix + ",");
sql.append("pq_freq_converter_status_" + suffix + ",")
.append("pq_dip_data_" + suffix + ",")
.append("pq_freq_converter_test_res_" + suffix + ",");
}
sql.deleteCharAt(sql.length() - 1);

View File

@@ -0,0 +1,22 @@
package com.njcn.gather.freqConverter.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.db.mybatisplus.handler.DynamicTableNameHandler;
import com.njcn.gather.freqConverter.mapper.PqFreqConverterTestResMapper;
import com.njcn.gather.freqConverter.pojo.po.PqFreqConverterTestRes;
import com.njcn.gather.freqConverter.service.IPqFreqConverterTestResService;
import org.springframework.stereotype.Service;
/**
* @author caozehui
* @data 2026-04-14
*/
@Service
public class PqFreqConverterTestResServiceImpl extends ServiceImpl<PqFreqConverterTestResMapper, PqFreqConverterTestRes> implements IPqFreqConverterTestResService {
@Override
public void clearAllData(Integer suffix) {
DynamicTableNameHandler.setTableName("pq_freq_converter_test_res_" + suffix);
this.remove(null);
DynamicTableNameHandler.remove();
}
}