Merge branch 'test' of https://git.will-way.cn/zgty/zt-qms into test
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:
|
||||
|
||||
@@ -51,7 +51,6 @@ public class IotCaaClientControlDeviceHander implements IotDataHander {
|
||||
String realClient = realClientIp + ":" + realClientPort;
|
||||
log.info("{},, APP客户端控制设备 :{}", realClient, controlDevice);
|
||||
String deviceId = controlDevice.getDeviceId();
|
||||
log.info("设备ID::{}", deviceId);
|
||||
if (StringUtils.isBlank(deviceId)) {
|
||||
JSONObject json = null;
|
||||
json = new JSONObject();
|
||||
@@ -68,54 +67,10 @@ public class IotCaaClientControlDeviceHander implements IotDataHander {
|
||||
}
|
||||
String deviceCode = IotUtils.transDeviceIdToCode(deviceId);
|
||||
ChannelContext deviceChannelContext = Tio.getByBsId(channelContext.getTioConfig(), deviceCode);
|
||||
IotDeviceSessionForRedisContext deviceContext = redisSessionComponent.getByDeviceId(deviceId);
|
||||
|
||||
IotPacket iotPacket = new IotPacket();
|
||||
JSONObject json = null;
|
||||
if (deviceChannelContext != null) {
|
||||
// IotDeviceSessionContext deviceSessionContext = (IotDeviceSessionContext) deviceChannelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
|
||||
ChannelContext oldDeviceChannelContext = deviceContext.getControlChannelContext();
|
||||
if (controlDevice.getIsControl()) {
|
||||
//设置控制点的channelContext
|
||||
deviceContext.setControlChannelContext(channelContext);
|
||||
deviceContext.setControlRealName(controlDevice.getControlRealName());
|
||||
|
||||
controlDevice.setMsg("设备被“" + controlDevice.getControlRealName() + "”远程控制!");
|
||||
channelContext.set(ChannelContextConstant.CONTROL_DEVICE_ID, deviceCode);
|
||||
} else {
|
||||
//设置控制点的channelContext
|
||||
deviceContext.setControlChannelContext(null);
|
||||
deviceContext.setControlRealName("");
|
||||
controlDevice.setMsg("“" + controlDevice.getControlRealName() + "”取消设备远程控制!");
|
||||
channelContext.set(ChannelContextConstant.CONTROL_DEVICE_ID, "");
|
||||
}
|
||||
redisSessionComponent.update(deviceContext);
|
||||
//获取客户端连接信息
|
||||
String deviceRealClientIp = deviceChannelContext.get("realClientIp") == null ? deviceChannelContext.getClientNode().getIp() : deviceChannelContext.get("realClientIp").toString();
|
||||
String deviceRealClientPort = deviceChannelContext.get("realClientPort") == null ? deviceChannelContext.getClientNode().getPort() + "" : deviceChannelContext.get("realClientPort").toString();
|
||||
String deviceRealClient = deviceRealClientIp + ":" + deviceRealClientPort;
|
||||
log.info("{}, 设备:{},旧控制:{},新的控制:{}", realClient, deviceRealClient, oldDeviceChannelContext, realClient);
|
||||
|
||||
json = new JSONObject();
|
||||
json.put("msgId", msgId);
|
||||
json.put("cmd", Command.REPLY_CONTROL_DEVICE.getName());
|
||||
json.put("clientType", ClientType.CAA_CLIENT.getName());
|
||||
controlDevice.setSuccess(true);
|
||||
json.put("data", controlDevice);
|
||||
iotPacket.setBody((json.toJSONString() + "\r\n").getBytes(Charset.forName(IotPacket.CHARSET)));
|
||||
|
||||
//todo 发送通知
|
||||
TioConfig tioConfig = deviceChannelContext.getTioConfig();
|
||||
TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();
|
||||
tioClusterConfig.publish(new TioClusterVo(iotPacket));
|
||||
Tio.sendToGroup(channelContext.getTioConfig(), CAA_ALL_CLIENT, iotPacket, new ChannelContextFilter() {
|
||||
@Override
|
||||
public boolean filter(ChannelContext cc) {
|
||||
if (channelContext.getId().equals(cc.getId())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}});
|
||||
} else {
|
||||
if (deviceChannelContext == null) {
|
||||
json = new JSONObject();
|
||||
json.put("msgId", msgId);
|
||||
json.put("cmd", Command.REPLY_CONTROL_DEVICE.getName());
|
||||
@@ -125,7 +80,52 @@ public class IotCaaClientControlDeviceHander implements IotDataHander {
|
||||
json.put("data", controlDevice);
|
||||
iotPacket.setBody((json.toJSONString() + "\r\n").getBytes(Charset.forName(IotPacket.CHARSET)));
|
||||
Tio.send(channelContext, iotPacket);
|
||||
return null;
|
||||
}
|
||||
if (controlDevice.getIsControl()) {
|
||||
//控制
|
||||
// deviceContext.setControlChannelContext(channelContext);
|
||||
IotDeviceSessionForRedisContext deviceContext = redisSessionComponent.getByDeviceId(deviceId);
|
||||
deviceContext.setControlRealName(controlDevice.getControlRealName());
|
||||
channelContext.set(ChannelContextConstant.CONTROL_DEVICE_ID, deviceCode);
|
||||
controlDevice.setMsg("设备被“" + controlDevice.getControlRealName() + "”远程控制!");
|
||||
redisSessionComponent.update(deviceContext);
|
||||
} else {
|
||||
//释放
|
||||
redisSessionComponent.clearControlByDeviceId(deviceId, channelContext);
|
||||
controlDevice.setMsg("“" + controlDevice.getControlRealName() + "”取消设备远程控制!");
|
||||
}
|
||||
|
||||
//获取客户端连接信息
|
||||
// String deviceRealClientIp = deviceChannelContext.get("realClientIp") == null ? deviceChannelContext.getClientNode().getIp() : deviceChannelContext.get("realClientIp").toString();
|
||||
// String deviceRealClientPort = deviceChannelContext.get("realClientPort") == null ? deviceChannelContext.getClientNode().getPort() + "" : deviceChannelContext.get("realClientPort").toString();
|
||||
// String deviceRealClient = deviceRealClientIp + ":" + deviceRealClientPort;
|
||||
// log.info("{}, 设备:{},旧控制:{},新的控制:{}", realClient, deviceRealClient, oldDeviceChannelContext, realClient);
|
||||
|
||||
json = new JSONObject();
|
||||
json.put("msgId", msgId);
|
||||
json.put("cmd", Command.REPLY_CONTROL_DEVICE.getName());
|
||||
json.put("clientType", ClientType.CAA_CLIENT.getName());
|
||||
controlDevice.setSuccess(true);
|
||||
json.put("data", controlDevice);
|
||||
iotPacket.setBody((json.toJSONString() + "\r\n").getBytes(Charset.forName(IotPacket.CHARSET)));
|
||||
//发送通知到其他服务实例
|
||||
TioConfig tioConfig = deviceChannelContext.getTioConfig();
|
||||
TioClusterConfig tioClusterConfig = tioConfig.getTioClusterConfig();
|
||||
TioClusterVo iotClusterVo = new TioClusterVo(iotPacket);
|
||||
iotClusterVo.setGroup(CAA_ALL_CLIENT);
|
||||
tioClusterConfig.publish(iotClusterVo);
|
||||
|
||||
//发送消息到本服务实例连接的客户端
|
||||
Tio.sendToGroup(channelContext.getTioConfig(), CAA_ALL_CLIENT, iotPacket, new ChannelContextFilter() {
|
||||
@Override
|
||||
public boolean filter(ChannelContext cc) {
|
||||
if (channelContext.getId().equals(cc.getId())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}}
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,30 +1,57 @@
|
||||
package com.zt.plat.module.qms.iot.tcpserver.device;
|
||||
|
||||
import com.zt.plat.framework.tenant.core.context.TenantContextHolder;
|
||||
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.IotDeviceSessionForRedisContext;
|
||||
import com.zt.plat.module.qms.iot.tcpserver.publisher.IotDeviceRegisterPublisher;
|
||||
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceInfomationDO;
|
||||
import com.zt.plat.module.qms.resource.device.service.DeviceInfomationService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tech.zzjc.tio.core.ChannelContext;
|
||||
import tech.zzjc.tio.core.Tio;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class RedisSessionComponent {
|
||||
|
||||
@Autowired
|
||||
public RedisTemplate redisTemplate;
|
||||
@Autowired private RedisTemplate redisTemplate;
|
||||
@Autowired private DeviceInfomationService deviceInfomationService;
|
||||
private final long timeoutSeconds = 60;
|
||||
|
||||
|
||||
public void save(String deviceCode, String deviceType){
|
||||
long timeoutSeconds = 60;
|
||||
String deviceId = IotUtils.transDeviceCodeToId(deviceCode);
|
||||
IotDeviceSessionForRedisContext context = new IotDeviceSessionForRedisContext();
|
||||
context.setDeviceId(deviceId);
|
||||
context.setDeviceType(deviceType);
|
||||
context.setDeviceCode(deviceCode);
|
||||
public void update(IotDeviceSessionForRedisContext context){
|
||||
String deviceId = IotUtils.transDeviceCodeToId(context.getDeviceCode());
|
||||
context.setRegTime(new Date());
|
||||
redisTemplate.opsForValue().set(getRedisKey(deviceId), context, timeoutSeconds, TimeUnit.SECONDS);
|
||||
String key = getRedisKeyByDeviceId(deviceId);
|
||||
log.info("key={}", key);
|
||||
redisTemplate.opsForValue().set(key, context, timeoutSeconds, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void clearControlByDeviceCode(String deviceCode, ChannelContext channelContext){
|
||||
IotDeviceSessionForRedisContext context = getByDeviceCode(deviceCode);
|
||||
context.setControlRealName("");
|
||||
context.setControlChannelContext(null);
|
||||
channelContext.set(ChannelContextConstant.CONTROL_DEVICE_ID, "");
|
||||
redisTemplate.opsForValue().set(getRedisKeyByDeviceCode(deviceCode), context, timeoutSeconds, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void clearControlByDeviceId(String deviceId, ChannelContext channelContext){
|
||||
IotDeviceSessionForRedisContext context = getByDeviceId(deviceId);
|
||||
context.setControlRealName("");
|
||||
context.setControlChannelContext(null);
|
||||
channelContext.set(ChannelContextConstant.CONTROL_DEVICE_ID, "");
|
||||
redisTemplate.opsForValue().set(getRedisKeyByDeviceId(deviceId), context, timeoutSeconds, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void deleteByDeviceId(String deviceId){
|
||||
redisTemplate.delete(getRedisKeyByDeviceCode(deviceId));
|
||||
}
|
||||
|
||||
public IotDeviceSessionForRedisContext getByDeviceCode(String deviceCode){
|
||||
@@ -33,19 +60,56 @@ public class RedisSessionComponent {
|
||||
}
|
||||
|
||||
public IotDeviceSessionForRedisContext getByDeviceId(String deviceId){
|
||||
return (IotDeviceSessionForRedisContext) redisTemplate.opsForValue().get(getRedisKey(deviceId));
|
||||
return (IotDeviceSessionForRedisContext) redisTemplate.opsForValue().get(getRedisKeyByDeviceId(deviceId));
|
||||
}
|
||||
|
||||
public String getRedisKey(String deviceCode){
|
||||
public String getRedisKeyByDeviceCode(String deviceCode){
|
||||
String deviceId = IotUtils.transDeviceCodeToId(deviceCode);
|
||||
return getRedisKeyByDeviceId(deviceId);
|
||||
}
|
||||
public String getRedisKeyByDeviceId(String deviceId){
|
||||
String redisKey = IotDeviceSessionForRedisContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY + "-" + deviceId;
|
||||
return redisKey;
|
||||
}
|
||||
|
||||
public void update(IotDeviceSessionForRedisContext context){
|
||||
long timeoutSeconds = 60;
|
||||
String deviceId = IotUtils.transDeviceCodeToId(context.getDeviceCode());
|
||||
context.setRegTime(new Date());
|
||||
redisTemplate.opsForValue().set(getRedisKey(deviceId), context, timeoutSeconds, TimeUnit.SECONDS);
|
||||
public void regDevice(ChannelContext channelContext, String deviceCode, String deviceType){
|
||||
IotDeviceSessionForRedisContext deviceContext = getByDeviceCode(deviceCode);
|
||||
if(deviceContext != null && Tio.getByBsId(channelContext.getTioConfig(), deviceCode) != null){
|
||||
return;
|
||||
}
|
||||
|
||||
TenantContextHolder.setIgnore(true);
|
||||
try{
|
||||
String deviceId = IotUtils.transDeviceCodeToId(deviceCode);
|
||||
DeviceInfomationDO deviceInfo = deviceInfomationService.getDeviceInfomation(Long.valueOf(deviceId));
|
||||
IotDeviceSessionForRedisContext context = new IotDeviceSessionForRedisContext();
|
||||
context.setDeviceId(deviceId);
|
||||
context.setDeviceType(deviceType);
|
||||
context.setDeviceCode(deviceCode);
|
||||
if(deviceInfo != null){
|
||||
context.setDeviceName(deviceInfo.getProductName());
|
||||
context.setTenantId(deviceInfo.getTenantId().toString());
|
||||
deviceInfo.setIsConnect("1");
|
||||
deviceInfo.setLastConnectTime(LocalDateTime.now());
|
||||
deviceInfomationService.updateDeviceInfomation(deviceInfo);
|
||||
}
|
||||
context.setRegTime(new Date());
|
||||
context.setControlRealName( "");
|
||||
redisTemplate.opsForValue().set(getRedisKeyByDeviceId(deviceId), context, timeoutSeconds, TimeUnit.SECONDS);
|
||||
|
||||
channelContext.set("regTime", new Date());
|
||||
//绑定业务id
|
||||
Tio.bindBsId(channelContext, deviceCode);
|
||||
//设备注册事件
|
||||
IotDeviceRegisterPublisher.publishEvent(deviceCode, deviceType);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
log.error("设备注册异常:{}", e.getMessage());
|
||||
}finally {
|
||||
TenantContextHolder.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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