Commit 4f962bd1f73fed4b7fb1c4186300ecd3ca0ac59f

Authored by alwayssuper
1 parent 2b27085e

[fix]:code review

Showing 12 changed files with 68 additions and 31 deletions
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
... ... @@ -30,6 +30,7 @@ public interface ErrorCodeConstants {
30 30 ErrorCode DEVICE_GATEWAY_NOT_EXISTS = new ErrorCode(1_050_003_004, "网关设备不存在");
31 31 ErrorCode DEVICE_NOT_GATEWAY = new ErrorCode(1_050_003_005, "设备不是网关设备");
32 32 ErrorCode DEVICE_IMPORT_LIST_IS_EMPTY = new ErrorCode(1_050_003_006, "导入设备数据不能为空!");
  33 + ErrorCode DEVICE_DATA_CONTENT_JSON_PARSE_ERROR = new ErrorCode(1_050_003_007, "导入设备数据格式错误!");
33 34  
34 35 // ========== 产品分类 1-050-004-000 ==========
35 36 ErrorCode PRODUCT_CATEGORY_NOT_EXISTS = new ErrorCode(1_050_004_000, "产品分类不存在");
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceDataController.java
... ... @@ -55,11 +55,10 @@ public class IotDeviceDataController {
55 55 // TODO:数据权限
56 56 @PostMapping("/simulator")
57 57 @Operation(summary = "模拟设备")
58   - public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulcatorReqVO) {
59   - //TODO:先生成一下设备日志 后续完善模拟设备代码逻辑
  58 + public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
  59 + //TODO:先使用 IotDeviceDataSimulatorSaveReqVO 另外content里数据类型的效验前端也没做,后端应该要要效验一下,这块后续看看怎么安排
60 60 // TODO @super:应该 deviceDataService 里面有个 simulatorDevice,然后里面去 insert 日志!
61   - IotDevicePropertyReportReqDTO simulatorReqVO = new IotDevicePropertyReportReqDTO();
62   - deviceDataService.simulateSend(simulatorReqVO);
  61 + deviceDataService.simulatorSend(simulatorReqVO);
63 62 return success(true);
64 63 }
65 64  
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/vo/deviceData/IotDeviceDataSimulatorSaveReqVO.java
... ... @@ -38,7 +38,6 @@ public class IotDeviceDataSimulatorSaveReqVO {
38 38 @NotEmpty(message = "数据内容不能为空")
39 39 private String content;
40 40  
41   - // TODO @芋艿:需要讨论下,reportTime 到底以那个为准!
42 41 @Schema(description = "上报时间", requiredMode = Schema.RequiredMode.REQUIRED)
43 42 private Long reportTime;
44 43  
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/consumer/deviceconsumer/DeviceConsumer.java
... ... @@ -30,7 +30,9 @@ public class DeviceConsumer {
30 30 @Async
31 31 public void onMessage(ThingModelMessage message) {
32 32 log.info("[onMessage][消息内容({})]", message);
33   - deviceDataService.saveDeviceDataTest(message);
  33 + // 设备数据记录
  34 +// deviceDataService.saveDeviceDataTest(message);
  35 + // 设备日志记录
34 36 iotDeviceLogDataService.saveDeviceLog(message);
35 37 }
36 38  
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/consumer/simulatesend/SimulateSendConsumer.java deleted
1   -package cn.iocoder.yudao.module.iot.mq.consumer.simulatesend;
2   -
3   -/**
4   - * TODO @alwayssuper:记得实现,还有类注释哈
5   - *
6   - * @author alwayssuper
7   - */
8   -public class SimulateSendConsumer {
9   -}
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceLogDataService.java
... ... @@ -37,7 +37,11 @@ public interface IotDeviceLogDataService {
37 37 */
38 38 PageResult<IotDeviceLogDO> getDeviceLogPage(IotDeviceLogPageReqVO pageReqVO);
39 39  
40   -
  40 + /**
  41 + * 插入设备日志
  42 + *
  43 + * @param msg 设备数据
  44 + */
41 45 void saveDeviceLog(ThingModelMessage msg);
42 46  
43 47 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceLogDataServiceImpl.java
1 1 package cn.iocoder.yudao.module.iot.service.device;
2 2  
  3 +import cn.hutool.json.JSONUtil;
3 4 import cn.iocoder.yudao.framework.common.pojo.PageResult;
4 5 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
5 6 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
... ... @@ -12,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
12 13 import org.springframework.stereotype.Service;
13 14 import org.springframework.validation.annotation.Validated;
14 15  
  16 +import java.util.Date;
15 17 import java.util.List;
16 18  
17 19 /**
... ... @@ -79,7 +81,19 @@ public class IotDeviceLogDataServiceImpl implements IotDeviceLogDataService{
79 81  
80 82 @Override
81 83 public void saveDeviceLog(ThingModelMessage msg) {
  84 + // 1. 构建设备日志对象
  85 + IotDeviceLogDO iotDeviceLogDO = IotDeviceLogDO.builder()
  86 + .id(msg.getId()) // 消息ID
  87 + .deviceKey(msg.getDeviceKey()) // 设备标识
  88 + .productKey(msg.getProductKey()) // 产品标识
  89 + .type(msg.getMethod()) // 消息类型,使用method作为类型
  90 + .subType("property") // TODO:这块先写死,后续优化
  91 + .content(JSONUtil.toJsonStr(msg)) // TODO:后续优化
  92 + .reportTime(msg.getTime()) // 上报时间
  93 + .build();
82 94  
  95 + // 2. 插入设备日志
  96 + iotDeviceLogDataMapper.insert(iotDeviceLogDO);
83 97 }
84 98  
85 99 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataService.java
... ... @@ -46,7 +46,7 @@ public interface IotDevicePropertyDataService {
46 46 * @param simulatorReqVO 设备数据
47 47 */
48 48  
49   - void simulateSend(IotDevicePropertyReportReqDTO simulatorReqVO);
  49 + void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO);
50 50  
51 51 /**
52 52 * 获得设备属性最新数据
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataServiceImpl.java
... ... @@ -3,8 +3,10 @@ package cn.iocoder.yudao.module.iot.service.device;
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.date.DateUtil;
5 5 import cn.hutool.core.map.MapUtil;
  6 +import cn.hutool.core.util.IdUtil;
6 7 import cn.hutool.core.util.StrUtil;
7 8 import cn.hutool.json.JSONObject;
  9 +import cn.hutool.json.JSONUtil;
8 10 import cn.iocoder.yudao.framework.common.pojo.PageResult;
9 11 import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
10 12 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
... ... @@ -39,8 +41,10 @@ import java.util.HashMap;
39 41 import java.util.List;
40 42 import java.util.Map;
41 43  
  44 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
42 45 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
43 46 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
  47 +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_DATA_CONTENT_JSON_PARSE_ERROR;
44 48  
45 49 /**
46 50 * IoT 设备【属性】数据 Service 实现类
... ... @@ -164,24 +168,33 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
164 168 thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
165 169 }
166 170  
167   - //TODO: copy了saveDeviceData的逻辑,后续看看这块怎么优化
  171 + //TODO: copy 了 saveDeviceData 的逻辑,后续看看这块怎么优化
168 172 @Override
169   - public void simulateSend(IotDevicePropertyReportReqDTO simulatorReqVO) {
170   - // 1. 根据产品 key 和设备名称,获得设备信息
171   - IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceName(simulatorReqVO.getProductKey(), simulatorReqVO.getDeviceName());
172   - // 2. 解析消息,保存数据
173   - JSONObject jsonObject = new JSONObject(simulatorReqVO.getParams());
174   - log.info("[saveDeviceData][productKey({}) deviceName({}) data({})]", simulatorReqVO.getProductKey(), simulatorReqVO.getDeviceName(), jsonObject);
  173 + public void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
  174 + // 1. 根据设备 key ,获得设备信息
  175 + IotDeviceDO device = deviceService.getDeviceByDeviceKey(simulatorReqVO.getDeviceKey());
  176 +
  177 + // 2. 解析 content 为 JSON 对象
  178 + JSONObject contentJson;
  179 + try {
  180 + contentJson = JSONUtil.parseObj(simulatorReqVO.getContent());
  181 + } catch (Exception e) {
  182 + throw exception(DEVICE_DATA_CONTENT_JSON_PARSE_ERROR);
  183 + }
  184 +
  185 + // 3. 构建物模型消息
175 186 ThingModelMessage thingModelMessage = ThingModelMessage.builder()
176   - .id(jsonObject.getStr("id"))
177   - .sys(jsonObject.get("sys"))
178   - .method(jsonObject.getStr("method"))
179   - .params(jsonObject.get("params"))
180   - .time(jsonObject.getLong("time") == null ? System.currentTimeMillis() : jsonObject.getLong("time"))
  187 + .id(IdUtil.fastSimpleUUID()) // TODO:后续优化
  188 + .sys(null)// TODO:这块先写死,后续优化
  189 + .method("thing.event.property.post") // TODO:这块先写死,后续优化
  190 + .params(contentJson) // 将 content 作为 params
  191 + .time(simulatorReqVO.getReportTime()) // 使用上报时间
181 192 .productKey(simulatorReqVO.getProductKey())
182   - .deviceName(simulatorReqVO.getDeviceName())
  193 + .deviceName(device.getDeviceName())
183 194 .deviceKey(device.getDeviceKey())
184 195 .build();
  196 +
  197 + // 4. 发送模拟消息
185 198 simulateSendProducer.sendSimulateMessage(thingModelMessage);
186 199 }
187 200  
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java
... ... @@ -72,6 +72,15 @@ public interface IotDeviceService {
72 72 */
73 73 IotDeviceDO getDevice(Long id);
74 74  
  75 +
  76 + /**
  77 + * 根据设备 key 获得设备
  78 + *
  79 + * @param deviceKey 编号
  80 + * @return IoT 设备
  81 + */
  82 + IotDeviceDO getDeviceByDeviceKey(String deviceKey);
  83 +
75 84 /**
76 85 * ��得设备分页
77 86 *
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java
... ... @@ -198,6 +198,11 @@ public class IotDeviceServiceImpl implements IotDeviceService {
198 198 }
199 199  
200 200 @Override
  201 + public IotDeviceDO getDeviceByDeviceKey(String deviceKey) {
  202 + return deviceMapper.selectByDeviceKey(deviceKey);
  203 + }
  204 +
  205 + @Override
201 206 public PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO) {
202 207 return deviceMapper.selectPage(pageReqVO);
203 208 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDeviceLogDataMapper.xml
... ... @@ -27,7 +27,7 @@
27 27  
28 28 <!-- 插入设备日志数据 在子表不存在的情况下 可在数据插入时 自动建表 -->
29 29 <insert id="insert">
30   - INSERT INTO device_log_${log.deviceKey} (id, product_key, type, subType, content, report_time)
  30 + INSERT INTO device_log_${log.deviceKey} (ts, id, product_key, type, subType, content, report_time)
31 31 USING device_log
32 32 TAGS ('${log.deviceKey}')
33 33 VALUES (
... ...