Ver código fonte

feat(iot):【网关设备:68%】整体初步实现(修复少量 review agent 发现的缺陷)

YunaiV 4 meses atrás
pai
commit
268a2ad4cf

+ 5 - 6
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);
         }

+ 3 - 4
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) {

+ 44 - 0
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
+ * <p>
+ * 用于 thing.topo.change 下行消息的 params 参数
+ *
+ * @author 芋道源码
+ * @see <a href="https://help.aliyun.com/zh/marketplace/notify-gateway-topology-changes">阿里云 - 通知网关拓扑关系变化</a>
+ */
+@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<IotDeviceIdentity> subList;
+
+    public static IotDeviceTopoChangeReqDTO ofCreate(List<IotDeviceIdentity> subList) {
+        return new IotDeviceTopoChangeReqDTO(STATUS_CREATE, subList);
+    }
+
+    public static IotDeviceTopoChangeReqDTO ofDelete(List<IotDeviceIdentity> subList) {
+        return new IotDeviceTopoChangeReqDTO(STATUS_DELETE, subList);
+    }
+
+}