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