Commit bf41d47fa843e29a7bbce04aa418bc539f6f2f52
1 parent
a5a3aea5
review:【IoT 物联网】网关 TCP 的接入代码
Showing
7 changed files
with
21 additions
and
10 deletions
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java
| @@ -85,6 +85,7 @@ public class IotGatewayConfiguration { | @@ -85,6 +85,7 @@ public class IotGatewayConfiguration { | ||
| 85 | @Slf4j | 85 | @Slf4j |
| 86 | public static class TcpProtocolConfiguration { | 86 | public static class TcpProtocolConfiguration { |
| 87 | 87 | ||
| 88 | + // TODO @haohao:close | ||
| 88 | @Bean | 89 | @Bean |
| 89 | public Vertx tcpVertx() { | 90 | public Vertx tcpVertx() { |
| 90 | return Vertx.vertx(); | 91 | return Vertx.vertx(); |
| @@ -92,16 +93,17 @@ public class IotGatewayConfiguration { | @@ -92,16 +93,17 @@ public class IotGatewayConfiguration { | ||
| 92 | 93 | ||
| 93 | @Bean | 94 | @Bean |
| 94 | public IotTcpUpstreamProtocol iotTcpUpstreamProtocol(Vertx tcpVertx, IotGatewayProperties gatewayProperties, | 95 | public IotTcpUpstreamProtocol iotTcpUpstreamProtocol(Vertx tcpVertx, IotGatewayProperties gatewayProperties, |
| 95 | - IotTcpConnectionManager connectionManager, IotDeviceMessageService messageService, | ||
| 96 | - IotDeviceService deviceService, IotDeviceCommonApi deviceApi) { | 96 | + IotTcpConnectionManager connectionManager, |
| 97 | + IotDeviceMessageService messageService, | ||
| 98 | + IotDeviceService deviceService, IotDeviceCommonApi deviceApi) { | ||
| 97 | return new IotTcpUpstreamProtocol(tcpVertx, gatewayProperties, connectionManager, | 99 | return new IotTcpUpstreamProtocol(tcpVertx, gatewayProperties, connectionManager, |
| 98 | messageService, deviceService, deviceApi); | 100 | messageService, deviceService, deviceApi); |
| 99 | } | 101 | } |
| 100 | 102 | ||
| 101 | @Bean | 103 | @Bean |
| 102 | public IotTcpDownstreamSubscriber iotTcpDownstreamSubscriber(IotTcpUpstreamProtocol tcpUpstreamProtocol, | 104 | public IotTcpDownstreamSubscriber iotTcpDownstreamSubscriber(IotTcpUpstreamProtocol tcpUpstreamProtocol, |
| 103 | - IotMessageBus messageBus, | ||
| 104 | - IotTcpDownstreamHandler downstreamHandler) { | 105 | + IotMessageBus messageBus, |
| 106 | + IotTcpDownstreamHandler downstreamHandler) { | ||
| 105 | return new IotTcpDownstreamSubscriber(tcpUpstreamProtocol, messageBus, downstreamHandler); | 107 | return new IotTcpDownstreamSubscriber(tcpUpstreamProtocol, messageBus, downstreamHandler); |
| 106 | } | 108 | } |
| 107 | 109 |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java
| @@ -108,10 +108,12 @@ public class IotGatewayProperties { | @@ -108,10 +108,12 @@ public class IotGatewayProperties { | ||
| 108 | */ | 108 | */ |
| 109 | @NotNull(message = "是否开启不能为空") | 109 | @NotNull(message = "是否开启不能为空") |
| 110 | private Boolean enabled; | 110 | private Boolean enabled; |
| 111 | + // TODO @haohao:加个默认值? | ||
| 111 | /** | 112 | /** |
| 112 | * 服务端口 | 113 | * 服务端口 |
| 113 | */ | 114 | */ |
| 114 | private Integer serverPort; | 115 | private Integer serverPort; |
| 116 | + // TODO @haohao:应该不用?一般都监听 0.0.0.0 哈; | ||
| 115 | /** | 117 | /** |
| 116 | * 服务主机 | 118 | * 服务主机 |
| 117 | */ | 119 | */ |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/router/IotEmqxDownstreamHandler.java
| @@ -79,7 +79,7 @@ public class IotEmqxDownstreamHandler { | @@ -79,7 +79,7 @@ public class IotEmqxDownstreamHandler { | ||
| 79 | // 2. 根据消息方法和回复状态,构建 topic | 79 | // 2. 根据消息方法和回复状态,构建 topic |
| 80 | boolean isReply = IotDeviceMessageUtils.isReplyMessage(message); | 80 | boolean isReply = IotDeviceMessageUtils.isReplyMessage(message); |
| 81 | 81 | ||
| 82 | - // TODO @芋艿:看看基于 message 的 method 去反向推导; | 82 | + // TODO @haohao:看看基于 message 的 method 去反向推导; |
| 83 | // 3. 根据消息方法类型构建对应的主题 | 83 | // 3. 根据消息方法类型构建对应的主题 |
| 84 | switch (methodEnum) { | 84 | switch (methodEnum) { |
| 85 | case PROPERTY_POST: | 85 | case PROPERTY_POST: |
| @@ -88,21 +88,18 @@ public class IotEmqxDownstreamHandler { | @@ -88,21 +88,18 @@ public class IotEmqxDownstreamHandler { | ||
| 88 | return IotMqttTopicUtils.buildPropertyPostReplyTopic(productKey, deviceName); | 88 | return IotMqttTopicUtils.buildPropertyPostReplyTopic(productKey, deviceName); |
| 89 | } | 89 | } |
| 90 | break; | 90 | break; |
| 91 | - | ||
| 92 | case PROPERTY_SET: | 91 | case PROPERTY_SET: |
| 93 | // 属性设置:只支持非回复消息(下行) | 92 | // 属性设置:只支持非回复消息(下行) |
| 94 | if (!isReply) { | 93 | if (!isReply) { |
| 95 | return IotMqttTopicUtils.buildPropertySetTopic(productKey, deviceName); | 94 | return IotMqttTopicUtils.buildPropertySetTopic(productKey, deviceName); |
| 96 | } | 95 | } |
| 97 | break; | 96 | break; |
| 98 | - | ||
| 99 | case EVENT_POST: | 97 | case EVENT_POST: |
| 100 | // 事件上报:只支持回复消息(下行) | 98 | // 事件上报:只支持回复消息(下行) |
| 101 | if (isReply) { | 99 | if (isReply) { |
| 102 | return IotMqttTopicUtils.buildEventPostReplyTopicGeneric(productKey, deviceName); | 100 | return IotMqttTopicUtils.buildEventPostReplyTopicGeneric(productKey, deviceName); |
| 103 | } | 101 | } |
| 104 | break; | 102 | break; |
| 105 | - | ||
| 106 | case SERVICE_INVOKE: | 103 | case SERVICE_INVOKE: |
| 107 | // 服务调用:支持请求和回复 | 104 | // 服务调用:支持请求和回复 |
| 108 | if (isReply) { | 105 | if (isReply) { |
| @@ -110,14 +107,12 @@ public class IotEmqxDownstreamHandler { | @@ -110,14 +107,12 @@ public class IotEmqxDownstreamHandler { | ||
| 110 | } else { | 107 | } else { |
| 111 | return IotMqttTopicUtils.buildServiceTopicGeneric(productKey, deviceName); | 108 | return IotMqttTopicUtils.buildServiceTopicGeneric(productKey, deviceName); |
| 112 | } | 109 | } |
| 113 | - | ||
| 114 | case CONFIG_PUSH: | 110 | case CONFIG_PUSH: |
| 115 | // 配置推送:平台向设备推送配置(下行请求),设备回复确认(上行回复) | 111 | // 配置推送:平台向设备推送配置(下行请求),设备回复确认(上行回复) |
| 116 | if (!isReply) { | 112 | if (!isReply) { |
| 117 | return IotMqttTopicUtils.buildConfigPushTopic(productKey, deviceName); | 113 | return IotMqttTopicUtils.buildConfigPushTopic(productKey, deviceName); |
| 118 | } | 114 | } |
| 119 | break; | 115 | break; |
| 120 | - | ||
| 121 | default: | 116 | default: |
| 122 | log.warn("[buildTopicByMethod][未处理的消息方法: {}]", methodEnum); | 117 | log.warn("[buildTopicByMethod][未处理的消息方法: {}]", methodEnum); |
| 123 | break; | 118 | break; |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotTcpConnectionManager.java
| @@ -16,6 +16,7 @@ import java.util.concurrent.ConcurrentMap; | @@ -16,6 +16,7 @@ import java.util.concurrent.ConcurrentMap; | ||
| 16 | @Slf4j | 16 | @Slf4j |
| 17 | public class IotTcpConnectionManager { | 17 | public class IotTcpConnectionManager { |
| 18 | 18 | ||
| 19 | + // TODO @haohao:要考虑,相同设备,多次连接的情况哇? | ||
| 19 | /** | 20 | /** |
| 20 | * 连接集合 | 21 | * 连接集合 |
| 21 | * | 22 | * |
| @@ -50,6 +51,7 @@ public class IotTcpConnectionManager { | @@ -50,6 +51,7 @@ public class IotTcpConnectionManager { | ||
| 50 | * @param socket Netty Channel | 51 | * @param socket Netty Channel |
| 51 | */ | 52 | */ |
| 52 | public void removeConnection(NetSocket socket) { | 53 | public void removeConnection(NetSocket socket) { |
| 54 | + // TODO @haohao:vertx 的 socket,有没办法设置一些属性,类似 netty 的;目的是,避免遍历 connectionMap 去操作哈; | ||
| 53 | connectionMap.entrySet().stream() | 55 | connectionMap.entrySet().stream() |
| 54 | .filter(entry -> entry.getValue().equals(socket)) | 56 | .filter(entry -> entry.getValue().equals(socket)) |
| 55 | .findFirst() | 57 | .findFirst() |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/router/IotTcpConnectionHandler.java
| @@ -64,6 +64,7 @@ public class IotTcpConnectionHandler implements Handler<Buffer> { | @@ -64,6 +64,7 @@ public class IotTcpConnectionHandler implements Handler<Buffer> { | ||
| 64 | public void handle(Buffer buffer) { | 64 | public void handle(Buffer buffer) { |
| 65 | log.info("[handle][接收到数据: {}]", buffer); | 65 | log.info("[handle][接收到数据: {}]", buffer); |
| 66 | try { | 66 | try { |
| 67 | + // TODO @haohao:可以调研下,做个对比表格哈; | ||
| 67 | // 1. 处理认证 | 68 | // 1. 处理认证 |
| 68 | if (!authenticated) { | 69 | if (!authenticated) { |
| 69 | handleAuthentication(buffer); | 70 | handleAuthentication(buffer); |
| @@ -80,6 +81,11 @@ public class IotTcpConnectionHandler implements Handler<Buffer> { | @@ -80,6 +81,11 @@ public class IotTcpConnectionHandler implements Handler<Buffer> { | ||
| 80 | private void handleAuthentication(Buffer buffer) { | 81 | private void handleAuthentication(Buffer buffer) { |
| 81 | // 1. 解析认证信息 | 82 | // 1. 解析认证信息 |
| 82 | // TODO @芋艿:这里的认证协议,需要和设备端约定。默认为 productKey,deviceName,password | 83 | // TODO @芋艿:这里的认证协议,需要和设备端约定。默认为 productKey,deviceName,password |
| 84 | + // TODO @haohao:这里,要不也 json 解析?类似 http 是 { | ||
| 85 | + // "clientId": "4aymZgOTOOCrDKRT.small", | ||
| 86 | + // "username": "small&4aymZgOTOOCrDKRT", | ||
| 87 | + // "password": "509e2b08f7598eb139d276388c600435913ba4c94cd0d50aebc5c0d1855bcb75" | ||
| 88 | + //} | ||
| 83 | String[] parts = buffer.toString().split(","); | 89 | String[] parts = buffer.toString().split(","); |
| 84 | if (parts.length != 3) { | 90 | if (parts.length != 3) { |
| 85 | log.error("[handleAuthentication][认证信息({})格式不正确]", buffer); | 91 | log.error("[handleAuthentication][认证信息({})格式不正确]", buffer); |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/router/IotTcpDownstreamHandler.java
| @@ -22,6 +22,7 @@ import org.springframework.stereotype.Component; | @@ -22,6 +22,7 @@ import org.springframework.stereotype.Component; | ||
| 22 | public class IotTcpDownstreamHandler { | 22 | public class IotTcpDownstreamHandler { |
| 23 | 23 | ||
| 24 | private final IotTcpConnectionManager connectionManager; | 24 | private final IotTcpConnectionManager connectionManager; |
| 25 | + | ||
| 25 | private final IotDeviceMessageService messageService; | 26 | private final IotDeviceMessageService messageService; |
| 26 | 27 | ||
| 27 | /** | 28 | /** |
| @@ -43,6 +44,7 @@ public class IotTcpDownstreamHandler { | @@ -43,6 +44,7 @@ public class IotTcpDownstreamHandler { | ||
| 43 | // 3. 发送消息 | 44 | // 3. 发送消息 |
| 44 | socket.write(Buffer.buffer(bytes)); | 45 | socket.write(Buffer.buffer(bytes)); |
| 45 | // TODO @芋艿:这里的换行符,需要和设备端约定 | 46 | // TODO @芋艿:这里的换行符,需要和设备端约定 |
| 47 | + // TODO @haohao:tcp 要不定长?很少 \n 哈。然后有个 magic number;可以参考 dubbo rpc; | ||
| 46 | socket.write("\n"); | 48 | socket.write("\n"); |
| 47 | } | 49 | } |
| 48 | 50 |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/util/IotMqttTopicUtils.java
| @@ -16,6 +16,7 @@ public final class IotMqttTopicUtils { | @@ -16,6 +16,7 @@ public final class IotMqttTopicUtils { | ||
| 16 | */ | 16 | */ |
| 17 | private static final String SYS_TOPIC_PREFIX = "/sys/"; | 17 | private static final String SYS_TOPIC_PREFIX = "/sys/"; |
| 18 | 18 | ||
| 19 | + // TODO @haohao:这个要删除哇? | ||
| 19 | /** | 20 | /** |
| 20 | * 服务调用主题前缀 | 21 | * 服务调用主题前缀 |
| 21 | */ | 22 | */ |
| @@ -36,6 +37,7 @@ public final class IotMqttTopicUtils { | @@ -36,6 +37,7 @@ public final class IotMqttTopicUtils { | ||
| 36 | */ | 37 | */ |
| 37 | public static final String MQTT_EVENT_PATH = "/mqtt/event"; | 38 | public static final String MQTT_EVENT_PATH = "/mqtt/event"; |
| 38 | 39 | ||
| 40 | + // TODO @haohao:这个要删除哇? | ||
| 39 | /** | 41 | /** |
| 40 | * MQTT 授权接口路径(预留) | 42 | * MQTT 授权接口路径(预留) |
| 41 | * 对应 EMQX HTTP 授权插件的授权检查接口 | 43 | * 对应 EMQX HTTP 授权插件的授权检查接口 |