Commit 40a92426916bd09a95cb906d7a03ae07e035f64f

Authored by YunaiV
1 parent a0a26c3d

reactor:【IoT 物联网】阅读 thingsboard 后,增加 IotDeviceMessage 优化的 TODO

yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/messagebus/core/rocketmq/RocketMQIotMessageBus.java
@@ -41,6 +41,7 @@ public class RocketMQIotMessageBus implements IotMessageBus { @@ -41,6 +41,7 @@ public class RocketMQIotMessageBus implements IotMessageBus {
41 41
42 @Override 42 @Override
43 public void post(String topic, Object message) { 43 public void post(String topic, Object message) {
  44 + // TODO @芋艿:需要 orderly!
44 SendResult result = rocketMQTemplate.syncSend(topic, JsonUtils.toJsonString(message)); 45 SendResult result = rocketMQTemplate.syncSend(topic, JsonUtils.toJsonString(message));
45 log.info("[post][topic({}) 发送消息({}) result({})]", topic, message, result); 46 log.info("[post][topic({}) 发送消息({}) result({})]", topic, message, result);
46 } 47 }
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/mq/message/IotDeviceMessage.java
@@ -32,11 +32,13 @@ public class IotDeviceMessage { @@ -32,11 +32,13 @@ public class IotDeviceMessage {
32 */ 32 */
33 public static final String MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC = MESSAGE_BUS_DEVICE_MESSAGE_TOPIC + "_%s"; 33 public static final String MESSAGE_BUS_GATEWAY_DEVICE_MESSAGE_TOPIC = MESSAGE_BUS_DEVICE_MESSAGE_TOPIC + "_%s";
34 34
  35 + // TODO @芋艿:thingsboard 对应 id,全部由后端生成,由于追溯;是不是调整下?
35 /** 36 /**
36 * 消息编号 37 * 消息编号
37 */ 38 */
38 private String messageId; 39 private String messageId;
39 40
  41 + // TODO @芋艿:thingsboard 是使用 deviceId
40 /** 42 /**
41 * 设备信息 43 * 设备信息
42 */ 44 */
@@ -46,6 +48,7 @@ public class IotDeviceMessage { @@ -46,6 +48,7 @@ public class IotDeviceMessage {
46 */ 48 */
47 private String deviceName; 49 private String deviceName;
48 50
  51 + // TODO @芋艿:thingsboard 只定义了 type;相当于 type + identifier 结合!TbMsgType
49 /** 52 /**
50 * 消息类型 53 * 消息类型
51 * 54 *
@@ -59,6 +62,8 @@ public class IotDeviceMessage { @@ -59,6 +62,8 @@ public class IotDeviceMessage {
59 */ 62 */
60 private String identifier; 63 private String identifier;
61 64
  65 + // TODO @芋艿:thingsboard 只有 data 字段,没有 code 字段;
  66 + // TODO @芋艿:要不提前序列化成字符串???类似 thingsboard 的 data 字段
62 /** 67 /**
63 * 请求参数 68 * 请求参数
64 * 69 *
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/router/IotHttpUpstreamHandler.java
@@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil; @@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.map.MapUtil; 4 import cn.hutool.core.map.MapUtil;
5 import cn.hutool.core.util.StrUtil; 5 import cn.hutool.core.util.StrUtil;
6 import cn.hutool.extra.spring.SpringUtil; 6 import cn.hutool.extra.spring.SpringUtil;
7 -import cn.iocoder.yudao.framework.common.pojo.CommonResult;  
8 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; 7 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
9 import cn.iocoder.yudao.module.iot.core.mq.producer.IotDeviceMessageProducer; 8 import cn.iocoder.yudao.module.iot.core.mq.producer.IotDeviceMessageProducer;
10 import cn.iocoder.yudao.module.iot.gateway.enums.IotDeviceTopicEnum; 9 import cn.iocoder.yudao.module.iot.gateway.enums.IotDeviceTopicEnum;
@@ -41,16 +40,6 @@ public class IotHttpUpstreamHandler implements Handler<RoutingContext> { @@ -41,16 +40,6 @@ public class IotHttpUpstreamHandler implements Handler<RoutingContext> {
41 + IotDeviceTopicEnum.EVENT_POST_TOPIC_PREFIX.getTopic() + ":identifier" 40 + IotDeviceTopicEnum.EVENT_POST_TOPIC_PREFIX.getTopic() + ":identifier"
42 + IotDeviceTopicEnum.EVENT_POST_TOPIC_SUFFIX.getTopic(); 41 + IotDeviceTopicEnum.EVENT_POST_TOPIC_SUFFIX.getTopic();
43 42
44 - /**  
45 - * 事件上报方法前缀  
46 - */  
47 - private static final String EVENT_METHOD_PREFIX = "thing.event.";  
48 -  
49 - /**  
50 - * 事件上报方法后缀  
51 - */  
52 - private static final String EVENT_METHOD_SUFFIX = ".post";  
53 -  
54 private final IotHttpUpstreamProtocol protocol; 43 private final IotHttpUpstreamProtocol protocol;
55 44
56 private final IotDeviceMessageProducer deviceMessageProducer; 45 private final IotDeviceMessageProducer deviceMessageProducer;
@@ -64,40 +53,22 @@ public class IotHttpUpstreamHandler implements Handler<RoutingContext> { @@ -64,40 +53,22 @@ public class IotHttpUpstreamHandler implements Handler<RoutingContext> {
64 public void handle(RoutingContext context) { 53 public void handle(RoutingContext context) {
65 String path = context.request().path(); 54 String path = context.request().path();
66 // 1. 解析通用参数 55 // 1. 解析通用参数
67 - Map<String, String> params = parseCommonParams(context);  
68 - String productKey = params.get("productKey");  
69 - String deviceName = params.get("deviceName"); 56 + String productKey = context.pathParam("productKey");
  57 + String deviceName = context.pathParam("deviceName");
70 JsonObject body = context.body().asJsonObject(); 58 JsonObject body = context.body().asJsonObject();
71 59
72 // 2. 根据路径模式处理不同类型的请求 60 // 2. 根据路径模式处理不同类型的请求
73 if (isPropertyPostPath(path)) { 61 if (isPropertyPostPath(path)) {
74 // 处理属性上报 62 // 处理属性上报
75 handlePropertyPost(context, productKey, deviceName, body); 63 handlePropertyPost(context, productKey, deviceName, body);
76 - return;  
77 - }  
78 -  
79 - if (isEventPostPath(path)) { 64 + } else if (isEventPostPath(path)) {
80 // 处理事件上报 65 // 处理事件上报
81 String identifier = context.pathParam("identifier"); 66 String identifier = context.pathParam("identifier");
82 handleEventPost(context, productKey, deviceName, identifier, body); 67 handleEventPost(context, productKey, deviceName, identifier, body);
83 - return;  
84 } 68 }
85 } 69 }
86 70
87 /** 71 /**
88 - * 解析通用参数  
89 - *  
90 - * @param routingContext 路由上下文  
91 - * @return 参数映射  
92 - */  
93 - private Map<String, String> parseCommonParams(RoutingContext routingContext) {  
94 - Map<String, String> params = MapUtil.newHashMap();  
95 - params.put("productKey", routingContext.pathParam("productKey"));  
96 - params.put("deviceName", routingContext.pathParam("deviceName"));  
97 - return params;  
98 - }  
99 -  
100 - /**  
101 * 判断是否是属性上报路径 72 * 判断是否是属性上报路径
102 * 73 *
103 * @param path 路径 74 * @param path 路径
@@ -134,8 +105,8 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; { @@ -134,8 +105,8 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; {
134 // 1.2 发送消息 105 // 1.2 发送消息
135 deviceMessageProducer.sendDeviceMessage(message); 106 deviceMessageProducer.sendDeviceMessage(message);
136 107
137 - // 2. 返回响应  
138 - sendResponse(routingContext, null); 108 +// // 2. 返回响应
  109 +// sendResponse(routingContext, null);
139 } 110 }
140 111
141 /** 112 /**
@@ -155,49 +126,6 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; { @@ -155,49 +126,6 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; {
155 // 126 //
156 // // 事件上报 127 // // 事件上报
157 // CommonResult<Boolean> result = deviceUpstreamApi.reportDeviceEvent(reportReqDTO); 128 // CommonResult<Boolean> result = deviceUpstreamApi.reportDeviceEvent(reportReqDTO);
158 -//  
159 -// // 返回响应  
160 -// sendResponse(routingContext, requestId, method, result);  
161 - }  
162 -  
163 - /**  
164 - * 发送响应  
165 - *  
166 - * @param routingContext 路由上下文  
167 - * @param result 结果  
168 - */  
169 - private void sendResponse(RoutingContext routingContext,  
170 - CommonResult<Boolean> result) {  
171 -// // TODO @芋艿:后续再优化  
172 -// IotStandardResponse response;  
173 -// if (result == null ) {  
174 -// response = IotStandardResponse.success(requestId, method, null);  
175 -// } else if (result.isSuccess()) {  
176 -// response = IotStandardResponse.success(requestId, method, result.getData());  
177 -// } else {  
178 -// response = IotStandardResponse.error(requestId, method, result.getCode(), result.getMsg());  
179 -// }  
180 -// IotNetComponentCommonUtils.writeJsonResponse(routingContext, response);  
181 - }  
182 -  
183 - /**  
184 - * 从路径确定方法名  
185 - *  
186 - * @param path 路径  
187 - * @param routingContext 路由上下文  
188 - * @return 方法名  
189 - */  
190 - private String determineMethodFromPath(String path, RoutingContext routingContext) {  
191 - if (StrUtil.contains(path, "/property/")) {  
192 - return null;  
193 - }  
194 -  
195 - return EVENT_METHOD_PREFIX  
196 - + (routingContext.pathParams().containsKey("identifier")  
197 - ? routingContext.pathParam("identifier")  
198 - : "unknown")  
199 - +  
200 - EVENT_METHOD_SUFFIX;  
201 } 129 }
202 130
203 // TODO @芋艿:这块在看看 131 // TODO @芋艿:这块在看看