|
@@ -1,6 +1,9 @@
|
|
|
package cn.iocoder.yudao.module.iot.service.thinkmodelfunction;
|
|
|
|
|
|
-import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.*;
|
|
|
+import cn.iocoder.yudao.framework.common.validation.Telephone;
|
|
|
+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.ThingModelService;
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.dataType.ThingModelArgument;
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.dataType.ThingModelArraySpecs;
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.dataType.ThingModelArrayType;
|
|
@@ -9,19 +12,20 @@ import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThi
|
|
|
import cn.iocoder.yudao.module.iot.convert.thinkmodelfunction.IotThinkModelFunctionConvert;
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
|
|
|
import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper;
|
|
|
-import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import cn.iocoder.yudao.module.iot.enums.product.IotThingModelTypeEnum;
|
|
|
import jakarta.annotation.Resource;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
-import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY;
|
|
|
+import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER;
|
|
|
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.THINK_MODEL_FUNCTION_NOT_EXISTS;
|
|
|
|
|
|
@Slf4j
|
|
@@ -32,153 +36,133 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
|
|
|
@Resource
|
|
|
private IotThinkModelFunctionMapper thinkModelFunctionMapper;
|
|
|
|
|
|
- private ObjectMapper objectMapper = new ObjectMapper();
|
|
|
-
|
|
|
@Override
|
|
|
public Long createThinkModelFunction(IotThinkModelFunctionSaveReqVO createReqVO) {
|
|
|
- log.info("创建物模型,参数:{}", createReqVO);
|
|
|
- // 验证 ProductKey 对应的产品物模型是否已存在
|
|
|
- validateThinkModelFunctionNotExistsByProductKey(createReqVO.getProductKey());
|
|
|
+ // 校验功能标识符在同一产品下是否唯一
|
|
|
+ validateIdentifierUnique(createReqVO.getProductId(), createReqVO.getIdentifier());
|
|
|
+
|
|
|
// 转换请求对象为数据对象
|
|
|
IotThinkModelFunctionDO thinkModelFunction = IotThinkModelFunctionConvert.INSTANCE.convert(createReqVO);
|
|
|
- // 自动生成属性上报事件和属性设置、获取服务
|
|
|
- generateDefaultEventsAndServices(createReqVO, thinkModelFunction);
|
|
|
+
|
|
|
// 插入数据库
|
|
|
thinkModelFunctionMapper.insert(thinkModelFunction);
|
|
|
- // 返回生成的 ID
|
|
|
- return thinkModelFunction.getId();
|
|
|
- }
|
|
|
|
|
|
- private void validateThinkModelFunctionNotExistsByProductKey(String productKey) {
|
|
|
- if (thinkModelFunctionMapper.selectByProductKey(productKey) != null) {
|
|
|
- throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY);
|
|
|
+ // 如果创建的是属性,需要更新默认的事件和服务
|
|
|
+ if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
|
|
|
+ generateDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public void deleteThinkModelFunction(Long id) {
|
|
|
- log.info("删除物模型,id:{}", id);
|
|
|
- // 校验物模型是否存在
|
|
|
- validateThinkModelFunctionExists(id);
|
|
|
- // 删除物模型
|
|
|
- thinkModelFunctionMapper.deleteById(id);
|
|
|
+ // 返回生成的 ID
|
|
|
+ return thinkModelFunction.getId();
|
|
|
}
|
|
|
|
|
|
- private void validateThinkModelFunctionExists(Long id) {
|
|
|
- if (thinkModelFunctionMapper.selectById(id) == null) {
|
|
|
- throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS);
|
|
|
+ private void validateIdentifierUnique(Long productId, String identifier) {
|
|
|
+ IotThinkModelFunctionDO existingFunction = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier);
|
|
|
+ if (existingFunction != null) {
|
|
|
+ throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public IotThinkModelFunctionDO getThinkModelFunctionByProductKey(String productKey) {
|
|
|
- return thinkModelFunctionMapper.selectByProductKey(productKey);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public IotThinkModelFunctionDO getThinkModelFunctionByProductId(Long productId) {
|
|
|
- return thinkModelFunctionMapper.selectByProductId(productId);
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void updateThinkModelFunction(IotThinkModelFunctionSaveReqVO updateReqVO) {
|
|
|
- log.info("更新物模型,参数:{}", updateReqVO);
|
|
|
- // 校验物模型是否存在
|
|
|
+ // 校验功能是否存在
|
|
|
validateThinkModelFunctionExists(updateReqVO.getId());
|
|
|
- // 校验 ProductKey 是否唯一
|
|
|
- validateProductKeyUnique(updateReqVO.getId(), updateReqVO.getProductKey());
|
|
|
+
|
|
|
+ // 校验功能标识符是否唯一
|
|
|
+ validateIdentifierUniqueForUpdate(updateReqVO.getId(), updateReqVO.getProductId(), updateReqVO.getIdentifier());
|
|
|
+
|
|
|
// 转换请求对象为数据对象
|
|
|
IotThinkModelFunctionDO thinkModelFunction = IotThinkModelFunctionConvert.INSTANCE.convert(updateReqVO);
|
|
|
- // 自动生成或更新属性上报事件和属性设置、获取服务
|
|
|
- generateDefaultEventsAndServices(updateReqVO, thinkModelFunction);
|
|
|
+
|
|
|
// 更新数据库
|
|
|
thinkModelFunctionMapper.updateById(thinkModelFunction);
|
|
|
- }
|
|
|
|
|
|
- private void validateProductKeyUnique(Long id, String productKey) {
|
|
|
- IotThinkModelFunctionDO existingFunction = thinkModelFunctionMapper.selectByProductKey(productKey);
|
|
|
- if (existingFunction != null && !existingFunction.getId().equals(id)) {
|
|
|
- throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_PRODUCT_KEY);
|
|
|
+ // 如果更新的是属性,需要更新默认的事件和服务
|
|
|
+ if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
|
|
|
+ generateDefaultEventsAndServices(updateReqVO.getProductId(), updateReqVO.getProductKey());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据属性列表,自动生成属性上报事件和属性设置、获取服务
|
|
|
- */
|
|
|
- private void generateDefaultEventsAndServices(IotThinkModelFunctionSaveReqVO reqVO, IotThinkModelFunctionDO thinkModelFunction) {
|
|
|
- // 获取属性列表
|
|
|
- List<ThingModelProperty> properties = reqVO.getProperties();
|
|
|
- if (properties == null) {
|
|
|
- properties = new ArrayList<>();
|
|
|
+ private void validateIdentifierUniqueForUpdate(Long id, Long productId, String identifier) {
|
|
|
+ IotThinkModelFunctionDO existingFunction = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, identifier);
|
|
|
+ if (existingFunction != null && !existingFunction.getId().equals(id)) {
|
|
|
+ throw exception(THINK_MODEL_FUNCTION_EXISTS_BY_IDENTIFIER);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 获取现有的事件和服务
|
|
|
- List<ThingModelEvent> existingEvents = reqVO.getEvents() != null ? new ArrayList<>(reqVO.getEvents()) : new ArrayList<>();
|
|
|
- List<ThingModelService> existingServices = reqVO.getServices() != null ? new ArrayList<>(reqVO.getServices()) : new ArrayList<>();
|
|
|
-
|
|
|
- // 生成或更新属性上报事件
|
|
|
- ThingModelEvent propertyPostEvent = generatePropertyPostEvent(properties);
|
|
|
- updateEventInList(existingEvents, propertyPostEvent);
|
|
|
|
|
|
- // 生成或更新属性设置和获取服务
|
|
|
- ThingModelService propertySetService = generatePropertySetService(properties);
|
|
|
- updateServiceInList(existingServices, propertySetService);
|
|
|
+ @Override
|
|
|
+ public void deleteThinkModelFunction(Long id) {
|
|
|
+ // 校验功能是否存在
|
|
|
+ IotThinkModelFunctionDO functionDO = thinkModelFunctionMapper.selectById(id);
|
|
|
+ if (functionDO == null) {
|
|
|
+ throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS);
|
|
|
+ }
|
|
|
|
|
|
- ThingModelService propertyGetService = generatePropertyGetService(properties);
|
|
|
- updateServiceInList(existingServices, propertyGetService);
|
|
|
+ // 删除功能
|
|
|
+ thinkModelFunctionMapper.deleteById(id);
|
|
|
|
|
|
- // 更新 thinkModelFunction 对象的 events 和 services 字段
|
|
|
- try {
|
|
|
- thinkModelFunction.setEvents(objectMapper.writeValueAsString(existingEvents));
|
|
|
- thinkModelFunction.setServices(objectMapper.writeValueAsString(existingServices));
|
|
|
- } catch (JsonProcessingException e) {
|
|
|
- throw new RuntimeException("序列化事件和服务时发生错误", e);
|
|
|
+ // 如果删除的是属性,需要更新默认的事件和服务
|
|
|
+ if (Objects.equals(functionDO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
|
|
|
+ generateDefaultEventsAndServices(functionDO.getProductId(), functionDO.getProductKey());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 在事件列表中更新或添加事件
|
|
|
+ * 校验功能是否存在
|
|
|
+ *
|
|
|
+ * @param id 功能编号
|
|
|
*/
|
|
|
- private void updateEventInList(List<ThingModelEvent> events, ThingModelEvent newEvent) {
|
|
|
- if (newEvent == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- for (int i = 0; i < events.size(); i++) {
|
|
|
- ThingModelEvent event = events.get(i);
|
|
|
- if (event.getIdentifier().equals(newEvent.getIdentifier())) {
|
|
|
- // 更新已有的事件
|
|
|
- events.set(i, newEvent);
|
|
|
- return;
|
|
|
- }
|
|
|
+ private void validateThinkModelFunctionExists(Long id) {
|
|
|
+ if (thinkModelFunctionMapper.selectById(id) == null) {
|
|
|
+ throw exception(THINK_MODEL_FUNCTION_NOT_EXISTS);
|
|
|
}
|
|
|
- // 如果不存在,则添加新的事件
|
|
|
- events.add(newEvent);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IotThinkModelFunctionDO getThinkModelFunction(Long id) {
|
|
|
+ return thinkModelFunctionMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<IotThinkModelFunctionDO> getThinkModelFunctionListByProductId(Long productId) {
|
|
|
+ return thinkModelFunctionMapper.selectListByProductId(productId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 在服务列表中更新或添加服务
|
|
|
+ * 生成默认的事件和服务
|
|
|
*/
|
|
|
- private void updateServiceInList(List<ThingModelService> services, ThingModelService newService) {
|
|
|
- if (newService == null) {
|
|
|
- return;
|
|
|
+ public void generateDefaultEventsAndServices(Long productId, String productKey) {
|
|
|
+ // 获取当前产品的所有属性列表
|
|
|
+ List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper.selectListByProductIdAndType(productId, IotThingModelTypeEnum.PROPERTY.getType());
|
|
|
+
|
|
|
+ // 生成属性上报事件
|
|
|
+ ThingModelEvent propertyPostEvent = generatePropertyPostEvent(propertyList);
|
|
|
+ if (propertyPostEvent != null) {
|
|
|
+ saveOrUpdateEvent(productId, productKey, propertyPostEvent);
|
|
|
}
|
|
|
- for (int i = 0; i < services.size(); i++) {
|
|
|
- ThingModelService service = services.get(i);
|
|
|
- if (service.getIdentifier().equals(newService.getIdentifier())) {
|
|
|
- // 更新已有的服务
|
|
|
- services.set(i, newService);
|
|
|
- return;
|
|
|
- }
|
|
|
+
|
|
|
+ // 生成属性设置服务
|
|
|
+ ThingModelService propertySetService = generatePropertySetService(propertyList);
|
|
|
+ if (propertySetService != null) {
|
|
|
+ saveOrUpdateService(productId, productKey, propertySetService);
|
|
|
}
|
|
|
- // 如果不存在,则添加新的服务
|
|
|
- services.add(newService);
|
|
|
- }
|
|
|
|
|
|
+ // 生成属性获取服务
|
|
|
+ ThingModelService propertyGetService = generatePropertyGetService(propertyList);
|
|
|
+ if (propertyGetService != null) {
|
|
|
+ saveOrUpdateService(productId, productKey, propertyGetService);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 生成属性上报事件
|
|
|
*/
|
|
|
- private ThingModelEvent generatePropertyPostEvent(List<ThingModelProperty> properties) {
|
|
|
+ private ThingModelEvent generatePropertyPostEvent(List<IotThinkModelFunctionDO> propertyList) {
|
|
|
+ if (propertyList == null || propertyList.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
ThingModelEvent event = new ThingModelEvent();
|
|
|
event.setIdentifier("post");
|
|
|
event.setName("属性上报");
|
|
@@ -188,7 +172,8 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
|
|
|
|
|
|
// 将属性列表转换为事件的输出参数
|
|
|
List<ThingModelArgument> outputData = new ArrayList<>();
|
|
|
- for (ThingModelProperty property : properties) {
|
|
|
+ for (IotThinkModelFunctionDO functionDO : propertyList) {
|
|
|
+ ThingModelProperty property = functionDO.getProperty();
|
|
|
ThingModelArgument arg = new ThingModelArgument();
|
|
|
arg.setIdentifier(property.getIdentifier());
|
|
|
arg.setName(property.getName());
|
|
@@ -205,9 +190,14 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
|
|
|
/**
|
|
|
* 生成属性设置服务
|
|
|
*/
|
|
|
- private ThingModelService generatePropertySetService(List<ThingModelProperty> properties) {
|
|
|
+ private ThingModelService generatePropertySetService(List<IotThinkModelFunctionDO> propertyList) {
|
|
|
+ if (propertyList == null || propertyList.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
List<ThingModelArgument> inputData = new ArrayList<>();
|
|
|
- for (ThingModelProperty property : properties) {
|
|
|
+ for (IotThinkModelFunctionDO functionDO : propertyList) {
|
|
|
+ ThingModelProperty property = functionDO.getProperty();
|
|
|
if ("w".equals(property.getAccessMode()) || "rw".equals(property.getAccessMode())) {
|
|
|
ThingModelArgument arg = new ThingModelArgument();
|
|
|
arg.setIdentifier(property.getIdentifier());
|
|
@@ -239,9 +229,14 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
|
|
|
/**
|
|
|
* 生成属性获取服务
|
|
|
*/
|
|
|
- private ThingModelService generatePropertyGetService(List<ThingModelProperty> properties) {
|
|
|
+ private ThingModelService generatePropertyGetService(List<IotThinkModelFunctionDO> propertyList) {
|
|
|
+ if (propertyList == null || propertyList.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
List<ThingModelArgument> outputData = new ArrayList<>();
|
|
|
- for (ThingModelProperty property : properties) {
|
|
|
+ for (IotThinkModelFunctionDO functionDO : propertyList) {
|
|
|
+ ThingModelProperty property = functionDO.getProperty();
|
|
|
if ("r".equals(property.getAccessMode()) || "rw".equals(property.getAccessMode())) {
|
|
|
ThingModelArgument arg = new ThingModelArgument();
|
|
|
arg.setIdentifier(property.getIdentifier());
|
|
@@ -275,10 +270,8 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
|
|
|
ThingModelArrayType arrayType = new ThingModelArrayType();
|
|
|
arrayType.setType("array");
|
|
|
ThingModelArraySpecs arraySpecs = new ThingModelArraySpecs();
|
|
|
- // 不指定数组长度,size 可以为 0 或者省略
|
|
|
ThingModelTextType textType = new ThingModelTextType();
|
|
|
textType.setType("text");
|
|
|
- // 如果有需要,可以设置 TextType 的 specs,如长度限制
|
|
|
arraySpecs.setItem(textType);
|
|
|
arrayType.setSpecs(arraySpecs);
|
|
|
|
|
@@ -289,4 +282,54 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
|
|
|
|
|
|
return service;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存或更新事件
|
|
|
+ */
|
|
|
+ private void saveOrUpdateEvent(Long productId, String productKey, ThingModelEvent event) {
|
|
|
+ // 检查是否已存在相同的事件
|
|
|
+ IotThinkModelFunctionDO existingEvent = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, event.getIdentifier());
|
|
|
+ IotThinkModelFunctionDO functionDO = new IotThinkModelFunctionDO();
|
|
|
+ functionDO.setProductId(productId);
|
|
|
+ functionDO.setProductKey(productKey);
|
|
|
+ functionDO.setIdentifier(event.getIdentifier());
|
|
|
+ functionDO.setName(event.getName());
|
|
|
+ functionDO.setDescription(event.getDescription());
|
|
|
+ functionDO.setType(IotThingModelTypeEnum.EVENT.getType());
|
|
|
+ functionDO.setEvent(event);
|
|
|
+
|
|
|
+ if (existingEvent != null) {
|
|
|
+ // 更新事件
|
|
|
+ functionDO.setId(existingEvent.getId());
|
|
|
+ thinkModelFunctionMapper.updateById(functionDO);
|
|
|
+ } else {
|
|
|
+ // 创建新的事件
|
|
|
+ thinkModelFunctionMapper.insert(functionDO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存或更新事服务
|
|
|
+ */
|
|
|
+ private void saveOrUpdateService(Long productId, String productKey, ThingModelService service) {
|
|
|
+ // 检查是否已存在相同的服务
|
|
|
+ IotThinkModelFunctionDO existingService = thinkModelFunctionMapper.selectByProductIdAndIdentifier(productId, service.getIdentifier());
|
|
|
+ IotThinkModelFunctionDO functionDO = new IotThinkModelFunctionDO();
|
|
|
+ functionDO.setProductId(productId);
|
|
|
+ functionDO.setProductKey(productKey);
|
|
|
+ functionDO.setIdentifier(service.getIdentifier());
|
|
|
+ functionDO.setName(service.getName());
|
|
|
+ functionDO.setDescription(service.getDescription());
|
|
|
+ functionDO.setType(IotThingModelTypeEnum.SERVICE.getType());
|
|
|
+ functionDO.setService(service);
|
|
|
+
|
|
|
+ if (existingService != null) {
|
|
|
+ // 更新服务
|
|
|
+ functionDO.setId(existingService.getId());
|
|
|
+ thinkModelFunctionMapper.updateById(functionDO);
|
|
|
+ } else {
|
|
|
+ // 创建新的服务
|
|
|
+ thinkModelFunctionMapper.insert(functionDO);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|