Commit d8d37d1bb94b5d141fdfc91a5a6dc8d293807d55

Authored by 安浩浩
1 parent 9658ed1a

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

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thinkmodelfunction/IotThinkModelFunctionController.http
... ... @@ -19,8 +19,8 @@ Authorization: Bearer {{token}}
19 19 "dataType": {
20 20 "type": "float",
21 21 "specs": {
22   - "min": -40.0,
23   - "max": 125.0,
  22 + "min": -10.0,
  23 + "max": 100.0,
24 24 "step": 0.1,
25 25 "unit": "℃"
26 26 }
... ... @@ -29,6 +29,39 @@ Authorization: Bearer {{token}}
29 29 }
30 30 }
31 31  
  32 +### 请求 /iot/think-model-function/create 接口 => 成功
  33 +POST {{baseUrl}}/iot/think-model-function/create
  34 +Content-Type: application/json
  35 +tenant-id: {{adminTenentId}}
  36 +Authorization: Bearer {{token}}
  37 +
  38 +{
  39 + "productId": 1001,
  40 + "productKey": "smart-sensor-001",
  41 + "identifier": "Humidity",
  42 + "name": "湿度",
  43 + "description": "当前湿度值",
  44 + "type": 1,
  45 + "property": {
  46 + "identifier": "Humidity",
  47 + "name": "湿度",
  48 + "accessMode": "r",
  49 + "required": true,
  50 + "dataType": {
  51 + "type": "float",
  52 + "specs": {
  53 + "min": 0.0,
  54 + "max": 100.0,
  55 + "step": 0.1,
  56 + "unit": "%"
  57 + }
  58 + },
  59 + "description": "当前湿度值"
  60 + }
  61 +}
  62 +
  63 +
  64 +
32 65  
33 66 ### 请求 /iot/think-model-function/update 接口 => 成功
34 67 PUT {{baseUrl}}/iot/think-model-function/update
... ... @@ -37,7 +70,7 @@ tenant-id: {{adminTenentId}}
37 70 Authorization: Bearer {{token}}
38 71  
39 72 {
40   - "id": 1,
  73 + "id": 11,
41 74 "productId": 1001,
42 75 "productKey": "smart-sensor-001",
43 76 "identifier": "Temperature",
... ... @@ -52,8 +85,8 @@ Authorization: Bearer {{token}}
52 85 "dataType": {
53 86 "type": "float",
54 87 "specs": {
55   - "min": -10.0,
56   - "max": 100.0,
  88 + "min": -111.0,
  89 + "max": 222.0,
57 90 "step": 0.1,
58 91 "unit": "℃"
59 92 }
... ... @@ -63,12 +96,12 @@ Authorization: Bearer {{token}}
63 96 }
64 97  
65 98 ### 请求 /iot/think-model-function/delete 接口 => 成功
66   -DELETE {{baseUrl}}/iot/think-model-function/delete?id=1
  99 +DELETE {{baseUrl}}/iot/think-model-function/delete?id=7
67 100 tenant-id: {{adminTenentId}}
68 101 Authorization: Bearer {{token}}
69 102  
70 103 ### 请求 /iot/think-model-function/get 接口 => 成功
71   -GET {{baseUrl}}/iot/think-model-function/get?id=4
  104 +GET {{baseUrl}}/iot/think-model-function/get?id=10
72 105 tenant-id: {{adminTenentId}}
73 106 Authorization: Bearer {{token}}
74 107  
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thinkmodelfunction/IotThinkModelFunctionServiceImpl.java
... ... @@ -2,6 +2,8 @@ package cn.iocoder.yudao.module.iot.service.thinkmodelfunction;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.util.ObjectUtil;
  5 +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
  6 +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
5 7 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelEvent;
6 8 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelProperty;
7 9 import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.ThingModelService;
... ... @@ -22,7 +24,6 @@ import org.springframework.transaction.annotation.Transactional;
22 24 import org.springframework.validation.annotation.Validated;
23 25  
24 26 import java.util.*;
25   -import java.util.stream.Collectors;
26 27  
27 28 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
28 29 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
... ... @@ -54,12 +55,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
54 55  
55 56 // 3. 如果创建的是属性,需要更新默认的事件和服务
56 57 if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
57   - // 获取当前属性列表,并添加新插入的属性
58   - // TODO @haohao:是不是插入后,查询已经包含了 function
59   - List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
60   - .selectListByProductIdAndType(createReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
61   - propertyList.add(function);
62   - createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey(), propertyList);
  58 + createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
63 59 }
64 60 return function.getId();
65 61 }
... ... @@ -76,6 +72,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
76 72 public void updateThinkModelFunction(IotThinkModelFunctionSaveReqVO updateReqVO) {
77 73 // 1. 校验功能是否存在
78 74 validateThinkModelFunctionExists(updateReqVO.getId());
  75 +
79 76 // 2. 校验功能标识符是否唯一
80 77 validateIdentifierUniqueForUpdate(updateReqVO.getId(), updateReqVO.getProductId(), updateReqVO.getIdentifier());
81 78  
... ... @@ -85,17 +82,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
85 82  
86 83 // 4. 如果更新的是属性,需要更新默认的事件和服务
87 84 if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
88   - // 获取当前属性列表,更新其中的属性
89   - // TODO @haohao:是不是更新后,查询出来的,已经是最新的 thinkModelFunction
90   - List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
91   - .selectListByProductIdAndType(updateReqVO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
92   - for (int i = 0; i < propertyList.size(); i++) {
93   - if (propertyList.get(i).getId().equals(thinkModelFunction.getId())) {
94   - propertyList.set(i, thinkModelFunction);
95   - break;
96   - }
97   - }
98   - createDefaultEventsAndServices(updateReqVO.getProductId(), updateReqVO.getProductKey(), propertyList);
  85 + createDefaultEventsAndServices(updateReqVO.getProductId(), updateReqVO.getProductKey());
99 86 }
100 87 }
101 88  
... ... @@ -120,12 +107,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
120 107  
121 108 // 3. 如果删除的是属性,需要更新默认的事件和服务
122 109 if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
123   - // 获取当前属性列表,移除已删除的属性
124   - // TODO @haohao:是不是删除后,已经没有 id 对应的记录啦?
125   - List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
126   - .selectListByProductIdAndType(functionDO.getProductId(), IotThingModelTypeEnum.PROPERTY.getType());
127   - propertyList.removeIf(property -> property.getId().equals(id));
128   - createDefaultEventsAndServices(functionDO.getProductId(), functionDO.getProductKey(), propertyList);
  110 + createDefaultEventsAndServices(functionDO.getProductId(), functionDO.getProductKey());
