Commit 610ae6d53237c5c9705830bc4c3db12c1f0669a0

Authored by YunaiV
1 parent 432e1ed2

feat:【iot】优化 gateway 整体代码风格(空行)等

Showing 16 changed files with 31 additions and 65 deletions
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/mqtt/manager/IotMqttConnectionManager.java
... ... @@ -88,9 +88,9 @@ public class IotMqttConnectionManager {
88 88 connectionMap.remove(oldEndpoint);
89 89 }
90 90  
  91 + // 注册新连接
91 92 connectionMap.put(endpoint, connectionInfo);
92 93 deviceEndpointMap.put(deviceId, endpoint);
93   -
94 94 log.info("[registerConnection][注册设备连接,设备 ID: {},连接: {},product key: {},device name: {}]",
95 95 deviceId, getEndpointAddress(endpoint), connectionInfo.getProductKey(), connectionInfo.getDeviceName());
96 96 }
... ... @@ -102,13 +102,12 @@ public class IotMqttConnectionManager {
102 102 */
103 103 public void unregisterConnection(MqttEndpoint endpoint) {
104 104 ConnectionInfo connectionInfo = connectionMap.remove(endpoint);
105   - if (connectionInfo != null) {
106   - Long deviceId = connectionInfo.getDeviceId();
107   - deviceEndpointMap.remove(deviceId);
108   -
109   - log.info("[unregisterConnection][注销设备连接,设备 ID: {},连接: {}]", deviceId,
110   - getEndpointAddress(endpoint));
  105 + if (connectionInfo == null) {
  106 + return;
111 107 }
  108 + Long deviceId = connectionInfo.getDeviceId();
  109 + deviceEndpointMap.remove(deviceId);
  110 + log.info("[unregisterConnection][注销设备连接,设备 ID: {},连接: {}]", deviceId, getEndpointAddress(endpoint));
112 111 }
113 112  
114 113 /**
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/mqtt/router/IotMqttUpstreamHandler.java
... ... @@ -471,9 +471,7 @@ public class IotMqttUpstreamHandler {
471 471 /**
472 472 * 注册连接
473 473 */
474   - private void registerConnection(MqttEndpoint endpoint, IotDeviceRespDTO device,
475   - String clientId) {
476   -
  474 + private void registerConnection(MqttEndpoint endpoint, IotDeviceRespDTO device, String clientId) {
477 475 IotMqttConnectionManager.ConnectionInfo connectionInfo = new IotMqttConnectionManager.ConnectionInfo()
478 476 .setDeviceId(device.getId())
479 477 .setProductKey(device.getProductKey())
... ... @@ -481,7 +479,6 @@ public class IotMqttUpstreamHandler {
481 479 .setClientId(clientId)
482 480 .setAuthenticated(true)
483 481 .setRemoteAddress(connectionManager.getEndpointAddress(endpoint));
484   -
485 482 connectionManager.registerConnection(endpoint, device.getId(), connectionInfo);
486 483 }
487 484  
... ... @@ -510,15 +507,13 @@ public class IotMqttUpstreamHandler {
510 507 IotDeviceMessage offlineMessage = IotDeviceMessage.buildStateOffline();
511 508 deviceMessageService.sendDeviceMessage(offlineMessage, connectionInfo.getProductKey(),
512 509 connectionInfo.getDeviceName(), serverId);
513   - log.info("[cleanupConnection][设备离线,设备 ID: {},设备名称: {}]",
514   - connectionInfo.getDeviceId(), connectionInfo.getDeviceName());
  510 + log.info("[cleanupConnection][设备离线,设备 ID: {},设备名称: {}]", connectionInfo.getDeviceId(), connectionInfo.getDeviceName());
515 511 }
516 512  
517 513 // 注销连接
518 514 connectionManager.unregisterConnection(endpoint);
519 515 } catch (Exception e) {
520   - log.error("[cleanupConnection][清理连接失败,客户端 ID: {},错误: {}]",
521   - endpoint.clientIdentifier(), e.getMessage());
  516 + log.error("[cleanupConnection][清理连接失败,客户端 ID: {},错误: {}]", endpoint.clientIdentifier(), e.getMessage());
522 517 }
523 518 }
524 519  
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotTcpDownstreamSubscriber.java
... ... @@ -34,7 +34,7 @@ public class IotTcpDownstreamSubscriber implements IotMessageSubscriber<IotDevic
34 34 public void init() {
35 35 // 初始化下游处理器
36 36 this.downstreamHandler = new IotTcpDownstreamHandler(messageService, connectionManager);
37   -
  37 + // 注册下游订阅者
38 38 messageBus.register(this);
39 39 log.info("[init][TCP 下游订阅者初始化完成,服务器 ID: {},Topic: {}]",
40 40 protocol.getServerId(), getTopic());
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/manager/IotTcpConnectionManager.java
... ... @@ -51,9 +51,9 @@ public class IotTcpConnectionManager {
51 51 connectionMap.remove(oldSocket);
52 52 }
53 53  
  54 + // 注册新连接
54 55 connectionMap.put(socket, connectionInfo);
55 56 deviceSocketMap.put(deviceId, socket);
56   -
57 57 log.info("[registerConnection][注册设备连接,设备 ID: {},连接: {},product key: {},device name: {}]",
58 58 deviceId, socket.remoteAddress(), connectionInfo.getProductKey(), connectionInfo.getDeviceName());
59 59 }
... ... @@ -65,12 +65,12 @@ public class IotTcpConnectionManager {
65 65 */
66 66 public void unregisterConnection(NetSocket socket) {
67 67 ConnectionInfo connectionInfo = connectionMap.remove(socket);
68   - if (connectionInfo != null) {
69   - Long deviceId = connectionInfo.getDeviceId();
70   - deviceSocketMap.remove(deviceId);
71   - log.info("[unregisterConnection][注销设备连接,设备 ID: {},连接: {}]",
72   - deviceId, socket.remoteAddress());
  68 + if (connectionInfo == null) {
  69 + return;
73 70 }
  71 + Long deviceId = connectionInfo.getDeviceId();
  72 + deviceSocketMap.remove(deviceId);
  73 + log.info("[unregisterConnection][注销设备连接,设备 ID: {},连接: {}]", deviceId, socket.remoteAddress());
74 74 }
75 75  
76 76 /**
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/udp/IotUdpDownstreamSubscriber.java
... ... @@ -34,7 +34,7 @@ public class IotUdpDownstreamSubscriber implements IotMessageSubscriber<IotDevic
34 34 public void init() {
35 35 // 初始化下游处理器
36 36 this.downstreamHandler = new IotUdpDownstreamHandler(messageService, sessionManager, protocol);
37   -
  37 + // 注册下游订阅者
38 38 messageBus.register(this);
39 39 log.info("[init][UDP 下游订阅者初始化完成,服务器 ID: {},Topic: {}]",
40 40 protocol.getServerId(), getTopic());
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/websocket/IotWebSocketDownstreamSubscriber.java
... ... @@ -34,7 +34,7 @@ public class IotWebSocketDownstreamSubscriber implements IotMessageSubscriber<Io
34 34 public void init() {
35 35 // 初始化下游处理器
36 36 this.downstreamHandler = new IotWebSocketDownstreamHandler(messageService, connectionManager);
37   -
  37 + // 注册下游订阅者
38 38 messageBus.register(this);
39 39 log.info("[init][WebSocket 下游订阅者初始化完成,服务器 ID: {},Topic: {}]",
40 40 protocol.getServerId(), getTopic());
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/websocket/IotWebSocketUpstreamProtocol.java
... ... @@ -73,8 +73,7 @@ public class IotWebSocketUpstreamProtocol {
73 73 httpServer.webSocketHandler(socket -> {
74 74 // 验证路径
75 75 if (ObjUtil.notEqual(wsProperties.getPath(), socket.path())) {
76   - log.warn("[webSocketHandler][WebSocket 路径不匹配,拒绝连接,路径: {},期望: {}]",
77   - socket.path(), wsProperties.getPath());
  76 + log.warn("[webSocketHandler][WebSocket 路径不匹配,拒绝连接,路径: {},期望: {}]", socket.path(), wsProperties.getPath());
78 77 // TODO @AI:已经被废弃,看看换什么其他方法;
79 78 socket.reject();
80 79 return;
... ... @@ -89,8 +88,7 @@ public class IotWebSocketUpstreamProtocol {
89 88 // 3. 启动服务器
90 89 try {
91 90 httpServer.listen().result();
92   - log.info("[start][IoT 网关 WebSocket 协议启动成功,端口:{},路径:{}]",
93   - wsProperties.getPort(), wsProperties.getPath());
  91 + log.info("[start][IoT 网关 WebSocket 协议启动成功,端口:{},路径:{}]", wsProperties.getPort(), wsProperties.getPath());
94 92 } catch (Exception e) {
95 93 log.error("[start][IoT 网关 WebSocket 协议启动失败]", e);
96 94 throw e;
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/websocket/manager/IotWebSocketConnectionManager.java
... ... @@ -51,9 +51,9 @@ public class IotWebSocketConnectionManager {
51 51 connectionMap.remove(oldSocket);
52 52 }
53 53  
  54 + // 注册新连接
54 55 connectionMap.put(socket, connectionInfo);
55 56 deviceSocketMap.put(deviceId, socket);
56   -
57 57 log.info("[registerConnection][注册设备连接,设备 ID: {},连接: {},product key: {},device name: {}]",
58 58 deviceId, socket.remoteAddress(), connectionInfo.getProductKey(), connectionInfo.getDeviceName());
59 59 }
... ... @@ -65,12 +65,13 @@ public class IotWebSocketConnectionManager {
65 65 */
66 66 public void unregisterConnection(ServerWebSocket socket) {
67 67 ConnectionInfo connectionInfo = connectionMap.remove(socket);
68   - if (connectionInfo != null) {
69   - Long deviceId = connectionInfo.getDeviceId();
70   - deviceSocketMap.remove(deviceId);
71   - log.info("[unregisterConnection][注销设备连接,设备 ID: {},连接: {}]",
72   - deviceId, socket.remoteAddress());
  68 + if (connectionInfo == null) {
  69 + return;
73 70 }
  71 + Long deviceId = connectionInfo.getDeviceId();
  72 + deviceSocketMap.remove(deviceId);
  73 + log.info("[unregisterConnection][注销设备连接,设备 ID: {},连接: {}]",
  74 + deviceId, socket.remoteAddress());
74 75 }
75 76  
76 77 /**
... ...
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/websocket/router/IotWebSocketUpstreamHandler.java
... ... @@ -327,8 +327,7 @@ public class IotWebSocketUpstreamHandler implements Handler<ServerWebSocket> {
327 327 .build();
328 328  
329 329 int code = success ? 0 : 401;
330   - IotDeviceMessage responseMessage = IotDeviceMessage.replyOf(requestId, AUTH_METHOD, responseData,
331   - code, message);
  330 + IotDeviceMessage responseMessage = IotDeviceMessage.replyOf(requestId, AUTH_METHOD, responseData, code, message);
332 331  
333 332 byte[] encodedData = deviceMessageService.encodeDeviceMessage(responseMessage, CODEC_TYPE);
334 333 socket.writeTextMessage(new String(encodedData, StandardCharsets.UTF_8));
... ...
yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotDirectDeviceTcpProtocolIntegrationTest.java
... ... @@ -84,9 +84,6 @@ public class IotDirectDeviceTcpProtocolIntegrationTest {
84 84 // 1.2 编码
85 85 byte[] payload = CODEC.encode(request);
86 86 log.info("[testAuth][Codec: {}, 请求消息: {}, 数据包长度: {} 字节]", CODEC.type(), request, payload.length);
87   - if (CODEC instanceof IotTcpBinaryDeviceMessageCodec) {
88   - log.info("[testAuth][二进制数据包(HEX): {}]", HexUtil.encodeHexStr(payload));
89   - }
90 87  
91 88 // 2.1 发送请求
92 89 try (Socket socket = new Socket(SERVER_HOST, SERVER_PORT)) {
... ... @@ -123,9 +120,6 @@ public class IotDirectDeviceTcpProtocolIntegrationTest {
123 120 // 1.2 编码
124 121 byte[] payload = CODEC.encode(request);
125 122 log.info("[testDeviceRegister][Codec: {}, 请求消息: {}, 数据包长度: {} 字节]", CODEC.type(), request, payload.length);
126   - if (CODEC instanceof IotTcpBinaryDeviceMessageCodec) {
127   - log.info("[testDeviceRegister][二进制数据包(HEX): {}]", HexUtil.encodeHexStr(payload));
128   - }
129 123  
130 124 // 2.1 发送请求
131 125 try (Socket socket = new Socket(SERVER_HOST, SERVER_PORT)) {
... ...
yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotGatewayDeviceTcpProtocolIntegrationTest.java
1 1 package cn.iocoder.yudao.module.iot.gateway.protocol.tcp;
2 2  
3 3 import cn.hutool.core.map.MapUtil;
4   -import cn.hutool.core.util.HexUtil;
5 4 import cn.hutool.core.util.IdUtil;
6 5 import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO;
7 6 import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
... ... @@ -99,9 +98,6 @@ public class IotGatewayDeviceTcpProtocolIntegrationTest {
99 98 // 1.2 编码
100 99 byte[] payload = CODEC.encode(request);
101 100 log.info("[testAuth][Codec: {}, 请求消息: {}, 数据包长度: {} 字节]", CODEC.type(), request, payload.length);
102   - if (CODEC instanceof IotTcpBinaryDeviceMessageCodec) {
103   - log.info("[testAuth][二进制数据包(HEX): {}]", HexUtil.encodeHexStr(payload));
104   - }
105 101  
106 102 // 2.1 发送请求
107 103 try (Socket socket = new Socket(SERVER_HOST, SERVER_PORT)) {
... ...
yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotGatewaySubDeviceTcpProtocolIntegrationTest.java
1 1 package cn.iocoder.yudao.module.iot.gateway.protocol.tcp;
2 2  
3 3 import cn.hutool.core.map.MapUtil;
4   -import cn.hutool.core.util.HexUtil;
5 4 import cn.hutool.core.util.IdUtil;
6 5 import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO;
7 6 import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
... ... @@ -85,9 +84,6 @@ public class IotGatewaySubDeviceTcpProtocolIntegrationTest {
85 84 // 1.2 编码
86 85 byte[] payload = CODEC.encode(request);
87 86 log.info("[testAuth][Codec: {}, 请求消息: {}, 数据包长度: {} 字节]", CODEC.type(), request, payload.length);
88   - if (CODEC instanceof IotTcpBinaryDeviceMessageCodec) {
89   - log.info("[testAuth][二进制数据包(HEX): {}]", HexUtil.encodeHexStr(payload));
90   - }
91 87  
92 88 // 2.1 发送请求
93 89 try (Socket socket = new Socket(SERVER_HOST, SERVER_PORT)) {
... ...
yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/udp/IotDirectDeviceUdpProtocolIntegrationTest.java
1 1 package cn.iocoder.yudao.module.iot.gateway.protocol.udp;
2 2  
3 3 import cn.hutool.core.map.MapUtil;
4   -import cn.hutool.core.util.HexUtil;
5 4 import cn.hutool.core.util.IdUtil;
6 5 import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO;
7 6 import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
... ... @@ -90,9 +89,6 @@ public class IotDirectDeviceUdpProtocolIntegrationTest {
90 89 // 1.2 编码
91 90 byte[] payload = CODEC.encode(request);
92 91 log.info("[testAuth][Codec: {}, 请求消息: {}, 数据包长度: {} 字节]", CODEC.type(), request, payload.length);
93   - if (CODEC instanceof IotTcpBinaryDeviceMessageCodec) {
94   - log.info("[testAuth][二进制数据包(HEX): {}]", HexUtil.encodeHexStr(payload));
95   - }
96 92  
97 93 // 2.1 发送请求
98 94 try (DatagramSocket socket = new DatagramSocket()) {
... ... @@ -130,9 +126,6 @@ public class IotDirectDeviceUdpProtocolIntegrationTest {
130 126 // 1.2 编码
131 127 byte[] payload = CODEC.encode(request);
132 128 log.info("[testDeviceRegister][Codec: {}, 请求消息: {}, 数据包长度: {} 字节]", CODEC.type(), request, payload.length);
133   - if (CODEC instanceof IotTcpBinaryDeviceMessageCodec) {
134   - log.info("[testDeviceRegister][二进制数据包(HEX): {}]", HexUtil.encodeHexStr(payload));
135   - }
136 129  
137 130 // 2.1 发送请求
138 131 try (DatagramSocket socket = new DatagramSocket()) {
... ...
yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/udp/IotGatewayDeviceUdpProtocolIntegrationTest.java
1 1 package cn.iocoder.yudao.module.iot.gateway.protocol.udp;
2 2  
3 3 import cn.hutool.core.map.MapUtil;
4   -import cn.hutool.core.util.HexUtil;
5 4 import cn.hutool.core.util.IdUtil;
6 5 import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO;
7 6 import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
... ... @@ -102,9 +101,6 @@ public class IotGatewayDeviceUdpProtocolIntegrationTest {
102 101 // 1.2 编码
103 102 byte[] payload = CODEC.encode(request);
104 103 log.info("[testAuth][Codec: {}, 请求消息: {}, 数据包长度: {} 字节]", CODEC.type(), request, payload.length);
105   - if (CODEC instanceof IotTcpBinaryDeviceMessageCodec) {
106   - log.info("[testAuth][二进制数据包(HEX): {}]", HexUtil.encodeHexStr(payload));
107   - }
108 104  
109 105 // 2.1 发送请求
110 106 try (DatagramSocket socket = new DatagramSocket()) {
... ...
yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/udp/IotGatewaySubDeviceUdpProtocolIntegrationTest.java
1 1 package cn.iocoder.yudao.module.iot.gateway.protocol.udp;
2 2  
3 3 import cn.hutool.core.map.MapUtil;
4   -import cn.hutool.core.util.HexUtil;
5 4 import cn.hutool.core.util.IdUtil;
6 5 import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO;
7 6 import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
... ... @@ -91,9 +90,6 @@ public class IotGatewaySubDeviceUdpProtocolIntegrationTest {
91 90 // 1.2 编码
92 91 byte[] payload = CODEC.encode(request);
93 92 log.info("[testAuth][Codec: {}, 请求消息: {}, 数据包长度: {} 字节]", CODEC.type(), request, payload.length);
94   - if (CODEC instanceof IotTcpBinaryDeviceMessageCodec) {
95   - log.info("[testAuth][二进制数据包(HEX): {}]", HexUtil.encodeHexStr(payload));
96   - }
97 93  
98 94 // 2.1 发送请求
99 95 try (DatagramSocket socket = new DatagramSocket()) {
... ...
yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/websocket/IotDirectDeviceWebSocketProtocolIntegrationTest.java
... ... @@ -81,6 +81,8 @@ public class IotDirectDeviceWebSocketProtocolIntegrationTest {
81 81  
82 82 // ===================== 认证测试 =====================
83 83  
  84 + // TODO @AI:参考 /Users/yunai/Java/ruoyi-vue-pro-jdk25/yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotDirectDeviceTcpProtocolIntegrationTest.java 或 /Users/yunai/Java/ruoyi-vue-pro-jdk25/yudao-module-iot/yudao-module-iot-gateway/src/test/java/cn/iocoder/yudao/module/iot/gateway/protocol/mqtt/IotDirectDeviceMqttProtocolIntegrationTest.java 类,优化代码结构
  85 +
84 86 /**
85 87 * 认证测试:获取设备 Token
86 88 */
... ... @@ -96,6 +98,7 @@ public class IotDirectDeviceWebSocketProtocolIntegrationTest {
96 98 .setPort(SERVER_PORT)
97 99 .setURI(WS_PATH);
98 100  
  101 + // TODO @AI:这里有告警;Deprecate /instead use WebSocketClient.connect(WebSocketConnectOptions)
99 102 client.webSocket(options).onComplete(ar -> {
100 103 if (ar.succeeded()) {
101 104 WebSocket ws = ar.result();
... ...