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

This commit is contained in:
puhui999 2024-12-18 18:07:49 +08:00
parent 10da1e095b
commit 0352dda469
6 changed files with 72 additions and 75 deletions

View File

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

View File

@ -32,7 +32,7 @@ public class IotDeviceDataDO {
* <p> * <p>
* 关联 {@link IotProductThinkModelDO#getId()} * 关联 {@link IotProductThinkModelDO#getId()}
*/ */
private Long thinkModelFunctionId; private Long thinkModelId;
/** /**
* 产品标识 * 产品标识

View File

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

View File

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

View File

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

View File

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