Commit e26903b06ba68e56712e4c65a9f05128b0fd9cf2

Authored by 安浩浩
1 parent 87a43b83

【功能新增】IoT: 增加产品脚本管理功能,包括增删改查及测试接口

Showing 13 changed files with 758 additions and 1 deletions
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
@@ -75,4 +75,7 @@ public interface ErrorCodeConstants { @@ -75,4 +75,7 @@ public interface ErrorCodeConstants {
75 // ========== IoT 规则场景(场景联动) 1-050-011-000 ========== 75 // ========== IoT 规则场景(场景联动) 1-050-011-000 ==========
76 ErrorCode RULE_SCENE_NOT_EXISTS = new ErrorCode(1_050_011_000, "IoT 规则场景(场景联动)不存在"); 76 ErrorCode RULE_SCENE_NOT_EXISTS = new ErrorCode(1_050_011_000, "IoT 规则场景(场景联动)不存在");
77 77
  78 + // ========== IoT 产品脚本信息 1-050-012-000 ==========
  79 + ErrorCode PRODUCT_SCRIPT_NOT_EXISTS = new ErrorCode(1_050_012_000, "IoT 产品脚本信息不存在");
  80 +
78 } 81 }
79 \ No newline at end of file 82 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/pom.xml
@@ -69,6 +69,13 @@ @@ -69,6 +69,13 @@
69 <artifactId>yudao-spring-boot-starter-excel</artifactId> 69 <artifactId>yudao-spring-boot-starter-excel</artifactId>
70 </dependency> 70 </dependency>
71 71
  72 + <!-- 脚本插件相关 -->
  73 + <dependency>
  74 + <groupId>cn.iocoder.boot</groupId>
  75 + <artifactId>yudao-module-iot-plugin-script</artifactId>
  76 + <version>${revision}</version>
  77 + </dependency>
  78 +
