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