129 111 }
130 112 }
131 113  
... ... @@ -153,8 +135,12 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
153 135 /**
154 136 * 创建默认的事件和服务
155 137 */
156   - public void createDefaultEventsAndServices(Long productId, String productKey, List<IotThinkModelFunctionDO> propertyList) {
157   - // 1. 生成新的事件和服务列表
  138 + public void createDefaultEventsAndServices(Long productId, String productKey) {
  139 + // 1. 获取当前属性列表
  140 + List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
  141 + .selectListByProductIdAndType(productId, IotThingModelTypeEnum.PROPERTY.getType());
  142 +
  143 + // 2. 生成新的事件和服务列表
158 144 List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>();
159 145 // 生成属性上报事件
160 146 ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList);
... ... @@ -175,7 +161,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
175 161 newFunctionList.add(getServiceFunction);
176 162 }
177 163  
178   - // 2. 获取数据库中的默认的旧事件和服务列表
  164 + // 3. 获取数据库中的默认的旧事件和服务列表
179 165 List<IotThinkModelFunctionDO> oldFunctionList = thinkModelFunctionMapper.selectListByProductIdAndIdentifiersAndTypes(
180 166 productId,
181 167 Arrays.asList("post", "set", "get"),
... ... @@ -185,6 +171,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
185 171 // 3.1 使用 diffList 方法比较新旧列表
186 172 List<List<IotThinkModelFunctionDO>> diffResult = diffList(oldFunctionList, newFunctionList,
187 173 // TODO @haohao:是不是用 id 比较相同就 ok 哈。如果可以的化,下面的 update 可以更简单
  174 + // 继续使用 identifier 和 type 进行比较:这样可以准确地匹配对应的功能对象。
188 175 (oldFunc, newFunc) -> Objects.equals(oldFunc.getIdentifier(), newFunc.getIdentifier())
189 176 && Objects.equals(oldFunc.getType(), newFunc.getType()));
190 177 List<IotThinkModelFunctionDO> createList = diffResult.get(0); // 需要新增的
... ... @@ -206,8 +193,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
206 193 }
207 194 }
208 195 if (CollUtil.isNotEmpty(deleteList)) {
209   - // TODO @haohao:使用 convertSet 简化。
210   - List<Long> idsToDelete = deleteList.stream().map(IotThinkModelFunctionDO::getId).collect(Collectors.toList());
  196 + Set<Long> idsToDelete = CollectionUtils.convertSet(deleteList, IotThinkModelFunctionDO::getId);
211 197 thinkModelFunctionMapper.deleteByIds(idsToDelete);
212 198 }
213 199 }
... ... @@ -217,11 +203,8 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
217 203 */
218 204 private IotThinkModelFunctionDO findFunctionByIdentifierAndType(List<IotThinkModelFunctionDO> functionList,
219 205 String identifier, Integer type) {
220   - // TODO @haohao:这个可以使用 CollUtil.findOne 简化只有一行
221   - return functionList.stream()
222   - .filter(func -> Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type))
223   - .findFirst()
224   - .orElse(null);
  206 + return CollUtil.findOne(functionList, func ->
  207 + Objects.equals(func.getIdentifier(), identifier) && Objects.equals(func.getType(), type));
225 208 }
226 209  
227 210 /**
... ... @@ -332,9 +315,8 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
332 315 List<ThingModelArgument> outputData = new ArrayList<>();
333 316 for (IotThinkModelFunctionDO functionDO : propertyList) {
334 317 ThingModelProperty property = functionDO.getProperty();
335   - // TODO @haohao:ObjectUtils.equalsAny(),进一步简化判断
336   - if (IotAccessModeEnum.READ.getMode().equals(property.getAccessMode())
337   - || IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
  318 + if (ObjectUtils.equalsAny(property.getAccessMode(),
  319 + IotAccessModeEnum.READ.getMode(), IotAccessModeEnum.READ_WRITE.getMode())) {
338 320 ThingModelArgument arg = new ThingModelArgument()
339 321 .setIdentifier(property.getIdentifier())
340 322 .setName(property.getName())
... ...