72 <!-- 消息队列相关 --> 79 <!-- 消息队列相关 -->
73 <dependency> 80 <dependency>
74 <groupId>org.apache.rocketmq</groupId> 81 <groupId>org.apache.rocketmq</groupId>
@@ -87,7 +94,7 @@ @@ -87,7 +94,7 @@
87 </dependency> 94 </dependency>
88 95
89 <dependency> 96 <dependency>
90 - <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 --> 97 + <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 -->
91 <artifactId>pf4j-spring</artifactId> 98 <artifactId>pf4j-spring</artifactId>
92 </dependency> 99 </dependency>
93 100
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/IotProductScriptController.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.product;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  4 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  5 +import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.*;
  7 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
  8 +import cn.iocoder.yudao.module.iot.service.product.IotProductScriptService;
  9 +import io.swagger.v3.oas.annotations.Operation;
  10 +import io.swagger.v3.oas.annotations.Parameter;
  11 +import io.swagger.v3.oas.annotations.tags.Tag;
  12 +import jakarta.annotation.Resource;
  13 +import jakarta.validation.Valid;
  14 +import org.springframework.security.access.prepost.PreAuthorize;
  15 +import org.springframework.validation.annotation.Validated;
  16 +import org.springframework.web.bind.annotation.*;
  17 +
  18 +import java.util.List;
  19 +
  20 +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
  21 +
  22 +@Tag(name = "管理后台 - IoT 产品脚本信息")
  23 +@RestController
  24 +@RequestMapping("/iot/product-script")
  25 +@Validated
  26 +public class IotProductScriptController {
  27 +
  28 + @Resource
  29 + private IotProductScriptService productScriptService;
  30 +
  31 + @PostMapping("/create")
  32 + @Operation(summary = "创建产品脚本")
  33 + @PreAuthorize("@ss.hasPermission('iot:product-script:create')")
  34 + public CommonResult<Long> createProductScript(@Valid @RequestBody IotProductScriptSaveReqVO createReqVO) {
  35 + return success(productScriptService.createProductScript(createReqVO));
  36 + }
  37 +
  38 + @PutMapping("/update")
  39 + @Operation(summary = "更新产品脚本")
  40 + @PreAuthorize("@ss.hasPermission('iot:product-script:update')")
  41 + public CommonResult<Boolean> updateProductScript(@Valid @RequestBody IotProductScriptSaveReqVO updateReqVO) {
  42 + productScriptService.updateProductScript(updateReqVO);
  43 + return success(true);
  44 + }
  45 +
  46 + @DeleteMapping("/delete")
  47 + @Operation(summary = "删除产品脚本")
  48 + @Parameter(name = "id", description = "编号", required = true)
  49 + @PreAuthorize("@ss.hasPermission('iot:product-script:delete')")
  50 + public CommonResult<Boolean> deleteProductScript(@RequestParam("id") Long id) {
  51 + productScriptService.deleteProductScript(id);
  52 + return success(true);
  53 + }
  54 +
  55 + @GetMapping("/get")
  56 + @Operation(summary = "获得产品脚本详情")
  57 + @Parameter(name = "id", description = "编号", required = true, example = "1024")
  58 + @PreAuthorize("@ss.hasPermission('iot:product-script:query')")
  59 + public CommonResult<IotProductScriptRespVO> getProductScript(@RequestParam("id") Long id) {
  60 + IotProductScriptDO productScript = productScriptService.getProductScript(id);
  61 + return success(BeanUtils.toBean(productScript, IotProductScriptRespVO.class));
  62 + }
  63 +
  64 + @GetMapping("/list-by-product")
  65 + @Operation(summary = "获得产品的脚本列表")
  66 + @Parameter(name = "productId", description = "产品编号", required = true, example = "1024")
  67 + @PreAuthorize("@ss.hasPermission('iot:product-script:query')")
  68 + public CommonResult<List<IotProductScriptRespVO>> getProductScriptListByProductId(
  69 + @RequestParam("productId") Long productId) {
  70 + List<IotProductScriptDO> list = productScriptService.getProductScriptListByProductId(productId);
  71 + return success(BeanUtils.toBean(list, IotProductScriptRespVO.class));
  72 + }
  73 +
  74 + @GetMapping("/page")
  75 + @Operation(summary = "获得产品脚本分页")
  76 + @PreAuthorize("@ss.hasPermission('iot:product-script:query')")
  77 + public CommonResult<PageResult<IotProductScriptRespVO>> getProductScriptPage(
  78 + @Valid IotProductScriptPageReqVO pageReqVO) {
  79 + PageResult<IotProductScriptDO> pageResult = productScriptService.getProductScriptPage(pageReqVO);
  80 + return success(BeanUtils.toBean(pageResult, IotProductScriptRespVO.class));
  81 + }
  82 +
  83 + @PostMapping("/test")
  84 + @Operation(summary = "测试产品脚本")
  85 + @PreAuthorize("@ss.hasPermission('iot:product-script:test')")
  86 + public CommonResult<IotProductScriptTestRespVO> testProductScript(
  87 + @Valid @RequestBody IotProductScriptTestReqVO testReqVO) {
  88 + return success(productScriptService.testProductScript(testReqVO));
  89 + }
  90 +
  91 + @PutMapping("/update-status")
  92 + @Operation(summary = "更新产品脚本状态")
  93 + @PreAuthorize("@ss.hasPermission('iot:product-script:update')")
  94 + public CommonResult<Boolean> updateProductScriptStatus(
  95 + @Valid @RequestBody IotProductScriptUpdateStatusReqVO updateStatusReqVO) {
  96 + productScriptService.updateProductScriptStatus(updateStatusReqVO.getId(), updateStatusReqVO.getStatus());
  97 + return success(true);
  98 + }
  99 +}
