From 268a2ad4cff9cb8319644fe18e771fba775d82c1 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 24 Jan 2026 19:31:47 +0800 Subject: [PATCH] feat(iot):【网关设备:68%】整体初步实现(修复少量 review agent 发现的缺陷) --- yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java | 11 +++++------ yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/message/IotDeviceMessageServiceImpl.java | 7 +++---- yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/topo/IotDeviceTopoChangeReqDTO.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/topo/IotDeviceTopoChangeReqDTO.java diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java index 3b5efb1..cfdfafa 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java @@ -605,7 +605,8 @@ public class IotDeviceServiceImpl implements IotDeviceService { if (!IotProductDeviceTypeEnum.isGatewaySub(device.getDeviceType())) { throw exception(DEVICE_NOT_GATEWAY_SUB, device.getProductKey(), device.getDeviceName()); } - if (ObjUtil.equals(device.getGatewayId(), gatewayId)) { + // 已绑定到其他网关,拒绝绑定(需先解绑) + if (device.getGatewayId() != null && ObjUtil.notEqual(device.getGatewayId(), gatewayId)) { throw exception(DEVICE_GATEWAY_BINDTO_EXISTS, device.getProductKey(), device.getDeviceName()); } } @@ -652,8 +653,7 @@ public class IotDeviceServiceImpl implements IotDeviceService { throw exception(DEVICE_NOT_GATEWAY); } // 1.2 解析参数 - IotDeviceTopoAddReqDTO params = JsonUtils.parseObject(JsonUtils.toJsonString(message.getParams()), - IotDeviceTopoAddReqDTO.class); + IotDeviceTopoAddReqDTO params = JsonUtils.convertObject(message.getParams(), IotDeviceTopoAddReqDTO.class); if (params == null || CollUtil.isEmpty(params.getSubDevices())) { throw exception(DEVICE_TOPO_PARAMS_INVALID); } @@ -693,7 +693,7 @@ public class IotDeviceServiceImpl implements IotDeviceService { checkSubDeviceCanBind(subDevice, gatewayDevice.getId()); // 2. 更新数据库 - deviceMapper.updateById(new IotDeviceDO().setId(subDevice.getId()).setGatewayId(subDevice.getGatewayId())); + deviceMapper.updateById(new IotDeviceDO().setId(subDevice.getId()).setGatewayId(gatewayDevice.getId())); log.info("[addDeviceTopo][网关({}/{}) 绑定子设备({}/{})]", gatewayDevice.getProductKey(), gatewayDevice.getDeviceName(), subDevice.getProductKey(), subDevice.getDeviceName()); @@ -710,8 +710,7 @@ public class IotDeviceServiceImpl implements IotDeviceService { throw exception(DEVICE_NOT_GATEWAY); } // 1.2 解析参数 - IotDeviceTopoDeleteReqDTO params = JsonUtils.parseObject(JsonUtils.toJsonString(message.getParams()), - IotDeviceTopoDeleteReqDTO.class); + IotDeviceTopoDeleteReqDTO params = JsonUtils.convertObject(message.getParams(), IotDeviceTopoDeleteReqDTO.class); if (params == null || CollUtil.isEmpty(params.getSubDevices())) { throw exception(DEVICE_TOPO_PARAMS_INVALID); } diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/message/IotDeviceMessageServiceImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/message/IotDeviceMessageServiceImpl.java index 36f65c3..0fc1e25 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/message/IotDeviceMessageServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/message/IotDeviceMessageServiceImpl.java @@ -251,9 +251,8 @@ public class IotDeviceMessageServiceImpl implements IotDeviceMessageService { */ private void handlePackMessage(IotDeviceMessage packMessage, IotDeviceDO gatewayDevice) { // 1. 解析参数 - IotDevicePropertyPackPostReqDTO params = JsonUtils.parseObject( - JsonUtils.toJsonString(packMessage.getParams()), - IotDevicePropertyPackPostReqDTO.class); + IotDevicePropertyPackPostReqDTO params = JsonUtils.convertObject( + packMessage.getParams(), IotDevicePropertyPackPostReqDTO.class); if (params == null) { log.warn("[handlePackMessage][消息({}) 参数解析失败]", packMessage); return; @@ -272,7 +271,7 @@ public class IotDeviceMessageServiceImpl implements IotDeviceMessageService { IotDeviceDO subDevice = deviceService.getDeviceFromCache(identity.getProductKey(), identity.getDeviceName()); if (subDevice == null) { log.warn("[handlePackMessage][子设备({}/{}) 不存在]", identity.getProductKey(), identity.getDeviceName()); - return; + continue; } sendDevicePackData(subDevice, packMessage.getServerId(), subDeviceData.getProperties(), subDeviceData.getEvents()); } catch (Exception ex) { diff --git a/yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/topo/IotDeviceTopoChangeReqDTO.java b/yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/topo/IotDeviceTopoChangeReqDTO.java new file mode 100644 index 0000000..0198206 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/topo/IotDeviceTopoChangeReqDTO.java @@ -0,0 +1,44 @@ +package cn.iocoder.yudao.module.iot.core.topic.topo; + +import cn.iocoder.yudao.module.iot.core.topic.IotDeviceIdentity; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * IoT 设备拓扑关系变更通知 Request DTO + *

+ * 用于 thing.topo.change 下行消息的 params 参数 + * + * @author 芋道源码 + * @see 阿里云 - 通知网关拓扑关系变化 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class IotDeviceTopoChangeReqDTO { + + public static final Integer STATUS_CREATE = 0; + public static final Integer STATUS_DELETE = 1; + + /** + * 拓扑关系状态 + */ + private Integer status; + + /** + * 子设备列表 + */ + private List subList; + + public static IotDeviceTopoChangeReqDTO ofCreate(List subList) { + return new IotDeviceTopoChangeReqDTO(STATUS_CREATE, subList); + } + + public static IotDeviceTopoChangeReqDTO ofDelete(List subList) { + return new IotDeviceTopoChangeReqDTO(STATUS_DELETE, subList); + } + +} -- libgit2 0.21.4