Merge remote-tracking branch 'yudao/feature/iot' into iot

# Conflicts:
#	yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/ThingModelEvent.java
#	yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/thingmodel/model/ThingModelService.java
#	yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/thingmodel/IotThingModelServiceImpl.java
This commit is contained in:
puhui999 2024-12-27 10:54:52 +08:00
commit f623a57862
3 changed files with 41 additions and 46 deletions

View File

@ -22,16 +22,13 @@ public class ThingModelEvent {
*/ */
private String name; private String name;
/** /**
* 是否是标准品类的必选事件 * 是否是标准品类的必选事件
*
* - true
* - false
*/ */
private Boolean required; private Boolean required;
/** /**
* 事件类型 * 事件类型
* *
* 关联枚举 {@link IotThingModelServiceEventTypeEnum} * 枚举 {@link IotThingModelServiceEventTypeEnum}
*/ */
private String type; private String type;
/** /**

View File

@ -22,16 +22,13 @@ public class ThingModelService {
*/ */
private String name; private String name;
/** /**
* 是否是标准品类的必选服务 * 是否是标准品类的必选服务
*
* - true
* - false
*/ */
private Boolean required; private Boolean required;
/** /**
* 调用类型 * 调用类型
* *
* 关联枚举 {@link IotThingModelServiceCallTypeEnum} * 枚举 {@link IotThingModelServiceCallTypeEnum}
*/ */
private String callType; private String callType;
/** /**

View File

@ -185,43 +185,44 @@ public class IotThingModelServiceImpl implements IotThingModelService {
*/ */
public void createDefaultEventsAndServices(Long productId, String productKey) { public void createDefaultEventsAndServices(Long productId, String productKey) {
// 1. 获取当前属性列表 // 1. 获取当前属性列表
List<IotThingModelDO> propertyList = thingModelMapper List<IotThingModelDO> properties = thingModelMapper
.selectListByProductIdAndType(productId, IotThingModelTypeEnum.PROPERTY.getType()); .selectListByProductIdAndType(productId, IotThingModelTypeEnum.PROPERTY.getType());
// 2. 生成新的事件和服务列表 // 2. 生成新的事件和服务列表
List<IotThingModelDO> newThingModelList = new ArrayList<>(); List<IotThingModelDO> newThingModels = new ArrayList<>();
// 2.1 生成属性上报事件 // 2.1 生成属性上报事件
ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList); ThingModelEvent propertyPostEvent = generatePropertyPostEvent(properties);
if (propertyPostEvent != null) { if (propertyPostEvent != null) {
newThingModelList.add(buildEventThingModelDO(productId, productKey, propertyPostEvent, "属性上报事件")); newThingModels.add(buildEventThingModelDO(productId, productKey, propertyPostEvent, "属性上报事件"));
} }
// 2.2 生成属性设置服务 // 2.2 生成属性设置服务
ThingModelService propertySetService = generatePropertySetService(propertyList); ThingModelService propertySetService = generatePropertySetService(properties);
if (propertySetService != null) { if (propertySetService != null) {
newThingModelList.add(buildServiceThingModelDO(productId, productKey, propertySetService, "属性设置服务")); newThingModels.add(buildServiceThingModelDO(productId, productKey, propertySetService, "属性设置服务"));
} }
// 2.3 生成属性获取服务 // 2.3 生成属性获取服务
ThingModelService propertyGetService = generatePropertyGetService(propertyList); ThingModelService propertyGetService = generatePropertyGetService(properties);
if (propertyGetService != null) { if (propertyGetService != null) {
newThingModelList.add(buildServiceThingModelDO(productId, productKey, propertyGetService,"属性获取服务")); newThingModels.add(buildServiceThingModelDO(productId, productKey, propertyGetService, "属性获取服务"));
} }
// 3.1 获取数据库中的默认的旧事件和服务列表 // 3.1 获取数据库中的默认的旧事件和服务列表
List<IotThingModelDO> oldThingModelList = thingModelMapper.selectListByProductIdAndIdentifiersAndTypes( List<IotThingModelDO> oldThingModels = thingModelMapper.selectListByProductIdAndIdentifiersAndTypes(
productId, productId,
Arrays.asList("post", "set", "get"), Arrays.asList("post", "set", "get"),
Arrays.asList(IotThingModelTypeEnum.EVENT.getType(), IotThingModelTypeEnum.SERVICE.getType()) Arrays.asList(IotThingModelTypeEnum.EVENT.getType(), IotThingModelTypeEnum.SERVICE.getType())
); );
// 3.2 创建默认的事件和服务 // 3.2 创建默认的事件和服务
createDefaultEventsAndServices(oldThingModelList, newThingModelList); createDefaultEventsAndServices(oldThingModels, newThingModels);
} }
/** /**
* 创建默认的事件和服务 * 创建默认的事件和服务
*/ */
private void createDefaultEventsAndServices(List<IotThingModelDO> oldThingModelList, List<IotThingModelDO> newThingModelList) { private void createDefaultEventsAndServices(List<IotThingModelDO> oldThingModels,
// 1.1 使用 diffList 方法比较新旧列表 List<IotThingModelDO> newThingModels) {
List<List<IotThingModelDO>> diffResult = diffList(oldThingModelList, newThingModelList, // 使用 diffList 方法比较新旧列表
List<List<IotThingModelDO>> diffResult = diffList(oldThingModels, newThingModels,
(oldVal, newVal) -> { (oldVal, newVal) -> {
// 继续使用 identifier type 进行比较这样可以准确地匹配对应的功能对象 // 继续使用 identifier type 进行比较这样可以准确地匹配对应的功能对象
boolean same = Objects.equals(oldVal.getIdentifier(), newVal.getIdentifier()) boolean same = Objects.equals(oldVal.getIdentifier(), newVal.getIdentifier())
@ -231,7 +232,7 @@ public class IotThingModelServiceImpl implements IotThingModelService {
} }
return same; return same;
}); });
// 1.2 批量添加修改删除 // 批量添加修改删除
if (CollUtil.isNotEmpty(diffResult.get(0))) { if (CollUtil.isNotEmpty(diffResult.get(0))) {
thingModelMapper.insertBatch(diffResult.get(0)); thingModelMapper.insertBatch(diffResult.get(0));
} }
@ -246,8 +247,8 @@ public class IotThingModelServiceImpl implements IotThingModelService {
/** /**
* 构建事件功能对象 * 构建事件功能对象
*/ */
private IotThingModelDO buildEventThingModelDO(Long productId, String productKey, ThingModelEvent event, private IotThingModelDO buildEventThingModelDO(Long productId, String productKey,
String description) { ThingModelEvent event, String description) {
return new IotThingModelDO().setProductId(productId).setProductKey(productKey) return new IotThingModelDO().setProductId(productId).setProductKey(productKey)
.setIdentifier(event.getIdentifier()).setName(event.getName()).setDescription(description) .setIdentifier(event.getIdentifier()).setName(event.getName()).setDescription(description)
.setType(IotThingModelTypeEnum.EVENT.getType()).setEvent(event); .setType(IotThingModelTypeEnum.EVENT.getType()).setEvent(event);
@ -256,8 +257,8 @@ public class IotThingModelServiceImpl implements IotThingModelService {
/** /**
* 构建服务功能对象 * 构建服务功能对象
*/ */
private IotThingModelDO buildServiceThingModelDO(Long productId, String productKey, ThingModelService service, private IotThingModelDO buildServiceThingModelDO(Long productId, String productKey,
String description) { ThingModelService service, String description) {
return new IotThingModelDO().setProductId(productId).setProductKey(productKey) return new IotThingModelDO().setProductId(productId).setProductKey(productKey)
.setIdentifier(service.getIdentifier()).setName(service.getName()).setDescription(description) .setIdentifier(service.getIdentifier()).setName(service.getName()).setDescription(description)
.setType(IotThingModelTypeEnum.SERVICE.getType()).setService(service); .setType(IotThingModelTypeEnum.SERVICE.getType()).setService(service);
@ -266,64 +267,64 @@ public class IotThingModelServiceImpl implements IotThingModelService {
/** /**
* 生成属性上报事件 * 生成属性上报事件
*/ */
private ThingModelEvent generatePropertyPostEvent(List<IotThingModelDO> thingModelList) { private ThingModelEvent generatePropertyPostEvent(List<IotThingModelDO> thingModels) {
// 1.1 没有属性则不生成 // 没有属性则不生成
if (CollUtil.isEmpty(thingModelList)) { if (CollUtil.isEmpty(thingModels)) {
return null; return null;
} }
// 1.2 生成属性上报事件 // 生成属性上报事件
return new ThingModelEvent().setIdentifier("post").setName("属性上报").setMethod("thing.event.property.post") return new ThingModelEvent().setIdentifier("post").setName("属性上报").setMethod("thing.event.property.post")
.setType(IotThingModelServiceEventTypeEnum.INFO.getType()) .setType(IotThingModelServiceEventTypeEnum.INFO.getType())
.setOutputParams(buildInputOutputParam(thingModelList, IotThingModelParamDirectionEnum.OUTPUT)); .setOutputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.OUTPUT));
} }
/** /**
* 生成属性设置服务 * 生成属性设置服务
*/ */
private ThingModelService generatePropertySetService(List<IotThingModelDO> thingModelList) { private ThingModelService generatePropertySetService(List<IotThingModelDO> thingModels) {
// 1.1 过滤出所有可写属性 // 1.1 过滤出所有可写属性
thingModelList = filterList(thingModelList, thingModel -> thingModels = filterList(thingModels, thingModel ->
IotThingModelAccessModeEnum.READ_WRITE.getMode().equals(thingModel.getProperty().getAccessMode())); IotThingModelAccessModeEnum.READ_WRITE.getMode().equals(thingModel.getProperty().getAccessMode()));
// 1.2 没有可写属性则不生成 // 1.2 没有可写属性则不生成
if (CollUtil.isEmpty(thingModelList)) { if (CollUtil.isEmpty(thingModels)) {
return null; return null;
} }
// 2. 生成属性设置服务 // 2. 生成属性设置服务
return new ThingModelService().setIdentifier("set").setName("属性设置").setMethod("thing.service.property.set") return new ThingModelService().setIdentifier("set").setName("属性设置").setMethod("thing.service.property.set")
.setCallType(IotThingModelServiceCallTypeEnum.ASYNC.getType()) .setCallType(IotThingModelServiceCallTypeEnum.ASYNC.getType())
.setInputParams(buildInputOutputParam(thingModelList, IotThingModelParamDirectionEnum.INPUT)) .setInputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.INPUT))
.setOutputParams(Collections.emptyList()); // 属性设置服务一般不需要输出参数 .setOutputParams(Collections.emptyList()); // 属性设置服务一般不需要输出参数
} }
/** /**
* 生成属性获取服务 * 生成属性获取服务
*/ */
private ThingModelService generatePropertyGetService(List<IotThingModelDO> thingModelList) { private ThingModelService generatePropertyGetService(List<IotThingModelDO> thingModels) {
// 1.1 没有属性则不生成 // 1.1 没有属性则不生成
if (CollUtil.isEmpty(thingModelList)) { if (CollUtil.isEmpty(thingModels)) {
return null; return null;
} }
// 1.2 生成属性获取服务 // 1.2 生成属性获取服务
return new ThingModelService().setIdentifier("get").setName("属性获取").setMethod("thing.service.property.get") return new ThingModelService().setIdentifier("get").setName("属性获取").setMethod("thing.service.property.get")
.setCallType(IotThingModelServiceCallTypeEnum.ASYNC.getType()) .setCallType(IotThingModelServiceCallTypeEnum.ASYNC.getType())
.setInputParams(buildInputOutputParam(thingModelList, IotThingModelParamDirectionEnum.INPUT)) .setInputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.INPUT))
.setOutputParams(buildInputOutputParam(thingModelList, IotThingModelParamDirectionEnum.OUTPUT)); .setOutputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.OUTPUT));
} }
/** /**
* 构建输入/输出参数列表 * 构建输入/输出参数列表
* *
* @param thingModelList 属性列表 * @param thingModels 属性列表
* @return 输入/输出参数列表 * @return 输入/输出参数列表
*/ */
private List<ThingModelParam> buildInputOutputParam(List<IotThingModelDO> thingModelList, private List<ThingModelParam> buildInputOutputParam(List<IotThingModelDO> thingModels,
IotThingModelParamDirectionEnum directionEnum) { IotThingModelParamDirectionEnum direction) {
return convertList(thingModelList, thingModel -> return convertList(thingModels, thingModel ->
BeanUtils.toBean(thingModel.getProperty(), ThingModelParam.class).setParaOrder(0) // TODO @puhui999: 先搞个默认值看看怎么个事 BeanUtils.toBean(thingModel.getProperty(), ThingModelParam.class).setParaOrder(0) // TODO @puhui999: 先搞个默认值看看怎么个事
.setDirection(directionEnum.getDirection())); .setDirection(direction.getDirection()));
} }
} }