Commit 043d82e5b6e88c3533974a62911dd7dab796ff07
1 parent
8e80a53a
【功能优化】IoT:完整实现 saveDeviceProperty 的设备属性日志的保存
Showing
2 changed files
with
18 additions
and
20 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDevicePropertyDataMapper.java
| ... | ... | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.iot.dal.tdengine; |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.util.ObjectUtil; |
| 5 | 5 | import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; |
| 6 | +import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; | |
| 6 | 7 | import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO; |
| 7 | 8 | import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField; |
| 8 | 9 | import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS; |
| ... | ... | @@ -77,6 +78,9 @@ public interface IotDevicePropertyDataMapper { |
| 77 | 78 | void alterProductPropertySTableDropField(@Param("productKey") String productKey, |
| 78 | 79 | @Param("field") TDengineTableField field); |
| 79 | 80 | |
| 81 | + void insert(@Param("device") IotDeviceDO device, | |
| 82 | + @Param("properties") Map<String, Object> properties); | |
| 83 | + | |
| 80 | 84 | // TODO @芋艿:待实现 |
| 81 | 85 | /** |
| 82 | 86 | * 获取历史数据列表 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/data/IotDevicePropertyServiceImpl.java
| ... | ... | @@ -6,7 +6,7 @@ import cn.hutool.core.util.StrUtil; |
| 6 | 6 | import cn.hutool.json.JSONObject; |
| 7 | 7 | import cn.hutool.json.JSONUtil; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 9 | -import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | |
| 9 | +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; | |
| 10 | 10 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO; |
| 11 | 11 | import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataSimulatorSaveReqVO; |
| 12 | 12 | import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDateOrTextDataSpecs; |
| ... | ... | @@ -16,7 +16,6 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO; |
| 16 | 16 | import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO; |
| 17 | 17 | import cn.iocoder.yudao.module.iot.dal.redis.device.DevicePropertyRedisDAO; |
| 18 | 18 | import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyDataMapper; |
| 19 | -import cn.iocoder.yudao.module.iot.enums.IotConstants; | |
| 20 | 19 | import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum; |
| 21 | 20 | import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum; |
| 22 | 21 | import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField; |
| ... | ... | @@ -27,7 +26,6 @@ import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService; |
| 27 | 26 | import jakarta.annotation.Resource; |
| 28 | 27 | import jakarta.validation.Valid; |
| 29 | 28 | import lombok.extern.slf4j.Slf4j; |
| 30 | -import org.springframework.beans.factory.annotation.Value; | |
| 31 | 29 | import org.springframework.stereotype.Service; |
| 32 | 30 | |
| 33 | 31 | import java.util.ArrayList; |
| ... | ... | @@ -63,9 +61,6 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService { |
| 63 | 61 | .put(IotDataSpecsDataTypeEnum.ARRAY.getDataType(), TDengineTableField.TYPE_NCHAR) // TODO 芋艿:怎么映射!!!! |
| 64 | 62 | .build(); |
| 65 | 63 | |
| 66 | - @Value("${spring.datasource.dynamic.datasource.tdengine.url}") | |
| 67 | - private String url; | |
| 68 | - | |
| 69 | 64 | @Resource |
| 70 | 65 | private IotDeviceService deviceService; |
| 71 | 66 | @Resource |
| ... | ... | @@ -123,22 +118,21 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService { |
| 123 | 118 | } |
| 124 | 119 | |
| 125 | 120 | @Override |
| 121 | + @TenantIgnore // TODO @芋艿:租户的缓存问题,需要考虑下。因为会存在一会又 tenantId,一会没有! | |
| 126 | 122 | public void saveDeviceProperty(IotDeviceMessage message) { |
| 127 | 123 | if (!(message.getData() instanceof Map)) { |
| 128 | 124 | log.error("[saveDeviceProperty][消息内容({}) 的 data 类型不正确]", message); |
| 129 | 125 | return; |
| 130 | 126 | } |
| 131 | 127 | // 1. 获得设备信息 |
| 132 | - IotDeviceDO device = TenantUtils.executeIgnore(() -> | |
| 133 | - deviceService.getDeviceByProductKeyAndDeviceName(message.getProductKey(), message.getDeviceName())); | |
| 128 | + IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceName(message.getProductKey(), message.getDeviceName()); | |
| 134 | 129 | if (device == null) { |
| 135 | 130 | log.error("[saveDeviceProperty][消息({}) 对应的设备不存在]", message); |
| 136 | 131 | return; |
| 137 | 132 | } |
| 138 | 133 | |
| 139 | 134 | // 2. 根据物模型,拼接合法的属性 |
| 140 | - List<IotThingModelDO> thingModels = TenantUtils.executeIgnore(() -> | |
| 141 | - thingModelService.getThingModelListByProductId(device.getProductId())); | |
| 135 | + List<IotThingModelDO> thingModels = thingModelService.getThingModelListByProductId(device.getProductId()); | |
| 142 | 136 | Map<String, Object> properties = new HashMap<>(); |
| 143 | 137 | ((Map<?, ?>) message.getData()).forEach((key, value) -> { |
| 144 | 138 | if (CollUtil.findOne(thingModels, thingModel -> thingModel.getIdentifier().equals(key)) == null) { |
| ... | ... | @@ -152,10 +146,10 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService { |
| 152 | 146 | return; |
| 153 | 147 | } |
| 154 | 148 | |
| 155 | - // 3.1 保存属性【数据】 | |
| 156 | - // TODO 芋艿,未实现 | |
| 149 | + // 3.1 保存设备属性【数据】 | |
| 150 | + devicePropertyDataMapper.insert(device, properties); | |
| 157 | 151 | |
| 158 | - // 3.2 保存属性【日志】 | |
| 152 | + // 3.2 保存设备属性【日志】 | |
| 159 | 153 | deviceDataRedisDAO.set(message.getDeviceKey(), convertMap(properties.entrySet(), Map.Entry::getKey, |
| 160 | 154 | entry -> IotDevicePropertyDO.builder().value(entry.getValue()).updateTime(message.getReportTime()).build())); |
| 161 | 155 | } |
| ... | ... | @@ -248,12 +242,12 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService { |
| 248 | 242 | return null; // TODO 芋艿:晚点实现 |
| 249 | 243 | } |
| 250 | 244 | |
| 251 | - private String getDatabaseName() { | |
| 252 | - return StrUtil.subAfter(url, "/", true); | |
| 253 | - } | |
| 254 | - | |
| 255 | - private static String getDeviceTableName(String productKey, String deviceName) { | |
| 256 | - return String.format(IotConstants.DEVICE_TABLE_NAME_FORMAT, productKey, deviceName); | |
| 257 | - } | |
| 245 | +// private String getDatabaseName() { | |
| 246 | +// return StrUtil.subAfter(url, "/", true); | |
| 247 | +// } | |
| 248 | +// | |
| 249 | +// private static String getDeviceTableName(String productKey, String deviceName) { | |
| 250 | +// return String.format(IotConstants.DEVICE_TABLE_NAME_FORMAT, productKey, deviceName); | |
| 251 | +// } | |
| 258 | 252 | |
| 259 | 253 | } |
| 260 | 254 | \ No newline at end of file | ... | ... |