!1172 【代码优化】IOT: ThingModel

Merge pull request !1172 from puhui999/iot
This commit is contained in:
芋道源码 2024-12-28 13:02:00 +00:00 committed by Gitee
commit 4014e1b025
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
25 changed files with 310 additions and 317 deletions

View File

@ -10,7 +10,7 @@ import lombok.Getter;
*/
@AllArgsConstructor
@Getter
public enum IotProductThingModelAccessModeEnum {
public enum IotThingModelAccessModeEnum {
READ_ONLY("r"),
READ_WRITE("rw");

View File

@ -10,7 +10,7 @@ import lombok.Getter;
*/
@AllArgsConstructor
@Getter
public enum IotProductThingModelParamDirectionEnum {
public enum IotThingModelParamDirectionEnum {
INPUT("input"), // 输入参数
OUTPUT("output"); // 输出参数

View File

@ -10,7 +10,7 @@ import lombok.Getter;
*/
@AllArgsConstructor
@Getter
public enum IotProductThingModelServiceCallTypeEnum {
public enum IotThingModelServiceCallTypeEnum {
ASYNC("async"), // 异步调用
SYNC("sync"); // 同步调用

View File

@ -10,7 +10,7 @@ import lombok.Getter;
*/
@AllArgsConstructor
@Getter
public enum IotProductThingModelServiceEventTypeEnum {
public enum IotThingModelServiceEventTypeEnum {
INFO("info"), // 信息
ALERT("alert"), // 告警

View File

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

View File

@ -1,84 +0,0 @@
package cn.iocoder.yudao.module.iot.controller.admin.thingmodel;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelSaveReqVO;
import cn.iocoder.yudao.module.iot.convert.thingmodel.IotProductThingModelConvert;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotProductThingModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - IoT 产品物模型")
@RestController
@RequestMapping("/iot/product-thing-model")
@Validated
public class IotProductThingModelController {
@Resource
private IotProductThingModelService thingModelService;
@PostMapping("/create")
@Operation(summary = "创建产品物模型")
@PreAuthorize("@ss.hasPermission('iot:product-thing-model:create')")
public CommonResult<Long> createProductThingModel(@Valid @RequestBody IotProductThingModelSaveReqVO createReqVO) {
return success(thingModelService.createProductThingModel(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新产品物模型")
@PreAuthorize("@ss.hasPermission('iot:product-thing-model:update')")
public CommonResult<Boolean> updateProductThingModel(@Valid @RequestBody IotProductThingModelSaveReqVO updateReqVO) {
thingModelService.updateProductThingModel(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除产品物模型")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('iot:product-thing-model:delete')")
public CommonResult<Boolean> deleteProductThingModel(@RequestParam("id") Long id) {
thingModelService.deleteProductThingModel(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得产品物模型")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('iot:product-thing-model:query')")
public CommonResult<IotProductThingModelRespVO> getProductThingModel(@RequestParam("id") Long id) {
IotProductThingModelDO thingModel = thingModelService.getProductThingModel(id);
return success(IotProductThingModelConvert.INSTANCE.convert(thingModel));
}
@GetMapping("/list-by-product-id")
@Operation(summary = "获得产品物模型")
@Parameter(name = "productId", description = "产品ID", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('iot:product-thing-model:query')")
public CommonResult<List<IotProductThingModelRespVO>> getProductThingModelListByProductId(@RequestParam("productId") Long productId) {
List<IotProductThingModelDO> list = thingModelService.getProductThingModelListByProductId(productId);
return success(IotProductThingModelConvert.INSTANCE.convertList(list));
}
@GetMapping("/page")
@Operation(summary = "获得产品物模型分页")
@PreAuthorize("@ss.hasPermission('iot:product-thing-model:query')")
public CommonResult<PageResult<IotProductThingModelRespVO>> getProductThingModelPage(@Valid IotProductThingModelPageReqVO pageReqVO) {
PageResult<IotProductThingModelDO> pageResult = thingModelService.getProductThingModelPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, IotProductThingModelRespVO.class));
}
}

View File

@ -25,8 +25,7 @@ Authorization: Bearer {{token}}
"defaultValue": "30",
"unit": "%",
"unitName": "百分比"
},
"description": "当前温度值"
}
}
}
@ -46,7 +45,6 @@ Authorization: Bearer {{token}}
"property": {
"identifier": "switch",
"name": "开关",
"description": "温度计开关",
"accessMode": "rw",
"required": true,
"dataType": "bool",
@ -81,7 +79,6 @@ Authorization: Bearer {{token}}
"property": {
"identifier": "argb",
"name": "温度计 argb 颜色",
"description": "温度计 argb 颜色",
"accessMode": "rw",
"required": true,
"dataType": "array",
@ -93,7 +90,6 @@ Authorization: Bearer {{token}}
{
"identifier": "switch",
"name": "开关",
"description": "温度计开关",
"accessMode": "rw",
"required": true,
"dataType": "struct",
@ -126,8 +122,7 @@ Authorization: Bearer {{token}}
"defaultValue": "30",
"unit": "%",
"unitName": "百分比"
},
"description": "当前温度值"
}
}
]
}
@ -151,7 +146,6 @@ Authorization: Bearer {{token}}
"property": {
"identifier": "switch",
"name": "开关",
"description": "温度计开关",
"accessMode": "r",
"required": true,
"dataType": "bool",

View File

@ -0,0 +1,84 @@
package cn.iocoder.yudao.module.iot.controller.admin.thingmodel;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelSaveReqVO;
import cn.iocoder.yudao.module.iot.convert.thingmodel.IotThingModelConvert;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - IoT 产品物模型")
@RestController
@RequestMapping("/iot/thing-model")
@Validated
public class IotThingModelController {
@Resource
private IotThingModelService thingModelService;
@PostMapping("/create")
@Operation(summary = "创建产品物模型")
@PreAuthorize("@ss.hasPermission('iot:thing-model:create')")
public CommonResult<Long> createThingModel(@Valid @RequestBody IotThingModelSaveReqVO createReqVO) {
return success(thingModelService.createThingModel(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新产品物模型")
@PreAuthorize("@ss.hasPermission('iot:thing-model:update')")
public CommonResult<Boolean> updateThingModel(@Valid @RequestBody IotThingModelSaveReqVO updateReqVO) {
thingModelService.updateThingModel(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除产品物模型")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('iot:thing-model:delete')")
public CommonResult<Boolean> deleteThingModel(@RequestParam("id") Long id) {
thingModelService.deleteThingModel(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得产品物模型")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('iot:thing-model:query')")
public CommonResult<IotThingModelRespVO> getThingModel(@RequestParam("id") Long id) {
IotThingModelDO thingModel = thingModelService.getThingModel(id);
return success(IotThingModelConvert.INSTANCE.convert(thingModel));
}
@GetMapping("/list-by-product-id")
@Operation(summary = "获得产品物模型")
@Parameter(name = "productId", description = "产品ID", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('iot:thing-model:query')")
public CommonResult<List<IotThingModelRespVO>> getThingModelListByProductId(@RequestParam("productId") Long productId) {
List<IotThingModelDO> list = thingModelService.getThingModelListByProductId(productId);
return success(IotThingModelConvert.INSTANCE.convertList(list));
}
@GetMapping("/page")
@Operation(summary = "获得产品物模型分页")
@PreAuthorize("@ss.hasPermission('iot:thing-model:query')")
public CommonResult<PageResult<IotThingModelRespVO>> getThingModelPage(@Valid IotThingModelPageReqVO pageReqVO) {
PageResult<IotThingModelDO> pageResult = thingModelService.getProductThingModelPage(pageReqVO);
return success(BeanUtils.toBean(pageResult, IotThingModelRespVO.class));
}
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelServiceEventTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelServiceEventTypeEnum;
import lombok.Data;
import java.util.List;
@ -28,7 +28,7 @@ public class ThingModelEvent {
/**
* 事件类型
*
* 枚举 {@link IotProductThingModelServiceEventTypeEnum}
* 枚举 {@link IotThingModelServiceEventTypeEnum}
*/
private String type;
/**
@ -36,7 +36,7 @@ public class ThingModelEvent {
*
* 输出参数定义事件调用后返回的结果或反馈信息用于确认操作结果或提供额外的信息
*/
private List<ThingModelInputOutputParam> outputParams;
private List<ThingModelParam> outputParams;
/**
* 标识设备需要执行的具体操作
*/

View File

@ -1,18 +1,18 @@
package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDataSpecs;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelParamDirectionEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelParamDirectionEnum;
import lombok.Data;
import java.util.List;
/**
* IOT 产品物模型中的参数 // TODO @puhui999 考虑要不改成 ThingModelParam
* IOT 产品物模型中的参数
*
* @author HUIHUI
*/
@Data
public class ThingModelInputOutputParam {
public class ThingModelParam {
/**
* 参数标识符
@ -25,7 +25,7 @@ public class ThingModelInputOutputParam {
/**
* 用于区分输入或输出参数
*
* 关联枚举 {@link IotProductThingModelParamDirectionEnum}
* 关联枚举 {@link IotThingModelParamDirectionEnum}
*/
private String direction;
/**

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType.ThingModelDataSpecs;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelAccessModeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelAccessModeEnum;
import lombok.Data;
import java.util.List;
@ -27,7 +27,7 @@ public class ThingModelProperty {
/**
* 云端可以对该属性进行的操作类型
*
* 枚举 {@link IotProductThingModelAccessModeEnum}
* 枚举 {@link IotThingModelAccessModeEnum}
*/
private String accessMode;
/**

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelServiceCallTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelServiceCallTypeEnum;
import lombok.Data;
import java.util.List;
@ -28,7 +28,7 @@ public class ThingModelService {
/**
* 调用类型
*
* 枚举 {@link IotProductThingModelServiceCallTypeEnum}
* 枚举 {@link IotThingModelServiceCallTypeEnum}
*/
private String callType;
/**
@ -36,13 +36,13 @@ public class ThingModelService {
*
* 输入参数定义服务调用时所需提供的信息用于控制设备行为或执行特定任务
*/
private List<ThingModelInputOutputParam> inputParams;
private List<ThingModelParam> inputParams;
/**
* 服务的输出参数
*
* 输出参数定义服务调用后返回的结果或反馈信息用于确认操作结果或提供额外的信息
*/
private List<ThingModelInputOutputParam> outputParams;
private List<ThingModelParam> outputParams;
/**
* 标识设备需要执行的具体操作
*/

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.dataType;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelAccessModeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelAccessModeEnum;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -27,7 +27,7 @@ public class ThingModelStructDataSpecs extends ThingModelDataSpecs {
private String name;
/**
* 云端可以对该属性进行的操作类型
* 关联枚举 {@link IotProductThingModelAccessModeEnum}
* 关联枚举 {@link IotThingModelAccessModeEnum}
*/
private String accessMode;
/**

View File

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

View File

@ -13,7 +13,7 @@ import java.time.LocalDateTime;
@Schema(description = "管理后台 - IoT 产品物模型 Response VO")
@Data
@ExcelIgnoreUnannotated
public class IotProductThingModelRespVO {
public class IotThingModelRespVO {
@Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "21816")
@ExcelProperty("产品ID")

View File

@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelEvent;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelProperty;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelService;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
@ -12,7 +12,7 @@ import lombok.Data;
@Schema(description = "管理后台 - IoT 产品物模型新增/修改 Request VO")
@Data
public class IotProductThingModelSaveReqVO {
public class IotThingModelSaveReqVO {
@Schema(description = "编号", example = "1")
private Long id;
@ -38,7 +38,7 @@ public class IotProductThingModelSaveReqVO {
@Schema(description = "功能类型", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "功能类型不能为空")
@InEnum(IotProductThingModelTypeEnum.class)
@InEnum(IotThingModelTypeEnum.class)
private Integer type;
@Schema(description = "属性", requiredMode = Schema.RequiredMode.REQUIRED)

View File

@ -3,10 +3,10 @@ package cn.iocoder.yudao.module.iot.convert.thingmodel;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelEvent;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelProperty;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelService;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
@ -16,44 +16,44 @@ import java.util.List;
import java.util.Objects;
@Mapper
public interface IotProductThingModelConvert {
public interface IotThingModelConvert {
IotProductThingModelConvert INSTANCE = Mappers.getMapper(IotProductThingModelConvert.class);
IotThingModelConvert INSTANCE = Mappers.getMapper(IotThingModelConvert.class);
// SaveReqVO 转换为 DO
@Mapping(target = "property", expression = "java(convertToProperty(bean))")
@Mapping(target = "event", expression = "java(convertToEvent(bean))")
@Mapping(target = "service", expression = "java(convertToService(bean))")
IotProductThingModelDO convert(IotProductThingModelSaveReqVO bean);
IotThingModelDO convert(IotThingModelSaveReqVO bean);
// DO 转换为 RespVO
@Mapping(target = "property", source = "property")
@Mapping(target = "event", source = "event")
@Mapping(target = "service", source = "service")
IotProductThingModelRespVO convert(IotProductThingModelDO bean);
IotThingModelRespVO convert(IotThingModelDO bean);
// 批量转换
List<IotProductThingModelRespVO> convertList(List<IotProductThingModelDO> list);
List<IotThingModelRespVO> convertList(List<IotThingModelDO> list);
@Named("convertToProperty")
default ThingModelProperty convertToProperty(IotProductThingModelSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotProductThingModelTypeEnum.PROPERTY.getType())) {
default ThingModelProperty convertToProperty(IotThingModelSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
return bean.getProperty();
}
return null;
}
@Named("convertToEvent")
default ThingModelEvent convertToEvent(IotProductThingModelSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotProductThingModelTypeEnum.EVENT.getType())) {
default ThingModelEvent convertToEvent(IotThingModelSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotThingModelTypeEnum.EVENT.getType())) {
return bean.getEvent();
}
return null;
}
@Named("convertToService")
default ThingModelService convertToService(IotProductThingModelSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotProductThingModelTypeEnum.SERVICE.getType())) {
default ThingModelService convertToService(IotThingModelSaveReqVO bean) {
if (Objects.equals(bean.getType(), IotThingModelTypeEnum.SERVICE.getType())) {
return bean.getService();
}
return null;

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.module.iot.dal.dataobject.device;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -30,7 +30,7 @@ public class IotDeviceDataDO {
/**
* 物模型编号
* <p>
* 关联 {@link IotProductThingModelDO#getId()}
* 关联 {@link IotThingModelDO#getId()}
*/
private Long thingModelId;
@ -51,21 +51,21 @@ public class IotDeviceDataDO {
/**
* 属性标识符
* <p>
* 关联 {@link IotProductThingModelDO#getIdentifier()}
* 关联 {@link IotThingModelDO#getIdentifier()}
*/
private String identifier;
/**
* 属性名称
* <p>
* 关联 {@link IotProductThingModelDO#getName()}
* 关联 {@link IotThingModelDO#getName()}
*/
private String name;
/**
* 数据类型
* <p>
* 关联 {@link IotProductThingModelDO#getProperty()#getDataType()}
* 关联 {@link IotThingModelDO#getProperty()#getDataType()}
*/
private String dataType;

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelE
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelProperty;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelService;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
@ -16,21 +16,20 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
// TODO @huihuiIotProductThingModelDO => IotThingModelDO
/**
* IoT 产品物模型功能 DO
* <p>
* 每个 {@link IotProductDO} {@link IotProductThingModelDO} 一对多的关系它的每个属性事件服务都对应一条记录
* 每个 {@link IotProductDO} {@link IotThingModelDO} 一对多的关系它的每个属性事件服务都对应一条记录
*
* @author 芋道源码
*/
@TableName(value = "iot_product_thing_model", autoResultMap = true)
@KeySequence("iot_product_thing_model_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@TableName(value = "iot_thing_model", autoResultMap = true)
@KeySequence("iot_thing_model_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class IotProductThingModelDO extends BaseDO {
public class IotThingModelDO extends BaseDO {
/**
* 物模型功能编号
@ -67,7 +66,7 @@ public class IotProductThingModelDO extends BaseDO {
/**
* 功能类型
* <p>
* 枚举 {@link IotProductThingModelTypeEnum}
* 枚举 {@link IotThingModelTypeEnum}
*/
private Integer type;

View File

@ -1,62 +0,0 @@
package cn.iocoder.yudao.module.iot.dal.mysql.thingmodel;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* IoT 产品物模型 Mapper
*
* @author 芋道源码
*/
@Mapper
public interface IotProductThingModelMapper extends BaseMapperX<IotProductThingModelDO> {
default PageResult<IotProductThingModelDO> selectPage(IotProductThingModelPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<IotProductThingModelDO>()
.eqIfPresent(IotProductThingModelDO::getIdentifier, reqVO.getIdentifier())
.likeIfPresent(IotProductThingModelDO::getName, reqVO.getName())
.eqIfPresent(IotProductThingModelDO::getType, reqVO.getType())
.eqIfPresent(IotProductThingModelDO::getProductId, reqVO.getProductId())
.notIn(IotProductThingModelDO::getIdentifier, "get", "set", "post")
.orderByDesc(IotProductThingModelDO::getId));
}
default IotProductThingModelDO selectByProductIdAndIdentifier(Long productId, String identifier) {
return selectOne(IotProductThingModelDO::getProductId, productId,
IotProductThingModelDO::getIdentifier, identifier);
}
default List<IotProductThingModelDO> selectListByProductId(Long productId) {
return selectList(IotProductThingModelDO::getProductId, productId);
}
default List<IotProductThingModelDO> selectListByProductIdAndType(Long productId, Integer type) {
return selectList(IotProductThingModelDO::getProductId, productId,
IotProductThingModelDO::getType, type);
}
default List<IotProductThingModelDO> selectListByProductIdAndIdentifiersAndTypes(Long productId,
List<String> identifiers,
List<Integer> types) {
return selectList(new LambdaQueryWrapperX<IotProductThingModelDO>()
.eq(IotProductThingModelDO::getProductId, productId)
.in(IotProductThingModelDO::getIdentifier, identifiers)
.in(IotProductThingModelDO::getType, types));
}
default IotProductThingModelDO selectByProductIdAndName(Long productId, String name) {
return selectOne(IotProductThingModelDO::getProductId, productId,
IotProductThingModelDO::getName, name);
}
default List<IotProductThingModelDO> selectListByProductKey(String productKey) {
return selectList(IotProductThingModelDO::getProductKey, productKey);
}
}

View File

@ -0,0 +1,62 @@
package cn.iocoder.yudao.module.iot.dal.mysql.thingmodel;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* IoT 产品物模型 Mapper
*
* @author 芋道源码
*/
@Mapper
public interface IotThingModelMapper extends BaseMapperX<IotThingModelDO> {
default PageResult<IotThingModelDO> selectPage(IotThingModelPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<IotThingModelDO>()
.eqIfPresent(IotThingModelDO::getIdentifier, reqVO.getIdentifier())
.likeIfPresent(IotThingModelDO::getName, reqVO.getName())
.eqIfPresent(IotThingModelDO::getType, reqVO.getType())
.eqIfPresent(IotThingModelDO::getProductId, reqVO.getProductId())
.notIn(IotThingModelDO::getIdentifier, "get", "set", "post")
.orderByDesc(IotThingModelDO::getId));
}
default IotThingModelDO selectByProductIdAndIdentifier(Long productId, String identifier) {
return selectOne(IotThingModelDO::getProductId, productId,
IotThingModelDO::getIdentifier, identifier);
}
default List<IotThingModelDO> selectListByProductId(Long productId) {
return selectList(IotThingModelDO::getProductId, productId);
}
default List<IotThingModelDO> selectListByProductIdAndType(Long productId, Integer type) {
return selectList(IotThingModelDO::getProductId, productId,
IotThingModelDO::getType, type);
}
default List<IotThingModelDO> selectListByProductIdAndIdentifiersAndTypes(Long productId,
List<String> identifiers,
List<Integer> types) {
return selectList(new LambdaQueryWrapperX<IotThingModelDO>()
.eq(IotThingModelDO::getProductId, productId)
.in(IotThingModelDO::getIdentifier, identifiers)
.in(IotThingModelDO::getType, types));
}
default IotThingModelDO selectByProductIdAndName(Long productId, String name) {
return selectOne(IotThingModelDO::getProductId, productId,
IotThingModelDO::getName, name);
}
default List<IotThingModelDO> selectListByProductKey(String productKey) {
return selectList(IotThingModelDO::getProductKey, productKey);
}
}

View File

@ -13,17 +13,17 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDataDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyDataMapper;
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.thingmodel.IotDataSpecsDataTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotProductThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.framework.tdengine.core.TDengineTableField;
import cn.iocoder.yudao.module.iot.service.product.IotProductService;
import cn.iocoder.yudao.module.iot.service.tdengine.IotThingModelMessageService;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotProductThingModelService;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
@ -71,7 +71,7 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
@Resource
private IotThingModelMessageService thingModelMessageService;
@Resource
private IotProductThingModelService thingModelService;
private IotThingModelService thingModelService;
@Resource
private IotProductService productService;
@ -88,8 +88,8 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
public void defineDevicePropertyData(Long productId) {
// 1.1 查询产品和物模型
IotProductDO product = productService.validateProductExists(productId);
List<IotProductThingModelDO> thingModels = filterList(thingModelService.getProductThingModelListByProductId(productId),
thingModel -> IotProductThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
List<IotThingModelDO> thingModels = filterList(thingModelService.getThingModelListByProductId(productId),
thingModel -> IotThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
// 1.2 解析 DB 里的字段
List<TDengineTableField> oldFields = new ArrayList<>();
try {
@ -115,7 +115,7 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
devicePropertyDataMapper.alterProductPropertySTable(product.getProductKey(), oldFields, newFields);
}
private List<TDengineTableField> buildTableFieldList(List<IotProductThingModelDO> thingModels) {
private List<TDengineTableField> buildTableFieldList(List<IotThingModelDO> thingModels) {
return convertList(thingModels, thingModel -> {
TDengineTableField field = new TDengineTableField(
StrUtil.toUnderlineCase(thingModel.getIdentifier()), // TDengine 字段默认都是小写
@ -153,8 +153,8 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
// 1. 获取设备信息
IotDeviceDO device = deviceService.getDevice(deviceDataReqVO.getDeviceId());
// 2. 获取设备属性最新数据
List<IotProductThingModelDO> thingModelList = thingModelService.getProductThingModelListByProductKey(device.getProductKey());
thingModelList = filterList(thingModelList, thingModel -> IotProductThingModelTypeEnum.PROPERTY.getType()
List<IotThingModelDO> thingModelList = thingModelService.getProductThingModelListByProductKey(device.getProductKey());
thingModelList = filterList(thingModelList, thingModel -> IotThingModelTypeEnum.PROPERTY.getType()
.equals(thingModel.getType()));
// 3. 过滤标识符和属性名称

View File

@ -11,16 +11,16 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.FieldParser;
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.tdengine.ThingModelMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO;
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.thingmodel.IotProductThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
import cn.iocoder.yudao.module.iot.service.device.IotDeviceService;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotProductThingModelService;
import cn.iocoder.yudao.module.iot.service.thingmodel.IotThingModelService;
import cn.iocoder.yudao.module.iot.service.product.IotProductService;
import cn.iocoder.yudao.module.iot.util.IotTdDatabaseUtils;
import jakarta.annotation.Resource;
@ -52,7 +52,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
private String url;
@Resource
private IotProductThingModelService iotProductThingModelService;
private IotThingModelService iotThingModelService;
@Resource
private IotDeviceService iotDeviceService;
@Resource
@ -83,7 +83,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
// 2. 获取设备属性并进行物模型校验过滤非物模型属性
Map<String, Object> params = thingModelMessage.dataToMap();
List<IotProductThingModelDO> thingModelList = getValidThingModelList(thingModelMessage.getProductKey());
List<IotThingModelDO> thingModelList = getValidThingModelList(thingModelMessage.getProductKey());
if (thingModelList.isEmpty()) {
return;
}
@ -102,9 +102,9 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
.build());
}
private List<IotProductThingModelDO> getValidThingModelList(String productKey) {
return filterList(iotProductThingModelService.getProductThingModelListByProductKey(productKey),
thingModel -> IotProductThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
private List<IotThingModelDO> getValidThingModelList(String productKey) {
return filterList(iotThingModelService.getProductThingModelListByProductKey(productKey),
thingModel -> IotThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
}
@Override
@ -134,17 +134,17 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
tdEngineDDLMapper.createSuperTable(new TdTableDO(databaseName, superTableName, schemaFields, tagsFields));
}
private List<IotProductThingModelDO> getValidFunctionList(String productKey) {
return filterList(iotProductThingModelService.getProductThingModelListByProductKey(productKey),
thingModel -> IotProductThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
private List<IotThingModelDO> getValidFunctionList(String productKey) {
return filterList(iotThingModelService.getProductThingModelListByProductKey(productKey),
thingModel -> IotThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
}
private List<TdFieldDO> filterAndCollectValidFields(Map<String, Object> params, List<IotProductThingModelDO> thingModelList, IotDeviceDO device, Long time) {
private List<TdFieldDO> filterAndCollectValidFields(Map<String, Object> params, List<IotThingModelDO> thingModelList, IotDeviceDO device, Long time) {
// 1. 获取属性标识符集合
Set<String> propertyIdentifiers = convertSet(thingModelList, IotProductThingModelDO::getIdentifier);
Set<String> propertyIdentifiers = convertSet(thingModelList, IotThingModelDO::getIdentifier);
// 2. 构建属性标识符和属性的映射
Map<String, IotProductThingModelDO> thingModelMap = convertMap(thingModelList, IotProductThingModelDO::getIdentifier);
Map<String, IotThingModelDO> thingModelMap = convertMap(thingModelList, IotThingModelDO::getIdentifier);
// 3. 过滤并收集有效的属性字段
List<TdFieldDO> schemaFieldValues = new ArrayList<>();
@ -164,21 +164,21 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
* 缓存设备属性
*
* @param device 设备信息
* @param iotProductThingModelDO 物模型属性
* @param iotThingModelDO 物模型属性
* @param val 属性值
* @param time 时间
*/
private void setDeviceDataCache(IotDeviceDO device, IotProductThingModelDO iotProductThingModelDO, Object val, Long time) {
private void setDeviceDataCache(IotDeviceDO device, IotThingModelDO iotThingModelDO, Object val, Long time) {
IotDeviceDataDO deviceData = IotDeviceDataDO.builder()
.productKey(device.getProductKey())
.deviceName(device.getDeviceName())
.identifier(iotProductThingModelDO.getIdentifier())
.identifier(iotThingModelDO.getIdentifier())
.value(val != null ? val.toString() : null)
.updateTime(DateUtil.toLocalDateTime(new Date(time)))
.deviceId(device.getId())
.thingModelId(iotProductThingModelDO.getId())
.name(iotProductThingModelDO.getName())
.dataType(iotProductThingModelDO.getProperty().getDataType())
.thingModelId(iotThingModelDO.getId())
.name(iotThingModelDO.getName())
.dataType(iotThingModelDO.getProperty().getDataType())
.build();
deviceDataRedisDAO.set(deviceData);
}

View File

@ -1,9 +1,9 @@
package cn.iocoder.yudao.module.iot.service.thingmodel;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import jakarta.validation.Valid;
import java.util.List;
@ -13,7 +13,7 @@ import java.util.List;
*
* @author 芋道源码
*/
public interface IotProductThingModelService {
public interface IotThingModelService {
/**
* 创建产品物模型
@ -21,21 +21,21 @@ public interface IotProductThingModelService {
* @param createReqVO 创建信息
* @return 编号
*/
Long createProductThingModel(@Valid IotProductThingModelSaveReqVO createReqVO);
Long createThingModel(@Valid IotThingModelSaveReqVO createReqVO);
/**
* 更新产品物模型
*
* @param updateReqVO 更新信息
*/
void updateProductThingModel(@Valid IotProductThingModelSaveReqVO updateReqVO);
void updateThingModel(@Valid IotThingModelSaveReqVO updateReqVO);
/**
* 删除产品物模型
*
* @param id 编号
*/
void deleteProductThingModel(Long id);
void deleteThingModel(Long id);
/**
* 获得产品物模型
@ -43,7 +43,7 @@ public interface IotProductThingModelService {
* @param id 编号
* @return 产品物模型
*/
IotProductThingModelDO getProductThingModel(Long id);
IotThingModelDO getThingModel(Long id);
/**
* 获得产品物模型列表
@ -51,7 +51,7 @@ public interface IotProductThingModelService {
* @param productId 产品编号
* @return 产品物模型列表
*/
List<IotProductThingModelDO> getProductThingModelListByProductId(Long productId);
List<IotThingModelDO> getThingModelListByProductId(Long productId);
/**
* 获得产品物模型分页
@ -59,7 +59,7 @@ public interface IotProductThingModelService {
* @param pageReqVO 分页查询
* @return 产品物模型分页
*/
PageResult<IotProductThingModelDO> getProductThingModelPage(IotProductThingModelPageReqVO pageReqVO);
PageResult<IotThingModelDO> getProductThingModelPage(IotThingModelPageReqVO pageReqVO);
/**
* 获得产品物模型列表
@ -67,6 +67,6 @@ public interface IotProductThingModelService {
* @param productKey 产品 Key
* @return 产品物模型列表
*/
List<IotProductThingModelDO> getProductThingModelListByProductKey(String productKey);
List<IotThingModelDO> getProductThingModelListByProductKey(String productKey);
}

View File

@ -5,14 +5,14 @@ import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelEvent;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelInputOutputParam;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelParam;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.model.ThingModelService;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotProductThingModelSaveReqVO;
import cn.iocoder.yudao.module.iot.convert.thingmodel.IotProductThingModelConvert;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.thingmodel.vo.IotThingModelSaveReqVO;
import cn.iocoder.yudao.module.iot.convert.thingmodel.IotThingModelConvert;
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotProductThingModelDO;
import cn.iocoder.yudao.module.iot.dal.mysql.thingmodel.IotProductThingModelMapper;
import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.dal.mysql.thingmodel.IotThingModelMapper;
import cn.iocoder.yudao.module.iot.enums.product.IotProductStatusEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.*;
import cn.iocoder.yudao.module.iot.service.product.IotProductService;
@ -36,17 +36,17 @@ import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
@Service
@Validated
@Slf4j
public class IotProductThingModelServiceImpl implements IotProductThingModelService {
public class IotThingModelServiceImpl implements IotThingModelService {
@Resource
private IotProductThingModelMapper productThingModelMapper;
private IotThingModelMapper thingModelMapper;
@Resource
private IotProductService productService;
@Override
@Transactional(rollbackFor = Exception.class)
public Long createProductThingModel(IotProductThingModelSaveReqVO createReqVO) {
public Long createThingModel(IotThingModelSaveReqVO createReqVO) {
// 1. 校验功能标识符在同一产品下是否唯一
validateIdentifierUnique(createReqVO.getProductId(), createReqVO.getIdentifier());
@ -60,11 +60,11 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
validateProductStatus(createReqVO.getProductId());
// 5. 插入数据库
IotProductThingModelDO thingModel = IotProductThingModelConvert.INSTANCE.convert(createReqVO);
productThingModelMapper.insert(thingModel);
IotThingModelDO thingModel = IotThingModelConvert.INSTANCE.convert(createReqVO);
thingModelMapper.insert(thingModel);
// 6. 如果创建的是属性需要更新默认的事件和服务
if (Objects.equals(createReqVO.getType(), IotProductThingModelTypeEnum.PROPERTY.getType())) {
if (Objects.equals(createReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
createDefaultEventsAndServices(createReqVO.getProductId(), createReqVO.getProductKey());
}
// TODO @puhui999: 服务和事件的情况 method 怎么设置在前端设置还是后端设置
@ -73,7 +73,7 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
@Override
@Transactional(rollbackFor = Exception.class)
public void updateProductThingModel(IotProductThingModelSaveReqVO updateReqVO) {
public void updateThingModel(IotThingModelSaveReqVO updateReqVO) {
// 1. 校验功能是否存在
validateProductThingModelMapperExists(updateReqVO.getId());
@ -84,20 +84,20 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
validateProductStatus(updateReqVO.getProductId());
// 4. 更新数据库
IotProductThingModelDO thingModel = IotProductThingModelConvert.INSTANCE.convert(updateReqVO);
productThingModelMapper.updateById(thingModel);
IotThingModelDO thingModel = IotThingModelConvert.INSTANCE.convert(updateReqVO);
thingModelMapper.updateById(thingModel);
// 5. 如果更新的是属性需要更新默认的事件和服务
if (Objects.equals(updateReqVO.getType(), IotProductThingModelTypeEnum.PROPERTY.getType())) {
if (Objects.equals(updateReqVO.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
createDefaultEventsAndServices(updateReqVO.getProductId(), updateReqVO.getProductKey());
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteProductThingModel(Long id) {
public void deleteThingModel(Long id) {
// 1. 校验功能是否存在
IotProductThingModelDO thingModel = productThingModelMapper.selectById(id);
IotThingModelDO thingModel = thingModelMapper.selectById(id);
if (thingModel == null) {
throw exception(THING_MODEL_NOT_EXISTS);
}
@ -106,32 +106,32 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
validateProductStatus(thingModel.getProductId());
// 2. 删除功能
productThingModelMapper.deleteById(id);
thingModelMapper.deleteById(id);
// 3. 如果删除的是属性需要更新默认的事件和服务
if (Objects.equals(thingModel.getType(), IotProductThingModelTypeEnum.PROPERTY.getType())) {
if (Objects.equals(thingModel.getType(), IotThingModelTypeEnum.PROPERTY.getType())) {
createDefaultEventsAndServices(thingModel.getProductId(), thingModel.getProductKey());
}
}
@Override
public IotProductThingModelDO getProductThingModel(Long id) {
return productThingModelMapper.selectById(id);
public IotThingModelDO getThingModel(Long id) {
return thingModelMapper.selectById(id);
}
@Override
public List<IotProductThingModelDO> getProductThingModelListByProductId(Long productId) {
return productThingModelMapper.selectListByProductId(productId);
public List<IotThingModelDO> getThingModelListByProductId(Long productId) {
return thingModelMapper.selectListByProductId(productId);
}
@Override
public PageResult<IotProductThingModelDO> getProductThingModelPage(IotProductThingModelPageReqVO pageReqVO) {
return productThingModelMapper.selectPage(pageReqVO);
public PageResult<IotThingModelDO> getProductThingModelPage(IotThingModelPageReqVO pageReqVO) {
return thingModelMapper.selectPage(pageReqVO);
}
@Override
public List<IotProductThingModelDO> getProductThingModelListByProductKey(String productKey) {
return productThingModelMapper.selectListByProductKey(productKey);
public List<IotThingModelDO> getProductThingModelListByProductKey(String productKey) {
return thingModelMapper.selectListByProductKey(productKey);
}
/**
@ -140,13 +140,13 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
* @param id 功能编号
*/
private void validateProductThingModelMapperExists(Long id) {
if (productThingModelMapper.selectById(id) == null) {
if (thingModelMapper.selectById(id) == null) {
throw exception(THING_MODEL_NOT_EXISTS);
}
}
private void validateIdentifierUniqueForUpdate(Long id, Long productId, String identifier) {
IotProductThingModelDO thingModel = productThingModelMapper.selectByProductIdAndIdentifier(productId, identifier);
IotThingModelDO thingModel = thingModelMapper.selectByProductIdAndIdentifier(productId, identifier);
if (thingModel != null && ObjectUtil.notEqual(thingModel.getId(), id)) {
throw exception(THING_MODEL_IDENTIFIER_EXISTS);
}
@ -167,14 +167,14 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
}
private void validateNameUnique(Long productId, String name) {
IotProductThingModelDO thingModel = productThingModelMapper.selectByProductIdAndName(productId, name);
IotThingModelDO thingModel = thingModelMapper.selectByProductIdAndName(productId, name);
if (thingModel != null) {
throw exception(THING_MODEL_NAME_EXISTS);
}
}
private void validateIdentifierUnique(Long productId, String identifier) {
IotProductThingModelDO thingModel = productThingModelMapper.selectByProductIdAndIdentifier(productId, identifier);
IotThingModelDO thingModel = thingModelMapper.selectByProductIdAndIdentifier(productId, identifier);
if (thingModel != null) {
throw exception(THING_MODEL_IDENTIFIER_EXISTS);
}
@ -185,11 +185,11 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
*/
public void createDefaultEventsAndServices(Long productId, String productKey) {
// 1. 获取当前属性列表
List<IotProductThingModelDO> properties = productThingModelMapper
.selectListByProductIdAndType(productId, IotProductThingModelTypeEnum.PROPERTY.getType());
List<IotThingModelDO> properties = thingModelMapper
.selectListByProductIdAndType(productId, IotThingModelTypeEnum.PROPERTY.getType());
// 2. 生成新的事件和服务列表
List<IotProductThingModelDO> newThingModels = new ArrayList<>();
List<IotThingModelDO> newThingModels = new ArrayList<>();
// 2.1 生成属性上报事件
ThingModelEvent propertyPostEvent = generatePropertyPostEvent(properties);
if (propertyPostEvent != null) {
@ -203,14 +203,14 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
// 2.3 生成属性获取服务
ThingModelService propertyGetService = generatePropertyGetService(properties);
if (propertyGetService != null) {
newThingModels.add(buildServiceThingModelDO(productId, productKey, propertyGetService,"属性获取服务"));
newThingModels.add(buildServiceThingModelDO(productId, productKey, propertyGetService, "属性获取服务"));
}
// 3.1 获取数据库中的默认的旧事件和服务列表
List<IotProductThingModelDO> oldThingModels = productThingModelMapper.selectListByProductIdAndIdentifiersAndTypes(
List<IotThingModelDO> oldThingModels = thingModelMapper.selectListByProductIdAndIdentifiersAndTypes(
productId,
Arrays.asList("post", "set", "get"),
Arrays.asList(IotProductThingModelTypeEnum.EVENT.getType(), IotProductThingModelTypeEnum.SERVICE.getType())
Arrays.asList(IotThingModelTypeEnum.EVENT.getType(), IotThingModelTypeEnum.SERVICE.getType())
);
// 3.2 创建默认的事件和服务
createDefaultEventsAndServices(oldThingModels, newThingModels);
@ -219,10 +219,10 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
/**
* 创建默认的事件和服务
*/
private void createDefaultEventsAndServices(List<IotProductThingModelDO> oldThingModels,
List<IotProductThingModelDO> newThingModels) {
private void createDefaultEventsAndServices(List<IotThingModelDO> oldThingModels,
List<IotThingModelDO> newThingModels) {
// 使用 diffList 方法比较新旧列表
List<List<IotProductThingModelDO>> diffResult = diffList(oldThingModels, newThingModels,
List<List<IotThingModelDO>> diffResult = diffList(oldThingModels, newThingModels,
(oldVal, newVal) -> {
// 继续使用 identifier type 进行比较这样可以准确地匹配对应的功能对象
boolean same = Objects.equals(oldVal.getIdentifier(), newVal.getIdentifier())
@ -234,40 +234,40 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
});
// 批量添加修改删除
if (CollUtil.isNotEmpty(diffResult.get(0))) {
productThingModelMapper.insertBatch(diffResult.get(0));
thingModelMapper.insertBatch(diffResult.get(0));
}
if (CollUtil.isNotEmpty(diffResult.get(1))) {
productThingModelMapper.updateBatch(diffResult.get(1));
thingModelMapper.updateBatch(diffResult.get(1));
}
if (CollUtil.isNotEmpty(diffResult.get(2))) {
productThingModelMapper.deleteByIds(convertSet(diffResult.get(2), IotProductThingModelDO::getId));
thingModelMapper.deleteByIds(convertSet(diffResult.get(2), IotThingModelDO::getId));
}
}
/**
* 构建事件功能对象
*/
private IotProductThingModelDO buildEventThingModelDO(Long productId, String productKey,
ThingModelEvent event, String description) {
return new IotProductThingModelDO().setProductId(productId).setProductKey(productKey)
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(IotProductThingModelTypeEnum.EVENT.getType()).setEvent(event);
.setType(IotThingModelTypeEnum.EVENT.getType()).setEvent(event);
}
/**
* 构建服务功能对象
*/
private IotProductThingModelDO buildServiceThingModelDO(Long productId, String productKey,
ThingModelService service, String description) {
return new IotProductThingModelDO().setProductId(productId).setProductKey(productKey)
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(IotProductThingModelTypeEnum.SERVICE.getType()).setService(service);
.setType(IotThingModelTypeEnum.SERVICE.getType()).setService(service);
}
/**
* 生成属性上报事件
*/
private ThingModelEvent generatePropertyPostEvent(List<IotProductThingModelDO> thingModels) {
private ThingModelEvent generatePropertyPostEvent(List<IotThingModelDO> thingModels) {
// 没有属性则不生成
if (CollUtil.isEmpty(thingModels)) {
return null;
@ -275,17 +275,17 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
// 生成属性上报事件
return new ThingModelEvent().setIdentifier("post").setName("属性上报").setMethod("thing.event.property.post")
.setType(IotProductThingModelServiceEventTypeEnum.INFO.getType())
.setOutputParams(buildInputOutputParam(thingModels, IotProductThingModelParamDirectionEnum.OUTPUT));
.setType(IotThingModelServiceEventTypeEnum.INFO.getType())
.setOutputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.OUTPUT));
}
/**
* 生成属性设置服务
*/
private ThingModelService generatePropertySetService(List<IotProductThingModelDO> thingModels) {
private ThingModelService generatePropertySetService(List<IotThingModelDO> thingModels) {
// 1.1 过滤出所有可写属性
thingModels = filterList(thingModels, thingModel ->
IotProductThingModelAccessModeEnum.READ_WRITE.getMode().equals(thingModel.getProperty().getAccessMode()));
IotThingModelAccessModeEnum.READ_WRITE.getMode().equals(thingModel.getProperty().getAccessMode()));
// 1.2 没有可写属性则不生成
if (CollUtil.isEmpty(thingModels)) {
return null;
@ -293,15 +293,15 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
// 2. 生成属性设置服务
return new ThingModelService().setIdentifier("set").setName("属性设置").setMethod("thing.service.property.set")
.setCallType(IotProductThingModelServiceCallTypeEnum.ASYNC.getType())
.setInputParams(buildInputOutputParam(thingModels, IotProductThingModelParamDirectionEnum.INPUT))
.setCallType(IotThingModelServiceCallTypeEnum.ASYNC.getType())
.setInputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.INPUT))
.setOutputParams(Collections.emptyList()); // 属性设置服务一般不需要输出参数
}
/**
* 生成属性获取服务
*/
private ThingModelService generatePropertyGetService(List<IotProductThingModelDO> thingModels) {
private ThingModelService generatePropertyGetService(List<IotThingModelDO> thingModels) {
// 1.1 没有属性则不生成
if (CollUtil.isEmpty(thingModels)) {
return null;
@ -309,9 +309,9 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
// 1.2 生成属性获取服务
return new ThingModelService().setIdentifier("get").setName("属性获取").setMethod("thing.service.property.get")
.setCallType(IotProductThingModelServiceCallTypeEnum.ASYNC.getType())
.setInputParams(buildInputOutputParam(thingModels, IotProductThingModelParamDirectionEnum.INPUT))
.setOutputParams(buildInputOutputParam(thingModels, IotProductThingModelParamDirectionEnum.OUTPUT));
.setCallType(IotThingModelServiceCallTypeEnum.ASYNC.getType())
.setInputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.INPUT))
.setOutputParams(buildInputOutputParam(thingModels, IotThingModelParamDirectionEnum.OUTPUT));
}
/**
@ -320,10 +320,10 @@ public class IotProductThingModelServiceImpl implements IotProductThingModelServ
* @param thingModels 属性列表
* @return 输入/输出参数列表
*/
private List<ThingModelInputOutputParam> buildInputOutputParam(List<IotProductThingModelDO> thingModels,
IotProductThingModelParamDirectionEnum direction) {
private List<ThingModelParam> buildInputOutputParam(List<IotThingModelDO> thingModels,
IotThingModelParamDirectionEnum direction) {
return convertList(thingModels, thingModel ->
BeanUtils.toBean(thingModel.getProperty(), ThingModelInputOutputParam.class).setParaOrder(0) // TODO @puhui999: 先搞个默认值看看怎么个事
BeanUtils.toBean(thingModel.getProperty(), ThingModelParam.class).setParaOrder(0) // TODO @puhui999: 先搞个默认值看看怎么个事
.setDirection(direction.getDirection()));
}