Browse Source

feat(iot):将 IotDeviceMessage 中的 Map params 抽成 Topic DTO 对象

新增 IotDeviceStateUpdateReqDTO、IotDeviceOtaUpgradeReqDTO、IotDeviceOtaProgressReqDTO,
替换 IotDeviceMessage 和 IotOtaTaskRecordServiceImpl 中的裸 Map 构造和解析,
删除 IotDeviceMessage.buildOtaUpgrade() 方法。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
YunaiV 5 months ago
parent
commit
f64d84bb0c

+ 13 - 10
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/ota/IotOtaTaskRecordServiceImpl.java

@@ -3,11 +3,15 @@ package cn.iocoder.yudao.module.iot.service.ota;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.convert.Convert;
 import cn.hutool.core.lang.Assert;
-import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
+import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.iot.controller.admin.ota.vo.task.record.IotOtaTaskRecordPageReqVO;
+import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
+import cn.iocoder.yudao.module.iot.core.topic.ota.IotDeviceOtaProgressReqDTO;
+import cn.iocoder.yudao.module.iot.core.topic.ota.IotDeviceOtaUpgradeReqDTO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.ota.IotOtaFirmwareDO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.ota.IotOtaTaskRecordDO;
@@ -133,9 +137,9 @@ public class IotOtaTaskRecordServiceImpl implements IotOtaTaskRecordService {
     public boolean pushOtaTaskRecord(IotOtaTaskRecordDO record, IotOtaFirmwareDO fireware, IotDeviceDO device) {
         try {
             // 1. 推送 OTA 任务记录
-            IotDeviceMessage message = IotDeviceMessage.buildOtaUpgrade(
-                    fireware.getVersion(), fireware.getFileUrl(), fireware.getFileSize(),
-                    fireware.getFileDigestAlgorithm(), fireware.getFileDigestValue());
+            IotDeviceOtaUpgradeReqDTO params = BeanUtils.toBean(fireware, IotDeviceOtaUpgradeReqDTO.class);
+            IotDeviceMessage message = IotDeviceMessage.requestOf(
+                    IotDeviceMessageMethodEnum.OTA_UPGRADE.getMethod(), params);
             deviceMessageService.sendDeviceMessage(message, device);
 
             // 2. 更新 OTA 升级记录状态为进行中
@@ -163,17 +167,16 @@ public class IotOtaTaskRecordServiceImpl implements IotOtaTaskRecordService {
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    @SuppressWarnings("unchecked")
     public void updateOtaRecordProgress(IotDeviceDO device, IotDeviceMessage message) {
         // 1.1 参数解析
-        Map<String, Object> params = (Map<String, Object>) message.getParams();
-        String version = MapUtil.getStr(params, "version");
+        IotDeviceOtaProgressReqDTO params = JsonUtils.convertObject(message.getParams(), IotDeviceOtaProgressReqDTO.class);
+        String version = params.getVersion();
         Assert.notBlank(version, "version 不能为空");
-        Integer status = MapUtil.getInt(params, "status");
+        Integer status = params.getStatus();
         Assert.notNull(status, "status 不能为空");
         Assert.notNull(IotOtaTaskRecordStatusEnum.of(status), "status 状态不正确");
-        String description = MapUtil.getStr(params, "description");
-        Integer progress = MapUtil.getInt(params, "progress");
+        String description = params.getDescription();
+        Integer progress = params.getProgress();
         Assert.notNull(progress, "progress 不能为空");
         Assert.isTrue(progress >= 0 && progress <= 100, "progress 必须在 0-100 之间");
         // 1.2 查询 OTA 升级记录

+ 3 - 11
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/mq/message/IotDeviceMessage.java

@@ -1,9 +1,9 @@
 package cn.iocoder.yudao.module.iot.core.mq.message;
 
-import cn.hutool.core.map.MapUtil;
 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
 import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
 import cn.iocoder.yudao.module.iot.core.enums.device.IotDeviceStateEnum;
+import cn.iocoder.yudao.module.iot.core.topic.state.IotDeviceStateUpdateReqDTO;
 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
@@ -149,20 +149,12 @@ public class IotDeviceMessage {
 
     public static IotDeviceMessage buildStateUpdateOnline() {
         return requestOf(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod(),
-                MapUtil.of("state", IotDeviceStateEnum.ONLINE.getState()));
+                new IotDeviceStateUpdateReqDTO(IotDeviceStateEnum.ONLINE.getState()));
     }
 
     public static IotDeviceMessage buildStateOffline() {
         return requestOf(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod(),
-                MapUtil.of("state", IotDeviceStateEnum.OFFLINE.getState()));
-    }
-
-    public static IotDeviceMessage buildOtaUpgrade(String version, String fileUrl, Long fileSize,
-                                                   String fileDigestAlgorithm, String fileDigestValue) {
-        return requestOf(IotDeviceMessageMethodEnum.OTA_UPGRADE.getMethod(), MapUtil.builder()
-                .put("version", version).put("fileUrl", fileUrl).put("fileSize", fileSize)
-                .put("fileDigestAlgorithm", fileDigestAlgorithm).put("fileDigestValue", fileDigestValue)
-                .build());
+                new IotDeviceStateUpdateReqDTO(IotDeviceStateEnum.OFFLINE.getState()));
     }
 
 }

+ 40 - 0
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/ota/IotDeviceOtaProgressReqDTO.java

@@ -0,0 +1,40 @@
+package cn.iocoder.yudao.module.iot.core.topic.ota;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * IoT 设备 OTA 升级进度上报 Request DTO
+ * <p>
+ * 用于 thing.ota.progress 上行消息的 params 参数
+ *
+ * @author 芋道源码
+ * @see <a href="https://help.aliyun.com/zh/iot/user-guide/perform-ota-updates">阿里云 - OTA 升级</a>
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class IotDeviceOtaProgressReqDTO {
+
+    /**
+     * 固件版本号
+     */
+    private String version;
+
+    /**
+     * 升级状态
+     */
+    private Integer status;
+
+    /**
+     * 描述信息
+     */
+    private String description;
+
+    /**
+     * 升级进度(0-100)
+     */
+    private Integer progress;
+
+}

+ 45 - 0
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/ota/IotDeviceOtaUpgradeReqDTO.java

@@ -0,0 +1,45 @@
+package cn.iocoder.yudao.module.iot.core.topic.ota;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * IoT 设备 OTA 固件升级推送 Request DTO
+ * <p>
+ * 用于 thing.ota.upgrade 下行消息的 params 参数
+ *
+ * @author 芋道源码
+ * @see <a href="https://help.aliyun.com/zh/iot/user-guide/perform-ota-updates">阿里云 - OTA 升级</a>
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class IotDeviceOtaUpgradeReqDTO {
+
+    /**
+     * 固件版本号
+     */
+    private String version;
+
+    /**
+     * 固件文件下载地址
+     */
+    private String fileUrl;
+
+    /**
+     * 固件文件大小(字节)
+     */
+    private Long fileSize;
+
+    /**
+     * 固件文件摘要算法
+     */
+    private String fileDigestAlgorithm;
+
+    /**
+     * 固件文件摘要值
+     */
+    private String fileDigestValue;
+
+}

+ 24 - 0
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/state/IotDeviceStateUpdateReqDTO.java

@@ -0,0 +1,24 @@
+package cn.iocoder.yudao.module.iot.core.topic.state;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * IoT 设备状态更新 Request DTO
+ * <p>
+ * 用于 thing.state.update 消息的 params 参数
+ *
+ * @author 芋道源码
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class IotDeviceStateUpdateReqDTO {
+
+    /**
+     * 设备状态
+     */
+    private Integer state;
+
+}