Commit 40a92426916bd09a95cb906d7a03ae07e035f64f
1 parent
a0a26c3d
reactor:【IoT 物联网】阅读 thingsboard 后,增加 IotDeviceMessage 优化的 TODO
Showing
3 changed files
with
11 additions
and
77 deletions
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 | 41 | |
| 42 | 42 | @Override |
| 43 | 43 | public void post(String topic, Object message) { |
| 44 | + // TODO @芋艿:需要 orderly! | |
| 44 | 45 | SendResult result = rocketMQTemplate.syncSend(topic, JsonUtils.toJsonString(message)); |
| 45 | 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 | 32 | */ |
| 33 | 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 | 39 | private String messageId; |
| 39 | 40 | |
| 41 | + // TODO @芋艿:thingsboard 是使用 deviceId | |
| 40 | 42 | /** |
| 41 | 43 | * 设备信息 |
| 42 | 44 | */ |
| ... | ... | @@ -46,6 +48,7 @@ public class IotDeviceMessage { |
| 46 | 48 | */ |
| 47 | 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 | 62 | */ |
| 60 | 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 | 4 | import cn.hutool.core.map.MapUtil; |
| 5 | 5 | import cn.hutool.core.util.StrUtil; |
| 6 | 6 | import cn.hutool.extra.spring.SpringUtil; |
| 7 | -import cn.iocoder.yudao.framework.common.pojo.CommonResult; | |
| 8 | 7 | import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; |
| 9 | 8 | import cn.iocoder.yudao.module.iot.core.mq.producer.IotDeviceMessageProducer; |
| 10 | 9 | import cn.iocoder.yudao.module.iot.gateway.enums.IotDeviceTopicEnum; |
| ... | ... | @@ -41,16 +40,6 @@ public class IotHttpUpstreamHandler implements Handler<RoutingContext> { |
| 41 | 40 | + IotDeviceTopicEnum.EVENT_POST_TOPIC_PREFIX.getTopic() + ":identifier" |
| 42 | 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 | 43 | private final IotHttpUpstreamProtocol protocol; |
| 55 | 44 | |
| 56 | 45 | private final IotDeviceMessageProducer deviceMessageProducer; |
| ... | ... | @@ -64,40 +53,22 @@ public class IotHttpUpstreamHandler implements Handler<RoutingContext> { |
| 64 | 53 | public void handle(RoutingContext context) { |
| 65 | 54 | String path = context.request().path(); |
| 66 | 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 | 58 | JsonObject body = context.body().asJsonObject(); |
| 71 | 59 | |
| 72 | 60 | // 2. 根据路径模式处理不同类型的请求 |
| 73 | 61 | if (isPropertyPostPath(path)) { |
| 74 | 62 | // 处理属性上报 |
| 75 | 63 | handlePropertyPost(context, productKey, deviceName, body); |
| 76 | - return; | |
| 77 | - } | |
| 78 | - | |
| 79 | - if (isEventPostPath(path)) { | |
| 64 | + } else if (isEventPostPath(path)) { | |
| 80 | 65 | // 处理事件上报 |
| 81 | 66 | String identifier = context.pathParam("identifier"); |
| 82 | 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 | 74 | * @param path 路径 |
| ... | ... | @@ -134,8 +105,8 @@ public class IotHttpUpstreamHandler implements Handler<RoutingContext> { |
| 134 | 105 | // 1.2 发送消息 |
| 135 | 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<RoutingContext> { |
| 155 | 126 | // |
| 156 | 127 | // // 事件上报 |
| 157 | 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 | 131 | // TODO @芋艿:这块在看看 | ... | ... |