Merge remote-tracking branch 'yudao/feature/iot' into iot
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.product;
|
||||
|
||||
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.product.vo.script.*;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
|
||||
import cn.iocoder.yudao.module.iot.service.product.IotProductScriptService;
|
||||
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-script")
|
||||
@Validated
|
||||
public class IotProductScriptController {
|
||||
|
||||
@Resource
|
||||
private IotProductScriptService productScriptService;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建产品脚本")
|
||||
@PreAuthorize("@ss.hasPermission('iot:product-script:create')")
|
||||
public CommonResult<Long> createProductScript(@Valid @RequestBody IotProductScriptSaveReqVO createReqVO) {
|
||||
return success(productScriptService.createProductScript(createReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
@Operation(summary = "更新产品脚本")
|
||||
@PreAuthorize("@ss.hasPermission('iot:product-script:update')")
|
||||
public CommonResult<Boolean> updateProductScript(@Valid @RequestBody IotProductScriptSaveReqVO updateReqVO) {
|
||||
productScriptService.updateProductScript(updateReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@Operation(summary = "删除产品脚本")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('iot:product-script:delete')")
|
||||
public CommonResult<Boolean> deleteProductScript(@RequestParam("id") Long id) {
|
||||
productScriptService.deleteProductScript(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得产品脚本详情")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('iot:product-script:query')")
|
||||
public CommonResult<IotProductScriptRespVO> getProductScript(@RequestParam("id") Long id) {
|
||||
IotProductScriptDO productScript = productScriptService.getProductScript(id);
|
||||
return success(BeanUtils.toBean(productScript, IotProductScriptRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/list-by-product")
|
||||
@Operation(summary = "获得产品的脚本列表")
|
||||
@Parameter(name = "productId", description = "产品编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('iot:product-script:query')")
|
||||
public CommonResult<List<IotProductScriptRespVO>> getProductScriptListByProductId(
|
||||
@RequestParam("productId") Long productId) {
|
||||
List<IotProductScriptDO> list = productScriptService.getProductScriptListByProductId(productId);
|
||||
return success(BeanUtils.toBean(list, IotProductScriptRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得产品脚本分页")
|
||||
@PreAuthorize("@ss.hasPermission('iot:product-script:query')")
|
||||
public CommonResult<PageResult<IotProductScriptRespVO>> getProductScriptPage(
|
||||
@Valid IotProductScriptPageReqVO pageReqVO) {
|
||||
PageResult<IotProductScriptDO> pageResult = productScriptService.getProductScriptPage(pageReqVO);
|
||||
return success(BeanUtils.toBean(pageResult, IotProductScriptRespVO.class));
|
||||
}
|
||||
|
||||
@PostMapping("/test")
|
||||
@Operation(summary = "测试产品脚本")
|
||||
@PreAuthorize("@ss.hasPermission('iot:product-script:test')")
|
||||
public CommonResult<IotProductScriptTestRespVO> testProductScript(
|
||||
@Valid @RequestBody IotProductScriptTestReqVO testReqVO) {
|
||||
return success(productScriptService.testProductScript(testReqVO));
|
||||
}
|
||||
|
||||
@PutMapping("/update-status")
|
||||
@Operation(summary = "更新产品脚本状态")
|
||||
@PreAuthorize("@ss.hasPermission('iot:product-script:update')")
|
||||
public CommonResult<Boolean> updateProductScriptStatus(
|
||||
@Valid @RequestBody IotProductScriptUpdateStatusReqVO updateStatusReqVO) {
|
||||
productScriptService.updateProductScriptStatus(updateStatusReqVO.getId(), updateStatusReqVO.getStatus());
|
||||
return success(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import cn.iocoder.yudao.module.iot.enums.product.IotProductScriptLanguageEnum;
|
||||
import cn.iocoder.yudao.module.iot.enums.product.IotProductScriptStatusEnum;
|
||||
import cn.iocoder.yudao.module.iot.enums.product.IotProductScriptTypeEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 产品脚本信息分页 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class IotProductScriptPageReqVO extends PageParam {
|
||||
|
||||
@Schema(description = "产品ID", example = "28277")
|
||||
private Long productId;
|
||||
|
||||
@Schema(description = "产品唯一标识符")
|
||||
private String productKey;
|
||||
|
||||
@Schema(description = "脚本类型", example = "1")
|
||||
@InEnum(IotProductScriptTypeEnum.class)
|
||||
private Integer scriptType;
|
||||
|
||||
@Schema(description = "脚本语言")
|
||||
@InEnum(IotProductScriptLanguageEnum.class)
|
||||
private String scriptLanguage;
|
||||
|
||||
@Schema(description = "状态", example = "0")
|
||||
@InEnum(IotProductScriptStatusEnum.class)
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "备注说明", example = "你说的对")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "最后测试时间")
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
private LocalDateTime[] lastTestTime;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
private LocalDateTime[] createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 产品脚本信息 Response VO")
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
public class IotProductScriptRespVO {
|
||||
|
||||
@Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "26565")
|
||||
@ExcelProperty("主键")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "28277")
|
||||
@ExcelProperty("产品ID")
|
||||
private Long productId;
|
||||
|
||||
@Schema(description = "产品唯一标识符", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("产品唯一标识符")
|
||||
private String productKey;
|
||||
|
||||
@Schema(description = "脚本类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@ExcelProperty("脚本类型")
|
||||
private Integer scriptType;
|
||||
|
||||
@Schema(description = "脚本内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("脚本内容")
|
||||
private String scriptContent;
|
||||
|
||||
@Schema(description = "脚本语言", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("脚本语言")
|
||||
private String scriptLanguage;
|
||||
|
||||
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
|
||||
@ExcelProperty("状态")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "备注说明", example = "你说的对")
|
||||
@ExcelProperty("备注说明")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "最后测试时间")
|
||||
@ExcelProperty("最后测试时间")
|
||||
private LocalDateTime lastTestTime;
|
||||
|
||||
@Schema(description = "最后测试结果(0=失败 1=成功)")
|
||||
@ExcelProperty("最后测试结果(0=失败 1=成功)")
|
||||
private Integer lastTestResult;
|
||||
|
||||
@Schema(description = "脚本版本号", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("脚本版本号")
|
||||
private Integer version;
|
||||
|
||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@ExcelProperty("创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import cn.iocoder.yudao.module.iot.enums.product.IotProductScriptLanguageEnum;
|
||||
import cn.iocoder.yudao.module.iot.enums.product.IotProductScriptStatusEnum;
|
||||
import cn.iocoder.yudao.module.iot.enums.product.IotProductScriptTypeEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 产品脚本信息新增/修改 Request VO")
|
||||
@Data
|
||||
public class IotProductScriptSaveReqVO {
|
||||
|
||||
@Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "26565")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "28277")
|
||||
@NotNull(message = "产品ID不能为空")
|
||||
private Long productId;
|
||||
|
||||
@Schema(description = "产品唯一标识符", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "产品唯一标识符不能为空")
|
||||
private String productKey;
|
||||
|
||||
@Schema(description = "脚本类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@NotNull(message = "脚本类型不能为空")
|
||||
@InEnum(IotProductScriptTypeEnum.class)
|
||||
private Integer scriptType;
|
||||
|
||||
@Schema(description = "脚本内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "脚本内容不能为空")
|
||||
private String scriptContent;
|
||||
|
||||
@Schema(description = "脚本语言", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "脚本语言不能为空")
|
||||
@InEnum(IotProductScriptLanguageEnum.class)
|
||||
private String scriptLanguage;
|
||||
|
||||
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
|
||||
@NotNull(message = "状态不能为空")
|
||||
@InEnum(IotProductScriptStatusEnum.class)
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "备注说明", example = "你说的对")
|
||||
private String remark;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import cn.iocoder.yudao.module.iot.enums.product.IotProductScriptTypeEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 产品脚本测试 Request VO")
|
||||
@Data
|
||||
public class IotProductScriptTestReqVO {
|
||||
|
||||
@Schema(description = "脚本ID,如果已保存脚本则传入", example = "1024")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
|
||||
@NotNull(message = "产品ID不能为空")
|
||||
private Long productId;
|
||||
|
||||
@Schema(description = "脚本类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@NotNull(message = "脚本类型不能为空")
|
||||
@InEnum(value = IotProductScriptTypeEnum.class)
|
||||
private Integer scriptType;
|
||||
|
||||
@Schema(description = "脚本内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "脚本内容不能为空")
|
||||
private String scriptContent;
|
||||
|
||||
@Schema(description = "脚本语言", requiredMode = Schema.RequiredMode.REQUIRED, example = "javascript")
|
||||
@NotEmpty(message = "脚本语言不能为空")
|
||||
private String scriptLanguage;
|
||||
|
||||
@Schema(description = "测试输入数据", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "测试输入数据不能为空")
|
||||
private String testInput;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 产品脚本测试 Response VO")
|
||||
@Data
|
||||
public class IotProductScriptTestRespVO {
|
||||
|
||||
@Schema(description = "测试是否成功", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private Boolean success;
|
||||
|
||||
@Schema(description = "测试结果输出")
|
||||
private Object output;
|
||||
|
||||
@Schema(description = "错误消息,失败时返回")
|
||||
private String errorMessage;
|
||||
|
||||
@Schema(description = "执行耗时(毫秒)")
|
||||
private Long executionTimeMs;
|
||||
|
||||
// 静态工厂方法 - 成功
|
||||
public static IotProductScriptTestRespVO success(Object output, Long executionTimeMs) {
|
||||
IotProductScriptTestRespVO respVO = new IotProductScriptTestRespVO();
|
||||
respVO.setSuccess(true);
|
||||
respVO.setOutput(output);
|
||||
respVO.setExecutionTimeMs(executionTimeMs);
|
||||
return respVO;
|
||||
}
|
||||
|
||||
// 静态工厂方法 - 失败
|
||||
public static IotProductScriptTestRespVO error(String errorMessage, Long executionTimeMs) {
|
||||
IotProductScriptTestRespVO respVO = new IotProductScriptTestRespVO();
|
||||
respVO.setSuccess(false);
|
||||
respVO.setErrorMessage(errorMessage);
|
||||
respVO.setExecutionTimeMs(executionTimeMs);
|
||||
return respVO;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import cn.iocoder.yudao.module.iot.enums.product.IotProductScriptStatusEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "管理后台 - IoT 产品脚本状态更新 Request VO")
|
||||
@Data
|
||||
public class IotProductScriptUpdateStatusReqVO {
|
||||
|
||||
@Schema(description = "脚本ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
@NotNull(message = "脚本ID不能为空")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
|
||||
@NotNull(message = "状态不能为空")
|
||||
@InEnum(IotProductScriptStatusEnum.class)
|
||||
private Integer status;
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
// TODO @芋艿:规则场景 要不要,统一改成 场景联动
|
||||
@Tag(name = "管理后台 - IoT 规则场景")
|
||||
@RestController
|
||||
@RequestMapping("/iot/rule-scene")
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package cn.iocoder.yudao.module.iot.controller.admin.rule.vo.scene;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
@@ -24,6 +26,7 @@ public class IotRuleScenePageReqVO extends PageParam {
|
||||
private String description;
|
||||
|
||||
@Schema(description = "场景状态", example = "1")
|
||||
@InEnum(CommonStatusEnum.class)
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.rule.IotRuleSceneActionTypeEnum;
|
||||
import lombok.Data;
|
||||
|
||||
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||
/**
|
||||
* 执行器配置
|
||||
*
|
||||
|
||||
@@ -9,6 +9,7 @@ import lombok.Data;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||
/**
|
||||
* 执行设备控制
|
||||
*
|
||||
|
||||
@@ -6,6 +6,7 @@ import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||
/**
|
||||
* 触发条件
|
||||
*
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
|
||||
import cn.iocoder.yudao.module.iot.enums.rule.IotRuleSceneTriggerConditionParameterOperatorEnum;
|
||||
import lombok.Data;
|
||||
|
||||
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||
/**
|
||||
* 触发条件参数
|
||||
*
|
||||
|
||||
@@ -7,6 +7,7 @@ import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||
/**
|
||||
* 触发器配置
|
||||
*
|
||||
|
||||
@@ -61,15 +61,15 @@ public class IotThingModelController {
|
||||
return success(BeanUtils.toBean(thingModel, IotThingModelRespVO.class));
|
||||
}
|
||||
|
||||
// TODO @puhui999:要不叫 get-tsl,去掉 product-id;后续,把
|
||||
@GetMapping("/tsl-by-product-id")
|
||||
@Operation(summary = "获得产品物模型 TSL")
|
||||
@Parameter(name = "productId", description = "产品ID", required = true, example = "1024")
|
||||
@Parameter(name = "productId", description = "产品 ID", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('iot:thing-model:query')")
|
||||
public CommonResult<IotThingModelTSLRespVO> getThingModelTslByProductId(@RequestParam("productId") Long productId) {
|
||||
return success(thingModelService.getThingModelTslByProductId(productId));
|
||||
}
|
||||
|
||||
// TODO @puhui @super:getThingModelListByProductId 和 getThingModelListByProductId 可以融合么?
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "获得产品物模型列表")
|
||||
@PreAuthorize("@ss.hasPermission('iot:thing-model:query')")
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package cn.iocoder.yudao.module.iot.dal.dataobject.product;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* IoT 产品脚本信息 DO
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@TableName("iot_product_script")
|
||||
@KeySequence("iot_product_script_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class IotProductScriptDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 产品ID
|
||||
*/
|
||||
private Long productId;
|
||||
/**
|
||||
* 产品唯一标识符
|
||||
*/
|
||||
private String productKey;
|
||||
/**
|
||||
* 脚本类型(property_parser=属性解析,event_parser=事件解析,command_encoder=命令编码)
|
||||
*/
|
||||
private String scriptType;
|
||||
/**
|
||||
* 脚本内容
|
||||
*/
|
||||
private String scriptContent;
|
||||
/**
|
||||
* 脚本语言
|
||||
*/
|
||||
private String scriptLanguage;
|
||||
/**
|
||||
* 状态(0=禁用 1=启用)
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 备注说明
|
||||
*/
|
||||
private String remark;
|
||||
/**
|
||||
* 最后测试时间
|
||||
*/
|
||||
private LocalDateTime lastTestTime;
|
||||
/**
|
||||
* 最后测试结果(0=失败 1=成功)
|
||||
*/
|
||||
private Integer lastTestResult;
|
||||
/**
|
||||
* 脚本版本号
|
||||
*/
|
||||
private Integer version;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.iocoder.yudao.module.iot.dal.mysql.product;
|
||||
|
||||
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.product.vo.script.IotProductScriptPageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* IoT 产品脚本信息 Mapper
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface IotProductScriptMapper extends BaseMapperX<IotProductScriptDO> {
|
||||
|
||||
default PageResult<IotProductScriptDO> selectPage(IotProductScriptPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<IotProductScriptDO>()
|
||||
.eqIfPresent(IotProductScriptDO::getProductId, reqVO.getProductId())
|
||||
.eqIfPresent(IotProductScriptDO::getProductKey, reqVO.getProductKey())
|
||||
.eqIfPresent(IotProductScriptDO::getScriptType, reqVO.getScriptType())
|
||||
.eqIfPresent(IotProductScriptDO::getScriptLanguage, reqVO.getScriptLanguage())
|
||||
.eqIfPresent(IotProductScriptDO::getStatus, reqVO.getStatus())
|
||||
.eqIfPresent(IotProductScriptDO::getRemark, reqVO.getRemark())
|
||||
.betweenIfPresent(IotProductScriptDO::getLastTestTime, reqVO.getLastTestTime())
|
||||
.betweenIfPresent(IotProductScriptDO::getCreateTime, reqVO.getCreateTime())
|
||||
.orderByDesc(IotProductScriptDO::getId));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package cn.iocoder.yudao.module.iot.service.product;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptPageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptTestReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptTestRespVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* IoT 产品脚本信息 Service 接口
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public interface IotProductScriptService {
|
||||
|
||||
/**
|
||||
* 创建IoT 产品脚本信息
|
||||
*
|
||||
* @param createReqVO 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createProductScript(@Valid IotProductScriptSaveReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新IoT 产品脚本信息
|
||||
*
|
||||
* @param updateReqVO 更新信息
|
||||
*/
|
||||
void updateProductScript(@Valid IotProductScriptSaveReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 删除IoT 产品脚本信息
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteProductScript(Long id);
|
||||
|
||||
/**
|
||||
* 获得IoT 产品脚本信息
|
||||
*
|
||||
* @param id 编号
|
||||
* @return IoT 产品脚本信息
|
||||
*/
|
||||
IotProductScriptDO getProductScript(Long id);
|
||||
|
||||
/**
|
||||
* 获得IoT 产品脚本信息分页
|
||||
*
|
||||
* @param pageReqVO 分页查询
|
||||
* @return IoT 产品脚本信息分页
|
||||
*/
|
||||
PageResult<IotProductScriptDO> getProductScriptPage(IotProductScriptPageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 获取产品的脚本列表
|
||||
*
|
||||
* @param productId 产品ID
|
||||
* @return 脚本列表
|
||||
*/
|
||||
List<IotProductScriptDO> getProductScriptListByProductId(Long productId);
|
||||
|
||||
/**
|
||||
* 测试产品脚本
|
||||
*
|
||||
* @param testReqVO 测试请求
|
||||
* @return 测试结果
|
||||
*/
|
||||
IotProductScriptTestRespVO testProductScript(@Valid IotProductScriptTestReqVO testReqVO);
|
||||
|
||||
/**
|
||||
* 更新产品脚本状态
|
||||
*
|
||||
* @param id 脚本ID
|
||||
* @param status 状态
|
||||
*/
|
||||
void updateProductScriptStatus(Long id, Integer status);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package cn.iocoder.yudao.module.iot.service.product;
|
||||
|
||||
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.product.vo.script.IotProductScriptPageReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptSaveReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptTestReqVO;
|
||||
import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptTestRespVO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
|
||||
import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductScriptMapper;
|
||||
import cn.iocoder.yudao.module.iot.plugin.script.context.PluginScriptContext;
|
||||
import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_NOT_EXISTS;
|
||||
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_SCRIPT_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* IoT 产品脚本信息 Service 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class IotProductScriptServiceImpl implements IotProductScriptService {
|
||||
|
||||
@Resource
|
||||
private IotProductScriptMapper productScriptMapper;
|
||||
|
||||
@Resource
|
||||
private IotProductService productService;
|
||||
|
||||
@Resource
|
||||
private ScriptService scriptService;
|
||||
|
||||
@Override
|
||||
public Long createProductScript(IotProductScriptSaveReqVO createReqVO) {
|
||||
// 验证产品是否存在
|
||||
validateProductExists(createReqVO.getProductId());
|
||||
|
||||
// 插入
|
||||
IotProductScriptDO productScript = BeanUtils.toBean(createReqVO, IotProductScriptDO.class);
|
||||
// 初始化版本为1
|
||||
productScript.setVersion(1);
|
||||
// 初始化测试相关字段
|
||||
productScript.setLastTestResult(null);
|
||||
productScript.setLastTestTime(null);
|
||||
productScriptMapper.insert(productScript);
|
||||
// 返回
|
||||
return productScript.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProductScript(IotProductScriptSaveReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
validateProductScriptExists(updateReqVO.getId());
|
||||
|
||||
// 获取旧的记录,保留版本号和测试信息
|
||||
IotProductScriptDO oldScript = getProductScript(updateReqVO.getId());
|
||||
|
||||
// 更新
|
||||
IotProductScriptDO updateObj = BeanUtils.toBean(updateReqVO, IotProductScriptDO.class);
|
||||
// 更新版本号
|
||||
updateObj.setVersion(oldScript.getVersion() + 1);
|
||||
// 保留测试相关信息
|
||||
updateObj.setLastTestTime(oldScript.getLastTestTime());
|
||||
updateObj.setLastTestResult(oldScript.getLastTestResult());
|
||||
productScriptMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteProductScript(Long id) {
|
||||
// 校验存在
|
||||
validateProductScriptExists(id);
|
||||
// 删除
|
||||
productScriptMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private void validateProductScriptExists(Long id) {
|
||||
if (productScriptMapper.selectById(id) == null) {
|
||||
throw exception(PRODUCT_SCRIPT_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateProductExists(Long productId) {
|
||||
IotProductDO product = productService.getProduct(productId);
|
||||
if (product == null) {
|
||||
throw exception(PRODUCT_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IotProductScriptDO getProductScript(Long id) {
|
||||
return productScriptMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<IotProductScriptDO> getProductScriptPage(IotProductScriptPageReqVO pageReqVO) {
|
||||
return productScriptMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IotProductScriptDO> getProductScriptListByProductId(Long productId) {
|
||||
return productScriptMapper.selectList(new LambdaQueryWrapper<IotProductScriptDO>()
|
||||
.eq(IotProductScriptDO::getProductId, productId)
|
||||
.orderByDesc(IotProductScriptDO::getId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public IotProductScriptTestRespVO testProductScript(IotProductScriptTestReqVO testReqVO) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
try {
|
||||
// 验证产品是否存在
|
||||
validateProductExists(testReqVO.getProductId());
|
||||
|
||||
// 根据ID获取已保存的脚本(如果有)
|
||||
IotProductScriptDO existingScript = null;
|
||||
if (testReqVO.getId() != null) {
|
||||
existingScript = getProductScript(testReqVO.getId());
|
||||
}
|
||||
|
||||
// 创建测试上下文
|
||||
PluginScriptContext context = new PluginScriptContext();
|
||||
IotProductDO product = productService.getProduct(testReqVO.getProductId());
|
||||
|
||||
// 设置设备上下文(使用产品信息,没有具体设备)
|
||||
context.withDeviceContext(product.getProductKey(), null);
|
||||
|
||||
// 设置输入参数
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("input", testReqVO.getTestInput());
|
||||
params.put("productKey", product.getProductKey());
|
||||
params.put("scriptType", testReqVO.getScriptType());
|
||||
|
||||
// 根据脚本类型设置特定参数
|
||||
switch (testReqVO.getScriptType()) {
|
||||
case 1: // PROPERTY_PARSER
|
||||
params.put("method", "property");
|
||||
break;
|
||||
case 2: // EVENT_PARSER
|
||||
params.put("method", "event");
|
||||
params.put("identifier", "default");
|
||||
break;
|
||||
case 3: // COMMAND_ENCODER
|
||||
params.put("method", "command");
|
||||
break;
|
||||
default:
|
||||
// 默认不添加额外参数
|
||||
}
|
||||
|
||||
// 添加所有参数到上下文
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
context.setParameter(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
// 执行脚本
|
||||
Object result = scriptService.executeScript(
|
||||
testReqVO.getScriptLanguage(),
|
||||
testReqVO.getScriptContent(),
|
||||
context);
|
||||
|
||||
// 更新测试结果(如果是已保存的脚本)
|
||||
if (existingScript != null) {
|
||||
IotProductScriptDO updateObj = new IotProductScriptDO();
|
||||
updateObj.setId(existingScript.getId());
|
||||
updateObj.setLastTestTime(LocalDateTime.now());
|
||||
updateObj.setLastTestResult(1); // 1表示成功
|
||||
productScriptMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
long executionTime = System.currentTimeMillis() - startTime;
|
||||
return IotProductScriptTestRespVO.success(result, executionTime);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("[testProductScript][测试脚本异常]", e);
|
||||
|
||||
// 如果是已保存的脚本,更新测试失败状态
|
||||
if (testReqVO.getId() != null) {
|
||||
try {
|
||||
IotProductScriptDO updateObj = new IotProductScriptDO();
|
||||
updateObj.setId(testReqVO.getId());
|
||||
updateObj.setLastTestTime(LocalDateTime.now());
|
||||
updateObj.setLastTestResult(0); // 0表示失败
|
||||
productScriptMapper.updateById(updateObj);
|
||||
} catch (Exception ex) {
|
||||
log.error("[testProductScript][更新脚本测试结果异常]", ex);
|
||||
}
|
||||
}
|
||||
|
||||
long executionTime = System.currentTimeMillis() - startTime;
|
||||
return IotProductScriptTestRespVO.error(e.getMessage(), executionTime);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProductScriptStatus(Long id, Integer status) {
|
||||
// 校验存在
|
||||
validateProductScriptExists(id);
|
||||
|
||||
// 更新状态
|
||||
IotProductScriptDO updateObj = new IotProductScriptDO();
|
||||
updateObj.setId(id);
|
||||
updateObj.setStatus(status);
|
||||
productScriptMapper.updateById(updateObj);
|
||||
}
|
||||
}
|
||||
@@ -151,26 +151,27 @@ public class IotThingModelServiceImpl implements IotThingModelService {
|
||||
return thingModelMapper.selectList(reqVO);
|
||||
}
|
||||
|
||||
// TODO @puhui999:这个转换,放在 controller 貌似也行?
|
||||
@Override
|
||||
public IotThingModelTSLRespVO getThingModelTslByProductId(Long productId) {
|
||||
IotThingModelTSLRespVO tslRespVO = new IotThingModelTSLRespVO();
|
||||
// 1. 获得产品所有物模型定义
|
||||
List<IotThingModelDO> thingModelList = thingModelMapper.selectListByProductId(productId);
|
||||
if (CollUtil.isEmpty(thingModelList)) {
|
||||
List<IotThingModelDO> thingModels = thingModelMapper.selectListByProductId(productId);
|
||||
if (CollUtil.isEmpty(thingModels)) {
|
||||
return tslRespVO;
|
||||
}
|
||||
|
||||
// 2.1 设置公共部分参数
|
||||
IotThingModelDO thingModel = thingModelList.get(0);
|
||||
IotThingModelDO thingModel = thingModels.get(0);
|
||||
tslRespVO.setProductId(thingModel.getProductId()).setProductKey(thingModel.getProductKey());
|
||||
// 2.2 处理属性列表
|
||||
tslRespVO.setProperties(convertList(filterList(thingModelList, item ->
|
||||
tslRespVO.setProperties(convertList(filterList(thingModels, item ->
|
||||
ObjUtil.equal(IotThingModelTypeEnum.PROPERTY.getType(), item.getType())), IotThingModelDO::getProperty));
|
||||
// 2.3 处理服务列表
|
||||
tslRespVO.setServices(convertList(filterList(thingModelList, item ->
|
||||
tslRespVO.setServices(convertList(filterList(thingModels, item ->
|
||||
ObjUtil.equal(IotThingModelTypeEnum.SERVICE.getType(), item.getType())), IotThingModelDO::getService));
|
||||
// 2.4 处理事件列表
|
||||
tslRespVO.setEvents(convertList(filterList(thingModelList, item ->
|
||||
tslRespVO.setEvents(convertList(filterList(thingModels, item ->
|
||||
ObjUtil.equal(IotThingModelTypeEnum.EVENT.getType(), item.getType())), IotThingModelDO::getEvent));
|
||||
return tslRespVO;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user