|
@@ -3,11 +3,14 @@ package cn.iocoder.yudao.module.iot.service.device;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.module.iot.api.device.dto.IotDevicePropertyReportReqDTO;
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
|
|
|
+import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO;
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDateOrTextDataSpecs;
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
|
|
@@ -22,6 +25,7 @@ import cn.iocoder.yudao.module.iot.enums.IotConstants;
|
|
|
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum;
|
|
|
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
|
|
|
import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField;
|
|
|
+import cn.iocoder.yudao.module.iot.mq.producer.simulatesend.SimulateSendProducer;
|
|
|
import cn.iocoder.yudao.module.iot.service.product.IotProductService;
|
|
|
import cn.iocoder.yudao.module.iot.service.tdengine.IotThingModelMessageService;
|
|
|
import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
|
|
@@ -37,8 +41,10 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
|
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
|
|
|
+import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_DATA_CONTENT_JSON_PARSE_ERROR;
|
|
|
|
|
|
/**
|
|
|
* IoT 设备【属性】数据 Service 实现类
|
|
@@ -76,6 +82,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
|
|
|
@Resource
|
|
|
private IotProductService productService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private SimulateSendProducer simulateSendProducer;
|
|
|
+
|
|
|
@Resource
|
|
|
private TdEngineDMLMapper tdEngineDMLMapper;
|
|
|
|
|
@@ -150,6 +159,45 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
|
|
|
thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
|
|
|
}
|
|
|
|
|
|
+ //TODO:后续捋一捋这块逻辑,先借鉴一下目前的代码
|
|
|
+ @Override
|
|
|
+ public void saveDeviceDataTest(ThingModelMessage thingModelMessage) {
|
|
|
+ // 1. 根据产品 key 和设备名称,获得设备信息
|
|
|
+ IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceName(thingModelMessage.getProductKey(), thingModelMessage.getDeviceName());
|
|
|
+ // 2. 保存数据
|
|
|
+ thingModelMessageService.saveThingModelMessage(device, thingModelMessage);
|
|
|
+ }
|
|
|
+
|
|
|
+ //TODO: copy 了 saveDeviceData 的逻辑,后续看看这块怎么优化
|
|
|
+ @Override
|
|
|
+ public void simulatorSend(IotDeviceDataSimulatorSaveReqVO simulatorReqVO) {
|
|
|
+ // 1. 根据设备 key ,获得设备信息
|
|
|
+ IotDeviceDO device = deviceService.getDeviceByDeviceKey(simulatorReqVO.getDeviceKey());
|
|
|
+
|
|
|
+ // 2. 解析 content 为 JSON 对象
|
|
|
+ JSONObject contentJson;
|
|
|
+ try {
|
|
|
+ contentJson = JSONUtil.parseObj(simulatorReqVO.getContent());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw exception(DEVICE_DATA_CONTENT_JSON_PARSE_ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 构建物模型消息
|
|
|
+ ThingModelMessage thingModelMessage = ThingModelMessage.builder()
|
|
|
+ .id(IdUtil.fastSimpleUUID()) // TODO:后续优化
|
|
|
+ .sys(null)// TODO:这块先写死,后续优化
|
|
|
+ .method("thing.event.property.post") // TODO:这块先写死,后续优化
|
|
|
+ .params(contentJson) // 将 content 作为 params
|
|
|
+ .time(simulatorReqVO.getReportTime()) // 使用上报时间
|
|
|
+ .productKey(simulatorReqVO.getProductKey())
|
|
|
+ .deviceName(device.getDeviceName())
|
|
|
+ .deviceKey(device.getDeviceKey())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 4. 发送模拟消息
|
|
|
+ simulateSendProducer.sendSimulateMessage(thingModelMessage);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<IotDeviceDataDO> getLatestDeviceProperties(@Valid IotDeviceDataPageReqVO deviceDataReqVO) {
|
|
|
List<IotDeviceDataDO> list = new ArrayList<>();
|
|
@@ -196,6 +244,7 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
|
|
|
SelectVisualDO selectVisualDO = new SelectVisualDO();
|
|
|
selectVisualDO.setDataBaseName(getDatabaseName());
|
|
|
selectVisualDO.setTableName(getDeviceTableName(device.getProductKey(), device.getDeviceName()));
|
|
|
+ selectVisualDO.setDeviceKey(device.getDeviceKey());
|
|
|
selectVisualDO.setFieldName(deviceDataReqVO.getIdentifier());
|
|
|
selectVisualDO.setStartTime(DateUtil.date(deviceDataReqVO.getTimes()[0].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()).getTime());
|
|
|
selectVisualDO.setEndTime(DateUtil.date(deviceDataReqVO.getTimes()[1].atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()).getTime());
|
|
@@ -203,8 +252,8 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
|
|
|
params.put("rows", deviceDataReqVO.getPageSize());
|
|
|
params.put("page", (deviceDataReqVO.getPageNo() - 1) * deviceDataReqVO.getPageSize());
|
|
|
selectVisualDO.setParams(params);
|
|
|
- pageResult.setList(tdEngineDMLMapper.selectHistoryDataList(selectVisualDO));
|
|
|
- pageResult.setTotal(tdEngineDMLMapper.selectHistoryCount(selectVisualDO));
|
|
|
+ pageResult.setList(devicePropertyDataMapper.selectHistoryDataList(selectVisualDO));
|
|
|
+ pageResult.setTotal(devicePropertyDataMapper.selectHistoryCount(selectVisualDO));
|
|
|
return pageResult;
|
|
|
}
|
|
|
|