fix:iot多实例调整
This commit is contained in:
@@ -14,7 +14,7 @@ package com.zt.plat.module.qms.iot.tcpserver;
|
|||||||
*/
|
*/
|
||||||
public enum IotDeviceType {
|
public enum IotDeviceType {
|
||||||
INFRARED("infrared", "红外"),
|
INFRARED("infrared", "红外"),
|
||||||
BALANCE("auncel", "天平"),
|
BALANCE("balance", "天平"),
|
||||||
THERMOMETER("temp", "温度计"),
|
THERMOMETER("temp", "温度计"),
|
||||||
PACKING_MACHINE("packing_machine", "自动定量包装机"),
|
PACKING_MACHINE("packing_machine", "自动定量包装机"),
|
||||||
WEIGHBRIDGE("weighbridge", "地磅"),
|
WEIGHBRIDGE("weighbridge", "地磅"),
|
||||||
|
|||||||
@@ -36,5 +36,5 @@ public interface EventConstant {
|
|||||||
/**
|
/**
|
||||||
* 天平
|
* 天平
|
||||||
*/
|
*/
|
||||||
String EVENT_BALANCE = "auncel";
|
String EVENT_BALANCE = "balance";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class BalanceDataCommandLineRunner implements CommandLineRunner {
|
|||||||
private final TioServerBootstrap tioServerBootstrap;
|
private final TioServerBootstrap tioServerBootstrap;
|
||||||
|
|
||||||
//正确天平数据超时时间6s
|
//正确天平数据超时时间6s
|
||||||
private final static long AUNCEL_DATA_TIMEOUT = 6 * 1000L;
|
private final static long BALANCE_DATA_TIMEOUT = 6 * 1000L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(String... args) throws Exception {
|
public void run(String... args) throws Exception {
|
||||||
@@ -52,13 +52,13 @@ public class BalanceDataCommandLineRunner implements CommandLineRunner {
|
|||||||
public void run() {
|
public void run() {
|
||||||
//第一次先休息一下
|
//第一次先休息一下
|
||||||
try {
|
try {
|
||||||
Thread.sleep(AUNCEL_DATA_TIMEOUT);
|
Thread.sleep(BALANCE_DATA_TIMEOUT);
|
||||||
} catch (InterruptedException e1) {
|
} catch (InterruptedException e1) {
|
||||||
log.error(e1.toString(), e1);
|
log.error(e1.toString(), e1);
|
||||||
}
|
}
|
||||||
while (!tioServerBootstrap.getServerTioConfig().isStopped()) {
|
while (!tioServerBootstrap.getServerTioConfig().isStopped()) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(AUNCEL_DATA_TIMEOUT);
|
Thread.sleep(BALANCE_DATA_TIMEOUT);
|
||||||
} catch (InterruptedException e1) {
|
} catch (InterruptedException e1) {
|
||||||
log.error(e1.toString(), 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 compareTime = lastBalanceDataTime == null ? SystemTimer.currTime : Long.parseLong(lastBalanceDataTime.toString()) ;
|
||||||
long currtime = SystemTimer.currTime;
|
long currtime = SystemTimer.currTime;
|
||||||
long interval = currtime - compareTime;
|
long interval = currtime - compareTime;
|
||||||
if (interval > AUNCEL_DATA_TIMEOUT) {
|
if (interval > BALANCE_DATA_TIMEOUT) {
|
||||||
// sessionContext.setControlRealName("");
|
// sessionContext.setControlRealName("");
|
||||||
// sessionContext.setControlChannelContext(null);
|
// sessionContext.setControlChannelContext(null);
|
||||||
sessionContext.setWeightValueList(null);
|
sessionContext.setWeightValueList(null);
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class RedisSessionComponent {
|
|||||||
String deviceId = IotUtils.transDeviceCodeToId(context.getDeviceCode());
|
String deviceId = IotUtils.transDeviceCodeToId(context.getDeviceCode());
|
||||||
context.setRegTime(new Date());
|
context.setRegTime(new Date());
|
||||||
String key = getRedisKeyByDeviceId(deviceId);
|
String key = getRedisKeyByDeviceId(deviceId);
|
||||||
log.info("key={}", key);
|
// log.info("key={}", key);
|
||||||
redisTemplate.opsForValue().set(key, context, timeoutSeconds, TimeUnit.SECONDS);
|
redisTemplate.opsForValue().set(key, context, timeoutSeconds, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ public class IotDeviceBalanceHandler implements IotDataHander {
|
|||||||
Tio.sendToGroup(tioConfig, CAA_ALL_CLIENT, packet);
|
Tio.sendToGroup(tioConfig, CAA_ALL_CLIENT, packet);
|
||||||
|
|
||||||
//通知天平数据到其他服务实例
|
//通知天平数据到其他服务实例
|
||||||
log.info("天平数据:" + realClient + ":" + data);
|
// log.info("天平数据:" + realClient + ":" + data);
|
||||||
TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();
|
TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();
|
||||||
TioClusterVo iotClusterVo = new TioClusterVo(packet);
|
TioClusterVo iotClusterVo = new TioClusterVo(packet);
|
||||||
iotClusterVo.setGroup(CAA_ALL_CLIENT);
|
iotClusterVo.setGroup(CAA_ALL_CLIENT);
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ public class IotDeviceHandler implements IotHander {
|
|||||||
String realClientPort = channelContext.get("realClientPort") == null ? channelContext.getClientNode().getPort() + "" : channelContext.get("realClientPort").toString();
|
String realClientPort = channelContext.get("realClientPort") == null ? channelContext.getClientNode().getPort() + "" : channelContext.get("realClientPort").toString();
|
||||||
String realClient = realClientIp + ":" + realClientPort + ", bsId=" + channelContext.getBsId();
|
String realClient = realClientIp + ":" + realClientPort + ", bsId=" + channelContext.getBsId();
|
||||||
String text = new String(iotPacket.getBody(), IotPacket.CHARSET);
|
String text = new String(iotPacket.getBody(), IotPacket.CHARSET);
|
||||||
|
// log.info("{}, 接收到数据:{}", realClient, text);
|
||||||
String[] infos = text.split("\\$");
|
String[] infos = text.split("\\$");
|
||||||
String reginfo = infos[0];
|
String reginfo = infos[0];
|
||||||
String[] regs = reginfo.split("-");
|
String[] regs = reginfo.split("-");
|
||||||
@@ -61,6 +62,21 @@ public class IotDeviceHandler implements IotHander {
|
|||||||
String deviceData = null; //设备数据
|
String deviceData = null; //设备数据
|
||||||
boolean regFlag = false;
|
boolean regFlag = false;
|
||||||
String errMsg = "{}, 传入参数错误,无注册信息,断开连接!传入参数:{}";
|
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) {
|
if ((reginfo.startsWith("reg-iot-") || reginfo.startsWith("r-i-") ) && regs.length == 4) {
|
||||||
deviceType = regs[2];
|
deviceType = regs[2];
|
||||||
@@ -71,10 +87,8 @@ public class IotDeviceHandler implements IotHander {
|
|||||||
deviceCode = regs[2];
|
deviceCode = regs[2];
|
||||||
deviceData = infos[1];
|
deviceData = infos[1];
|
||||||
}else{
|
}else{
|
||||||
//无注册信息,断开连接
|
deviceData = text;
|
||||||
log.error(errMsg, realClient, text);
|
deviceType = deviceContext.getDeviceType();
|
||||||
Tio.close(channelContext, "无注册信息,断开连接!");
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
if(ObjectUtils.isEmpty(deviceCode)){
|
if(ObjectUtils.isEmpty(deviceCode)){
|
||||||
log.error(errMsg, realClient, text);
|
log.error(errMsg, realClient, text);
|
||||||
@@ -83,19 +97,18 @@ public class IotDeviceHandler implements IotHander {
|
|||||||
}
|
}
|
||||||
if(regFlag){
|
if(regFlag){
|
||||||
redisSessionComponent.regDevice(channelContext, deviceCode, deviceType);
|
redisSessionComponent.regDevice(channelContext, deviceCode, deviceType);
|
||||||
|
//在sessionContext记录设备id
|
||||||
|
sessionContext.setDeviceCode(deviceCode);
|
||||||
|
sessionContext.setDeviceId(IotUtils.transDeviceCodeToId(deviceCode));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
IotDeviceSessionForRedisContext deviceContext = redisSessionComponent.getByDeviceCode(deviceCode);
|
|
||||||
if(deviceContext == null || Tio.getByBsId(channelContext.getTioConfig(), deviceCode) == null){
|
if(deviceContext == null || Tio.getByBsId(channelContext.getTioConfig(), deviceCode) == null){
|
||||||
redisSessionComponent.regDevice(channelContext, deviceCode, deviceType);
|
redisSessionComponent.regDevice(channelContext, deviceCode, deviceType);
|
||||||
}else{
|
}else{
|
||||||
//更新过期时间
|
//更新过期时间
|
||||||
redisSessionComponent.update(deviceContext);
|
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);
|
IotDataHander iotDataHander = handlerMap.get(deviceType);
|
||||||
if (iotDataHander == null) {
|
if (iotDataHander == null) {
|
||||||
log.error("{}, 找不到处理类,设备类型:{}", realClient, deviceType);
|
log.error("{}, 找不到处理类,设备类型:{}", realClient, deviceType);
|
||||||
|
|||||||
Reference in New Issue
Block a user