0 \ No newline at end of file 100 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/script/IotProductScriptPageReqVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.PageParam;
  4 +import io.swagger.v3.oas.annotations.media.Schema;
  5 +import lombok.Data;
  6 +import lombok.EqualsAndHashCode;
  7 +import lombok.ToString;
  8 +import org.springframework.format.annotation.DateTimeFormat;
  9 +
  10 +import java.time.LocalDateTime;
  11 +
  12 +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
  13 +
  14 +@Schema(description = "管理后台 - IoT 产品脚本信息分页 Request VO")
  15 +@Data
  16 +@EqualsAndHashCode(callSuper = true)
  17 +@ToString(callSuper = true)
  18 +public class IotProductScriptPageReqVO extends PageParam {
  19 +
  20 + @Schema(description = "产品ID", example = "28277")
  21 + private Long productId;
  22 +
  23 + @Schema(description = "产品唯一标识符")
  24 + private String productKey;
  25 +
  26 + @Schema(description = "脚本类型(property_parser=属性解析,event_parser=事件解析,command_encoder=命令编码)", example = "2")
  27 + private String scriptType;
  28 +
  29 + @Schema(description = "脚本语言")
  30 + private String scriptLanguage;
  31 +
  32 + @Schema(description = "状态(0=禁用 1=启用)", example = "2")
  33 + private Integer status;
  34 +
  35 + @Schema(description = "备注说明", example = "你说的对")
  36 + private String remark;
  37 +
  38 + @Schema(description = "最后测试时间")
  39 + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
  40 + private LocalDateTime[] lastTestTime;
  41 +
  42 + @Schema(description = "创建时间")
  43 + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
  44 + private LocalDateTime[] createTime;
  45 +
  46 +}
