Commit 268a2ad4cff9cb8319644fe18e771fba775d82c1

Authored by YunaiV
1 parent 05eb745f

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

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 {
605 605 if (!IotProductDeviceTypeEnum.isGatewaySub(device.getDeviceType())) {
606 606 throw exception(DEVICE_NOT_GATEWAY_SUB, device.getProductKey(), device.getDeviceName());
607 607 }
608   - if (ObjUtil.equals(device.getGatewayId(), gatewayId)) {
  608 + // 已绑定到其他网关,拒绝绑定(需先解绑)
  609 + if (device.getGatewayId() != null && ObjUtil.notEqual(device.getGatewayId(), gatewayId)) {
609 610 throw exception(DEVICE_GATEWAY_BINDTO_EXISTS, device.getProductKey(), device.getDeviceName());
610 611 }
611 612 }
... ... @@ -652,8 +653,7 @@ public class IotDeviceServiceImpl implements IotDeviceService {
652 653 throw exception(DEVICE_NOT_GATEWAY);
653 654 }
654 655 // 1.2 解析参数
655   - IotDeviceTopoAddReqDTO params = JsonUtils.parseObject(JsonUtils.toJsonString(message.getParams()),
656   - IotDeviceTopoAddReqDTO.class);
  656 + IotDeviceTopoAddReqDTO params = JsonUtils.convertObject(message.getParams(), IotDeviceTopoAddReqDTO.class);
657 657 if (params == null || CollUtil.isEmpty(params.getSubDevices())) {
658 658 throw exception(DEVICE_TOPO_PARAMS_INVALID);
659 659 }
... ... @@ -693,7 +693,7 @@ public class IotDeviceServiceImpl implements IotDeviceService {
693 693 checkSubDeviceCanBind(subDevice, gatewayDevice.getId());
694 694  
695 695 // 2. 更新数据库
696   - deviceMapper.updateById(new IotDeviceDO().setId(subDevice.getId()).setGatewayId(subDevice.getGatewayId()));
  696 + deviceMapper.updateById(new IotDeviceDO().setId(subDevice.getId()).setGatewayId(gatewayDevice.getId()));
697 697 log.info("[addDeviceTopo][网关({}/{}) 绑定子设备({}/{})]",
698 698 gatewayDevice.getProductKey(), gatewayDevice.getDeviceName(),
699 699 subDevice.getProductKey(), subDevice.getDeviceName());
... ... @@ -710,8 +710,7 @@ public class IotDeviceServiceImpl implements IotDeviceService {
710 710 throw exception(DEVICE_NOT_GATEWAY);
711 711 }
712 712 // 1.2 解析参数
713   - IotDeviceTopoDeleteReqDTO params = JsonUtils.parseObject(JsonUtils.toJsonString(message.getParams()),
714   - IotDeviceTopoDeleteReqDTO.class);
  713 + IotDeviceTopoDeleteReqDTO params = JsonUtils.convertObject(message.getParams(), IotDeviceTopoDeleteReqDTO.class);
715 714 if (params == null || CollUtil.isEmpty(params.getSubDevices())) {
716 715 throw exception(DEVICE_TOPO_PARAMS_INVALID);
717 716 }
... ...
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 {
251 251 */
252 252 private void handlePackMessage(IotDeviceMessage packMessage, IotDeviceDO gatewayDevice) {
253 253 // 1. 解析参数
254   - IotDevicePropertyPackPostReqDTO params = JsonUtils.parseObject(
255   - JsonUtils.toJsonString(packMessage.getParams()),
256   - IotDevicePropertyPackPostReqDTO.class);
  254 + IotDevicePropertyPackPostReqDTO params = JsonUtils.convertObject(
  255 + packMessage.getParams(), IotDevicePropertyPackPostReqDTO.class);
257 256 if (params == null) {
258 257 log.warn("[handlePackMessage][消息({}) 参数解析失败]", packMessage);
259 258 return;
... ... @@ -272,7 +271,7 @@ public class IotDeviceMessageServiceImpl implements IotDeviceMessageService {
272 271 IotDeviceDO subDevice = deviceService.getDeviceFromCache(identity.getProductKey(), identity.getDeviceName());
273 272 if (subDevice == null) {
274 273 log.warn("[handlePackMessage][子设备({}/{}) 不存在]", identity.getProductKey(), identity.getDeviceName());
275   - return;
  274 + continue;
276 275 }
277 276 sendDevicePackData(subDevice, packMessage.getServerId(), subDeviceData.getProperties(), subDeviceData.getEvents());
278 277 } catch (Exception ex) {
... ...
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/topic/topo/IotDeviceTopoChangeReqDTO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.core.topic.topo;
  2 +
  3 +import cn.iocoder.yudao.module.iot.core.topic.IotDeviceIdentity;
  4 +import lombok.AllArgsConstructor;
  5 +import lombok.Data;
  6 +import lombok.NoArgsConstructor;
  7 +
  8 +import java.util.List;
  9 +
  10 +/**
  11 + * IoT 设备拓扑关系变更通知 Request DTO
  12 + * <p>
  13 + * 用于 thing.topo.change 下行消息的 params 参数
  14 + *
  15 + * @author 芋道源码
  16 + * @see <a href="https://help.aliyun.com/zh/marketplace/notify-gateway-topology-changes">阿里云 - 通知网关拓扑关系变化</a>
  17 + */
  18 +@Data
  19 +@NoArgsConstructor
  20 +@AllArgsConstructor
  21 +public class IotDeviceTopoChangeReqDTO {
  22 +
  23 + public static final Integer STATUS_CREATE = 0;
  24 + public static final Integer STATUS_DELETE = 1;
  25 +
  26 + /**
  27 + * 拓扑关系状态
  28 + */
  29 + private Integer status;
  30 +
  31 + /**
  32 + * 子设备列表
  33 + */
  34 + private List<IotDeviceIdentity> subList;
  35 +
  36 + public static IotDeviceTopoChangeReqDTO ofCreate(List<IotDeviceIdentity> subList) {
  37 + return new IotDeviceTopoChangeReqDTO(STATUS_CREATE, subList);
  38 + }
  39 +
  40 + public static IotDeviceTopoChangeReqDTO ofDelete(List<IotDeviceIdentity> subList) {
  41 + return new IotDeviceTopoChangeReqDTO(STATUS_DELETE, subList);
  42 + }
  43 +
  44 +}
... ...