feat:iot多服务实例支持
This commit is contained in:
@@ -4,12 +4,16 @@ import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.ChannelContextConstant;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionContext;
|
||||
import com.github.xingfudeshi.knife4j.annotations.ApiOperationSupport;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionForRedisContext;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.device.RedisSessionComponent;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.ChannelStatInfo;
|
||||
import tech.zzjc.tio.core.ChannelContext;
|
||||
@@ -41,8 +45,8 @@ import java.util.stream.Collectors;
|
||||
@Tag(name = "iot连接管理-连接统计")
|
||||
public class IotConnectManagerStatsController {
|
||||
|
||||
@Resource
|
||||
private TioServerBootstrap tioServerBootstrap;
|
||||
@Autowired private TioServerBootstrap tioServerBootstrap;
|
||||
@Autowired public RedisSessionComponent redisSessionComponent;
|
||||
|
||||
@GetMapping("/all-connect")
|
||||
@ApiOperationSupport(order = 1)
|
||||
@@ -89,11 +93,18 @@ public class IotConnectManagerStatsController {
|
||||
break;
|
||||
case ChannelContextConstant.CONNECT_DEVICE://设备
|
||||
IotDeviceSessionContext deviceSessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
|
||||
String deviceId = deviceSessionContext.getDeviceId();
|
||||
String deviceCode = deviceSessionContext.getDeviceCode();
|
||||
String deviceType = deviceSessionContext.getDeviceType();
|
||||
String deviceName = deviceSessionContext.getDeviceName();
|
||||
String controlRealName = deviceSessionContext.getControlRealName() == null ? "无" : deviceSessionContext.getControlRealName();
|
||||
IotDeviceSessionForRedisContext deviceContext = redisSessionComponent.getByDeviceCode(deviceCode);
|
||||
String deviceId = "";
|
||||
String deviceType = "";
|
||||
String deviceName = "";
|
||||
String controlRealName = "";
|
||||
if(deviceContext != null){
|
||||
deviceId = deviceContext.getDeviceId();
|
||||
deviceType = deviceContext.getDeviceType();
|
||||
deviceName = deviceContext.getDeviceName();
|
||||
controlRealName = deviceContext.getControlRealName();
|
||||
}
|
||||
info.setDeviceId(deviceId);
|
||||
info.setDeviceCode(deviceCode);
|
||||
info.setDeviceType(deviceType);
|
||||
@@ -193,6 +204,12 @@ public class IotConnectManagerStatsController {
|
||||
@Operation(summary = "关闭连接")
|
||||
public CommonResult<?> close(@RequestParam(name="id",required=true) String id) {
|
||||
ChannelContext channelContext = Tio.getByChannelContextId(tioServerBootstrap.getServerTioConfig(), id);
|
||||
IotDeviceSessionContext sessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
|
||||
if(sessionContext != null){
|
||||
String deviceId = sessionContext.getDeviceId();
|
||||
if(!ObjectUtils.isEmpty(deviceId))
|
||||
redisSessionComponent.deleteByDeviceId(deviceId);
|
||||
}
|
||||
Tio.IpBlacklist.clear();//清空全局黑名单
|
||||
Tio.close(channelContext, "控制端主动关闭连接");
|
||||
return CommonResult.success("ok");
|
||||
@@ -211,10 +228,14 @@ public class IotConnectManagerStatsController {
|
||||
Tio.IpBlacklist.clear();//清空全局黑名单
|
||||
if(channelContext != null) {
|
||||
IotDeviceSessionContext deviceSessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
|
||||
String deviceId = deviceSessionContext.getDeviceId();
|
||||
if(!ObjectUtils.isEmpty(deviceId)){
|
||||
redisSessionComponent.clearControlByDeviceId(deviceId, channelContext);
|
||||
}
|
||||
//设置控制点的channelContext
|
||||
deviceSessionContext.setControlChannelContext(null);
|
||||
deviceSessionContext.setControlRealName("");
|
||||
channelContext.set(ChannelContextConstant.CONTROL_DEVICE_ID, "");
|
||||
// deviceSessionContext.setControlChannelContext(null);
|
||||
// deviceSessionContext.setControlRealName("");
|
||||
// channelContext.set(ChannelContextConstant.CONTROL_DEVICE_ID, "");
|
||||
}
|
||||
return CommonResult.success("ok");
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@ package com.zt.plat.module.qms.iot.tcpserver;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.*;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.IotClientSessionContext;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionContext;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.device.RedisSessionComponent;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.proxy.ProxyProtocolDecoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import tech.zzjc.tio.common.starter.annotation.TioServerAioListener;
|
||||
import tech.zzjc.tio.core.ChannelContext;
|
||||
import tech.zzjc.tio.core.Tio;
|
||||
@@ -32,6 +35,7 @@ public class IotServerAioListener implements ServerAioListener {
|
||||
|
||||
// @Autowired
|
||||
// private IMeasurePointService measurePointService;
|
||||
@Autowired public RedisSessionComponent redisSessionComponent;
|
||||
|
||||
@Override
|
||||
public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
|
||||
@@ -68,6 +72,20 @@ public class IotServerAioListener implements ServerAioListener {
|
||||
|
||||
//连接断开,更新数据库连接状态
|
||||
IotClientSessionContext iotClientSessionContext = (IotClientSessionContext) channelContext.get(IotClientSessionContext.DEFAULT_CLIENT_SESSION_CONTEXT_KEY);
|
||||
IotDeviceSessionContext sessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
|
||||
|
||||
String deviceId = sessionContext.getDeviceId();
|
||||
log.info("onBeforeClose deviceId:" + deviceId);
|
||||
//设备断开,清除redis记录
|
||||
if(!ObjectUtils.isEmpty(deviceId)){
|
||||
redisSessionComponent.deleteByDeviceId(deviceId);
|
||||
}
|
||||
|
||||
//客户端断开,清除控制
|
||||
Object deviceCode = channelContext.get(ChannelContextConstant.CONTROL_DEVICE_ID);
|
||||
if(!ObjectUtils.isEmpty(deviceCode)){
|
||||
redisSessionComponent.clearControlByDeviceCode(deviceCode.toString(), channelContext);
|
||||
}
|
||||
|
||||
//解绑业务ID
|
||||
Tio.unbindBsId(channelContext);
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.zt.plat.module.qms.iot.tcpserver;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import tech.zzjc.tio.core.ChannelContext;
|
||||
import tech.zzjc.tio.core.Tio;
|
||||
|
||||
@@ -62,10 +63,14 @@ public class IotUtils {
|
||||
}
|
||||
|
||||
public static String transDeviceCodeToId(String deviceCode){
|
||||
if(ObjectUtils.isEmpty(deviceCode))
|
||||
return "";
|
||||
return new BigInteger(deviceCode, Character.MAX_RADIX).toString();
|
||||
}
|
||||
|
||||
public static String transDeviceIdToCode(String deviceId){
|
||||
if(ObjectUtils.isEmpty(deviceId))
|
||||
return "";
|
||||
return new BigInteger(deviceId).toString(Character.MAX_RADIX);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.zt.plat.module.qms.iot.tcpserver;
|
||||
import com.zt.plat.module.qms.core.constant.CommonConstant;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.ChannelContextConstant;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionContext;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.device.RedisSessionComponent;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.handler.IotClientHandler;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.handler.IotDeviceHandler;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.handler.IotHander;
|
||||
@@ -45,6 +46,7 @@ public class IotWebSocketMsgHandler implements IWsMsgHandler {
|
||||
|
||||
@Autowired
|
||||
private IotHeartbeatHandler iotHeartbeatHandler;
|
||||
@Autowired public RedisSessionComponent redisSessionComponent;
|
||||
|
||||
@Override
|
||||
public HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
|
||||
@@ -112,17 +114,10 @@ public class IotWebSocketMsgHandler implements IWsMsgHandler {
|
||||
switch (connectType) {
|
||||
case ChannelContextConstant.CONNECT_CAA_CLIENT:
|
||||
//判断是否控制设备,如果控制设备则取消设备控制
|
||||
String deviceIdCode = channelContext.get(ChannelContextConstant.CONTROL_DEVICE_ID) == null ? "" : channelContext.get(ChannelContextConstant.CONTROL_DEVICE_ID).toString();
|
||||
if (StringUtils.isNotBlank(deviceIdCode)) {
|
||||
ChannelContext deviceChannelContext = Tio.getByBsId(channelContext.getTioConfig(), deviceIdCode);
|
||||
if (deviceChannelContext != null) {
|
||||
IotDeviceSessionContext deviceSessionContext = (IotDeviceSessionContext) deviceChannelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
|
||||
deviceSessionContext.setControlChannelContext(null);
|
||||
deviceSessionContext.setControlRealName("");
|
||||
String deviceCode = channelContext.get(ChannelContextConstant.CONTROL_DEVICE_ID) == null ? "" : channelContext.get(ChannelContextConstant.CONTROL_DEVICE_ID).toString();
|
||||
if (StringUtils.isNotBlank(deviceCode)) {
|
||||
redisSessionComponent.clearControlByDeviceCode(deviceCode, channelContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,7 @@ package com.zt.plat.module.qms.iot.tcpserver.handler;
|
||||
|
||||
import com.zt.plat.module.qms.iot.tcpserver.IotDeviceType;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.IotPacket;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.IotUtils;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.ChannelContextConstant;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionContext;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionForRedisContext;
|
||||
@@ -9,7 +10,6 @@ import com.zt.plat.module.qms.iot.tcpserver.device.RedisSessionComponent;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.device.handler.IotDeviceBalanceHandler;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.device.handler.IotDeviceThermometerHandler;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.device.handler.IotDeviceWeighbridgeHandler;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.publisher.IotDeviceRegisterPublisher;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -17,7 +17,6 @@ import org.springframework.util.ObjectUtils;
|
||||
import tech.zzjc.tio.core.ChannelContext;
|
||||
import tech.zzjc.tio.core.Tio;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -83,47 +82,20 @@ public class IotDeviceHandler implements IotHander {
|
||||
return null;
|
||||
}
|
||||
if(regFlag){
|
||||
regDevice(channelContext, deviceCode, deviceType);
|
||||
redisSessionComponent.regDevice(channelContext, deviceCode, deviceType);
|
||||
return null;
|
||||
}
|
||||
IotDeviceSessionForRedisContext deviceContext = redisSessionComponent.getByDeviceCode(deviceCode);
|
||||
if(deviceContext == null){
|
||||
regDevice(channelContext, deviceCode, deviceType);
|
||||
if(deviceContext == null || Tio.getByBsId(channelContext.getTioConfig(), deviceCode) == null){
|
||||
redisSessionComponent.regDevice(channelContext, deviceCode, deviceType);
|
||||
}else{
|
||||
//更新过期时间
|
||||
redisSessionComponent.save(deviceCode, deviceType);
|
||||
redisSessionComponent.update(deviceContext);
|
||||
}
|
||||
//在sessionContext记录设备id
|
||||
IotDeviceSessionContext sessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
|
||||
sessionContext.setDeviceCode(deviceCode);
|
||||
// if (reginfo.startsWith("iot-") && regs.length == 3) {//设备数据携带注册
|
||||
// deviceType = regs[1];
|
||||
// deviceCode = regs[2];
|
||||
// deviceData = infos[1];
|
||||
// //判断是否注册过
|
||||
// if (StringUtils.isBlank(sessionContext.getDeviceId())) {
|
||||
// sessionContext.setDeviceId(new BigInteger(deviceCode, Character.MAX_RADIX).toString());
|
||||
// sessionContext.setDeviceType(deviceType);
|
||||
// sessionContext.setDeviceCode(deviceCode);
|
||||
// saveRegisterInfoToRedis(deviceCode, deviceType);
|
||||
// channelContext.set("regTime", new Date());
|
||||
//
|
||||
// //绑定业务id
|
||||
// Tio.bindBsId(channelContext, deviceCode);
|
||||
//
|
||||
// //设备注册事件
|
||||
// IotDeviceRegisterPublisher.publishEvent(deviceCode, deviceType);
|
||||
// }
|
||||
// } else {
|
||||
// deviceType = sessionContext.getDeviceType();
|
||||
// deviceCode = sessionContext.getDeviceCode();
|
||||
// deviceData = text;
|
||||
// if (Tio.getByBsId(channelContext.getTioConfig(), deviceCode) == null) {
|
||||
// log.info("{}, 数据传输过程中。未绑定业务id消失,重新绑定!", realClient);
|
||||
// //绑定业务id
|
||||
// Tio.bindBsId(channelContext, deviceCode);
|
||||
// }
|
||||
// }
|
||||
sessionContext.setDeviceId(IotUtils.transDeviceCodeToId(deviceCode));
|
||||
IotDataHander iotDataHander = handlerMap.get(deviceType);
|
||||
if (iotDataHander == null) {
|
||||
log.error("{}, 找不到处理类,设备类型:{}", realClient, deviceType);
|
||||
@@ -132,20 +104,4 @@ public class IotDeviceHandler implements IotHander {
|
||||
iotDataHander.hander(null, deviceData.trim(), channelContext);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void regDevice(ChannelContext channelContext, String deviceCode, String deviceType){
|
||||
redisSessionComponent.save(deviceCode, deviceType);
|
||||
channelContext.set("regTime", new Date());
|
||||
//绑定业务id
|
||||
Tio.bindBsId(channelContext, deviceCode);
|
||||
//设备注册事件
|
||||
IotDeviceRegisterPublisher.publishEvent(deviceCode, deviceType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
import tech.zzjc.tio.core.ChannelContext;
|
||||
import tech.zzjc.tio.core.Tio;
|
||||
import tech.zzjc.tio.starter.TioServerBootstrap;
|
||||
|
||||
@@ -55,6 +56,7 @@ public class IotDeviceRegisterListener {
|
||||
Map<String, Object> source = (Map<String, Object>) event.getSource();
|
||||
DeviceInfomationDO deviceInfo = (DeviceInfomationDO) source.get(EventConstant.EVENT_IOT_DEVICE_REGISTER);
|
||||
Long deviceId = deviceInfo.getId();
|
||||
String deviceType = deviceInfo.getCollectDeviceType();
|
||||
final String bsId = Long.toString(deviceId, Character.MAX_RADIX);
|
||||
if("temp".equals(deviceInfo.getCollectDeviceType())) {
|
||||
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
|
||||
@@ -68,27 +70,7 @@ public class IotDeviceRegisterListener {
|
||||
}
|
||||
}, 1, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
TenantContextHolder.setIgnore(true);
|
||||
try{
|
||||
deviceInfo = deviceInfomationService.getDeviceInfomation(deviceId);
|
||||
// ChannelContext channelContext = Tio.getByBsId(tioServerBootstrap.getServerTioConfig(), bsId);
|
||||
// channelContext.set("TenantId", deviceInfo.getTenantId());
|
||||
// IotDeviceSessionContext sessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
|
||||
// sessionContext.setDeviceName(deviceInfo.getDeviceCode());
|
||||
IotDeviceSessionForRedisContext context = redisSessionComponent.getByDeviceCode(bsId);
|
||||
context.setDeviceName(deviceInfo.getProductName());
|
||||
context.setTenantId(deviceInfo.getTenantId().toString());
|
||||
redisSessionComponent.update(context);
|
||||
deviceInfo.setIsConnect("1");
|
||||
deviceInfo.setLastConnectTime(LocalDateTime.now());
|
||||
deviceInfomationService.updateDeviceInfomation(deviceInfo);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
log.error("设备注册异常:{}", e.getMessage());
|
||||
}finally {
|
||||
TenantContextHolder.clear();
|
||||
}
|
||||
|
||||
|
||||
ChannelContext channelContext = Tio.getByBsId(tioServerBootstrap.getServerTioConfig(), bsId);
|
||||
redisSessionComponent.regDevice(channelContext, bsId, deviceType);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user