【功能完善】IoT: 产品物模型

This commit is contained in:
puhui999 2024-12-16 10:50:48 +08:00
parent db9c485285
commit 741096e208
14 changed files with 48 additions and 62 deletions

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.iot.enums.product;
package cn.iocoder.yudao.module.iot.enums.thingmodel;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -10,10 +10,9 @@ import lombok.Getter;
*/
@AllArgsConstructor
@Getter
public enum IotAccessModeEnum {
public enum IotProductThingModelAccessModeEnum {
READ("r"),
WRITE("w"),
READ_ONLY("r"),
READ_WRITE("rw");
private final String mode;

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.iot.enums.product;
package cn.iocoder.yudao.module.iot.enums.thingmodel;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.AllArgsConstructor;
@ -13,13 +13,13 @@ import java.util.Arrays;
*/
@AllArgsConstructor
@Getter
public enum IotProductFunctionTypeEnum implements IntArrayValuable {
public enum IotProductThingModelTypeEnum implements IntArrayValuable {
PROPERTY(1, "属性"),
SERVICE(2, "服务"),
EVENT(3, "事件");
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotProductFunctionTypeEnum::getType).toArray();
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(IotProductThingModelTypeEnum::getType).toArray();
/**
* 类型
@ -30,8 +30,8 @@ public enum IotProductFunctionTypeEnum implements IntArrayValuable {
*/
private final String description;
public static IotProductFunctionTypeEnum valueOfType(Integer type) {
for (IotProductFunctionTypeEnum value : values()) {
public static IotProductThingModelTypeEnum valueOfType(Integer type) {
for (IotProductThingModelTypeEnum value : values()) {
if (value.getType().equals(type)) {
return value;
}

View File

@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingMod
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingModel.dataType.ThingModelDataType;
import lombok.Data;
import java.util.List;
@Data
public class ThingModelProperty {
@ -21,7 +23,7 @@ public class ThingModelProperty {
private String accessMode; // "rw""r""w"
private Boolean required;
// TODO @haohao这个是不是 dataSpecs dataSpecsListhttps://help.aliyun.com/zh/iot/developer-reference/api-a99t11
private ThingModelDataType dataType;
private ThingModelDataType dataSpecs;
private List<ThingModelDataType> dataSpecsList;
}

View File

@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.Data;
@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "dataType", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = ThingModelIntType.class, name = "int"),
@JsonSubTypes.Type(value = ThingModelFloatType.class, name = "float"),
@ -19,6 +19,6 @@ import lombok.Data;
})
public abstract class ThingModelDataType {
private String type;
private String dataType;
}

View File

@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.iot.enums.product.IotProductFunctionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@ -22,7 +22,7 @@ public class IotThinkModelFunctionPageReqVO extends PageParam {
private String name;
@Schema(description = "功能类型", example = "1")
@InEnum(IotProductFunctionTypeEnum.class)
@InEnum(IotProductThingModelTypeEnum.class)
private Integer type;
@Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED)

View File

@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.validation.InEnum;
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.enums.product.IotProductFunctionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
@ -38,7 +38,7 @@ public class IotThinkModelFunctionSaveReqVO {
@Schema(description = "功能类型", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "功能类型不能为空")
@InEnum(IotProductFunctionTypeEnum.class)
@InEnum(IotProductThingModelTypeEnum.class)
private Integer type;
@Schema(description = "属性", requiredMode = Schema.RequiredMode.REQUIRED)

View File

@ -1,23 +1,8 @@
package cn.iocoder.yudao.module.iot.convert.device;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.deviceData.IotDeviceDataRespVO;
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.vo.IotThinkModelFunctionRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
import cn.iocoder.yudao.module.iot.enums.product.IotProductFunctionTypeEnum;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Mapper
public interface IotDeviceDataConvert {

View File

@ -6,7 +6,7 @@ import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingMode
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.vo.IotThinkModelFunctionSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
import cn.iocoder.yudao.module.iot.enums.product.IotProductFunctionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@ -26,21 +26,21 @@ public interface IotThinkModelFunctionConvert {
IotThinkModelFunctionDO convert(IotThinkModelFunctionSaveReqVO bean);
default ThingModelProperty convertToProperty(IotThinkModelFunctionSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotProductFunctionTypeEnum.PROPERTY.getType())) {
if (Objects.equals(bean.getType(), IotProductThingModelTypeEnum.PROPERTY.getType())) {
return bean.getProperty();
}
return null;
}
default ThingModelEvent convertToEvent(IotThinkModelFunctionSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotProductFunctionTypeEnum.EVENT.getType())) {
if (Objects.equals(bean.getType(), IotProductThingModelTypeEnum.EVENT.getType())) {
return bean.getEvent();
}
return null;
}
default ThingModelService convertToService(IotThinkModelFunctionSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotProductFunctionTypeEnum.SERVICE.getType())) {
if (Objects.equals(bean.getType(), IotProductThingModelTypeEnum.SERVICE.getType())) {
return bean.getService();
}
return null;

View File

@ -39,7 +39,7 @@ public class FieldParser {
ThingModelDataType type = property.getDataType();
// 将物模型字段类型映射为td字段类型
String fType = TYPE_MAPPING.get(type.getType().toUpperCase());
String fType = TYPE_MAPPING.get(type.getDataType().toUpperCase());
// 如果字段类型为NCHAR默认长度为64
int dataLength = 0;

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.module.iot.controller.admin.thinkmodelfunction.thingMode
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.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.enums.product.IotProductFunctionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
@ -66,7 +66,7 @@ public class IotThinkModelFunctionDO extends BaseDO {
/**
* 功能类型
* <p>
* 枚举 {@link IotProductFunctionTypeEnum}
* 枚举 {@link IotProductThingModelTypeEnum}
*/
private Integer type;

View File

@ -13,7 +13,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkMod
import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO;
import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
import cn.iocoder.yudao.module.iot.enums.IotConstants;
import cn.iocoder.yudao.module.iot.enums.product.IotProductFunctionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.service.tdengine.IotThingModelMessageService;
import cn.iocoder.yudao.module.iot.service.thinkmodelfunction.IotThinkModelFunctionService;
import jakarta.annotation.Resource;
@ -75,7 +75,7 @@ public class IotDeviceDataServiceImpl implements IotDeviceDataService {
// 2. 获取设备属性最新数据
List<IotThinkModelFunctionDO> thinkModelFunctionList = thinkModelFunctionService.getThinkModelFunctionListByProductKey(device.getProductKey());
thinkModelFunctionList = thinkModelFunctionList.stream()
.filter(function -> IotProductFunctionTypeEnum.PROPERTY.getType()
.filter(function -> IotProductThingModelTypeEnum.PROPERTY.getType()
.equals(function.getType())).toList();
// 3. 过滤标识符和属性名称
@ -100,7 +100,7 @@ public class IotDeviceDataServiceImpl implements IotDeviceDataService {
deviceData.setDeviceId(deviceDataReqVO.getDeviceId());
deviceData.setThinkModelFunctionId(function.getId());
deviceData.setName(function.getName());
deviceData.setDataType(function.getProperty().getDataType().getType());
deviceData.setDataType(function.getProperty().getDataType().getDataType());
}
list.add(deviceData);
});

View File

@ -10,8 +10,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdFieldDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TdTableDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDDLMapper;
import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
import cn.iocoder.yudao.module.iot.enums.product.IotProductFunctionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
@ -217,8 +216,8 @@ public class IotSuperTableServiceImpl implements IotSuperTableService {
thingModel.setProductKey(product.getProductKey());
List<ThingModelProperty> properties = functionList.stream()
.filter(function -> IotProductFunctionTypeEnum.PROPERTY.equals(
IotProductFunctionTypeEnum.valueOfType(function.getType())))
.filter(function -> IotProductThingModelTypeEnum.PROPERTY.equals(
IotProductThingModelTypeEnum.valueOfType(function.getType())))
.map(this::buildThingModelProperty)
.collect(Collectors.toList());

View File

@ -17,7 +17,7 @@ import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDDLMapper;
import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
import cn.iocoder.yudao.module.iot.enums.IotConstants;
import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStatusEnum;
import cn.iocoder.yudao.module.iot.enums.product.IotProductFunctionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
import cn.iocoder.yudao.module.iot.service.thinkmodelfunction.IotThinkModelFunctionService;
import jakarta.annotation.Resource;
@ -94,7 +94,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
return iotThinkModelFunctionService
.getThinkModelFunctionListByProductKey(productKey)
.stream()
.filter(function -> IotProductFunctionTypeEnum.PROPERTY.getType().equals(function.getType()))
.filter(function -> IotProductThingModelTypeEnum.PROPERTY.getType().equals(function.getType()))
.toList();
}
@ -138,7 +138,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
.deviceId(device.getId())
.thinkModelFunctionId(iotThinkModelFunctionDO.getId())
.name(iotThinkModelFunctionDO.getName())
.dataType(iotThinkModelFunctionDO.getProperty().getDataType().getType())
.dataType(iotThinkModelFunctionDO.getProperty().getDataType().getDataType())
.build();
deviceDataRedisDAO.set(deviceData);
}

View File

@ -18,8 +18,8 @@ import cn.iocoder.yudao.module.iot.convert.thinkmodelfunction.IotThinkModelFunct
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thinkmodelfunction.IotThinkModelFunctionDO;
import cn.iocoder.yudao.module.iot.dal.mysql.thinkmodelfunction.IotThinkModelFunctionMapper;
import cn.iocoder.yudao.module.iot.enums.product.IotAccessModeEnum;
import cn.iocoder.yudao.module.iot.enums.product.IotProductFunctionTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelAccessModeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
import cn.iocoder.yudao.module.iot.service.product.IotProductService;
import cn.iocoder.yudao.module.iot.service.tdengine.IotSuperTableService;
@ -74,7 +74,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
thinkModelFunctionMapper.insert(function);
// 6. 如果创建的是属性需要更新默认的事件和服务
if (Objects.equals(createReqVO.getType(), IotProductFunctionTypeEnum.PROPERTY.getType())) {
if (Objects.equals(createReqVO.getType(), IotProductThingModelTypeEnum.PROPERTY.getType())) {
createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
}
return function.getId();
@ -128,7 +128,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
thinkModelFunctionMapper.updateById(thinkModelFunction);
// 5. 如果更新的是属性需要更新默认的事件和服务
if (Objects.equals(updateReqVO.getType(), IotProductFunctionTypeEnum.PROPERTY.getType())) {
if (Objects.equals(updateReqVO.getType(), IotProductThingModelTypeEnum.PROPERTY.getType())) {
createDefaultEventsAndServices(updateReqVO.getProductId(), updateReqVO.getProductKey());
}
}
@ -156,7 +156,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
thinkModelFunctionMapper.deleteById(id);
// 3. 如果删除的是属性需要更新默认的事件和服务
if (Objects.equals(functionDO.getType(), IotProductFunctionTypeEnum.PROPERTY.getType())) {
if (Objects.equals(functionDO.getType(), IotProductThingModelTypeEnum.PROPERTY.getType())) {
createDefaultEventsAndServices(functionDO.getProductId(), functionDO.getProductKey());
}
}
@ -210,7 +210,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
public void createDefaultEventsAndServices(Long productId, String productKey) {
// 1. 获取当前属性列表
List<IotThinkModelFunctionDO> propertyList = thinkModelFunctionMapper
.selectListByProductIdAndType(productId, IotProductFunctionTypeEnum.PROPERTY.getType());
.selectListByProductIdAndType(productId, IotProductThingModelTypeEnum.PROPERTY.getType());
// 2. 生成新的事件和服务列表
List<IotThinkModelFunctionDO> newFunctionList = new ArrayList<>();
@ -237,7 +237,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
List<IotThinkModelFunctionDO> oldFunctionList = thinkModelFunctionMapper.selectListByProductIdAndIdentifiersAndTypes(
productId,
Arrays.asList("post", "set", "get"),
Arrays.asList(IotProductFunctionTypeEnum.EVENT.getType(), IotProductFunctionTypeEnum.SERVICE.getType())
Arrays.asList(IotProductThingModelTypeEnum.EVENT.getType(), IotProductThingModelTypeEnum.SERVICE.getType())
);
// 3.1 使用 diffList 方法比较新旧列表
@ -300,7 +300,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
.setIdentifier(event.getIdentifier())
.setName(event.getName())
.setDescription(event.getDescription())
.setType(IotProductFunctionTypeEnum.EVENT.getType())
.setType(IotProductThingModelTypeEnum.EVENT.getType())
.setEvent(event);
}
@ -314,7 +314,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
.setIdentifier(service.getIdentifier())
.setName(service.getName())
.setDescription(service.getDescription())
.setType(IotProductFunctionTypeEnum.SERVICE.getType())
.setType(IotProductThingModelTypeEnum.SERVICE.getType())
.setService(service);
}
@ -358,9 +358,10 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
}
List<ThingModelArgument> inputData = new ArrayList<>();
// TODO @puhui999: 需要重构
for (IotThinkModelFunctionDO functionDO : propertyList) {
ThingModelProperty property = functionDO.getProperty();
if (IotAccessModeEnum.WRITE.getMode().equals(property.getAccessMode()) || IotAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
if (IotProductThingModelAccessModeEnum.WRITE.getMode().equals(property.getAccessMode()) || IotProductThingModelAccessModeEnum.READ_WRITE.getMode().equals(property.getAccessMode())) {
ThingModelArgument arg = new ThingModelArgument()
.setIdentifier(property.getIdentifier())
.setName(property.getName())
@ -399,7 +400,7 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
for (IotThinkModelFunctionDO functionDO : propertyList) {
ThingModelProperty property = functionDO.getProperty();
if (ObjectUtils.equalsAny(property.getAccessMode(),
IotAccessModeEnum.READ.getMode(), IotAccessModeEnum.READ_WRITE.getMode())) {
IotProductThingModelAccessModeEnum.READ.getMode(), IotProductThingModelAccessModeEnum.READ_WRITE.getMode())) {
ThingModelArgument arg = new ThingModelArgument()
.setIdentifier(property.getIdentifier())
.setName(property.getName())
@ -430,10 +431,10 @@ public class IotThinkModelFunctionServiceImpl implements IotThinkModelFunctionSe
// 创建数组类型元素类型为文本类型字符串
ThingModelArrayType arrayType = new ThingModelArrayType();
arrayType.setType("array");
arrayType.setDataType("array");
ThingModelArraySpecs arraySpecs = new ThingModelArraySpecs();
ThingModelTextType textType = new ThingModelTextType();
textType.setType("text");
textType.setDataType("text");
arraySpecs.setItem(textType);
arrayType.setSpecs(arraySpecs);
inputArg.setDataType(arrayType);