Commit ac624b74950104411b9556b7d131f5cf51d63649

Authored by YunaiV
1 parent c3485a3f

feat:【IoT 物联网】重新实现 IotDeviceLogMessageSubscriber 的日志记录

Showing 12 changed files with 91 additions and 109 deletions
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/device/IotDeviceMessageIdentifierEnum.java
@@ -11,7 +11,7 @@ import lombok.RequiredArgsConstructor; @@ -11,7 +11,7 @@ import lombok.RequiredArgsConstructor;
11 @RequiredArgsConstructor 11 @RequiredArgsConstructor
12 public enum IotDeviceMessageIdentifierEnum { 12 public enum IotDeviceMessageIdentifierEnum {
13 13
14 - PROPERTY_GET("get"), // 下行 TODO 芋艿:【讨论】貌似这个“上行”更合理?device 主动拉取配置。和 IotDevicePropertyGetReqDTO 一样的配置 14 + PROPERTY_GET("get"), // 下行
15 PROPERTY_SET("set"), // 下行 15 PROPERTY_SET("set"), // 下行
16 PROPERTY_REPORT("report"), // 上行 16 PROPERTY_REPORT("report"), // 上行
17 17
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/data/IotDeviceLogPageReqVO.java
@@ -9,6 +9,7 @@ import lombok.Data; @@ -9,6 +9,7 @@ import lombok.Data;
9 @Data 9 @Data
10 public class IotDeviceLogPageReqVO extends PageParam { 10 public class IotDeviceLogPageReqVO extends PageParam {
11 11
  12 + // TODO @芋艿:【优先级:中】改成通过 deviceId 查询;然后转换下;
12 @Schema(description = "设备标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123") 13 @Schema(description = "设备标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "device123")
13 @NotEmpty(message = "设备标识不能为空") 14 @NotEmpty(message = "设备标识不能为空")
14 private String deviceKey; 15 private String deviceKey;
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/device/IotDeviceLogDO.java
1 package cn.iocoder.yudao.module.iot.dal.dataobject.device; 1 package cn.iocoder.yudao.module.iot.dal.dataobject.device;
2 2
3 import cn.hutool.core.util.IdUtil; 3 import cn.hutool.core.util.IdUtil;
  4 +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
4 import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO; 5 import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
5 import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageIdentifierEnum; 6 import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageIdentifierEnum;
6 import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageTypeEnum; 7 import cn.iocoder.yudao.module.iot.enums.device.IotDeviceMessageTypeEnum;
7 -import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;  
8 import lombok.AllArgsConstructor; 8 import lombok.AllArgsConstructor;
9 import lombok.Builder; 9 import lombok.Builder;
10 import lombok.Data; 10 import lombok.Data;
@@ -31,11 +31,11 @@ public class IotDeviceLogDO { @@ -31,11 +31,11 @@ public class IotDeviceLogDO {
31 private String id; 31 private String id;
32 32
33 /** 33 /**
34 - * 请求编号 34 + * 消息编号
35 * 35 *
36 - * 对应 {@link IotDeviceMessage#getRequestId()} 字段 36 + * 对应 {@link IotDeviceMessage#getMessageId()} 字段
37 */ 37 */
38 - private String requestId; 38 + private String messageId;
39 39
40 /** 40 /**
41 * 产品标识 41 * 产品标识
@@ -50,11 +50,11 @@ public class IotDeviceLogDO { @@ -50,11 +50,11 @@ public class IotDeviceLogDO {
50 */ 50 */
51 private String deviceName; 51 private String deviceName;
52 /** 52 /**
53 - * 设备标识  
54 - * <p>  
55 - * 关联 {@link IotDeviceDO#getDeviceKey()}} 53 + * 设备编号
  54 + *
  55 + * 关联 {@link IotDeviceDO#getId()}
56 */ 56 */
57 - private String deviceKey; // 非存储字段,用于 TDengine 的 TAG 57 + private Long deviceId;
58 58
59 /** 59 /**
60 * 日志类型 60 * 日志类型
@@ -88,6 +88,11 @@ public class IotDeviceLogDO { @@ -88,6 +88,11 @@ public class IotDeviceLogDO {
88 private Long reportTime; 88 private Long reportTime;
89 89
90 /** 90 /**
  91 + * 租户编号
  92 + */
  93 + private Long tenantId;
  94 +
  95 + /**
91 * 时序时间 96 * 时序时间
92 */ 97 */
93 private Long ts; 98 private Long ts;
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/consumer/device/IotDeviceLogMessageSubscriber.java
@@ -39,11 +39,10 @@ public class IotDeviceLogMessageSubscriber implements IotMessageSubscriber&lt;IotDe @@ -39,11 +39,10 @@ public class IotDeviceLogMessageSubscriber implements IotMessageSubscriber&lt;IotDe
39 return "iot_device_log_consumer"; 39 return "iot_device_log_consumer";
40 } 40 }
41 41
42 - // TODO @芋艿:后续再对接这个细节逻辑;  
43 @Override 42 @Override
44 public void onMessage(IotDeviceMessage message) { 43 public void onMessage(IotDeviceMessage message) {
45 log.info("[onMessage][消息内容({})]", message); 44 log.info("[onMessage][消息内容({})]", message);
46 -// deviceLogService.createDeviceLog(message); 45 + deviceLogService.createDeviceLog(message);
47 } 46 }
48 47
49 } 48 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/message/IotDeviceMessage.java
@@ -17,6 +17,7 @@ import java.time.LocalDateTime; @@ -17,6 +17,7 @@ import java.time.LocalDateTime;
17 @NoArgsConstructor 17 @NoArgsConstructor
18 @AllArgsConstructor 18 @AllArgsConstructor
19 @Builder 19 @Builder
  20 +@Deprecated
20 public class IotDeviceMessage { 21 public class IotDeviceMessage {
21 22
22 /** 23 /**
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/control/IotDeviceDownstreamServiceImpl.java
@@ -146,14 +146,11 @@ public class IotDeviceDownstreamServiceImpl implements IotDeviceDownstreamServic @@ -146,14 +146,11 @@ public class IotDeviceDownstreamServiceImpl implements IotDeviceDownstreamServic
146 // TODO @super:【可优化】过滤掉不合法的属性 146 // TODO @super:【可优化】过滤掉不合法的属性
147 147
148 // 2. 发送请求 148 // 2. 发送请求
149 - // TODO @芋艿:deviceName 的设置  
150 - String deviceName = "xx";  
151 - Long tenantId = 1L;  
152 cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage message = cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage 149 cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage message = cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage
153 - .of(getProductKey(device, parentDevice), getDeviceName(device, parentDevice), deviceName,  
154 - null, tenantId); 150 + .of(getProductKey(device, parentDevice), getDeviceName(device, parentDevice));
155 String serverId = "192_168_64_1_8092"; 151 String serverId = "192_168_64_1_8092";
156 deviceMessageProducer.sendGatewayDeviceMessage(serverId, message); 152 deviceMessageProducer.sendGatewayDeviceMessage(serverId, message);
  153 + deviceMessageProducer.sendDeviceMessage(message);
157 // TODO @芋艿:后续可以清理掉 154 // TODO @芋艿:后续可以清理掉
158 return null; 155 return null;
159 } 156 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/data/IotDeviceLogService.java
@@ -2,8 +2,8 @@ package cn.iocoder.yudao.module.iot.service.device.data; @@ -2,8 +2,8 @@ package cn.iocoder.yudao.module.iot.service.device.data;
2 2
3 import cn.iocoder.yudao.framework.common.pojo.PageResult; 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDeviceLogPageReqVO; 4 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDeviceLogPageReqVO;
  5 +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
5 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; 6 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
6 -import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;  
7 7
8 import javax.annotation.Nullable; 8 import javax.annotation.Nullable;
9 import java.time.LocalDateTime; 9 import java.time.LocalDateTime;
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/data/IotDeviceLogServiceImpl.java
@@ -8,9 +8,11 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; @@ -8,9 +8,11 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
8 import cn.iocoder.yudao.framework.common.util.json.JsonUtils; 8 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
9 import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 9 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
10 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDeviceLogPageReqVO; 10 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.data.IotDeviceLogPageReqVO;
  11 +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
  12 +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
11 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; 13 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO;
12 import cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogMapper; 14 import cn.iocoder.yudao.module.iot.dal.tdengine.IotDeviceLogMapper;
13 -import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; 15 +import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
14 import com.baomidou.mybatisplus.core.metadata.IPage; 16 import com.baomidou.mybatisplus.core.metadata.IPage;
15 import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 17 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
16 import jakarta.annotation.Resource; 18 import jakarta.annotation.Resource;
@@ -35,6 +37,9 @@ import java.util.stream.Collectors; @@ -35,6 +37,9 @@ import java.util.stream.Collectors;
35 public class IotDeviceLogServiceImpl implements IotDeviceLogService { 37 public class IotDeviceLogServiceImpl implements IotDeviceLogService {
36 38
37 @Resource 39 @Resource
  40 + private IotDeviceService deviceService;
  41 +
  42 + @Resource
38 private IotDeviceLogMapper deviceLogMapper; 43 private IotDeviceLogMapper deviceLogMapper;
39 44
40 @Override 45 @Override
@@ -51,9 +56,16 @@ public class IotDeviceLogServiceImpl implements IotDeviceLogService { @@ -51,9 +56,16 @@ public class IotDeviceLogServiceImpl implements IotDeviceLogService {
51 56
52 @Override 57 @Override
53 public void createDeviceLog(IotDeviceMessage message) { 58 public void createDeviceLog(IotDeviceMessage message) {
  59 + IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache(
  60 + message.getProductKey(), message.getDeviceName());
  61 + if (device == null) {
  62 + log.error("[createDeviceLog][设备({}/{}) 不存在]", message.getProductKey(), message.getDeviceName());
  63 + return;
  64 + }
54 IotDeviceLogDO log = BeanUtils.toBean(message, IotDeviceLogDO.class) 65 IotDeviceLogDO log = BeanUtils.toBean(message, IotDeviceLogDO.class)
55 .setId(IdUtil.fastSimpleUUID()) 66 .setId(IdUtil.fastSimpleUUID())
56 - .setContent(JsonUtils.toJsonString(message.getData())); 67 + .setContent(JsonUtils.toJsonString(message.getData()))
  68 + .setTenantId(device.getTenantId());
57 deviceLogMapper.insert(log); 69 deviceLogMapper.insert(log);
58 } 70 }
59 71
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceLogMapper.xml
@@ -6,18 +6,19 @@ @@ -6,18 +6,19 @@
6 6
7 <update id="createDeviceLogSTable"> 7 <update id="createDeviceLogSTable">
8 CREATE STABLE IF NOT EXISTS device_log ( 8 CREATE STABLE IF NOT EXISTS device_log (
9 - ts TIMESTAMP,  
10 - id NCHAR(50),  
11 - product_key NCHAR(50),  
12 - device_name NCHAR(50),  
13 - type NCHAR(50),  
14 - identifier NCHAR(255),  
15 - content NCHAR(1024),  
16 - code INT,  
17 - report_time TIMESTAMP  
18 - ) TAGS (  
19 - device_key NCHAR(50)  
20 - ) 9 + ts TIMESTAMP,
  10 + id NCHAR(50),
  11 + message_id NCHAR(50),
  12 + type NCHAR(50),
  13 + identifier NCHAR(255),
  14 + content NCHAR(1024),
  15 + code INT,
  16 + report_time TIMESTAMP,
  17 + tenant_id BIGINT
  18 + ) TAGS (
  19 + product_key NCHAR(50),
  20 + device_name NCHAR(50)
  21 + )
21 </update> 22 </update>
22 23
23 <select id="showDeviceLogSTable" resultType="String"> 24 <select id="showDeviceLogSTable" resultType="String">
@@ -25,25 +26,21 @@ @@ -25,25 +26,21 @@
25 </select> 26 </select>
26 27
27 <insert id="insert"> 28 <insert id="insert">
28 - INSERT INTO device_log_${deviceKey} (ts, id, product_key, device_name, type, identifier, content, code, report_time) 29 + INSERT INTO device_log_${productKey}_${deviceName} (
  30 + ts, id, message_id, type, identifier,
  31 + content, code, report_time, tenant_id
  32 + )
29 USING device_log 33 USING device_log
30 - TAGS ('${deviceKey}') 34 + TAGS ('${productKey}', '${deviceName}')
31 VALUES ( 35 VALUES (
32 - NOW,  
33 - #{id},  
34 - #{productKey},  
35 - #{deviceName},  
36 - #{type},  
37 - #{identifier},  
38 - #{content},  
39 - #{code},  
40 - #{reportTime} 36 + NOW, #{id}, #{messageId}, #{type}, #{identifier},
  37 + #{content}, #{code}, #{reportTime}, #{tenantId}
41 ) 38 )
42 </insert> 39 </insert>
43 40
44 <select id="selectPage" resultType="cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO"> 41 <select id="selectPage" resultType="cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO">
45 SELECT ts, id, device_key, product_key, type, identifier, content, report_time 42 SELECT ts, id, device_key, product_key, type, identifier, content, report_time
46 - FROM device_log_${reqVO.deviceKey} 43 + FROM device_log_${productKey}_${deviceName}
47 <where> 44 <where>
48 <if test="reqVO.type != null and reqVO.type != ''"> 45 <if test="reqVO.type != null and reqVO.type != ''">
49 AND type = #{reqVO.type} 46 AND type = #{reqVO.type}
@@ -66,10 +63,10 @@ @@ -66,10 +63,10 @@
66 </select> 63 </select>
67 64
68 <select id="selectDeviceLogUpCountByHour" resultType="java.util.Map"> 65 <select id="selectDeviceLogUpCountByHour" resultType="java.util.Map">
69 - SELECT 66 + SELECT
70 TIMETRUNCATE(ts, 1h) as time, 67 TIMETRUNCATE(ts, 1h) as time,
71 - COUNT(*) as data  
72 - FROM 68 + COUNT(*) as data
  69 + FROM
73 <choose> 70 <choose>
74 <when test="deviceKey != null and deviceKey != ''"> 71 <when test="deviceKey != null and deviceKey != ''">
75 device_log_${deviceKey} 72 device_log_${deviceKey}
@@ -94,10 +91,10 @@ @@ -94,10 +91,10 @@
94 </select> 91 </select>
95 92
96 <select id="selectDeviceLogDownCountByHour" resultType="java.util.Map"> 93 <select id="selectDeviceLogDownCountByHour" resultType="java.util.Map">
97 - SELECT 94 + SELECT
98 TIMETRUNCATE(ts, 1h) as time, 95 TIMETRUNCATE(ts, 1h) as time,
99 - COUNT(*) as data  
100 - FROM 96 + COUNT(*) as data
  97 + FROM
101 <choose> 98 <choose>
102 <when test="deviceKey != null and deviceKey != ''"> 99 <when test="deviceKey != null and deviceKey != ''">
103 device_log_${deviceKey} 100 device_log_${deviceKey}
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/mq/message/IotDeviceMessage.java
@@ -11,8 +11,6 @@ import lombok.NoArgsConstructor; @@ -11,8 +11,6 @@ import lombok.NoArgsConstructor;
11 import java.time.LocalDateTime; 11 import java.time.LocalDateTime;
12 import java.util.Map; 12 import java.util.Map;
13 13
14 -// TODO @芋艿:参考阿里云的物模型,优化 IoT 上下行消息的设计,尽量保持一致(渐进式,不要一口气)!  
15 -  
16 /** 14 /**
17 * IoT 设备消息 15 * IoT 设备消息
18 */ 16 */
@@ -35,9 +33,9 @@ public class IotDeviceMessage { @@ -35,9 +33,9 @@ public class IotDeviceMessage {
35 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";
36 34
37 /** 35 /**
38 - * 请求编号 36 + * 消息编号
39 */ 37 */
40 - private String requestId; 38 + private String messageId;
41 39
42 /** 40 /**
43 * 设备信息 41 * 设备信息
@@ -47,10 +45,6 @@ public class IotDeviceMessage { @@ -47,10 +45,6 @@ public class IotDeviceMessage {
47 * 设备名称 45 * 设备名称
48 */ 46 */
49 private String deviceName; 47 private String deviceName;
50 - /**  
51 - * 设备标识  
52 - */  
53 - private String deviceKey;  
54 48
55 /** 49 /**
56 * 消息类型 50 * 消息类型
@@ -89,11 +83,6 @@ public class IotDeviceMessage { @@ -89,11 +83,6 @@ public class IotDeviceMessage {
89 */ 83 */
90 private String serverId; 84 private String serverId;
91 85
92 - /**  
93 - * 租户编号  
94 - */  
95 - private Long tenantId;  
96 -  
97 public IotDeviceMessage ofPropertyReport(Map<String, Object> properties) { 86 public IotDeviceMessage ofPropertyReport(Map<String, Object> properties) {
98 this.setType(IotDeviceMessageTypeEnum.PROPERTY.getType()); 87 this.setType(IotDeviceMessageTypeEnum.PROPERTY.getType());
99 this.setIdentifier(IotDeviceMessageIdentifierEnum.PROPERTY_REPORT.getIdentifier()); 88 this.setIdentifier(IotDeviceMessageIdentifierEnum.PROPERTY_REPORT.getIdentifier());
@@ -108,26 +97,27 @@ public class IotDeviceMessage { @@ -108,26 +97,27 @@ public class IotDeviceMessage {
108 return this; 97 return this;
109 } 98 }
110 99
111 - public static IotDeviceMessage of(String productKey, String deviceName, String deviceKey,  
112 - String serverId, Long tenantId) {  
113 - return of(productKey, deviceName, deviceKey,  
114 - null, null,  
115 - serverId, tenantId); 100 + public static IotDeviceMessage of(String productKey, String deviceName) {
  101 + return of(productKey, deviceName,
  102 + null, null);
116 } 103 }
117 104
118 - public static IotDeviceMessage of(String productKey, String deviceName, String deviceKey,  
119 - String requestId, LocalDateTime reportTime,  
120 - String serverId, Long tenantId) {  
121 - if (requestId == null) {  
122 - requestId = IotCoreUtils.generateRequestId();  
123 - } 105 + public static IotDeviceMessage of(String productKey, String deviceName,
  106 + String serverId) {
  107 + return of(productKey, deviceName,
  108 + null, serverId);
  109 + }
  110 +
  111 + public static IotDeviceMessage of(String productKey, String deviceName,
  112 + LocalDateTime reportTime, String serverId) {
124 if (reportTime == null) { 113 if (reportTime == null) {
125 reportTime = LocalDateTime.now(); 114 reportTime = LocalDateTime.now();
126 } 115 }
  116 + String messageId = IotCoreUtils.generateMessageId();
127 return IotDeviceMessage.builder() 117 return IotDeviceMessage.builder()
128 - .requestId(requestId).reportTime(reportTime)  
129 - .productKey(productKey).deviceName(deviceName).deviceKey(deviceKey)  
130 - .serverId(serverId).tenantId(tenantId).build(); 118 + .messageId(messageId).reportTime(reportTime)
  119 + .productKey(productKey).deviceName(deviceName)
  120 + .serverId(serverId).build();
131 } 121 }
132 122
133 // ========== Topic 相关 ========== 123 // ========== Topic 相关 ==========
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/util/IotCoreUtils.java
@@ -22,7 +22,7 @@ public class IotCoreUtils { @@ -22,7 +22,7 @@ public class IotCoreUtils {
22 return serverId.replaceAll("\\.", "_"); 22 return serverId.replaceAll("\\.", "_");
23 } 23 }
24 24
25 - public static String generateRequestId() { 25 + public static String generateMessageId() {
26 return IdUtil.fastSimpleUUID(); 26 return IdUtil.fastSimpleUUID();
27 } 27 }
28 28
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/router/IotHttpUpstreamHandler.java
@@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.iot.gateway.protocol.http.router; @@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.iot.gateway.protocol.http.router;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 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.IdUtil;  
6 -import cn.hutool.core.util.ObjUtil;  
7 import cn.hutool.core.util.StrUtil; 5 import cn.hutool.core.util.StrUtil;
8 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 6 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
9 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; 7 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
@@ -16,7 +14,6 @@ import io.vertx.ext.web.RoutingContext; @@ -16,7 +14,6 @@ import io.vertx.ext.web.RoutingContext;
16 import lombok.RequiredArgsConstructor; 14 import lombok.RequiredArgsConstructor;
17 import lombok.extern.slf4j.Slf4j; 15 import lombok.extern.slf4j.Slf4j;
18 16
19 -import java.time.LocalDateTime;  
20 import java.util.Map; 17 import java.util.Map;
21 18
22 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST; 19 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
@@ -69,36 +66,34 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; { @@ -69,36 +66,34 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; {
69 @Override 66 @Override
70 public void handle(RoutingContext routingContext) { 67 public void handle(RoutingContext routingContext) {
71 String path = routingContext.request().path(); 68 String path = routingContext.request().path();
72 - String requestId = IdUtil.fastSimpleUUID();  
73 69
74 try { 70 try {
75 // 1. 解析通用参数 71 // 1. 解析通用参数
76 - Map<String, String> params = parseCommonParams(routingContext, requestId); 72 + Map<String, String> params = parseCommonParams(routingContext);
77 String productKey = params.get("productKey"); 73 String productKey = params.get("productKey");
78 String deviceName = params.get("deviceName"); 74 String deviceName = params.get("deviceName");
79 JsonObject body = routingContext.body().asJsonObject(); 75 JsonObject body = routingContext.body().asJsonObject();
80 - requestId = params.get("requestId");  
81 76
82 // 2. 根据路径模式处理不同类型的请求 77 // 2. 根据路径模式处理不同类型的请求
83 if (isPropertyPostPath(path)) { 78 if (isPropertyPostPath(path)) {
84 // 处理属性上报 79 // 处理属性上报
85 - handlePropertyPost(routingContext, productKey, deviceName, requestId, body); 80 + handlePropertyPost(routingContext, productKey, deviceName, body);
86 return; 81 return;
87 } 82 }
88 83
89 if (isEventPostPath(path)) { 84 if (isEventPostPath(path)) {
90 // 处理事件上报 85 // 处理事件上报
91 String identifier = routingContext.pathParam("identifier"); 86 String identifier = routingContext.pathParam("identifier");
92 - handleEventPost(routingContext, productKey, deviceName, identifier, requestId, body); 87 + handleEventPost(routingContext, productKey, deviceName, identifier, body);
93 return; 88 return;
94 } 89 }
95 90
96 // 不支持的请求路径 91 // 不支持的请求路径
97 - sendErrorResponse(routingContext, requestId, "unknown", BAD_REQUEST.getCode(), "不支持的请求路径"); 92 + sendErrorResponse(routingContext, "unknown", BAD_REQUEST.getCode(), "不支持的请求路径");
98 } catch (Exception e) { 93 } catch (Exception e) {
99 log.error("[handle][处理上行请求异常] path={}", path, e); 94 log.error("[handle][处理上行请求异常] path={}", path, e);
100 String method = determineMethodFromPath(path, routingContext); 95 String method = determineMethodFromPath(path, routingContext);
101 - sendErrorResponse(routingContext, requestId, method, INTERNAL_SERVER_ERROR.getCode(), 96 + sendErrorResponse(routingContext, method, INTERNAL_SERVER_ERROR.getCode(),
102 INTERNAL_SERVER_ERROR.getMsg()); 97 INTERNAL_SERVER_ERROR.getMsg());
103 } 98 }
104 } 99 }
@@ -107,18 +102,12 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; { @@ -107,18 +102,12 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; {
107 * 解析通用参数 102 * 解析通用参数
108 * 103 *
109 * @param routingContext 路由上下文 104 * @param routingContext 路由上下文
110 - * @param defaultRequestId 默认请求 ID  
111 * @return 参数映射 105 * @return 参数映射
112 */ 106 */
113 - private Map<String, String> parseCommonParams(RoutingContext routingContext, String defaultRequestId) { 107 + private Map<String, String> parseCommonParams(RoutingContext routingContext) {
114 Map<String, String> params = MapUtil.newHashMap(); 108 Map<String, String> params = MapUtil.newHashMap();
115 params.put("productKey", routingContext.pathParam("productKey")); 109 params.put("productKey", routingContext.pathParam("productKey"));
116 params.put("deviceName", routingContext.pathParam("deviceName")); 110 params.put("deviceName", routingContext.pathParam("deviceName"));
117 -  
118 - JsonObject body = routingContext.body().asJsonObject();  
119 - String requestId = ObjUtil.defaultIfNull(body.getString("id"), defaultRequestId);  
120 - params.put("requestId", requestId);  
121 -  
122 return params; 111 return params;
123 } 112 }
124 113
@@ -149,23 +138,18 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; { @@ -149,23 +138,18 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; {
149 * @param routingContext 路由上下文 138 * @param routingContext 路由上下文
150 * @param productKey 产品 Key 139 * @param productKey 产品 Key
151 * @param deviceName 设备名称 140 * @param deviceName 设备名称
152 - * @param requestId 请求 ID  
153 * @param body 请求体 141 * @param body 请求体
154 */ 142 */
155 private void handlePropertyPost(RoutingContext routingContext, String productKey, String deviceName, 143 private void handlePropertyPost(RoutingContext routingContext, String productKey, String deviceName,
156 - String requestId, JsonObject body) { 144 + JsonObject body) {
157 // 1.1 构建设备消息 145 // 1.1 构建设备消息
158 - String deviceKey = "xxx"; // TODO @芋艿:待支持  
159 - Long tenantId = 1L; // TODO @芋艿:待支持  
160 - IotDeviceMessage message = IotDeviceMessage.of(productKey, deviceName, deviceKey,  
161 - requestId, LocalDateTime.now(),  
162 - protocol.getServerId(), tenantId) 146 + IotDeviceMessage message = IotDeviceMessage.of(productKey, deviceName, protocol.getServerId())
163 .ofPropertyReport(parsePropertiesFromBody(body)); 147 .ofPropertyReport(parsePropertiesFromBody(body));
164 // 1.2 发送消息 148 // 1.2 发送消息
165 deviceMessageProducer.sendDeviceMessage(message); 149 deviceMessageProducer.sendDeviceMessage(message);
166 150
167 // 2. 返回响应 151 // 2. 返回响应
168 - sendResponse(routingContext, requestId, null, null); 152 + sendResponse(routingContext, null);
169 } 153 }
170 154
171 /** 155 /**
@@ -175,11 +159,10 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; { @@ -175,11 +159,10 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; {
175 * @param productKey 产品 Key 159 * @param productKey 产品 Key
176 * @param deviceName 设备名称 160 * @param deviceName 设备名称
177 * @param identifier 事件标识符 161 * @param identifier 事件标识符
178 - * @param requestId 请求 ID  
179 * @param body 请求体 162 * @param body 请求体
180 */ 163 */
181 private void handleEventPost(RoutingContext routingContext, String productKey, String deviceName, 164 private void handleEventPost(RoutingContext routingContext, String productKey, String deviceName,
182 - String identifier, String requestId, JsonObject body) { 165 + String identifier, JsonObject body) {
183 // // 处理事件上报 166 // // 处理事件上报
184 // IotDeviceEventReportReqDTO reportReqDTO = parseEventReportRequest(productKey, deviceName, identifier, 167 // IotDeviceEventReportReqDTO reportReqDTO = parseEventReportRequest(productKey, deviceName, identifier,
185 // requestId, body); 168 // requestId, body);
@@ -196,11 +179,9 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; { @@ -196,11 +179,9 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; {
196 * 发送响应 179 * 发送响应
197 * 180 *
198 * @param routingContext 路由上下文 181 * @param routingContext 路由上下文
199 - * @param requestId 请求 ID  
200 - * @param method 方法名  
201 * @param result 结果 182 * @param result 结果
202 */ 183 */
203 - private void sendResponse(RoutingContext routingContext, String requestId, String method, 184 + private void sendResponse(RoutingContext routingContext,
204 CommonResult<Boolean> result) { 185 CommonResult<Boolean> result) {
205 // // TODO @芋艿:后续再优化 186 // // TODO @芋艿:后续再优化
206 // IotStandardResponse response; 187 // IotStandardResponse response;
@@ -218,12 +199,11 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; { @@ -218,12 +199,11 @@ public class IotHttpUpstreamHandler implements Handler&lt;RoutingContext&gt; {
218 * 发送错误响应 199 * 发送错误响应
219 * 200 *
220 * @param routingContext 路由上下文 201 * @param routingContext 路由上下文
221 - * @param requestId 请求 ID  
222 * @param method 方法名 202 * @param method 方法名
223 * @param code 错误代码 203 * @param code 错误代码
224 * @param message 错误消息 204 * @param message 错误消息
225 */ 205 */
226 - private void sendErrorResponse(RoutingContext routingContext, String requestId, String method, Integer code, 206 + private void sendErrorResponse(RoutingContext routingContext, String method, Integer code,
227 String message) { 207 String message) {
228 // IotStandardResponse errorResponse = IotStandardResponse.error(requestId, method, code, message); 208 // IotStandardResponse errorResponse = IotStandardResponse.error(requestId, method, code, message);
229 // IotNetComponentCommonUtils.writeJsonResponse(routingContext, errorResponse); 209 // IotNetComponentCommonUtils.writeJsonResponse(routingContext, errorResponse);