浏览代码

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

puhui999 8 月之前
父节点
当前提交
0352dda469

+ 5 - 1
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodel/model/dataType/ThinkModelArgument.java

@@ -1,5 +1,6 @@
 package cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.dataType;
 
+import cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.ThinkModelProperty;
 import lombok.Data;
 
 @Data
@@ -7,7 +8,10 @@ public class ThinkModelArgument {
 
     private String identifier;
     private String name;
-    private ThinkModelDataSpecs dataType;
+    /**
+     * 物模型中的属性
+     */
+    private ThinkModelProperty property;
     /**
      * 用于区分输入或输出参数,"input" 或 "output"
      */

+ 1 - 1
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 {
      * <p>
      * 关联 {@link IotProductThinkModelDO#getId()}
      */
-    private Long thinkModelFunctionId;
+    private Long thinkModelId;
 
     /**
      * 产品标识

+ 10 - 13
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 {
      */
     public static TdFieldDO parse(ThinkModelProperty property) {
         String fieldName = property.getIdentifier().toLowerCase();
-        //// TODO @puhui999: 需要重构
-        //ThinkModelDataSpecs type = property.getDataType();
-        //
-        //// 将物模型字段类型映射为td字段类型
-        //String fType = TYPE_MAPPING.get(type.getDataType().toUpperCase());
-        //
-        //// 如果字段类型为NCHAR,默认长度为64
-        //int dataLength = 0;
-        //if ("NCHAR".equals(fType)) {
-        //    dataLength = 64;
-        //}
-        //return new TdFieldDO(fieldName, fType, dataLength);
-        return null;
+
+        // 将物模型字段类型映射为td字段类型
+        String fType = TYPE_MAPPING.get(property.getDataType().toUpperCase());
+
+        // 如果字段类型为NCHAR,默认长度为64
+        int dataLength = 0;
+        if ("NCHAR".equals(fType)) {
+            dataLength = 64;
+        }
+        return new TdFieldDO(fieldName, fType, dataLength);
     }
 
     /**

+ 16 - 18
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;
 import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataPageReqVO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
-import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodel.IotProductThinkModelDO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThinkModelMessage;
+import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodel.IotProductThinkModelDO;
 import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO;
 import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
 import cn.iocoder.yudao.module.iot.enums.IotConstants;
@@ -28,6 +28,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
+
 @Slf4j
 @Service
 public class IotDeviceDataServiceImpl implements IotDeviceDataService {
@@ -73,35 +75,31 @@ public class IotDeviceDataServiceImpl implements IotDeviceDataService {
         // 1. 获取设备信息
         IotDeviceDO device = deviceService.getDevice(deviceDataReqVO.getDeviceId());
         // 2. 获取设备属性最新数据
-        List<IotProductThinkModelDO> thinkModelFunctionList = thinkModelFunctionService.getProductThinkModelListByProductKey(device.getProductKey());
-        thinkModelFunctionList = thinkModelFunctionList.stream()
-                .filter(function -> IotProductThinkModelTypeEnum.PROPERTY.getType()
-                        .equals(function.getType())).toList();
+        List<IotProductThinkModelDO> thinkModelList = thinkModelFunctionService.getProductThinkModelListByProductKey(device.getProductKey());
+        thinkModelList = filterList(thinkModelList, thinkModel -> IotProductThinkModelTypeEnum.PROPERTY.getType()
+                .equals(thinkModel.getType()));
 
         // 3. 过滤标识符和属性名称
         if (deviceDataReqVO.getIdentifier() != null) {
-            thinkModelFunctionList = thinkModelFunctionList.stream()
-                    .filter(function -> function.getIdentifier().toLowerCase().contains(deviceDataReqVO.getIdentifier().toLowerCase()))
-                    .toList();
+            thinkModelList = filterList(thinkModelList, thinkModel -> thinkModel.getIdentifier()
+                    .toLowerCase().contains(deviceDataReqVO.getIdentifier().toLowerCase()));
         }
         if (deviceDataReqVO.getName() != null) {
-            thinkModelFunctionList = thinkModelFunctionList.stream()
-                    .filter(function -> function.getName().toLowerCase().contains(deviceDataReqVO.getName().toLowerCase()))
-                    .toList();
+            thinkModelList = filterList(thinkModelList, thinkModel -> thinkModel.getName()
+                    .toLowerCase().contains(deviceDataReqVO.getName().toLowerCase()));
         }
         // 4. 获取设备属性最新数据
-        // TODO @puhui999: 需要重构
-        thinkModelFunctionList.forEach(function -> {
-            IotDeviceDataDO deviceData = deviceDataRedisDAO.get(device.getProductKey(), device.getDeviceName(), function.getIdentifier());
+        thinkModelList.forEach(thinkModel -> {
+            IotDeviceDataDO deviceData = deviceDataRedisDAO.get(device.getProductKey(), device.getDeviceName(), thinkModel.getIdentifier());
             if (deviceData == null) {
                 deviceData = new IotDeviceDataDO();
                 deviceData.setProductKey(device.getProductKey());
                 deviceData.setDeviceName(device.getDeviceName());
-                deviceData.setIdentifier(function.getIdentifier());
+                deviceData.setIdentifier(thinkModel.getIdentifier());
                 deviceData.setDeviceId(deviceDataReqVO.getDeviceId());
-                deviceData.setThinkModelFunctionId(function.getId());
-                deviceData.setName(function.getName());
-                //deviceData.setDataType(function.getProperty().getDataType().getDataType());
+                deviceData.setThinkModelId(thinkModel.getId());
+                deviceData.setName(thinkModel.getName());
+                deviceData.setDataType(thinkModel.getProperty().getDataType());
             }
             list.add(deviceData);
         });

+ 2 - 3
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
      * @param time                    时间
      */
     private void setDeviceDataCache(IotDeviceDO device, IotProductThinkModelDO iotProductThinkModelDO, Object val, Long time) {
-        // TODO @puhui999: 需要重构
         IotDeviceDataDO deviceData = IotDeviceDataDO.builder()
                 .productKey(device.getProductKey())
                 .deviceName(device.getDeviceName())
@@ -137,9 +136,9 @@ public class IotThinkModelMessageServiceImpl implements IotThinkModelMessageServ
                 .value(val != null ? val.toString() : null)
                 .updateTime(DateUtil.toLocalDateTime(new Date(time)))
                 .deviceId(device.getId())
-                .thinkModelFunctionId(iotProductThinkModelDO.getId())
+                .thinkModelId(iotProductThinkModelDO.getId())
                 .name(iotProductThinkModelDO.getName())
-                //.dataType(iotProductThinkModelDO.getProperty().getDataType().getDataType())
+                .dataType(iotProductThinkModelDO.getProperty().getDataType())
                 .build();
         deviceDataRedisDAO.set(deviceData);
     }

+ 38 - 39
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;
 import cn.hutool.core.util.ObjectUtil;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
+import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.ThinkModelEvent;
 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodel.model.ThinkModelProperty;
 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;
 import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodel.IotProductThinkModelDO;
 import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodel.IotProductThinkModelMapper;
 import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
+import cn.iocoder.yudao.module.iot.enums.thinkmodel.IotProductThinkModelAccessModeEnum;
 import cn.iocoder.yudao.module.iot.enums.thinkmodel.IotProductThinkModelTypeEnum;
 import cn.iocoder.yudao.module.iot.service.product.IotProductService;
 import cn.iocoder.yudao.module.iot.service.tdengine.IotSuperTableService;
@@ -72,7 +74,7 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
 
         // 6. 如果创建的是属性,需要更新默认的事件和服务
         if (Objects.equals(createReqVO.getType(), IotProductThinkModelTypeEnum.PROPERTY.getType())) {
-            //createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
+            createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
         }
         return function.getId();
     }
@@ -112,7 +114,7 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
     @Transactional(rollbackFor = Exception.class)
     public void updateProductThinkModel(IotProductThinkModelSaveReqVO updateReqVO) {
         // 1. 校验功能是否存在
-        validateproductThinkModelMapperExists(updateReqVO.getId());
+        validateProductThinkModelMapperExists(updateReqVO.getId());
 
         // 2. 校验功能标识符是否唯一
         validateIdentifierUniqueForUpdate(updateReqVO.getId(), updateReqVO.getProductId(), updateReqVO.getIdentifier());
@@ -163,7 +165,7 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
      *
      * @param id 功能编号
      */
-    private void validateproductThinkModelMapperExists(Long id) {
+    private void validateProductThinkModelMapperExists(Long id) {
         if (productThinkModelMapper.selectById(id) == null) {
             throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS);
         }
@@ -201,7 +203,6 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
         return productThinkModelMapper.selectListByProductKey(productKey);
     }
 
-    // TODO @puhui999: 需要重构
     /**
      * 创建默认的事件和服务
      */
@@ -333,14 +334,12 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
 
         // 将属性列表转换为事件的输出参数
         List<ThinkModelArgument> outputData = new ArrayList<>();
-        // TODO @puhui999: 需要重构
-        for (IotProductThinkModelDO functionDO : propertyList) {
-            ThinkModelProperty property = functionDO.getProperty();
+        for (IotProductThinkModelDO thinkModel : propertyList) {
             ThinkModelArgument arg = new ThinkModelArgument()
-                    .setIdentifier(property.getIdentifier())
-                    .setName(property.getName())
-                    //.setDataType(property.getDataType())
-                    .setDescription(property.getDescription())
+                    .setIdentifier(thinkModel.getIdentifier())
+                    .setName(thinkModel.getName())
+                    .setProperty(thinkModel.getProperty())
+                    .setDescription(thinkModel.getDescription())
                     .setDirection("output"); // 设置为输出参数
             outputData.add(arg);
         }
@@ -357,18 +356,17 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
         }
 
         List<ThinkModelArgument> inputData = new ArrayList<>();
-        // TODO @puhui999: 需要重构
-        for (IotProductThinkModelDO functionDO : propertyList) {
-            ThinkModelProperty property = functionDO.getProperty();
-            //if (IotProductThinkModelAccessModeEnum.WRITE.getMode().equals(property.getAccessMode()) || IotProductThinkModelAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
-            //    ThinkModelArgument arg = new ThinkModelArgument()
-            //            .setIdentifier(property.getIdentifier())
-            //            .setName(property.getName())
-            //            .setDataType(property.getDataType())
-            //            .setDescription(property.getDescription())
-            //            .setDirection("input"); // 设置为输入参数
-            //    inputData.add(arg);
-            //}
+        for (IotProductThinkModelDO thinkModel : propertyList) {
+            ThinkModelProperty property = thinkModel.getProperty();
+            if (IotProductThinkModelAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
+                ThinkModelArgument arg = new ThinkModelArgument()
+                        .setIdentifier(property.getIdentifier())
+                        .setName(property.getName())
+                        .setProperty(property)
+                        .setDescription(property.getDescription())
+                        .setDirection("input"); // 设置为输入参数
+                inputData.add(arg);
+            }
         }
         if (inputData.isEmpty()) {
             // 如果没有可写属性,不生成属性设置服务
@@ -394,20 +392,20 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
         if (propertyList == null || propertyList.isEmpty()) {
             return null;
         }
-        // TODO @puhui999: 需要重构
+
         List<ThinkModelArgument> outputData = new ArrayList<>();
         for (IotProductThinkModelDO functionDO : propertyList) {
             ThinkModelProperty property = functionDO.getProperty();
-            //if (ObjectUtils.equalsAny(property.getAccessMode(),
-            //        IotProductThinkModelAccessModeEnum.READ.getMode(), IotProductThinkModelAccessModeEnum.READ_WRITE.getMode())) {
-            //    ThinkModelArgument arg = new ThinkModelArgument()
-            //            .setIdentifier(property.getIdentifier())
-            //            .setName(property.getName())
-            //            .setDataType(property.getDataType())
-            //            .setDescription(property.getDescription())
-            //            .setDirection("output"); // 设置为输出参数
-            //    outputData.add(arg);
-            //}
+            if (ObjectUtils.equalsAny(property.getAccessMode(),
+                    IotProductThinkModelAccessModeEnum.READ_ONLY.getMode(), IotProductThinkModelAccessModeEnum.READ_WRITE.getMode())) {
+                ThinkModelArgument arg = new ThinkModelArgument()
+                        .setIdentifier(property.getIdentifier())
+                        .setName(property.getName())
+                        .setProperty(property)
+                        .setDescription(property.getDescription())
+                        .setDirection("output"); // 设置为输出参数
+                outputData.add(arg);
+            }
         }
         if (outputData.isEmpty()) {
             // 如果没有可读属性,不生成属性获取服务
@@ -428,15 +426,16 @@ public class IotProductThinkModelServiceImpl implements IotProductThinkModelServ
                 .setDescription("需要获取的属性标识符列表")
                 .setDirection("input"); // 设置为输入参数
 
-        // 创建数组类型,元素类型为文本类型(字符串)
+        // 创建数组类型,元素类型为文本类型(字符串)TODO @puhui999: 还得研究研究
         ThinkModelArrayDataSpecs arrayType = new ThinkModelArrayDataSpecs();
         arrayType.setDataType("array");
-        //ThinkModelArraySpecs arraySpecs = new ThinkModelArraySpecs();
+        inputArg.setProperty(new ThinkModelProperty().setIdentifier(inputArg.getIdentifier()).setName(inputArg.getName())
+                .setDescription(inputArg.getDescription()).setDataSpecs(arrayType));
+
         ThinkModelDateOrTextDataSpecs textType = new ThinkModelDateOrTextDataSpecs();
         textType.setDataType("text");
-        //arraySpecs.setItem(textType);
-        //arrayType.setSpecs(arraySpecs);
-        inputArg.setDataType(arrayType);
+        inputArg.setProperty(new ThinkModelProperty().setIdentifier(inputArg.getIdentifier()).setName(inputArg.getName())
+                .setDescription(inputArg.getDescription()).setDataSpecs(textType));
 
         service.setInputData(Collections.singletonList(inputArg));
         service.setOutputData(outputData);