Commit 7f0de1e34e102d06e182b8f47af7334ac45cca07

Authored by YunaiV
1 parent 45b8172a

【代码新增】IoT:server 实现事件上行的逻辑(不包括 http 插件部分)

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/control/IotDeviceUpstreamServiceImpl.java
@@ -67,7 +67,10 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService { @@ -67,7 +67,10 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService {
67 } 67 }
68 // 2.2 情况二:事件上报 68 // 2.2 情况二:事件上报
69 if (Objects.equals(simulatorReqVO.getType(), IotDeviceMessageTypeEnum.EVENT.getType())) { 69 if (Objects.equals(simulatorReqVO.getType(), IotDeviceMessageTypeEnum.EVENT.getType())) {
70 - // TODO 芋艿:待实现 70 + reportDeviceEvent(((IotDeviceEventReportReqDTO)
  71 + new IotDeviceEventReportReqDTO().setRequestId(requestId).setReportTime(LocalDateTime.now())
  72 + .setProductKey(device.getProductKey()).setDeviceName(device.getDeviceName()))
  73 + .setIdentifier(simulatorReqVO.getIdentifier()).setParams((Map<String, Object>) simulatorReqVO.getData()));
71 return; 74 return;
72 } 75 }
73 // 2.3 情况三:状态变更 76 // 2.3 情况三:状态变更
@@ -120,7 +123,7 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService { @@ -120,7 +123,7 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService {
120 @Override 123 @Override
121 public void reportDeviceProperty(IotDevicePropertyReportReqDTO reportReqDTO) { 124 public void reportDeviceProperty(IotDevicePropertyReportReqDTO reportReqDTO) {
122 // 1.1 获得设备 125 // 1.1 获得设备
123 - log.info("[reportDevicePropertyData][上报设备属性数据: {}]", reportReqDTO); 126 + log.info("[reportDevicePropertyData][上报设备属性: {}]", reportReqDTO);
124 IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache( 127 IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache(
125 reportReqDTO.getProductKey(), reportReqDTO.getDeviceName()); 128 reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
126 if (device == null) { 129 if (device == null) {
@@ -141,9 +144,24 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService { @@ -141,9 +144,24 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService {
141 144
142 @Override 145 @Override
143 public void reportDeviceEvent(IotDeviceEventReportReqDTO reportReqDTO) { 146 public void reportDeviceEvent(IotDeviceEventReportReqDTO reportReqDTO) {
144 - log.info("[reportDeviceEventData][上报设备事件数据: {}]", reportReqDTO); 147 + // 1.1 获得设备
  148 + log.info("[reportDeviceEventData][上报设备事件: {}]", reportReqDTO);
  149 + IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache(
  150 + reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
  151 + if (device == null) {
  152 + log.error("[reportDeviceEventData][设备({}/{})不存在]",
  153 + reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
  154 + return;
  155 + }
  156 + // 1.2 记录设备的最后时间
  157 + updateDeviceLastTime(device, reportReqDTO);
145 158
146 - // TODO 芋艿:待实现 159 + // 2. 发送设备消息
  160 + IotDeviceMessage message = BeanUtils.toBean(reportReqDTO, IotDeviceMessage.class)
  161 + .setType(IotDeviceMessageTypeEnum.EVENT.getType())
  162 + .setIdentifier(reportReqDTO.getIdentifier())
  163 + .setData(reportReqDTO.getParams());
  164 + sendDeviceMessage(message, device);
147 } 165 }
148 166
149 private void updateDeviceLastTime(IotDeviceDO device, IotDeviceUpstreamAbstractReqDTO reqDTO) { 167 private void updateDeviceLastTime(IotDeviceDO device, IotDeviceUpstreamAbstractReqDTO reqDTO) {