Commit 0352dda469095b49555badf76fe33593a57c3974

Authored by puhui999
1 parent 10da1e09

【代码优化】IOT: 优化一些 TODO

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodel/model/dataType/ThinkModelArgument.java
1 1 package cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.dataType;
2 2  
  3 +import cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.ThinkModelProperty;
3 4 import lombok.Data;
4 5  
5 6 @Data
... ... @@ -7,7 +8,10 @@ public class ThinkModelArgument {
7 8  
8 9 private String identifier;
9 10 private String name;
10   - private ThinkModelDataSpecs dataType;
  11 + /**
  12 + * 物模型中的属性
  13 + */
  14 + private ThinkModelProperty property;
11 15 /**
12 16 * 用于区分输入或输出参数,"input" 或 "output"
13 17 */
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/device/IotDeviceDataDO.java
... ... @@ -32,7 +32,7 @@ public class IotDeviceDataDO {
32 32 * <p>
33 33 * 关联 {@link IotProductThinkModelDO#getId()}
34 34 */
35   - private Long thinkModelFunctionId;
  35 + private Long thinkModelId;
36 36  
37 37 /**
38 38 * 产品标识
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/tdengine/FieldParser.java
... ... @@ -35,19 +35,16 @@ public class FieldParser {
35 35 */
36 36 public static TdFieldDO parse(ThinkModelProperty property) {
37 37 String fieldName = property.getIdentifier().toLowerCase();
38   - //// TODO @puhui999: 需要重构
39   - //ThinkModelDataSpecs type = property.getDataType();
40   - //
41   - //// 将物模型字段类型映射为td字段类型
42   - //String fType = TYPE_MAPPING.get(type.getDataType().toUpperCase());
43   - //
44   - //// 如果字段类型为NCHAR,默认长度为64
45   - //int dataLength = 0;
46   - //if ("NCHAR".equals(fType)) {
47   - // dataLength = 64;
48   - //}
49   - //return new TdFieldDO(fieldName, fType, dataLength);
50   - return null;
  38 +
  39 + // 将物模型字段类型映射为td字段类型
  40 + String fType = TYPE_MAPPING.get(property.getDataType().toUpperCase());
  41 +
  42 + // 如果字段类型为NCHAR,默认长度为64
  43 + int dataLength = 0;
  44 + if ("NCHAR".equals(fType)) {
  45 + dataLength = 64;
  46 + }
  47 + return new TdFieldDO(fieldName, fType, dataLength);
51 48 }
52 49  
53 50 /**
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceDataServiceImpl.java
... ... @@ -7,9 +7,9 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
7 7 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
8 8 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
9 9 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
10   -import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodel.IotProductThinkModelDO;
11 10 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
12 11 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThinkModelMessage;
  12 +import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodel.IotProductThinkModelDO;
13 13 import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO;
14 14 import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
15 15 import cn.iocoder.yudao.module.iot.enums.IotConstants;
... ... @@ -28,6 +28,8 @@ import java.util.HashMap;
28 28 import java.util.List;
29 29 import java.util.Map;
30 30  
  31 +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
  32 +
31 33 @Slf4j
32 34 @Service
33 35 public class IotDeviceDataServiceImpl implements IotDeviceDataService {
... ... @@ -73,35 +75,31 @@ public class IotDeviceDataServiceImpl implements IotDeviceDataService {
73 75 // 1. 获取设备信息
74 76 IotDeviceDO device = deviceService.getDevice(deviceDataReqVO.getDeviceId());
75 77 // 2. 获取设备属性最新数据
76   - List<IotProductThinkModelDO> thinkModelFunctionList = thinkModelFunctionService.getProductThinkModelListByProductKey(device.getProductKey());
77   - thinkModelFunctionList = thinkModelFunctionList.stream()
78   - .filter(function -> IotProductThinkModelTypeEnum.PROPERTY.getType()
79   - .equals(function.getType())).toList();
  78 + List<IotProductThinkModelDO> thinkModelList = thinkModelFunctionService.getProductThinkModelListByProductKey(device.getProductKey());
  79 + thinkModelList = filterList(thinkModelList, thinkModel -> IotProductThinkModelTypeEnum.PROPERTY.getType()
  80 + .equals(thinkModel.getType()));
80 81  
81 82 // 3. 过滤标识符和属性名称
82 83 if (deviceDataReqVO.getIdentifier() != null) {
83   - thinkModelFunctionList = thinkModelFunctionList.stream()
84   - .filter(function -> function.getIdentifier().toLowerCase().contains(deviceDataReqVO.getIdentifier().toLowerCase()))
85   - .toList();
  84 + thinkModelList = filterList(thinkModelList, thinkModel -> thinkModel.getIdentifier()
  85 + .toLowerCase().contains(deviceDataReqVO.getIdentifier().toLowerCase()));
86 86 }
87 87 if (deviceDataReqVO.getName() != null) {
88   - thinkModelFunctionList = thinkModelFunctionList.stream()
89   - .filter(function -> function.getName().toLowerCase().contains(deviceDataReqVO.getName().toLowerCase()))
90   - .toList();
  88 + thinkModelList = filterList(thinkModelList, thinkModel -> thinkModel.getName()
  89 + .toLowerCase().contains(deviceDataReqVO.getName().toLowerCase()));
91 90 }
92 91 // 4. 获取设备属性最新数据
93   - // TODO @puhui999: 需要重构
94   - thinkModelFunctionList.forEach(function -> {
95   - IotDeviceDataDO deviceData = deviceDataRedisDAO.get(device.getProductKey(), device.getDeviceName(), function.getIdentifier());
  92 + thinkModelList.forEach(thinkModel -> {
  93 + IotDeviceDataDO deviceData = deviceDataRedisDAO.get(device.getProductKey(), device.getDeviceName(), thinkModel.getIdentifier());
96 94 if (deviceData == null) {
97 95 deviceData = new IotDeviceDataDO();
98 96 deviceData.setProductKey(device.getProductKey());
99 97 deviceData.setDeviceName(device.getDeviceName());
100   - deviceData.setIdentifier(function.getIdentifier());
  98 + deviceData.setIdentifier(thinkModel.getIdentifier());
101 99 deviceData.setDeviceId(deviceDataReqVO.getDeviceId());
102   - deviceData.setThinkModelFunctionId(function.getId());
103   - deviceData.setName(function.getName());
104   - //deviceData.setDataType(function.getProperty().getDataType().getDataType());
  100 + deviceData.setThinkModelId(thinkModel.getId());
  101 + deviceData.setName(thinkModel.getName());
  102 + deviceData.setDataType(thinkModel.getProperty().getDataType());
105 103 }
106 104 list.add(deviceData);
107 105 });
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/tdengine/IotThinkModelMessageServiceImpl.java
... ... @@ -129,7 +129,6 @@ public class IotThinkModelMessageServiceImpl implements IotThinkModelMessageServ
129 129 * @param time 时间
130 130 */
131 131 private void setDeviceDataCache(IotDeviceDO device, IotProductThinkModelDO iotProductThinkModelDO, Object val, Long time) {
132   - // TODO @puhui999: 需要重构
133 132 IotDeviceDataDO deviceData = IotDeviceDataDO.builder()
134 133 .productKey(device.getProductKey())
135 134 .deviceName(device.getDeviceName())
... ... @@ -137,9 +136,9 @@ public class IotThinkModelMessageServiceImpl implements IotThinkModelMessageServ
137 136 .value(val != null ? val.toString() : null)
138 137 .updateTime(DateUtil.toLocalDateTime(new Date(time)))
139 138 .deviceId(device.getId())
140   - .thinkModelFunctionId(iotProductThinkModelDO.getId())
  139 + .thinkModelId(iotProductThinkModelDO.getId())
141 140 .name(iotProductThinkModelDO.getName())
142   - //.dataType(iotProductThinkModelDO.getProperty().getDataType().getDataType())
  141 + .dataType(iotProductThinkModelDO.getProperty().getDataType())
143 142 .build();
144 143 deviceDataRedisDAO.set(deviceData);
145 144 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodel/IotProductThinkModelServiceImpl.java
... ... @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.util.ObjectUtil;
5 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 6 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
  7 +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
7 8 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.ThinkModelEvent;
8 9 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.ThinkModelProperty;
9 10 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.ThinkModelService;
... ... @@ -17,6 +18,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
17 18 import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodel.IotProductThinkModelDO;
18 19 import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodel.IotProductThinkModelMapper;
19 20 import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
  21 +import cn.iocoder.yudao.module.iot.enums.thinkmodel.IotProductThinkModelAccessModeEnum;
20 22 import cn.iocoder.yudao.module.iot.enums.thinkmodel.IotProductThinkModelTypeEnum;
21 23 import cn.iocoder.yudao.module.iot.service.product.IotProductService;
22 24 import cn.iocoder.yudao.module.iot.service.tdengine.IotSuperTableService;
... ... @@ -72,7 +74,7 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
72 74  
73 75 // 6. 如果创建的是属性,需要更新默认的事件和服务
74 76 if (Objects.equals(createReqVO.getType(), IotProductThinkModelTypeEnum.PROPERTY.getType())) {
75   - //createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
  77 + createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
76 78 }
77 79 return function.getId();
78 80 }
... ... @@ -112,7 +114,7 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
112 114 @Transactional(rollbackFor = Exception.class)
113 115 public void updateProductThinkModel(IotProductThinkModelSaveReqVO updateReqVO) {
114 116 // 1. 校验功能是否存在
115   - validateproductThinkModelMapperExists(updateReqVO.getId());
  117 + validateProductThinkModelMapperExists(updateReqVO.getId());
116 118  
117 119 // 2. 校验功能标识符是否唯一
118 120 validateIdentifierUniqueForUpdate(updateReqVO.getId(), updateReqVO.getProductId(), updateReqVO.getIdentifier());
... ... @@ -163,7 +165,7 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
163 165 *
164 166 * @param id 功能编号
165 167 */
166   - private void validateproductThinkModelMapperExists(Long id) {
  168 + private void validateProductThinkModelMapperExists(Long id) {
167 169 if (productThinkModelMapper.selectById(id) == null) {
168 170 throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS);
169 171 }
... ... @@ -201,7 +203,6 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
201 203 return productThinkModelMapper.selectListByProductKey(productKey);
202 204 }
203 205  
204   - // TODO @puhui999: 需要重构
205 206 /**
206 207 * 创建默认的事件和服务
207 208 */
... ... @@ -333,14 +334,12 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
333 334  
334 335 // 将属性列表转换为事件的输出参数
335 336 List<ThinkModelArgument> outputData = new ArrayList<>();
336   - // TODO @puhui999: 需要重构
337   - for (IotProductThinkModelDO functionDO : propertyList) {
338   - ThinkModelProperty property = functionDO.getProperty();
  337 + for (IotProductThinkModelDO thinkModel : propertyList) {
339 338 ThinkModelArgument arg = new ThinkModelArgument()
340   - .setIdentifier(property.getIdentifier())
341   - .setName(property.getName())
342   - //.setDataType(property.getDataType())
343   - .setDescription(property.getDescription())
  339 + .setIdentifier(thinkModel.getIdentifier())
  340 + .setName(thinkModel.getName())
  341 + .setProperty(thinkModel.getProperty())
  342 + .setDescription(thinkModel.getDescription())
344 343 .setDirection("output"); // 设置为输出参数
345 344 outputData.add(arg);
346 345 }
... ... @@ -357,18 +356,17 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
357 356 }
358 357  
359 358 List<ThinkModelArgument> inputData = new ArrayList<>();
360   - // TODO @puhui999: 需要重构
361   - for (IotProductThinkModelDO functionDO : propertyList) {
362   - ThinkModelProperty property = functionDO.getProperty();
363   - //if (IotProductThinkModelAccessModeEnum.WRITE.getMode().equals(property.getAccessMode()) || IotProductThinkModelAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
364   - // ThinkModelArgument arg = new ThinkModelArgument()
365   - // .setIdentifier(property.getIdentifier())
366   - // .setName(property.getName())
367   - // .setDataType(property.getDataType())
368   - // .setDescription(property.getDescription())
369   - // .setDirection("input"); // 设置为输入参数
370   - // inputData.add(arg);
371   - //}
  359 + for (IotProductThinkModelDO thinkModel : propertyList) {
  360 + ThinkModelProperty property = thinkModel.getProperty();
  361 + if (IotProductThinkModelAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
  362 + ThinkModelArgument arg = new ThinkModelArgument()
  363 + .setIdentifier(property.getIdentifier())
  364 + .setName(property.getName())
  365 + .setProperty(property)
  366 + .setDescription(property.getDescription())
  367 + .setDirection("input"); // 设置为输入参数
  368 + inputData.add(arg);
  369 + }
372 370 }
373 371 if (inputData.isEmpty()) {
374 372 // 如果没有可写属性,不生成属性设置服务
... ... @@ -394,20 +392,20 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
394 392 if (propertyList == null || propertyList.isEmpty()) {
395 393 return null;
396 394 }
397   - // TODO @puhui999: 需要重构
  395 +
398 396 List<ThinkModelArgument> outputData = new ArrayList<>();
399 397 for (IotProductThinkModelDO functionDO : propertyList) {
400 398 ThinkModelProperty property = functionDO.getProperty();
401   - //if (ObjectUtils.equalsAny(property.getAccessMode(),
402   - // IotProductThinkModelAccessModeEnum.READ.getMode(), IotProductThinkModelAccessModeEnum.READ_WRITE.getMode())) {
403   - // ThinkModelArgument arg = new ThinkModelArgument()
404   - // .setIdentifier(property.getIdentifier())
405   - // .setName(property.getName())
406   - // .setDataType(property.getDataType())
407   - // .setDescription(property.getDescription())
408   - // .setDirection("output"); // 设置为输出参数
409   - // outputData.add(arg);
410   - //}
  399 + if (ObjectUtils.equalsAny(property.getAccessMode(),
  400 + IotProductThinkModelAccessModeEnum.READ_ONLY.getMode(), IotProductThinkModelAccessModeEnum.READ_WRITE.getMode())) {
  401 + ThinkModelArgument arg = new ThinkModelArgument()
  402 + .setIdentifier(property.getIdentifier())
  403 + .setName(property.getName())
  404 + .setProperty(property)
  405 + .setDescription(property.getDescription())
  406 + .setDirection("output"); // 设置为输出参数
  407 + outputData.add(arg);
  408 + }
411 409 }
412 410 if (outputData.isEmpty()) {
413 411 // 如果没有可读属性,不生成属性获取服务
... ... @@ -428,15 +426,16 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
428 426 .setDescription("需要获取的属性标识符列表")
429 427 .setDirection("input"); // 设置为输入参数
430 428  
431   - // 创建数组类型,元素类型为文本类型(字符串)
  429 + // 创建数组类型,元素类型为文本类型(字符串)TODO @puhui999: 还得研究研究
432 430 ThinkModelArrayDataSpecs arrayType = new ThinkModelArrayDataSpecs();
433 431 arrayType.setDataType("array");
434   - //ThinkModelArraySpecs arraySpecs = new ThinkModelArraySpecs();
  432 + inputArg.setProperty(new ThinkModelProperty().setIdentifier(inputArg.getIdentifier()).setName(inputArg.getName())
  433 + .setDescription(inputArg.getDescription()).setDataSpecs(arrayType));
  434 +
435 435 ThinkModelDateOrTextDataSpecs textType = new ThinkModelDateOrTextDataSpecs();
436 436 textType.setDataType("text");
437   - //arraySpecs.setItem(textType);
438   - //arrayType.setSpecs(arraySpecs);
439   - inputArg.setDataType(arrayType);
  437 + inputArg.setProperty(new ThinkModelProperty().setIdentifier(inputArg.getIdentifier()).setName(inputArg.getName())
  438 + .setDescription(inputArg.getDescription()).setDataSpecs(textType));
440 439  
441 440 service.setInputData(Collections.singletonList(inputArg));
442 441 service.setOutputData(outputData);
... ...