fix:iot多实例调整

This commit is contained in:
FCL
2025-10-11 16:30:58 +08:00
parent 1b6e9f5dcb
commit 8dedb9854c
6 changed files with 30 additions and 17 deletions

View File

@@ -14,7 +14,7 @@ package com.zt.plat.module.qms.iot.tcpserver;
*/
public enum IotDeviceType {
INFRARED("infrared", "红外"),
BALANCE("auncel", "天平"),
BALANCE("balance", "天平"),
THERMOMETER("temp", "温度计"),
PACKING_MACHINE("packing_machine", "自动定量包装机"),
WEIGHBRIDGE("weighbridge", "地磅"),

View File

@@ -36,5 +36,5 @@ public interface EventConstant {
/**
* 天平
*/
String EVENT_BALANCE = "auncel";
String EVENT_BALANCE = "balance";
}

View File

@@ -40,7 +40,7 @@ public class BalanceDataCommandLineRunner implements CommandLineRunner {
private final TioServerBootstrap tioServerBootstrap;
//正确天平数据超时时间6s
private final static long AUNCEL_DATA_TIMEOUT = 6 * 1000L;
private final static long BALANCE_DATA_TIMEOUT = 6 * 1000L;
@Override
public void run(String... args) throws Exception {
@@ -52,13 +52,13 @@ public class BalanceDataCommandLineRunner implements CommandLineRunner {
public void run() {
//第一次先休息一下
try {
Thread.sleep(AUNCEL_DATA_TIMEOUT);
Thread.sleep(BALANCE_DATA_TIMEOUT);
} catch (InterruptedException e1) {
log.error(e1.toString(), e1);
}
while (!tioServerBootstrap.getServerTioConfig().isStopped()) {
try {
Thread.sleep(AUNCEL_DATA_TIMEOUT);
Thread.sleep(BALANCE_DATA_TIMEOUT);
} catch (InterruptedException e1) {
log.error(e1.toString(), e1);
}
@@ -82,7 +82,7 @@ public class BalanceDataCommandLineRunner implements CommandLineRunner {
long compareTime = lastBalanceDataTime == null ? SystemTimer.currTime : Long.parseLong(lastBalanceDataTime.toString()) ;
long currtime = SystemTimer.currTime;
long interval = currtime - compareTime;
if (interval > AUNCEL_DATA_TIMEOUT) {
if (interval > BALANCE_DATA_TIMEOUT) {
// sessionContext.setControlRealName("");
// sessionContext.setControlChannelContext(null);
sessionContext.setWeightValueList(null);

View File

@@ -30,7 +30,7 @@ public class RedisSessionComponent {
String deviceId = IotUtils.transDeviceCodeToId(context.getDeviceCode());
context.setRegTime(new Date());
String key = getRedisKeyByDeviceId(deviceId);
log.info("key={}", key);
// log.info("key={}", key);
redisTemplate.opsForValue().set(key, context, timeoutSeconds, TimeUnit.SECONDS);
}

View File

@@ -136,7 +136,7 @@ public class IotDeviceBalanceHandler implements IotDataHander {
Tio.sendToGroup(tioConfig, CAA_ALL_CLIENT, packet);
//通知天平数据到其他服务实例
log.info("天平数据:" + realClient + ":" + data);
// log.info("天平数据:" + realClient + ":" + data);
TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();
TioClusterVo iotClusterVo = new TioClusterVo(packet);
iotClusterVo.setGroup(CAA_ALL_CLIENT);

View File

@@ -53,6 +53,7 @@ public class IotDeviceHandler implements IotHander {
String realClientPort = channelContext.get("realClientPort") == null ? channelContext.getClientNode().getPort() + "" : channelContext.get("realClientPort").toString();
String realClient = realClientIp + ":" + realClientPort + ", bsId=" + channelContext.getBsId();
String text = new String(iotPacket.getBody(), IotPacket.CHARSET);
// log.info("{}, 接收到数据:{}", realClient, text);
String[] infos = text.split("\\$");
String reginfo = infos[0];
String[] regs = reginfo.split("-");
@@ -61,6 +62,21 @@ public class IotDeviceHandler implements IotHander {
String deviceData = null; //设备数据
boolean regFlag = false;
String errMsg = "{}, 传入参数错误,无注册信息,断开连接!传入参数:{}";
IotDeviceSessionContext sessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
deviceCode = sessionContext.getDeviceCode();
IotDeviceSessionForRedisContext deviceContext = null;
if(!ObjectUtils.isEmpty(deviceCode)){
deviceContext = redisSessionComponent.getByDeviceCode(deviceCode);
}
if(deviceContext == null
&& !reginfo.startsWith("reg-iot-")
&& !reginfo.startsWith("r-i-")
&& !reginfo.startsWith("iot-")){
//无注册信息,断开连接
log.error(errMsg, realClient, text);
Tio.close(channelContext, "无注册信息,断开连接!");
return null;
}
//设备注册信息
if ((reginfo.startsWith("reg-iot-") || reginfo.startsWith("r-i-") ) && regs.length == 4) {
deviceType = regs[2];
@@ -71,10 +87,8 @@ public class IotDeviceHandler implements IotHander {
deviceCode = regs[2];
deviceData = infos[1];
}else{
//无注册信息,断开连接
log.error(errMsg, realClient, text);
Tio.close(channelContext, "无注册信息,断开连接!");
return null;
deviceData = text;
deviceType = deviceContext.getDeviceType();
}
if(ObjectUtils.isEmpty(deviceCode)){
log.error(errMsg, realClient, text);
@@ -83,19 +97,18 @@ public class IotDeviceHandler implements IotHander {
}
if(regFlag){
redisSessionComponent.regDevice(channelContext, deviceCode, deviceType);
//在sessionContext记录设备id
sessionContext.setDeviceCode(deviceCode);
sessionContext.setDeviceId(IotUtils.transDeviceCodeToId(deviceCode));
return null;
}
IotDeviceSessionForRedisContext deviceContext = redisSessionComponent.getByDeviceCode(deviceCode);
if(deviceContext == null || Tio.getByBsId(channelContext.getTioConfig(), deviceCode) == null){
redisSessionComponent.regDevice(channelContext, deviceCode, deviceType);
}else{
//更新过期时间
redisSessionComponent.update(deviceContext);
}
//在sessionContext记录设备id
IotDeviceSessionContext sessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
sessionContext.setDeviceCode(deviceCode);
sessionContext.setDeviceId(IotUtils.transDeviceCodeToId(deviceCode));
IotDataHander iotDataHander = handlerMap.get(deviceType);
if (iotDataHander == null) {
log.error("{}, 找不到处理类,设备类型:{}", realClient, deviceType);