Commit b4288bc39397f9de68d3f3ada18bc7fe5435ab5e

Authored by YunaiV
1 parent 4075fde7

【代码评审】IoT:ThingModel 维护

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/ThingModelEvent.java
@@ -22,16 +22,13 @@ public class ThingModelEvent { @@ -22,16 +22,13 @@ public class ThingModelEvent {
22 */ 22 */
23 private String name; 23 private String name;
24 /** 24 /**
25 - * 是否是标准品类的必选事件。  
26 - *  
27 - * - true:是  
28 - * - false:否 25 + * 是否是标准品类的必选事件
29 */ 26 */
30 private Boolean required; 27 private Boolean required;
31 /** 28 /**
32 * 事件类型 29 * 事件类型
33 * 30 *
34 - * 关联枚举 {@link IotProductThingModelServiceEventTypeEnum} 31 + * 枚举 {@link IotProductThingModelServiceEventTypeEnum}
35 */ 32 */
36 private String type; 33 private String type;
37 /** 34 /**
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/ThingModelService.java
@@ -22,16 +22,13 @@ public class ThingModelService { @@ -22,16 +22,13 @@ public class ThingModelService {
22 */ 22 */
23 private String name; 23 private String name;
24 /** 24 /**
25 - * 是否是标准品类的必选服务。  
26 - *  
27 - * - true:是  
28 - * - false:否 25 + * 是否是标准品类的必选服务
29 */ 26 */
30 private Boolean required; 27 private Boolean required;
31 /** 28 /**
32 * 调用类型 29 * 调用类型
33 * 30 *
34 - * 关联枚举 {@link IotProductThingModelServiceCallTypeEnum} 31 + * 枚举 {@link IotProductThingModelServiceCallTypeEnum}
35 */ 32 */
36 private String callType; 33 private String callType;
37 /** 34 /**
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thingmodel/IotProductThingModelServiceImpl.java
@@ -185,43 +185,44 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ @@ -185,43 +185,44 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
185 */ 185 */
186 public void createDefaultEventsAndServices(Long productId, String productKey) { 186 public void createDefaultEventsAndServices(Long productId, String productKey) {
187 // 1. 获取当前属性列表 187 // 1. 获取当前属性列表
188 - List<IotProductThingModelDO> propertyList = productThingModelMapper 188 + List<IotProductThingModelDO> properties = productThingModelMapper
189 .selectListByProductIdAndType(productId, IotProductThingModelTypeEnum.PROPERTY.getType()); 189 .selectListByProductIdAndType(productId, IotProductThingModelTypeEnum.PROPERTY.getType());
190 190
191 // 2. 生成新的事件和服务列表 191 // 2. 生成新的事件和服务列表
192 - List<IotProductThingModelDO> newThingModelList = new ArrayList<>(); 192 + List<IotProductThingModelDO> newThingModels = new ArrayList<>();
193 // 2.1 生成属性上报事件 193 // 2.1 生成属性上报事件
194 - ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList); 194 + ThingModelEvent propertyPostEvent = generatePropertyPostEvent(properties);
195 if (propertyPostEvent != null) { 195 if (propertyPostEvent != null) {
196 - newThingModelList.add(buildEventThingModelDO(productId, productKey, propertyPostEvent, "属性上报事件")); 196 + newThingModels.add(buildEventThingModelDO(productId, productKey, propertyPostEvent, "属性上报事件"));
197 } 197 }
198 // 2.2 生成属性设置服务 198 // 2.2 生成属性设置服务
199 - ThingModelService propertySetService = generatePropertySetService(propertyList); 199 + ThingModelService propertySetService = generatePropertySetService(properties);
200 if (propertySetService != null) { 200 if (propertySetService != null) {
201 - newThingModelList.add(buildServiceThingModelDO(productId, productKey, propertySetService, "属性设置服务")); 201 + newThingModels.add(buildServiceThingModelDO(productId, productKey, propertySetService, "属性设置服务"));
202 } 202 }
203 // 2.3 生成属性获取服务 203 // 2.3 生成属性获取服务
204 - ThingModelService propertyGetService = generatePropertyGetService(propertyList); 204 + ThingModelService propertyGetService = generatePropertyGetService(properties);
205 if (propertyGetService != null) { 205 if (propertyGetService != null) {
206 - newThingModelList.add(buildServiceThingModelDO(productId, productKey, propertyGetService,"属性获取服务")); 206 + newThingModels.add(buildServiceThingModelDO(productId, productKey, propertyGetService,"属性获取服务"));
207 } 207 }
208 208
209 // 3.1 获取数据库中的默认的旧事件和服务列表 209 // 3.1 获取数据库中的默认的旧事件和服务列表
210 - List<IotProductThingModelDO> oldThingModelList = productThingModelMapper.selectListByProductIdAndIdentifiersAndTypes( 210 + List<IotProductThingModelDO> oldThingModels = productThingModelMapper.selectListByProductIdAndIdentifiersAndTypes(
211 productId, 211 productId,
212 Arrays.asList("post", "set", "get"), 212 Arrays.asList("post", "set", "get"),
213 Arrays.asList(IotProductThingModelTypeEnum.EVENT.getType(), IotProductThingModelTypeEnum.SERVICE.getType()) 213 Arrays.asList(IotProductThingModelTypeEnum.EVENT.getType(), IotProductThingModelTypeEnum.SERVICE.getType())
214 ); 214 );
215 // 3.2 创建默认的事件和服务 215 // 3.2 创建默认的事件和服务
216 - createDefaultEventsAndServices(oldThingModelList, newThingModelList); 216 + createDefaultEventsAndServices(oldThingModels, newThingModels);
217 } 217 }
218 218
219 /** 219 /**
220 * 创建默认的事件和服务 220 * 创建默认的事件和服务
221 */ 221 */
222 - private void createDefaultEventsAndServices(List<IotProductThingModelDO> oldThingModelList, List<IotProductThingModelDO> newThingModelList) {  
223 - // 1.1 使用 diffList 方法比较新旧列表  
224 - List<List<IotProductThingModelDO>> diffResult = diffList(oldThingModelList, newThingModelList, 222 + private void createDefaultEventsAndServices(List<IotProductThingModelDO> oldThingModels,
  223 + List<IotProductThingModelDO> newThingModels) {
  224 + // 使用 diffList 方法比较新旧列表
  225 + List<List<IotProductThingModelDO>> diffResult = diffList(oldThingModels, newThingModels,
225 (oldVal, newVal) -> { 226 (oldVal, newVal) -> {
226 // 继续使用 identifier 和 type 进行比较:这样可以准确地匹配对应的功能对象。 227 // 继续使用 identifier 和 type 进行比较:这样可以准确地匹配对应的功能对象。
227 boolean same = Objects.equals(oldVal.getIdentifier(), newVal.getIdentifier()) 228 boolean same = Objects.equals(oldVal.getIdentifier(), newVal.getIdentifier())
@@ -231,7 +232,7 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ @@ -231,7 +232,7 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
231 } 232 }
232 return same; 233 return same;
233 }); 234 });
234 - // 1.2 批量添加、修改、删除 235 + // 批量添加、修改、删除
235 if (CollUtil.isNotEmpty(diffResult.get(0))) { 236 if (CollUtil.isNotEmpty(diffResult.get(0))) {
236 productThingModelMapper.insertBatch(diffResult.get(0)); 237 productThingModelMapper.insertBatch(diffResult.get(0));
237 } 238 }
@@ -246,8 +247,8 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ @@ -246,8 +247,8 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
246 /** 247 /**
247 * 构建事件功能对象 248 * 构建事件功能对象
248 */ 249 */
249 - private IotProductThingModelDO buildEventThingModelDO(Long productId, String productKey, ThingModelEvent event,  
250 - String description) { 250 + private IotProductThingModelDO buildEventThingModelDO(Long productId, String productKey,
  251 + ThingModelEvent event, String description) {
251 return new IotProductThingModelDO().setProductId(productId).setProductKey(productKey) 252 return new IotProductThingModelDO().setProductId(productId).setProductKey(productKey)
252 .setIdentifier(event.getIdentifier()).setName(event.getName()).setDescription(description) 253 .setIdentifier(event.getIdentifier()).setName(event.getName()).setDescription(description)
253 .setType(IotProductThingModelTypeEnum.EVENT.getType()).setEvent(event); 254 .setType(IotProductThingModelTypeEnum.EVENT.getType()).setEvent(event);
@@ -256,8 +257,8 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ @@ -256,8 +257,8 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
256 /** 257 /**
257 * 构建服务功能对象 258 * 构建服务功能对象
258 */ 259 */
259 - private IotProductThingModelDO buildServiceThingModelDO(Long productId, String productKey, ThingModelService service,  
260 - String description) { 260 + private IotProductThingModelDO buildServiceThingModelDO(Long productId, String productKey,
  261 + ThingModelService service, String description) {
261 return new IotProductThingModelDO().setProductId(productId).setProductKey(productKey) 262 return new IotProductThingModelDO().setProductId(productId).setProductKey(productKey)
262 .setIdentifier(service.getIdentifier()).setName(service.getName()).setDescription(description) 263 .setIdentifier(service.getIdentifier()).setName(service.getName()).setDescription(description)
263 .setType(IotProductThingModelTypeEnum.SERVICE.getType()).setService(service); 264 .setType(IotProductThingModelTypeEnum.SERVICE.getType()).setService(service);
@@ -266,64 +267,64 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ @@ -266,64 +267,64 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
266 /** 267 /**
267 * 生成属性上报事件 268 * 生成属性上报事件
268 */ 269 */
269 - private ThingModelEvent generatePropertyPostEvent(List<IotProductThingModelDO> thingModelList) {  
270 - // 1.1 没有属性则不生成  
271 - if (CollUtil.isEmpty(thingModelList)) { 270 + private ThingModelEvent generatePropertyPostEvent(List<IotProductThingModelDO> thingModels) {
  271 + // 没有属性则不生成
  272 + if (CollUtil.isEmpty(thingModels)) {
272 return null; 273 return null;
273 } 274 }
274 275
275 - // 1.2 生成属性上报事件 276 + // 生成属性上报事件
276 return new ThingModelEvent().setIdentifier("post").setName("属性上报").setMethod("thing.event.property.post") 277 return new ThingModelEvent().setIdentifier("post").setName("属性上报").setMethod("thing.event.property.post")
277 .setType(IotProductThingModelServiceEventTypeEnum.INFO.getType()) 278 .setType(IotProductThingModelServiceEventTypeEnum.INFO.getType())
278 - .setOutputParams(buildInputOutputParam(thingModelList, IotProductThingModelParamDirectionEnum.OUTPUT)); 279 + .setOutputParams(buildInputOutputParam(thingModels, IotProductThingModelParamDirectionEnum.OUTPUT));
279 } 280 }
280 281
281 /** 282 /**
282 * 生成属性设置服务 283 * 生成属性设置服务
283 */ 284 */
284 - private ThingModelService generatePropertySetService(List<IotProductThingModelDO> thingModelList) { 285 + private ThingModelService generatePropertySetService(List<IotProductThingModelDO> thingModels) {
285 // 1.1 过滤出所有可写属性 286 // 1.1 过滤出所有可写属性
286 - thingModelList = filterList(thingModelList, thingModel -> 287 + thingModels = filterList(thingModels, thingModel ->
287 IotProductThingModelAccessModeEnum.READ_WRITE.getMode().equals(thingModel.getProperty().getAccessMode())); 288 IotProductThingModelAccessModeEnum.READ_WRITE.getMode().equals(thingModel.getProperty().getAccessMode()));
288 // 1.2 没有可写属性则不生成 289 // 1.2 没有可写属性则不生成
289 - if (CollUtil.isEmpty(thingModelList)) { 290 + if (CollUtil.isEmpty(thingModels)) {
290 return null; 291 return null;
291 } 292 }
292 293
293 // 2. 生成属性设置服务 294 // 2. 生成属性设置服务
294 return new ThingModelService().setIdentifier("set").setName("属性设置").setMethod("thing.service.property.set") 295 return new ThingModelService().setIdentifier("set").setName("属性设置").setMethod("thing.service.property.set")
295 .setCallType(IotProductThingModelServiceCallTypeEnum.ASYNC.getType()) 296 .setCallType(IotProductThingModelServiceCallTypeEnum.ASYNC.getType())
296 - .setInputParams(buildInputOutputParam(thingModelList, IotProductThingModelParamDirectionEnum.INPUT)) 297 + .setInputParams(buildInputOutputParam(thingModels, IotProductThingModelParamDirectionEnum.INPUT))
297 .setOutputParams(Collections.emptyList()); // 属性设置服务一般不需要输出参数 298 .setOutputParams(Collections.emptyList()); // 属性设置服务一般不需要输出参数
298 } 299 }
299 300
300 /** 301 /**
301 * 生成属性获取服务 302 * 生成属性获取服务
302 */ 303 */
303 - private ThingModelService generatePropertyGetService(List<IotProductThingModelDO> thingModelList) { 304 + private ThingModelService generatePropertyGetService(List<IotProductThingModelDO> thingModels) {
304 // 1.1 没有属性则不生成 305 // 1.1 没有属性则不生成
305 - if (CollUtil.isEmpty(thingModelList)) { 306 + if (CollUtil.isEmpty(thingModels)) {
306 return null; 307 return null;
307 } 308 }
308 309
309 // 1.2 生成属性获取服务 310 // 1.2 生成属性获取服务
310 return new ThingModelService().setIdentifier("get").setName("属性获取").setMethod("thing.service.property.get") 311 return new ThingModelService().setIdentifier("get").setName("属性获取").setMethod("thing.service.property.get")
311 .setCallType(IotProductThingModelServiceCallTypeEnum.ASYNC.getType()) 312 .setCallType(IotProductThingModelServiceCallTypeEnum.ASYNC.getType())
312 - .setInputParams(buildInputOutputParam(thingModelList, IotProductThingModelParamDirectionEnum.INPUT))  
313 - .setOutputParams(buildInputOutputParam(thingModelList, IotProductThingModelParamDirectionEnum.OUTPUT)); 313 + .setInputParams(buildInputOutputParam(thingModels, IotProductThingModelParamDirectionEnum.INPUT))
  314 + .setOutputParams(buildInputOutputParam(thingModels, IotProductThingModelParamDirectionEnum.OUTPUT));
314 } 315 }
315 316
316 /** 317 /**
317 * 构建输入/输出参数列表 318 * 构建输入/输出参数列表
318 * 319 *
319 - * @param thingModelList 属性列表 320 + * @param thingModels 属性列表
320 * @return 输入/输出参数列表 321 * @return 输入/输出参数列表
321 */ 322 */
322 - private List<ThingModelInputOutputParam> buildInputOutputParam(List<IotProductThingModelDO> thingModelList,  
323 - IotProductThingModelParamDirectionEnum directionEnum) {  
324 - return convertList(thingModelList, thingModel -> 323 + private List<ThingModelInputOutputParam> buildInputOutputParam(List<IotProductThingModelDO> thingModels,
  324 + IotProductThingModelParamDirectionEnum direction) {
  325 + return convertList(thingModels, thingModel ->
325 BeanUtils.toBean(thingModel.getProperty(), ThingModelInputOutputParam.class).setParaOrder(0) // TODO @puhui999: 先搞个默认值看看怎么个事 326 BeanUtils.toBean(thingModel.getProperty(), ThingModelInputOutputParam.class).setParaOrder(0) // TODO @puhui999: 先搞个默认值看看怎么个事
326 - .setDirection(directionEnum.getDirection())); 327 + .setDirection(direction.getDirection()));
327 } 328 }
328 329
329 } 330 }