Commit f64d84bb0c8186b0adb6f3e1e6369f4eb0fdd256

Authored by YunaiV
1 parent df4849d1

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>
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; @@ -3,11 +3,15 @@ package cn.iocoder.yudao.module.iot.service.ota;
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.convert.Convert; 4 import cn.hutool.core.convert.Convert;
5 import cn.hutool.core.lang.Assert; 5 import cn.hutool.core.lang.Assert;
6 -import cn.hutool.core.map.MapUtil;  
7 import cn.hutool.core.util.StrUtil; 6 import cn.hutool.core.util.StrUtil;
8 import cn.iocoder.yudao.framework.common.pojo.PageResult; 7 import cn.iocoder.yudao.framework.common.pojo.PageResult;
  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.module.iot.controller.admin.ota.vo.task.record.IotOtaTaskRecordPageReqVO; 10 import cn.iocoder.yudao.module.iot.controller.admin.ota.vo.task.record.IotOtaTaskRecordPageReqVO;
  11 +import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
10 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; 12 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
  13 +import cn.iocoder.yudao.module.iot.core.topic.ota.IotDeviceOtaProgressReqDTO;
  14 +import cn.iocoder.yudao.module.iot.core.topic.ota.IotDeviceOtaUpgradeReqDTO;
11 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; 15 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
12 import cn.iocoder.yudao.module.iot.dal.dataobject.ota.IotOtaFirmwareDO; 16 import cn.iocoder.yudao.module.iot.dal.dataobject.ota.IotOtaFirmwareDO;
13 import cn.iocoder.yudao.module.iot.dal.dataobject.ota.IotOtaTaskRecordDO; 17 import cn.iocoder.yudao.module.iot.dal.dataobject.ota.IotOtaTaskRecordDO;
@@ -133,9 +137,9 @@ public class IotOtaTaskRecordServiceImpl implements IotOtaTaskRecordService { @@ -133,9 +137,9 @@ public class IotOtaTaskRecordServiceImpl implements IotOtaTaskRecordService {
133 public boolean pushOtaTaskRecord(IotOtaTaskRecordDO record, IotOtaFirmwareDO fireware, IotDeviceDO device) { 137 public boolean pushOtaTaskRecord(IotOtaTaskRecordDO record, IotOtaFirmwareDO fireware, IotDeviceDO device) {
134 try { 138 try {
135 // 1. 推送 OTA 任务记录 139 // 1. 推送 OTA 任务记录
136 - IotDeviceMessage message = IotDeviceMessage.buildOtaUpgrade(  
137 - fireware.getVersion(), fireware.getFileUrl(), fireware.getFileSize(),  
138 - fireware.getFileDigestAlgorithm(), fireware.getFileDigestValue()); 140 + IotDeviceOtaUpgradeReqDTO params = BeanUtils.toBean(fireware, IotDeviceOtaUpgradeReqDTO.class);
  141 + IotDeviceMessage message = IotDeviceMessage.requestOf(
  142 + IotDeviceMessageMethodEnum.OTA_UPGRADE.getMethod(), params);
139 deviceMessageService.sendDeviceMessage(message, device); 143 deviceMessageService.sendDeviceMessage(message, device);
140 144
141 // 2. 更新 OTA 升级记录状态为进行中 145 // 2. 更新 OTA 升级记录状态为进行中
@@ -163,17 +167,16 @@ public class IotOtaTaskRecordServiceImpl implements IotOtaTaskRecordService { @@ -163,17 +167,16 @@ public class IotOtaTaskRecordServiceImpl implements IotOtaTaskRecordService {
163 167
164 @Override 168 @Override
165 @Transactional(rollbackFor = Exception.class) 169 @Transactional(rollbackFor = Exception.class)
166 - @SuppressWarnings("unchecked")  
167 public void updateOtaRecordProgress(IotDeviceDO device, IotDeviceMessage message) { 170 public void updateOtaRecordProgress(IotDeviceDO device, IotDeviceMessage message) {
168 // 1.1 参数解析 171 // 1.1 参数解析
169 - Map<String, Object> params = (Map<String, Object>) message.getParams();  
170 - String version = MapUtil.getStr(params, "version"); 172 + IotDeviceOtaProgressReqDTO params = JsonUtils.convertObject(message.getParams(), IotDeviceOtaProgressReqDTO.class);
  173 + String version = params.getVersion();
171 Assert.notBlank(version, "version 不能为空"); 174 Assert.notBlank(version, "version 不能为空");
172 - Integer status = MapUtil.getInt(params, "status"); 175 + Integer status = params.getStatus();
173 Assert.notNull(status, "status 不能为空"); 176 Assert.notNull(status, "status 不能为空");
174 Assert.notNull(IotOtaTaskRecordStatusEnum.of(status), "status 状态不正确"); 177 Assert.notNull(IotOtaTaskRecordStatusEnum.of(status), "status 状态不正确");
175 - String description = MapUtil.getStr(params, "description");  
176 - Integer progress = MapUtil.getInt(params, "progress"); 178 + String description = params.getDescription();
  179 + Integer progress = params.getProgress();
177 Assert.notNull(progress, "progress 不能为空"); 180 Assert.notNull(progress, "progress 不能为空");
178 Assert.isTrue(progress >= 0 && progress <= 100, "progress 必须在 0-100 之间"); 181 Assert.isTrue(progress >= 0 && progress <= 100, "progress 必须在 0-100 之间");
179 // 1.2 查询 OTA 升级记录 182 // 1.2 查询 OTA 升级记录
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/mq/message/IotDeviceMessage.java
1 package cn.iocoder.yudao.module.iot.core.mq.message; 1 package cn.iocoder.yudao.module.iot.core.mq.message;
2 2
3 -import cn.hutool.core.map.MapUtil;  
4 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; 3 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
5 import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum; 4 import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
6 import cn.iocoder.yudao.module.iot.core.enums.device.IotDeviceStateEnum; 5 import cn.iocoder.yudao.module.iot.core.enums.device.IotDeviceStateEnum;
  6 +import cn.iocoder.yudao.module.iot.core.topic.state.IotDeviceStateUpdateReqDTO;
7 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils; 7 import cn.iocoder.yudao.module.iot.core.util.IotDeviceMessageUtils;
8 import lombok.AllArgsConstructor; 8 import lombok.AllArgsConstructor;
9 import lombok.Builder; 9 import lombok.Builder;
@@ -149,20 +149,12 @@ public class IotDeviceMessage { @@ -149,20 +149,12 @@ public class IotDeviceMessage {
149 149
150 public static IotDeviceMessage buildStateUpdateOnline() { 150 public static IotDeviceMessage buildStateUpdateOnline() {
151 return requestOf(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod(), 151 return requestOf(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod(),
152 - MapUtil.of("state", IotDeviceStateEnum.ONLINE.getState())); 152 + new IotDeviceStateUpdateReqDTO(IotDeviceStateEnum.ONLINE.getState()));
153 } 153 }
154 154
155 public static IotDeviceMessage buildStateOffline() { 155 public static IotDeviceMessage buildStateOffline() {
156 return requestOf(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod(), 156 return requestOf(IotDeviceMessageMethodEnum.STATE_UPDATE.getMethod(),
157 - MapUtil.of("state", IotDeviceStateEnum.OFFLINE.getState()));  
158 - }  
159 -  
160 - public static IotDeviceMessage buildOtaUpgrade(String version, String fileUrl, Long fileSize,  
161 - String fileDigestAlgorithm, String fileDigestValue) {  
162 - return requestOf(IotDeviceMessageMethodEnum.OTA_UPGRADE.getMethod(), MapUtil.builder()  
163 - .put("version", version).put("fileUrl", fileUrl).put("fileSize", fileSize)  
164 - .put("fileDigestAlgorithm", fileDigestAlgorithm).put("fileDigestValue", fileDigestValue)  
165 - .build()); 157 + new IotDeviceStateUpdateReqDTO(IotDeviceStateEnum.OFFLINE.getState()));
166 } 158 }
167 159
168 } 160 }
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/ota/IotDeviceOtaProgressReqDTO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.core.topic.ota;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Data;
  5 +import lombok.NoArgsConstructor;
  6 +
  7 +/**
  8 + * IoT 设备 OTA 升级进度上报 Request DTO
  9 + * <p>
  10 + * 用于 thing.ota.progress 上行消息的 params 参数
  11 + *
  12 + * @author 芋道源码
  13 + * @see <a href="https://help.aliyun.com/zh/iot/user-guide/perform-ota-updates">阿里云 - OTA 升级</a>
  14 + */
  15 +@Data
  16 +@NoArgsConstructor
  17 +@AllArgsConstructor
  18 +public class IotDeviceOtaProgressReqDTO {
  19 +
  20 + /**
  21 + * 固件版本号
  22 + */
  23 + private String version;
  24 +
  25 + /**
  26 + * 升级状态
  27 + */
  28 + private Integer status;
  29 +
  30 + /**
  31 + * 描述信息
  32 + */
  33 + private String description;
  34 +
  35 + /**
  36 + * 升级进度(0-100)
  37 + */
  38 + private Integer progress;
  39 +
  40 +}
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/ota/IotDeviceOtaUpgradeReqDTO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.core.topic.ota;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Data;
  5 +import lombok.NoArgsConstructor;
  6 +
  7 +/**
  8 + * IoT 设备 OTA 固件升级推送 Request DTO
  9 + * <p>
  10 + * 用于 thing.ota.upgrade 下行消息的 params 参数
  11 + *
  12 + * @author 芋道源码
  13 + * @see <a href="https://help.aliyun.com/zh/iot/user-guide/perform-ota-updates">阿里云 - OTA 升级</a>
  14 + */
  15 +@Data
  16 +@NoArgsConstructor
  17 +@AllArgsConstructor
  18 +public class IotDeviceOtaUpgradeReqDTO {
  19 +
  20 + /**
  21 + * 固件版本号
  22 + */
  23 + private String version;
  24 +
  25 + /**
  26 + * 固件文件下载地址
  27 + */
  28 + private String fileUrl;
  29 +
  30 + /**
  31 + * 固件文件大小(字节)
  32 + */
  33 + private Long fileSize;
  34 +
  35 + /**
  36 + * 固件文件摘要算法
  37 + */
  38 + private String fileDigestAlgorithm;
  39 +
  40 + /**
  41 + * 固件文件摘要值
  42 + */
  43 + private String fileDigestValue;
  44 +
  45 +}
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/state/IotDeviceStateUpdateReqDTO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.core.topic.state;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Data;
  5 +import lombok.NoArgsConstructor;
  6 +
  7 +/**
  8 + * IoT 设备状态更新 Request DTO
  9 + * <p>
  10 + * 用于 thing.state.update 消息的 params 参数
  11 + *
  12 + * @author 芋道源码
  13 + */
  14 +@Data
  15 +@NoArgsConstructor
  16 +@AllArgsConstructor
  17 +public class IotDeviceStateUpdateReqDTO {
  18 +
  19 + /**
  20 + * 设备状态
  21 + */
  22 + private Integer state;
  23 +
  24 +}