0 \ No newline at end of file 47 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/script/IotProductScriptRespVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
  2 +
  3 +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
  4 +import com.alibaba.excel.annotation.ExcelProperty;
  5 +import io.swagger.v3.oas.annotations.media.Schema;
  6 +import lombok.Data;
  7 +
  8 +import java.time.LocalDateTime;
  9 +
  10 +@Schema(description = "管理后台 - IoT 产品脚本信息 Response VO")
  11 +@Data
  12 +@ExcelIgnoreUnannotated
  13 +public class IotProductScriptRespVO {
  14 +
  15 + @Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "26565")
  16 + @ExcelProperty("主键")
  17 + private Long id;
  18 +
  19 + @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "28277")
  20 + @ExcelProperty("产品ID")
  21 + private Long productId;
  22 +
  23 + @Schema(description = "产品唯一标识符", requiredMode = Schema.RequiredMode.REQUIRED)
  24 + @ExcelProperty("产品唯一标识符")
  25 + private String productKey;
  26 +
  27 + @Schema(description = "脚本类型(property_parser=属性解析,event_parser=事件解析,command_encoder=命令编码)", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
  28 + @ExcelProperty("脚本类型(property_parser=属性解析,event_parser=事件解析,command_encoder=命令编码)")
  29 + private String scriptType;
  30 +
  31 + @Schema(description = "脚本内容", requiredMode = Schema.RequiredMode.REQUIRED)
  32 + @ExcelProperty("脚本内容")
  33 + private String scriptContent;
  34 +
  35 + @Schema(description = "脚本语言", requiredMode = Schema.RequiredMode.REQUIRED)
  36 + @ExcelProperty("脚本语言")
  37 + private String scriptLanguage;
  38 +
  39 + @Schema(description = "状态(0=禁用 1=启用)", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
  40 + @ExcelProperty("状态(0=禁用 1=启用)")
  41 + private Integer status;
  42 +
  43 + @Schema(description = "备注说明", example = "你说的对")
  44 + @ExcelProperty("备注说明")
  45 + private String remark;
  46 +
  47 + @Schema(description = "最后测试时间")
  48 + @ExcelProperty("最后测试时间")
  49 + private LocalDateTime lastTestTime;
  50 +
  51 + @Schema(description = "最后测试结果(0=失败 1=成功)")
  52 + @ExcelProperty("最后测试结果(0=失败 1=成功)")
  53 + private Integer lastTestResult;
  54 +
  55 + @Schema(description = "脚本版本号", requiredMode = Schema.RequiredMode.REQUIRED)
  56 + @ExcelProperty("脚本版本号")
  57 + private Integer version;
  58 +
  59 + @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
  60 + @ExcelProperty("创建时间")
  61 + private LocalDateTime createTime;
  62 +
  63 +}
0 \ No newline at end of file 64 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/script/IotProductScriptSaveReqVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import jakarta.validation.constraints.NotEmpty;
  5 +import jakarta.validation.constraints.NotNull;
  6 +import lombok.Data;
  7 +
  8 +@Schema(description = "管理后台 - IoT 产品脚本信息新增/修改 Request VO")
  9 +@Data
  10 +public class IotProductScriptSaveReqVO {
  11 +
  12 + @Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "26565")
  13 + private Long id;
  14 +
  15 + @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "28277")
  16 + @NotNull(message = "产品ID不能为空")
  17 + private Long productId;
  18 +
  19 + @Schema(description = "产品唯一标识符", requiredMode = Schema.RequiredMode.REQUIRED)
  20 + @NotEmpty(message = "产品唯一标识符不能为空")
  21 + private String productKey;
  22 +
  23 + @Schema(description = "脚本类型(property_parser=属性解析,event_parser=事件解析,command_encoder=命令编码)", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
  24 + @NotEmpty(message = "脚本类型(property_parser=属性解析,event_parser=事件解析,command_encoder=命令编码)不能为空")
  25 + private String scriptType;
  26 +
  27 + @Schema(description = "脚本内容", requiredMode = Schema.RequiredMode.REQUIRED)
  28 + @NotEmpty(message = "脚本内容不能为空")
  29 + private String scriptContent;
  30 +
  31 + @Schema(description = "脚本语言", requiredMode = Schema.RequiredMode.REQUIRED)
  32 + @NotEmpty(message = "脚本语言不能为空")
  33 + private String scriptLanguage;
  34 +
  35 + @Schema(description = "状态(0=禁用 1=启用)", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
  36 + @NotNull(message = "状态(0=禁用 1=启用)不能为空")
  37 + private Integer status;
  38 +
  39 + @Schema(description = "备注说明", example = "你说的对")
  40 + private String remark;
  41 +
  42 +}
0 \ No newline at end of file 43 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/script/IotProductScriptTestReqVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import jakarta.validation.constraints.NotEmpty;
  5 +import jakarta.validation.constraints.NotNull;
  6 +import lombok.Data;
  7 +
  8 +@Schema(description = "管理后台 - IoT 产品脚本测试 Request VO")
  9 +@Data
  10 +public class IotProductScriptTestReqVO {
  11 +
  12 + @Schema(description = "脚本ID,如果已保存脚本则传入", example = "1024")
  13 + private Long id;
  14 +
  15 + @Schema(description = "产品ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
  16 + @NotNull(message = "产品ID不能为空")
  17 + private Long productId;
  18 +
  19 + @Schema(description = "脚本类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "property_parser")
  20 + @NotEmpty(message = "脚本类型不能为空")
  21 + private String scriptType;
  22 +
  23 + @Schema(description = "脚本内容", requiredMode = Schema.RequiredMode.REQUIRED)
  24 + @NotEmpty(message = "脚本内容不能为空")
  25 + private String scriptContent;
  26 +
  27 + @Schema(description = "脚本语言", requiredMode = Schema.RequiredMode.REQUIRED, example = "javascript")
  28 + @NotEmpty(message = "脚本语言不能为空")
  29 + private String scriptLanguage;
  30 +
  31 + @Schema(description = "测试输入数据", requiredMode = Schema.RequiredMode.REQUIRED)
  32 + @NotEmpty(message = "测试输入数据不能为空")
  33 + private String testInput;
  34 +
  35 +}
0 \ No newline at end of file 36 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/script/IotProductScriptTestRespVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import lombok.Data;
  5 +
  6 +@Schema(description = "管理后台 - IoT 产品脚本测试 Response VO")
  7 +@Data
  8 +public class IotProductScriptTestRespVO {
  9 +
  10 + @Schema(description = "测试是否成功", requiredMode = Schema.RequiredMode.REQUIRED)
  11 + private Boolean success;
  12 +
  13 + @Schema(description = "测试结果输出")
  14 + private Object output;
  15 +
  16 + @Schema(description = "错误消息,失败时返回")
  17 + private String errorMessage;
  18 +
  19 + @Schema(description = "执行耗时(毫秒)")
  20 + private Long executionTimeMs;
  21 +
  22 + // 静态工厂方法 - 成功
  23 + public static IotProductScriptTestRespVO success(Object output, Long executionTimeMs) {
  24 + IotProductScriptTestRespVO respVO = new IotProductScriptTestRespVO();
  25 + respVO.setSuccess(true);
  26 + respVO.setOutput(output);
  27 + respVO.setExecutionTimeMs(executionTimeMs);
  28 + return respVO;
  29 + }
  30 +
  31 + // 静态工厂方法 - 失败
  32 + public static IotProductScriptTestRespVO error(String errorMessage, Long executionTimeMs) {
  33 + IotProductScriptTestRespVO respVO = new IotProductScriptTestRespVO();
  34 + respVO.setSuccess(false);
  35 + respVO.setErrorMessage(errorMessage);
  36 + respVO.setExecutionTimeMs(executionTimeMs);
  37 + return respVO;
  38 + }
  39 +}
0 \ No newline at end of file 40 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/product/vo/script/IotProductScriptUpdateStatusReqVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.controller.admin.product.vo.script;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import jakarta.validation.constraints.NotNull;
  5 +import lombok.Data;
  6 +
  7 +@Schema(description = "管理后台 - IoT 产品脚本状态更新 Request VO")
  8 +@Data
  9 +public class IotProductScriptUpdateStatusReqVO {
  10 +
  11 + @Schema(description = "脚本ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
  12 + @NotNull(message = "脚本ID不能为空")
  13 + private Long id;
  14 +
  15 + @Schema(description = "状态(0=禁用 1=启用)", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
  16 + @NotNull(message = "状态不能为空")
  17 + private Integer status;
  18 +
  19 +}
0 \ No newline at end of file 20 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/product/IotProductScriptDO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.dal.dataobject.product;
  2 +
  3 +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  4 +import com.baomidou.mybatisplus.annotation.KeySequence;
  5 +import com.baomidou.mybatisplus.annotation.TableId;
  6 +import com.baomidou.mybatisplus.annotation.TableName;
  7 +import lombok.*;
  8 +
  9 +import java.time.LocalDateTime;
  10 +
  11 +/**
  12 + * IoT 产品脚本信息 DO
  13 + *
  14 + * @author 芋道源码
  15 + */
  16 +@TableName("iot_product_script")
  17 +@KeySequence("iot_product_script_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
  18 +@Data
  19 +@EqualsAndHashCode(callSuper = true)
  20 +@ToString(callSuper = true)
  21 +@Builder
  22 +@NoArgsConstructor
  23 +@AllArgsConstructor
  24 +public class IotProductScriptDO extends BaseDO {
  25 +
  26 + /**
  27 + * 主键
  28 + */
  29 + @TableId
  30 + private Long id;
  31 + /**
  32 + * 产品ID
  33 + */
  34 + private Long productId;
  35 + /**
  36 + * 产品唯一标识符
  37 + */
  38 + private String productKey;
  39 + /**
  40 + * 脚本类型(property_parser=属性解析,event_parser=事件解析,command_encoder=命令编码)
  41 + */
  42 + private String scriptType;
  43 + /**
  44 + * 脚本内容
  45 + */
  46 + private String scriptContent;
  47 + /**
  48 + * 脚本语言
  49 + */
  50 + private String scriptLanguage;
  51 + /**
  52 + * 状态(0=禁用 1=启用)
  53 + */
  54 + private Integer status;
  55 + /**
  56 + * 备注说明
  57 + */
  58 + private String remark;
  59 + /**
  60 + * 最后测试时间
  61 + */
  62 + private LocalDateTime lastTestTime;
  63 + /**
  64 + * 最后测试结果(0=失败 1=成功)
  65 + */
  66 + private Integer lastTestResult;
  67 + /**
  68 + * 脚本版本号
  69 + */
  70 + private Integer version;
  71 +
  72 +}
0 \ No newline at end of file 73 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/mysql/product/IotProductScriptMapper.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.dal.mysql.product;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  4 +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
  5 +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptPageReqVO;
  7 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
  8 +import org.apache.ibatis.annotations.Mapper;
  9 +
  10 +/**
  11 + * IoT 产品脚本信息 Mapper
  12 + *
  13 + * @author 芋道源码
  14 + */
  15 +@Mapper
  16 +public interface IotProductScriptMapper extends BaseMapperX<IotProductScriptDO> {
  17 +
  18 + default PageResult<IotProductScriptDO> selectPage(IotProductScriptPageReqVO reqVO) {
  19 + return selectPage(reqVO, new LambdaQueryWrapperX<IotProductScriptDO>()
  20 + .eqIfPresent(IotProductScriptDO::getProductId, reqVO.getProductId())
  21 + .eqIfPresent(IotProductScriptDO::getProductKey, reqVO.getProductKey())
  22 + .eqIfPresent(IotProductScriptDO::getScriptType, reqVO.getScriptType())
  23 + .eqIfPresent(IotProductScriptDO::getScriptLanguage, reqVO.getScriptLanguage())
  24 + .eqIfPresent(IotProductScriptDO::getStatus, reqVO.getStatus())
  25 + .eqIfPresent(IotProductScriptDO::getRemark, reqVO.getRemark())
  26 + .betweenIfPresent(IotProductScriptDO::getLastTestTime, reqVO.getLastTestTime())
  27 + .betweenIfPresent(IotProductScriptDO::getCreateTime, reqVO.getCreateTime())
  28 + .orderByDesc(IotProductScriptDO::getId));
  29 + }
  30 +
  31 +}
0 \ No newline at end of file 32 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductScriptService.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.service.product;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  4 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptPageReqVO;
  5 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptSaveReqVO;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptTestReqVO;
  7 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptTestRespVO;
  8 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
  9 +import jakarta.validation.Valid;
  10 +
  11 +import java.util.List;
  12 +
  13 +/**
  14 + * IoT 产品脚本信息 Service 接口
  15 + *
  16 + * @author 芋道源码
  17 + */
  18 +public interface IotProductScriptService {
  19 +
  20 + /**
  21 + * 创建IoT 产品脚本信息
  22 + *
  23 + * @param createReqVO 创建信息
  24 + * @return 编号
  25 + */
  26 + Long createProductScript(@Valid IotProductScriptSaveReqVO createReqVO);
  27 +
  28 + /**
  29 + * 更新IoT 产品脚本信息
  30 + *
  31 + * @param updateReqVO 更新信息
  32 + */
  33 + void updateProductScript(@Valid IotProductScriptSaveReqVO updateReqVO);
  34 +
  35 + /**
  36 + * 删除IoT 产品脚本信息
  37 + *
  38 + * @param id 编号
  39 + */
  40 + void deleteProductScript(Long id);
  41 +
  42 + /**
  43 + * 获得IoT 产品脚本信息
  44 + *
  45 + * @param id 编号
  46 + * @return IoT 产品脚本信息
  47 + */
  48 + IotProductScriptDO getProductScript(Long id);
  49 +
  50 + /**
  51 + * 获得IoT 产品脚本信息分页
  52 + *
  53 + * @param pageReqVO 分页查询
  54 + * @return IoT 产品脚本信息分页
  55 + */
  56 + PageResult<IotProductScriptDO> getProductScriptPage(IotProductScriptPageReqVO pageReqVO);
  57 +
  58 + /**
  59 + * 获取产品的脚本列表
  60 + *
  61 + * @param productId 产品ID
  62 + * @return 脚本列表
  63 + */
  64 + List<IotProductScriptDO> getProductScriptListByProductId(Long productId);
  65 +
  66 + /**
  67 + * 测试产品脚本
  68 + *
  69 + * @param testReqVO 测试请求
  70 + * @return 测试结果
  71 + */
  72 + IotProductScriptTestRespVO testProductScript(@Valid IotProductScriptTestReqVO testReqVO);
  73 +
  74 + /**
  75 + * 更新产品脚本状态
  76 + *
  77 + * @param id 脚本ID
  78 + * @param status 状态
  79 + */
  80 + void updateProductScriptStatus(Long id, Integer status);
  81 +
  82 +}
0 \ No newline at end of file 83 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductScriptServiceImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.iot.service.product;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  4 +import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
  5 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptPageReqVO;
  6 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptSaveReqVO;
  7 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptTestReqVO;
  8 +import cn.iocoder.yudao.module.iot.controller.admin.product.vo.script.IotProductScriptTestRespVO;
  9 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductDO;
  10 +import cn.iocoder.yudao.module.iot.dal.dataobject.product.IotProductScriptDO;
  11 +import cn.iocoder.yudao.module.iot.dal.mysql.product.IotProductScriptMapper;
  12 +import cn.iocoder.yudao.module.iot.plugin.script.context.PluginScriptContext;
  13 +import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptService;
  14 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  15 +import jakarta.annotation.Resource;
  16 +import lombok.extern.slf4j.Slf4j;
  17 +import org.springframework.stereotype.Service;
  18 +import org.springframework.validation.annotation.Validated;
  19 +
  20 +import java.time.LocalDateTime;
  21 +import java.util.HashMap;
  22 +import java.util.List;
  23 +import java.util.Map;
  24 +
  25 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  26 +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_NOT_EXISTS;
  27 +import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.PRODUCT_SCRIPT_NOT_EXISTS;
  28 +
  29 +/**
  30 + * IoT 产品脚本信息 Service 实现类
  31 + *
  32 + * @author 芋道源码
  33 + */
  34 +@Service
  35 +@Validated
  36 +@Slf4j
  37 +public class IotProductScriptServiceImpl implements IotProductScriptService {
  38 +
  39 + @Resource
  40 + private IotProductScriptMapper productScriptMapper;
  41 +
  42 + @Resource
  43 + private IotProductService productService;
  44 +
  45 + @Resource
  46 + private ScriptService scriptService;
  47 +
  48 + @Override
  49 + public Long createProductScript(IotProductScriptSaveReqVO createReqVO) {
  50 + // 验证产品是否存在
  51 + validateProductExists(createReqVO.getProductId());
  52 +
  53 + // 插入
  54 + IotProductScriptDO productScript = BeanUtils.toBean(createReqVO, IotProductScriptDO.class);
  55 + // 初始化版本为1
  56 + productScript.setVersion(1);
  57 + // 初始化测试相关字段
  58 + productScript.setLastTestResult(null);
  59 + productScript.setLastTestTime(null);
  60 + productScriptMapper.insert(productScript);
  61 + // 返回
  62 + return productScript.getId();
  63 + }
  64 +
  65 + @Override
  66 + public void updateProductScript(IotProductScriptSaveReqVO updateReqVO) {
  67 + // 校验存在
  68 + validateProductScriptExists(updateReqVO.getId());
  69 +
  70 + // 获取旧的记录,保留版本号和测试信息
  71 + IotProductScriptDO oldScript = getProductScript(updateReqVO.getId());
  72 +
  73 + // 更新
  74 + IotProductScriptDO updateObj = BeanUtils.toBean(updateReqVO, IotProductScriptDO.class);
  75 + // 更新版本号
  76 + updateObj.setVersion(oldScript.getVersion() + 1);
  77 + // 保留测试相关信息
  78 + updateObj.setLastTestTime(oldScript.getLastTestTime());
  79 + updateObj.setLastTestResult(oldScript.getLastTestResult());
  80 + productScriptMapper.updateById(updateObj);
  81 + }
  82 +
  83 + @Override
  84 + public void deleteProductScript(Long id) {
  85 + // 校验存在
  86 + validateProductScriptExists(id);
  87 + // 删除
  88 + productScriptMapper.deleteById(id);
  89 + }
  90 +
  91 + private void validateProductScriptExists(Long id) {
  92 + if (productScriptMapper.selectById(id) == null) {
  93 + throw exception(PRODUCT_SCRIPT_NOT_EXISTS);
  94 + }
  95 + }
  96 +
  97 + private void validateProductExists(Long productId) {
  98 + IotProductDO product = productService.getProduct(productId);
  99 + if (product == null) {
  100 + throw exception(PRODUCT_NOT_EXISTS);
  101 + }
  102 + }
  103 +
  104 + @Override
  105 + public IotProductScriptDO getProductScript(Long id) {
  106 + return productScriptMapper.selectById(id);
  107 + }
  108 +
  109 + @Override
  110 + public PageResult<IotProductScriptDO> getProductScriptPage(IotProductScriptPageReqVO pageReqVO) {
  111 + return productScriptMapper.selectPage(pageReqVO);
  112 + }
  113 +
  114 + @Override
  115 + public List<IotProductScriptDO> getProductScriptListByProductId(Long productId) {
  116 + return productScriptMapper.selectList(new LambdaQueryWrapper<IotProductScriptDO>()
  117 + .eq(IotProductScriptDO::getProductId, productId)
  118 + .orderByDesc(IotProductScriptDO::getId));
  119 + }
  120 +
  121 + @Override
  122 + public IotProductScriptTestRespVO testProductScript(IotProductScriptTestReqVO testReqVO) {
  123 + long startTime = System.currentTimeMillis();
  124 +
  125 + try {
  126 + // 验证产品是否存在
  127 + validateProductExists(testReqVO.getProductId());
  128 +
  129 + // 根据ID获取已保存的脚本(如果有)
  130 + IotProductScriptDO existingScript = null;
  131 + if (testReqVO.getId() != null) {
  132 + existingScript = getProductScript(testReqVO.getId());
  133 + }
  134 +
  135 + // 创建测试上下文
  136 + PluginScriptContext context = new PluginScriptContext();
  137 + IotProductDO product = productService.getProduct(testReqVO.getProductId());
  138 +
  139 + // 设置设备上下文(使用产品信息,没有具体设备)
  140 + context.withDeviceContext(product.getProductKey(), null);
  141 +
  142 + // 设置输入参数
  143 + Map<String, Object> params = new HashMap<>();
  144 + params.put("input", testReqVO.getTestInput());
  145 + params.put("productKey", product.getProductKey());
  146 + params.put("scriptType", testReqVO.getScriptType());
  147 +
  148 + // 根据脚本类型设置特定参数
  149 + switch (testReqVO.getScriptType()) {
  150 + case "property_parser":
  151 + params.put("method", "property");
  152 + break;
  153 + case "event_parser":
  154 + params.put("method", "event");
  155 + params.put("identifier", "default");
  156 + break;
  157 + case "command_encoder":
  158 + params.put("method", "command");
  159 + break;
  160 + default:
  161 + // 默认不添加额外参数
  162 + }
  163 +
  164 + // 添加所有参数到上下文
  165 + for (Map.Entry<String, Object> entry : params.entrySet()) {
  166 + context.setParameter(entry.getKey(), entry.getValue());
  167 + }
  168 +
  169 + // 执行脚本
  170 + Object result = scriptService.executeScript(
  171 + testReqVO.getScriptLanguage(),
  172 + testReqVO.getScriptContent(),
  173 + context);
  174 +
  175 + // 更新测试结果(如果是已保存的脚本)
  176 + if (existingScript != null) {
  177 + IotProductScriptDO updateObj = new IotProductScriptDO();
  178 + updateObj.setId(existingScript.getId());
  179 + updateObj.setLastTestTime(LocalDateTime.now());
  180 + updateObj.setLastTestResult(1); // 1表示成功
  181 + productScriptMapper.updateById(updateObj);
  182 + }
  183 +
  184 + long executionTime = System.currentTimeMillis() - startTime;
  185 + return IotProductScriptTestRespVO.success(result, executionTime);
  186 +
  187 + } catch (Exception e) {
  188 + log.error("[testProductScript][测试脚本异常]", e);
  189 +
  190 + // 如果是已保存的脚本,更新测试失败状态
  191 + if (testReqVO.getId() != null) {
  192 + try {
  193 + IotProductScriptDO updateObj = new IotProductScriptDO();
  194 + updateObj.setId(testReqVO.getId());
  195 + updateObj.setLastTestTime(LocalDateTime.now());
  196 + updateObj.setLastTestResult(0); // 0表示失败
  197 + productScriptMapper.updateById(updateObj);
  198 + } catch (Exception ex) {
  199 + log.error("[testProductScript][更新脚本测试结果异常]", ex);
  200 + }
  201 + }
  202 +
  203 + long executionTime = System.currentTimeMillis() - startTime;
  204 + return IotProductScriptTestRespVO.error(e.getMessage(), executionTime);
  205 + }
  206 + }
  207 +
  208 + @Override
  209 + public void updateProductScriptStatus(Long id, Integer status) {
  210 + // 校验存在
  211 + validateProductScriptExists(id);
  212 +
  213 + // 更新状态
  214 + IotProductScriptDO updateObj = new IotProductScriptDO();
  215 + updateObj.setId(id);
  216 + updateObj.setStatus(status);
  217 + productScriptMapper.updateById(updateObj);
  218 + }
  219 +}
0 \ No newline at end of file 220 \ No newline at end of file