【功能优化】IOT 物模型接口

This commit is contained in:
安浩浩 2024-09-18 08:22:33 +08:00
parent 9658ed1a0d
commit d8d37d1bb9
2 changed files with 87 additions and 72 deletions

View File

@ -5,39 +5,6 @@ tenant-id: {{adminTenentId}}
Authorization: Bearer {{token}} Authorization: Bearer {{token}}
{ {
"productId": 1001,
"productKey": "smart-sensor-001",
"identifier": "Temperature",
"name": "温度",
"description": "当前温度值",
"type": 1,
"property": {
"identifier": "Temperature",
"name": "温度",
"accessMode": "r",
"required": true,
"dataType": {
"type": "float",
"specs": {
"min": -40.0,
"max": 125.0,
"step": 0.1,
"unit": "℃"
}
},
"description": "当前温度值"
}
}
### 请求 /iot/think-model-function/update 接口 => 成功
PUT {{baseUrl}}/iot/think-model-function/update
Content-Type: application/json
tenant-id: {{adminTenentId}}
Authorization: Bearer {{token}}
{
"id": 1,
"productId": 1001, "productId": 1001,
"productKey": "smart-sensor-001", "productKey": "smart-sensor-001",
"identifier": "Temperature", "identifier": "Temperature",
@ -62,13 +29,79 @@ Authorization: Bearer {{token}}
} }
} }
### 请求 /iot/think-model-function/create 接口 => 成功
POST {{baseUrl}}/iot/think-model-function/create
Content-Type: application/json
tenant-id: {{adminTenentId}}
Authorization: Bearer {{token}}
{
"productId": 1001,
"productKey": "smart-sensor-001",
"identifier": "Humidity",
"name": "湿度",
"description": "当前湿度值",
"type": 1,
"property": {
"identifier": "Humidity",
"name": "湿度",
"accessMode": "r",
"required": true,
"dataType": {
"type": "float",
"specs": {
"min": 0.0,
"max": 100.0,
"step": 0.1,
"unit": "%"
}
},
"description": "当前湿度值"
}
}
### 请求 /iot/think-model-function/update 接口 => 成功
PUT {{baseUrl}}/iot/think-model-function/update
Content-Type: application/json
tenant-id: {{adminTenentId}}
Authorization: Bearer {{token}}
{
"id": 11,
"productId": 1001,
"productKey": "smart-sensor-001",
"identifier": "Temperature",
"name": "温度",
"description": "当前温度值",
"type": 1,
"property": {
"identifier": "Temperature",
"name": "温度",
"accessMode": "r",
"required": true,
"dataType": {
"type": "float",
"specs": {
"min": -111.0,
"max": 222.0,
"step": 0.1,
"unit": "℃"
}
},
"description": "当前温度值"
}
}
### 请求 /iot/think-model-function/delete 接口 => 成功 ### 请求 /iot/think-model-function/delete 接口 => 成功
DELETE {{baseUrl}}/iot/think-model-function/delete?id=1 DELETE {{baseUrl}}/iot/think-model-function/delete?id=7
tenant-id: {{adminTenentId}} tenant-id: {{adminTenentId}}
Authorization: Bearer {{token}} Authorization: Bearer {{token}}
### 请求 /iot/think-model-function/get 接口 => 成功 ### 请求 /iot/think-model-function/get 接口 => 成功
GET {{baseUrl}}/iot/think-model-function/get?id=4 GET {{baseUrl}}/iot/think-model-function/get?id=10
tenant-id: {{adminTenentId}} tenant-id: {{adminTenentId}}
Authorization: Bearer {{token}} Authorization: Bearer {{token}}

View File

