Commit 2b27085ec2284b0db8ad42da553c0678f08ce526
1 parent
13ced8a1
feat:simulator2
Showing
3 changed files
with
39 additions
and
2 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceDataController.java
| ... | ... | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.iot.controller.admin.device; |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 6 | +import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; | |
| 6 | 7 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.*; |
| 7 | 8 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; |
| 8 | 9 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceLogDO; |
| ... | ... | @@ -54,10 +55,11 @@ public class IotDeviceDataController { |
| 54 | 55 | // TODO:数据权限 |
| 55 | 56 | @PostMapping("/simulator") |
| 56 | 57 | @Operation(summary = "模拟设备") |
| 57 | - public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulatorReqVO) { | |
| 58 | + public CommonResult<Boolean> simulatorDevice(@Valid @RequestBody IotDeviceDataSimulatorSaveReqVO simulcatorReqVO) { | |
| 58 | 59 | //TODO:先生成一下设备日志 后续完善模拟设备代码逻辑 |
| 59 | 60 | // TODO @super:应该 deviceDataService 里面有个 simulatorDevice,然后里面去 insert 日志! |
| 60 | - iotDeviceLogDataService.createDeviceLog(simulatorReqVO); | |
| 61 | + IotDevicePropertyReportReqDTO simulatorReqVO = new IotDevicePropertyReportReqDTO(); | |
| 62 | + deviceDataService.simulateSend(simulatorReqVO); | |
| 61 | 63 | return success(true); |
| 62 | 64 | } |
| 63 | 65 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataService.java
| ... | ... | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.iot.service.device; |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | 4 | import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; |
| 5 | 5 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO; |
| 6 | +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; | |
| 6 | 7 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; |
| 7 | 8 | import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage; |
| 8 | 9 | import jakarta.validation.Valid; |
| ... | ... | @@ -40,6 +41,14 @@ public interface IotDevicePropertyDataService { |
| 40 | 41 | void saveDeviceDataTest(ThingModelMessage thingModelMessage); |
| 41 | 42 | |
| 42 | 43 | /** |
| 44 | + * 模拟设备 | |
| 45 | + * | |
| 46 | + * @param simulatorReqVO 设备数据 | |
| 47 | + */ | |
| 48 | + | |
| 49 | + void simulateSend(IotDevicePropertyReportReqDTO simulatorReqVO); | |
| 50 | + | |
| 51 | + /** | |
| 43 | 52 | * 获得设备属性最新数据 |
| 44 | 53 | * |
| 45 | 54 | * @param deviceId 设备编号 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataServiceImpl.java
| ... | ... | @@ -8,6 +8,7 @@ import cn.hutool.json.JSONObject; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 9 | 9 | import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO; |
| 10 | 10 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO; |
| 11 | +import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; | |
| 11 | 12 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDateOrTextDataSpecs; |
| 12 | 13 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; |
| 13 | 14 | import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO; |
| ... | ... | @@ -22,6 +23,7 @@ import cn.iocoder.yudao.module.iot.enums.IotConstants; |
| 22 | 23 | import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum; |
| 23 | 24 | import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum; |
| 24 | 25 | import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField; |
| 26 | +import cn.iocoder.yudao.module.iot.mq.producer.simulatesend.SimulateSendProducer; | |
| 25 | 27 | import cn.iocoder.yudao.module.iot.service.product.IotProductService; |
| 26 | 28 | import cn.iocoder.yudao.module.iot.service.tdengine.IotThingModelMessageService; |
| 27 | 29 | import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService; |
| ... | ... | @@ -77,6 +79,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe |
| 77 | 79 | private IotProductService productService; |
| 78 | 80 | |
| 79 | 81 | @Resource |
| 82 | + private SimulateSendProducer simulateSendProducer; | |
| 83 | + | |
| 84 | + @Resource | |
| 80 | 85 | private TdEngineDMLMapper tdEngineDMLMapper; |
| 81 | 86 | |
| 82 | 87 | @Resource |
| ... | ... | @@ -159,6 +164,27 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe |
| 159 | 164 | thingModelMessageService.saveThingModelMessage(device, thingModelMessage); |
| 160 | 165 | } |
| 161 | 166 | |
| 167 | + //TODO: copy了saveDeviceData的逻辑,后续看看这块怎么优化 | |
| 168 | + @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); | |
| 175 | + 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")) | |
| 181 | + .productKey(simulatorReqVO.getProductKey()) | |
| 182 | + .deviceName(simulatorReqVO.getDeviceName()) | |
| 183 | + .deviceKey(device.getDeviceKey()) | |
| 184 | + .build(); | |
| 185 | + simulateSendProducer.sendSimulateMessage(thingModelMessage); | |
| 186 | + } | |
| 187 | + | |
| 162 | 188 | @Override |
| 163 | 189 | public List<IotDeviceDataDO> getLatestDeviceProperties(@Valid IotDeviceDataPageReqVO deviceDataReqVO) { |
| 164 | 190 | List<IotDeviceDataDO> list = new ArrayList<>(); | ... | ... |