@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.iot.service.thinkmodelfunction;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
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.thinkmodelfunction.thingModel.ThingModelEvent; import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelEvent;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelProperty; import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelProperty;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelService; import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelService;
@ -22,7 +24,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
@ -54,12 +55,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 3. 如果创建的是属性需要更新默认的事件和服务 // 3. 如果创建的是属性需要更新默认的事件和服务
if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
// 获取当前属性列表并添加新插入的属性 createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
// TODO @haohao是不是插入后查询已经包含了 function
List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
.selectListByProductIdAndType(createReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
propertyList.add(function);
createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey(), propertyList);
} }
return function.getId(); return function.getId();
} }
@ -76,6 +72,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
public void updateThinkModelFunction(IotThinkModelFunctionSaveReqVO updateReqVO) { public void updateThinkModelFunction(IotThinkModelFunctionSaveReqVO updateReqVO) {
// 1. 校验功能是否存在 // 1. 校验功能是否存在
validateThinkModelFunctionExists(updateReqVO.getId()); validateThinkModelFunctionExists(updateReqVO.getId());
// 2. 校验功能标识符是否唯一 // 2. 校验功能标识符是否唯一
validateIdentifierUniqueForUpdate(updateReqVO.getId(), updateReqVO.getProductId(), updateReqVO.getIdentifier()); validateIdentifierUniqueForUpdate(updateReqVO.getId(), updateReqVO.getProductId(), updateReqVO.getIdentifier());
@ -85,17 +82,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 4. 如果更新的是属性需要更新默认的事件和服务 // 4. 如果更新的是属性需要更新默认的事件和服务
if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
// 获取当前属性列表更新其中的属性 createDefaultEventsAndServices(updateReqVO.getProductId(), updateReqVO.getProductKey());
// TODO @haohao是不是更新后查询出来的已经是最新的 thinkModelFunction
List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
.selectListByProductIdAndType(updateReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
for (int i = 0; i < propertyList.size(); i++) {
if (propertyList.get(i).getId().equals(thinkModelFunction.getId())) {
propertyList.set(i, thinkModelFunction);
break;
}
}
createDefaultEventsAndServices(updateReqVO.getProductId(), updateReqVO.getProductKey(), propertyList);
} }
} }
@ -120,12 +107,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 3. 如果删除的是属性需要更新默认的事件和服务 // 3. 如果删除的是属性需要更新默认的事件和服务
if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) { if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
// 获取当前属性列表移除已删除的属性 createDefaultEventsAndServices(functionDO.getProductId(), functionDO.getProductKey());
// TODO @haohao是不是删除后已经没有 id 对应的记录啦
List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
.selectListByProductIdAndType(functionDO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
propertyList.removeIf(property -> property.getId().equals(id));
createDefaultEventsAndServices(functionDO.getProductId(), functionDO.getProductKey(), propertyList);
} }
} }
@ -153,8 +135,12 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
/** /**
* 创建默认的事件和服务 * 创建默认的事件和服务
*/ */
public void createDefaultEventsAndServices(Long productId, String productKey, List<IotThinkModelFunctionDO> propertyList) { public void createDefaultEventsAndServices(Long productId, String productKey) {
// 1. 生成新的事件和服务列表 // 1. 获取当前属性列表
List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
.selectListByProductIdAndType(productId, IotThingModelTypeEnum.PROPERTY.getType());
// 2. 生成新的事件和服务列表
List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>(); List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>();
// 生成属性上报事件 // 生成属性上报事件
ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList); ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList);
@ -175,7 +161,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
newFunctionList.add(getServiceFunction); newFunctionList.add(getServiceFunction);
} }
// 2. 获取数据库中的默认的旧事件和服务列表 // 3. 获取数据库中的默认的旧事件和服务列表
List<IotThinkModelFunctionDO> oldFunctionList = thinkModelFunctionMapper.selectListByProductIdAndIdentifiersAndTypes( List<IotThinkModelFunctionDO> oldFunctionList = thinkModelFunctionMapper.selectListByProductIdAndIdentifiersAndTypes(
productId, productId,
Arrays.asList("post", "set", "get"), Arrays.asList("post", "set", "get"),
@ -185,6 +171,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 3.1 使用 diffList 方法比较新旧列表 // 3.1 使用 diffList 方法比较新旧列表
List<List<IotThinkModelFunctionDO>> diffResult = diffList(oldFunctionList, newFunctionList, List<List<IotThinkModelFunctionDO>> diffResult = diffList(oldFunctionList, newFunctionList,
// TODO @haohao是不是用 id 比较相同就 ok 如果可以的化下面的 update 可以更简单 // TODO @haohao是不是用 id 比较相同就 ok 如果可以的化下面的 update 可以更简单
// 继续使用 identifier type 进行比较这样可以准确地匹配对应的功能对象
(oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier()) (oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier())
&& Objects.equals(oldFunc.getType(), newFunc.getType())); && Objects.equals(oldFunc.getType(), newFunc.getType()));
List<IotThinkModelFunctionDO> createList = diffResult.get(0); // 需要新增的 List<IotThinkModelFunctionDO> createList = diffResult.get(0); // 需要新增的
@ -206,8 +193,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
} }
} }
if (CollUtil.isNotEmpty(deleteList)) { if (CollUtil.isNotEmpty(deleteList)) {
// TODO @haohao使用 convertSet 简化 Set<Long> idsToDelete = CollectionUtils.convertSet(deleteList, IotThinkModelFunctionDO::getId);
List<Long> idsToDelete = deleteList.stream().map(IotThinkModelFunctionDO::getId).collect(Collectors.toList());
thinkModelFunctionMapper.deleteByIds(idsToDelete); thinkModelFunctionMapper.deleteByIds(idsToDelete);
} }
} }
@ -217,11 +203,8 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
*/ */
private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List<IotThinkModelFunctionDO> functionList, private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List<IotThinkModelFunctionDO> functionList,
String identifier, Integer type) { String identifier, Integer type) {
// TODO @haohao这个可以使用 CollUtil.findOne 简化只有一行 return CollUtil.findOne(functionList, func ->
return functionList.stream() Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type));
.filter(func -> Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type))
.findFirst()
.orElse(null);
} }
/** /**
@ -332,9 +315,8 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
List<ThingModelArgument> outputData = new ArrayList<>(); List<ThingModelArgument> outputData = new ArrayList<>();
for (IotThinkModelFunctionDO functionDO : propertyList) { for (IotThinkModelFunctionDO functionDO : propertyList) {
ThingModelProperty property = functionDO.getProperty(); ThingModelProperty property = functionDO.getProperty();
// TODO @haohaoObjectUtils.equalsAny()进一步简化判断 if (ObjectUtils.equalsAny(property.getAccessMode(),
if (IotAccessModeEnum.READ.getMode().equals(property.getAccessMode()) IotAccessModeEnum.READ.getMode(), IotAccessModeEnum.READ_WRITE.getMode())) {
|| IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
ThingModelArgument arg = new ThingModelArgument() ThingModelArgument arg = new ThingModelArgument()
.setIdentifier(property.getIdentifier()) .setIdentifier(property.getIdentifier())
.setName(property.getName()) .setName(property.getName())