Merge remote-tracking branch 'yudao/feature/iot' into iot
This commit is contained in:
commit
e3e3a00fba
|
@ -75,4 +75,7 @@ public interface ErrorCodeConstants {
|
||||||
// ========== IoT 规则场景(场景联动) 1-050-011-000 ==========
|
// ========== IoT 规则场景(场景联动) 1-050-011-000 ==========
|
||||||
ErrorCode RULE_SCENE_NOT_EXISTS = new ErrorCode(1_050_011_000, "IoT 规则场景(场景联动)不存在");
|
ErrorCode RULE_SCENE_NOT_EXISTS = new ErrorCode(1_050_011_000, "IoT 规则场景(场景联动)不存在");
|
||||||
|
|
||||||
|
// ========== IoT 产品脚本信息 1-050-012-000 ==========
|
||||||
|
ErrorCode PRODUCT_SCRIPT_NOT_EXISTS = new ErrorCode(1_050_012_000, "IoT 产品脚本信息不存在");
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.enums.product;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.core.ArrayValuable;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IoT 产品脚本语言枚举
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum IotProductScriptLanguageEnum implements ArrayValuable<String> {
|
||||||
|
|
||||||
|
JAVASCRIPT("javascript", "JavaScript"),
|
||||||
|
JAVA("java", "Java"),
|
||||||
|
PYTHON("python", "Python"),
|
||||||
|
;
|
||||||
|
|
||||||
|
public static final String[] ARRAYS = Arrays.stream(values()).map(IotProductScriptLanguageEnum::getCode)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
*/
|
||||||
|
private final String code;
|
||||||
|
/**
|
||||||
|
* 名称
|
||||||
|
*/
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] array() {
|
||||||
|
return ARRAYS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IotProductScriptLanguageEnum getByCode(String code) {
|
||||||
|
return Arrays.stream(values())
|
||||||
|
.filter(type -> type.getCode().equals(code))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.enums.product;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.core.ArrayValuable;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IoT 产品脚本状态枚举
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum IotProductScriptStatusEnum implements ArrayValuable<Integer> {
|
||||||
|
|
||||||
|
ENABLE(0, "启用"),
|
||||||
|
DISABLE(1, "禁用"),
|
||||||
|
;
|
||||||
|
|
||||||
|
public static final Integer[] ARRAYS = Arrays.stream(values()).map(IotProductScriptStatusEnum::getStatus)
|
||||||
|
.toArray(Integer[]::new);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 状态值
|
||||||
|
*/
|
||||||
|
private final Integer status;
|
||||||
|
/**
|
||||||
|
* 状态名
|
||||||
|
*/
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer[] array() {
|
||||||
|
return ARRAYS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IotProductScriptStatusEnum getByStatus(Integer status) {
|
||||||
|
return Arrays.stream(values())
|
||||||
|
.filter(type -> type.getStatus().equals(status))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isEnable(Integer status) {
|
||||||
|
return ENABLE.getStatus().equals(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isDisable(Integer status) {
|
||||||
|
return DISABLE.getStatus().equals(status);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.enums.product;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.core.ArrayValuable;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IoT 产品脚本类型枚举
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum IotProductScriptTypeEnum implements ArrayValuable<Integer> {
|
||||||
|
|
||||||
|
PROPERTY_PARSER(1, "property_parser", "属性解析"),
|
||||||
|
EVENT_PARSER(2, "event_parser", "事件解析"),
|
||||||
|
COMMAND_ENCODER(3, "command_encoder", "命令编码"),
|
||||||
|
;
|
||||||
|
|
||||||
|
public static final Integer[] ARRAYS = Arrays.stream(values()).map(IotProductScriptTypeEnum::getCode)
|
||||||
|
.toArray(Integer[]::new);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编码
|
||||||
|
*/
|
||||||
|
private final Integer code;
|
||||||
|
/**
|
||||||
|
* 类型
|
||||||
|
*/
|
||||||
|
private final String type;
|
||||||
|
/**
|
||||||
|
* 名称
|
||||||
|
*/
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer[] array() {
|
||||||
|
return ARRAYS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IotProductScriptTypeEnum getByCode(Integer code) {
|
||||||
|
return Arrays.stream(values())
|
||||||
|
.filter(type -> type.getCode().equals(code))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
}
|
|
@ -69,6 +69,13 @@
|
||||||
<artifactId>yudao-spring-boot-starter-excel</artifactId>
|
<artifactId>yudao-spring-boot-starter-excel</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 脚本插件相关 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.iocoder.boot</groupId>
|
||||||
|
<artifactId>yudao-module-iot-plugin-script</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 消息队列相关 -->
|
<!-- 消息队列相关 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.rocketmq</groupId>
|
<groupId>org.apache.rocketmq</groupId>
|
||||||
|
|
|
@ -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;
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||||
|
|
||||||
|
// TODO @芋艿:规则场景 要不要,统一改成 场景联动
|
||||||
@Tag(name = "管理后台 - IoT 规则场景")
|
@Tag(name = "管理后台 - IoT 规则场景")
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/iot/rule-scene")
|
@RequestMapping("/iot/rule-scene")
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package cn.iocoder.yudao.module.iot.controller.admin.rule.vo.scene;
|
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.pojo.PageParam;
|
||||||
|
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
@ -24,6 +26,7 @@ public class IotRuleScenePageReqVO extends PageParam {
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@Schema(description = "场景状态", example = "1")
|
@Schema(description = "场景状态", example = "1")
|
||||||
|
@InEnum(CommonStatusEnum.class)
|
||||||
private Integer status;
|
private Integer status;
|
||||||
|
|
||||||
@Schema(description = "创建时间")
|
@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 cn.iocoder.yudao.module.iot.enums.rule.IotRuleSceneActionTypeEnum;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||||
/**
|
/**
|
||||||
* 执行器配置
|
* 执行器配置
|
||||||
*
|
*
|
||||||
|
|
|
@ -9,6 +9,7 @@ import lombok.Data;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||||
/**
|
/**
|
||||||
* 执行设备控制
|
* 执行设备控制
|
||||||
*
|
*
|
||||||
|
|
|
@ -6,6 +6,7 @@ import lombok.Data;
|
||||||
|
|
||||||
import java.util.List;
|
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 cn.iocoder.yudao.module.iot.enums.rule.IotRuleSceneTriggerConditionParameterOperatorEnum;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||||
/**
|
/**
|
||||||
* 触发条件参数
|
* 触发条件参数
|
||||||
*
|
*
|
||||||
|
|
|
@ -7,6 +7,7 @@ import lombok.Data;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
// TODO @puhui999:这个要不内嵌到 IoTRuleSceneDO 里面?
|
||||||
/**
|
/**
|
||||||
* 触发器配置
|
* 触发器配置
|
||||||
*
|
*
|
||||||
|
|
|
@ -61,6 +61,7 @@ public class IotThingModelController {
|
||||||
return success(BeanUtils.toBean(thingModel, IotThingModelRespVO.class));
|
return success(BeanUtils.toBean(thingModel, IotThingModelRespVO.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO @puhui999:要不叫 get-tsl,去掉 product-id;后续,把
|
||||||
@GetMapping("/tsl-by-product-id")
|
@GetMapping("/tsl-by-product-id")
|
||||||
@Operation(summary = "获得产品物模型 TSL")
|
@Operation(summary = "获得产品物模型 TSL")
|
||||||
@Parameter(name = "productId", description = "产品 ID", required = true, example = "1024")
|
@Parameter(name = "productId", description = "产品 ID", required = true, example = "1024")
|
||||||
|
@ -69,7 +70,6 @@ public class IotThingModelController {
|
||||||
return success(thingModelService.getThingModelTslByProductId(productId));
|
return success(thingModelService.getThingModelTslByProductId(productId));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO @puhui @super:getThingModelListByProductId 和 getThingModelListByProductId 可以融合么?
|
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
@Operation(summary = "获得产品物模型列表")
|
@Operation(summary = "获得产品物模型列表")
|
||||||
@PreAuthorize("@ss.hasPermission('iot:thing-model:query')")
|
@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);
|
return thingModelMapper.selectList(reqVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO @puhui999:这个转换,放在 controller 貌似也行?
|
||||||
@Override
|
@Override
|
||||||
public IotThingModelTSLRespVO getThingModelTslByProductId(Long productId) {
|
public IotThingModelTSLRespVO getThingModelTslByProductId(Long productId) {
|
||||||
IotThingModelTSLRespVO tslRespVO = new IotThingModelTSLRespVO();
|
IotThingModelTSLRespVO tslRespVO = new IotThingModelTSLRespVO();
|
||||||
// 1. 获得产品所有物模型定义
|
// 1. 获得产品所有物模型定义
|
||||||
List<IotThingModelDO> thingModelList = thingModelMapper.selectListByProductId(productId);
|
List<IotThingModelDO> thingModels = thingModelMapper.selectListByProductId(productId);
|
||||||
if (CollUtil.isEmpty(thingModelList)) {
|
if (CollUtil.isEmpty(thingModels)) {
|
||||||
return tslRespVO;
|
return tslRespVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2.1 设置公共部分参数
|
// 2.1 设置公共部分参数
|
||||||
IotThingModelDO thingModel = thingModelList.get(0);
|
IotThingModelDO thingModel = thingModels.get(0);
|
||||||
tslRespVO.setProductId(thingModel.getProductId()).setProductKey(thingModel.getProductKey());
|
tslRespVO.setProductId(thingModel.getProductId()).setProductKey(thingModel.getProductKey());
|
||||||
// 2.2 处理属性列表
|
// 2.2 处理属性列表
|
||||||
tslRespVO.setProperties(convertList(filterList(thingModelList, item ->
|
tslRespVO.setProperties(convertList(filterList(thingModels, item ->
|
||||||
ObjUtil.equal(IotThingModelTypeEnum.PROPERTY.getType(), item.getType())), IotThingModelDO::getProperty));
|
ObjUtil.equal(IotThingModelTypeEnum.PROPERTY.getType(), item.getType())), IotThingModelDO::getProperty));
|
||||||
// 2.3 处理服务列表
|
// 2.3 处理服务列表
|
||||||
tslRespVO.setServices(convertList(filterList(thingModelList, item ->
|
tslRespVO.setServices(convertList(filterList(thingModels, item ->
|
||||||
ObjUtil.equal(IotThingModelTypeEnum.SERVICE.getType(), item.getType())), IotThingModelDO::getService));
|
ObjUtil.equal(IotThingModelTypeEnum.SERVICE.getType(), item.getType())), IotThingModelDO::getService));
|
||||||
// 2.4 处理事件列表
|
// 2.4 处理事件列表
|
||||||
tslRespVO.setEvents(convertList(filterList(thingModelList, item ->
|
tslRespVO.setEvents(convertList(filterList(thingModels, item ->
|
||||||
ObjUtil.equal(IotThingModelTypeEnum.EVENT.getType(), item.getType())), IotThingModelDO::getEvent));
|
ObjUtil.equal(IotThingModelTypeEnum.EVENT.getType(), item.getType())), IotThingModelDO::getEvent));
|
||||||
return tslRespVO;
|
return tslRespVO;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
<modules>
|
<modules>
|
||||||
<module>yudao-module-iot-plugin-common</module>
|
<module>yudao-module-iot-plugin-common</module>
|
||||||
|
<module>yudao-module-iot-plugin-script</module>
|
||||||
<module>yudao-module-iot-plugin-http</module>
|
<module>yudao-module-iot-plugin-http</module>
|
||||||
<module>yudao-module-iot-plugin-mqtt</module>
|
<module>yudao-module-iot-plugin-mqtt</module>
|
||||||
<module>yudao-module-iot-plugin-emqx</module>
|
<module>yudao-module-iot-plugin-emqx</module>
|
||||||
|
|
|
@ -161,5 +161,12 @@
|
||||||
<groupId>io.vertx</groupId>
|
<groupId>io.vertx</groupId>
|
||||||
<artifactId>vertx-web</artifactId>
|
<artifactId>vertx-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 添加脚本引擎模块依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.iocoder.boot</groupId>
|
||||||
|
<artifactId>yudao-module-iot-plugin-script</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
|
@ -9,7 +9,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
* 独立运行入口
|
* 独立运行入口
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@SpringBootApplication
|
@SpringBootApplication(scanBasePackages = {
|
||||||
|
// common 的包
|
||||||
|
"cn.iocoder.yudao.module.iot.plugin.common",
|
||||||
|
// http 的包
|
||||||
|
"cn.iocoder.yudao.module.iot.plugin.http",
|
||||||
|
// script 的包
|
||||||
|
"cn.iocoder.yudao.module.iot.plugin.script"
|
||||||
|
})
|
||||||
public class IotHttpPluginApplication {
|
public class IotHttpPluginApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import cn.iocoder.yudao.module.iot.plugin.common.downstream.IotDeviceDownstreamH
|
||||||
import cn.iocoder.yudao.module.iot.plugin.http.downstream.IotDeviceDownstreamHandlerImpl;
|
import cn.iocoder.yudao.module.iot.plugin.http.downstream.IotDeviceDownstreamHandlerImpl;
|
||||||
import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer;
|
import cn.iocoder.yudao.module.iot.plugin.http.upstream.IotDeviceUpstreamServer;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@ -19,8 +20,9 @@ public class IotPluginHttpAutoConfiguration {
|
||||||
|
|
||||||
@Bean(initMethod = "start", destroyMethod = "stop")
|
@Bean(initMethod = "start", destroyMethod = "stop")
|
||||||
public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi,
|
public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi,
|
||||||
IotPluginHttpProperties properties) {
|
IotPluginHttpProperties properties,
|
||||||
return new IotDeviceUpstreamServer(properties, deviceUpstreamApi);
|
ApplicationContext applicationContext) {
|
||||||
|
return new IotDeviceUpstreamServer(properties, deviceUpstreamApi, applicationContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -0,0 +1,228 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.http.script;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.context.PluginScriptContext;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptService;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP协议脚本处理服务
|
||||||
|
* 用于管理和执行设备数据解析脚本
|
||||||
|
*
|
||||||
|
* @author haohao
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class HttpScriptService {
|
||||||
|
|
||||||
|
private final ScriptService scriptService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本缓存,按产品Key缓存脚本内容
|
||||||
|
*/
|
||||||
|
private final Map<String, String> scriptCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析设备属性数据
|
||||||
|
*
|
||||||
|
* @param productKey 产品Key
|
||||||
|
* @param deviceName 设备名称
|
||||||
|
* @param payload 设备上报的原始数据
|
||||||
|
* @return 解析后的属性数据
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public Map<String, Object> parsePropertyData(String productKey, String deviceName, JsonObject payload) {
|
||||||
|
// 如果没有脚本,直接返回原始数据
|
||||||
|
String script = getScriptByProductKey(productKey);
|
||||||
|
if (StrUtil.isBlank(script)) {
|
||||||
|
if (payload != null && payload.containsKey("params")) {
|
||||||
|
return payload.getJsonObject("params").getMap();
|
||||||
|
}
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 创建脚本上下文
|
||||||
|
PluginScriptContext context = new PluginScriptContext();
|
||||||
|
context.withDeviceContext(productKey + ":" + deviceName, null);
|
||||||
|
context.withParameter("payload", payload.toString());
|
||||||
|
context.withParameter("method", "property");
|
||||||
|
|
||||||
|
// 执行脚本
|
||||||
|
Object result = scriptService.executeJavaScript(script, context);
|
||||||
|
log.debug("[parsePropertyData][产品:{} 设备:{} 原始数据:{} 解析结果:{}]",
|
||||||
|
productKey, deviceName, payload, result);
|
||||||
|
|
||||||
|
// 处理结果
|
||||||
|
if (result instanceof Map) {
|
||||||
|
return (Map<String, Object>) result;
|
||||||
|
} else if (result instanceof String) {
|
||||||
|
try {
|
||||||
|
return new JsonObject((String) result).getMap();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("[parsePropertyData][脚本返回的字符串不是有效的JSON] result:{}", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("[parsePropertyData][执行脚本解析属性数据异常] productKey:{} deviceName:{}",
|
||||||
|
productKey, deviceName, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析失败,返回空数据
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析设备事件数据
|
||||||
|
*
|
||||||
|
* @param productKey 产品Key
|
||||||
|
* @param deviceName 设备名称
|
||||||
|
* @param identifier 事件标识符
|
||||||
|
* @param payload 设备上报的原始数据
|
||||||
|
* @return 解析后的事件数据
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public Map<String, Object> parseEventData(String productKey, String deviceName, String identifier,
|
||||||
|
JsonObject payload) {
|
||||||
|
// 如果没有脚本,直接返回原始数据
|
||||||
|
String script = getScriptByProductKey(productKey);
|
||||||
|
if (StrUtil.isBlank(script)) {
|
||||||
|
if (payload != null && payload.containsKey("params")) {
|
||||||
|
return payload.getJsonObject("params").getMap();
|
||||||
|
}
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 创建脚本上下文
|
||||||
|
PluginScriptContext context = new PluginScriptContext();
|
||||||
|
context.withDeviceContext(productKey + ":" + deviceName, null);
|
||||||
|
context.withParameter("payload", payload.toString());
|
||||||
|
context.withParameter("method", "event");
|
||||||
|
context.withParameter("identifier", identifier);
|
||||||
|
|
||||||
|
// 执行脚本
|
||||||
|
Object result = scriptService.executeJavaScript(script, context);
|
||||||
|
log.debug("[parseEventData][产品:{} 设备:{} 事件:{} 原始数据:{} 解析结果:{}]",
|
||||||
|
productKey, deviceName, identifier, payload, result);
|
||||||
|
|
||||||
|
// 处理结果
|
||||||
|
if (result instanceof Map) {
|
||||||
|
return (Map<String, Object>) result;
|
||||||
|
} else if (result instanceof String) {
|
||||||
|
try {
|
||||||
|
return new JsonObject((String) result).getMap();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("[parseEventData][脚本返回的字符串不是有效的JSON] result:{}", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("[parseEventData][执行脚本解析事件数据异常] productKey:{} deviceName:{} identifier:{}",
|
||||||
|
productKey, deviceName, identifier, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析失败,返回空数据
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据产品Key获取脚本
|
||||||
|
*
|
||||||
|
* @param productKey 产品Key
|
||||||
|
* @return 脚本内容
|
||||||
|
*/
|
||||||
|
private String getScriptByProductKey(String productKey) {
|
||||||
|
// 从缓存中获取脚本
|
||||||
|
String script = scriptCache.get(productKey);
|
||||||
|
if (script != null) {
|
||||||
|
return script;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: 实际应用中,这里应从数据库或配置中心获取产品对应的脚本
|
||||||
|
// 此处仅为示例,提供一个默认脚本
|
||||||
|
if ("example_product".equals(productKey)) {
|
||||||
|
script = "/**\n" +
|
||||||
|
" * 设备数据解析脚本示例\n" +
|
||||||
|
" * @param payload 设备上报的原始数据\n" +
|
||||||
|
" * @param method 方法类型:property(属性)或event(事件)\n" +
|
||||||
|
" * @param identifier 事件标识符(仅当method为event时有值)\n" +
|
||||||
|
" * @return 解析后的数据\n" +
|
||||||
|
" */\n" +
|
||||||
|
"function parse() {\n" +
|
||||||
|
" // 解析JSON数据\n" +
|
||||||
|
" var data = JSON.parse(payload);\n" +
|
||||||
|
" var result = {};\n" +
|
||||||
|
" \n" +
|
||||||
|
" // 根据方法类型处理\n" +
|
||||||
|
" if (method === 'property') {\n" +
|
||||||
|
" // 属性数据解析\n" +
|
||||||
|
" if (data.params) {\n" +
|
||||||
|
" // 直接返回params中的数据\n" +
|
||||||
|
" return data.params;\n" +
|
||||||
|
" }\n" +
|
||||||
|
" } else if (method === 'event') {\n" +
|
||||||
|
" // 事件数据解析\n" +
|
||||||
|
" if (data.params) {\n" +
|
||||||
|
" return data.params;\n" +
|
||||||
|
" }\n" +
|
||||||
|
" }\n" +
|
||||||
|
" \n" +
|
||||||
|
" return result;\n" +
|
||||||
|
"}\n" +
|
||||||
|
"\n" +
|
||||||
|
"// 执行解析\n" +
|
||||||
|
"parse();";
|
||||||
|
|
||||||
|
// 缓存脚本
|
||||||
|
scriptCache.put(productKey, script);
|
||||||
|
}
|
||||||
|
|
||||||
|
return script;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置产品解析脚本
|
||||||
|
*
|
||||||
|
* @param productKey 产品Key
|
||||||
|
* @param script 脚本内容
|
||||||
|
*/
|
||||||
|
public void setScript(String productKey, String script) {
|
||||||
|
if (StrUtil.isNotBlank(productKey) && StrUtil.isNotBlank(script)) {
|
||||||
|
// 验证脚本是否有效
|
||||||
|
if (scriptService.validateScript("js", script)) {
|
||||||
|
scriptCache.put(productKey, script);
|
||||||
|
log.info("[setScript][设置产品:{}的解析脚本成功]", productKey);
|
||||||
|
} else {
|
||||||
|
log.warn("[setScript][脚本验证失败,不更新缓存] productKey:{}", productKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除产品解析脚本
|
||||||
|
*
|
||||||
|
* @param productKey 产品Key
|
||||||
|
*/
|
||||||
|
public void clearScript(String productKey) {
|
||||||
|
if (StrUtil.isNotBlank(productKey)) {
|
||||||
|
scriptCache.remove(productKey);
|
||||||
|
log.info("[clearScript][清除产品:{}的解析脚本]", productKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除所有脚本缓存
|
||||||
|
*/
|
||||||
|
public void clearAllScripts() {
|
||||||
|
scriptCache.clear();
|
||||||
|
log.info("[clearAllScripts][清除所有脚本缓存]");
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ import io.vertx.core.http.HttpServer;
|
||||||
import io.vertx.ext.web.Router;
|
import io.vertx.ext.web.Router;
|
||||||
import io.vertx.ext.web.handler.BodyHandler;
|
import io.vertx.ext.web.handler.BodyHandler;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IoT 设备下行服务端,接收来自 device 设备的请求,转发给 server 服务器
|
* IoT 设备下行服务端,接收来自 device 设备的请求,转发给 server 服务器
|
||||||
|
@ -24,7 +25,8 @@ public class IotDeviceUpstreamServer {
|
||||||
private final IotPluginHttpProperties properties;
|
private final IotPluginHttpProperties properties;
|
||||||
|
|
||||||
public IotDeviceUpstreamServer(IotPluginHttpProperties properties,
|
public IotDeviceUpstreamServer(IotPluginHttpProperties properties,
|
||||||
IotDeviceUpstreamApi deviceUpstreamApi) {
|
IotDeviceUpstreamApi deviceUpstreamApi,
|
||||||
|
ApplicationContext applicationContext) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
// 创建 Vertx 实例
|
// 创建 Vertx 实例
|
||||||
this.vertx = Vertx.vertx();
|
this.vertx = Vertx.vertx();
|
||||||
|
@ -33,7 +35,8 @@ public class IotDeviceUpstreamServer {
|
||||||
router.route().handler(BodyHandler.create()); // 处理 Body
|
router.route().handler(BodyHandler.create()); // 处理 Body
|
||||||
|
|
||||||
// 使用统一的 Handler 处理所有上行请求
|
// 使用统一的 Handler 处理所有上行请求
|
||||||
IotDeviceUpstreamVertxHandler upstreamHandler = new IotDeviceUpstreamVertxHandler(deviceUpstreamApi);
|
IotDeviceUpstreamVertxHandler upstreamHandler = new IotDeviceUpstreamVertxHandler(deviceUpstreamApi,
|
||||||
|
applicationContext);
|
||||||
router.post(IotDeviceUpstreamVertxHandler.PROPERTY_PATH).handler(upstreamHandler);
|
router.post(IotDeviceUpstreamVertxHandler.PROPERTY_PATH).handler(upstreamHandler);
|
||||||
router.post(IotDeviceUpstreamVertxHandler.EVENT_PATH).handler(upstreamHandler);
|
router.post(IotDeviceUpstreamVertxHandler.EVENT_PATH).handler(upstreamHandler);
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,12 @@ import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceStat
|
||||||
import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStateEnum;
|
import cn.iocoder.yudao.module.iot.enums.device.IotDeviceStateEnum;
|
||||||
import cn.iocoder.yudao.module.iot.plugin.common.pojo.IotStandardResponse;
|
import cn.iocoder.yudao.module.iot.plugin.common.pojo.IotStandardResponse;
|
||||||
import cn.iocoder.yudao.module.iot.plugin.common.util.IotPluginCommonUtils;
|
import cn.iocoder.yudao.module.iot.plugin.common.util.IotPluginCommonUtils;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.http.script.HttpScriptService;
|
||||||
import io.vertx.core.Handler;
|
import io.vertx.core.Handler;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -30,11 +31,9 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC
|
||||||
*
|
*
|
||||||
* @author haohao
|
* @author haohao
|
||||||
*/
|
*/
|
||||||
@RequiredArgsConstructor
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
|
|
||||||
// TODO @haohao:要不要类似 IotDeviceConfigSetVertxHandler 写的,把这些 PATH、METHOD 之类的抽走
|
|
||||||
/**
|
/**
|
||||||
* 属性上报路径
|
* 属性上报路径
|
||||||
*/
|
*/
|
||||||
|
@ -49,8 +48,14 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
private static final String EVENT_METHOD_SUFFIX = ".post";
|
private static final String EVENT_METHOD_SUFFIX = ".post";
|
||||||
|
|
||||||
private final IotDeviceUpstreamApi deviceUpstreamApi;
|
private final IotDeviceUpstreamApi deviceUpstreamApi;
|
||||||
|
private final HttpScriptService scriptService;
|
||||||
|
|
||||||
|
public IotDeviceUpstreamVertxHandler(IotDeviceUpstreamApi deviceUpstreamApi,
|
||||||
|
ApplicationContext applicationContext) {
|
||||||
|
this.deviceUpstreamApi = deviceUpstreamApi;
|
||||||
|
this.scriptService = applicationContext.getBean(HttpScriptService.class);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO @haohao:要不要分成多个 Handler?每个只解决一个问题哈。
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(RoutingContext routingContext) {
|
public void handle(RoutingContext routingContext) {
|
||||||
String path = routingContext.request().path();
|
String path = routingContext.request().path();
|
||||||
|
@ -68,7 +73,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
String method;
|
String method;
|
||||||
if (path.matches(".*/thing/event/property/post")) {
|
if (path.matches(".*/thing/event/property/post")) {
|
||||||
// 处理属性上报
|
// 处理属性上报
|
||||||
IotDevicePropertyReportReqDTO reportReqDTO = parsePropertyReportRequest(productKey, deviceName, requestId, body);
|
IotDevicePropertyReportReqDTO reportReqDTO = parsePropertyReportRequest(productKey, deviceName,
|
||||||
|
requestId, body);
|
||||||
|
|
||||||
// 设备上线
|
// 设备上线
|
||||||
updateDeviceState(reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
|
updateDeviceState(reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
|
||||||
|
@ -79,7 +85,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
} else if (path.matches(".*/thing/event/.+/post")) {
|
} else if (path.matches(".*/thing/event/.+/post")) {
|
||||||
// 处理事件上报
|
// 处理事件上报
|
||||||
String identifier = routingContext.pathParam("identifier");
|
String identifier = routingContext.pathParam("identifier");
|
||||||
IotDeviceEventReportReqDTO reportReqDTO = parseEventReportRequest(productKey, deviceName, identifier, requestId, body);
|
IotDeviceEventReportReqDTO reportReqDTO = parseEventReportRequest(productKey, deviceName, identifier,
|
||||||
|
requestId, body);
|
||||||
|
|
||||||
// 设备上线
|
// 设备上线
|
||||||
updateDeviceState(reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
|
updateDeviceState(reportReqDTO.getProductKey(), reportReqDTO.getDeviceName());
|
||||||
|
@ -89,7 +96,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
method = EVENT_METHOD_PREFIX + identifier + EVENT_METHOD_SUFFIX;
|
method = EVENT_METHOD_PREFIX + identifier + EVENT_METHOD_SUFFIX;
|
||||||
} else {
|
} else {
|
||||||
// 不支持的请求路径
|
// 不支持的请求路径
|
||||||
IotStandardResponse errorResponse = IotStandardResponse.error(requestId, "unknown", BAD_REQUEST.getCode(), "不支持的请求路径");
|
IotStandardResponse errorResponse = IotStandardResponse.error(requestId, "unknown",
|
||||||
|
BAD_REQUEST.getCode(), "不支持的请求路径");
|
||||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -108,7 +116,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
: EVENT_METHOD_PREFIX + (routingContext.pathParams().containsKey("identifier")
|
: EVENT_METHOD_PREFIX + (routingContext.pathParams().containsKey("identifier")
|
||||||
? routingContext.pathParam("identifier")
|
? routingContext.pathParam("identifier")
|
||||||
: "unknown") + EVENT_METHOD_SUFFIX;
|
: "unknown") + EVENT_METHOD_SUFFIX;
|
||||||
IotStandardResponse errorResponse = IotStandardResponse.error(requestId, method, INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg());
|
IotStandardResponse errorResponse = IotStandardResponse.error(requestId, method,
|
||||||
|
INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg());
|
||||||
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,7 +130,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
*/
|
*/
|
||||||
private void updateDeviceState(String productKey, String deviceName) {
|
private void updateDeviceState(String productKey, String deviceName) {
|
||||||
deviceUpstreamApi.updateDeviceState(((IotDeviceStateUpdateReqDTO) new IotDeviceStateUpdateReqDTO()
|
deviceUpstreamApi.updateDeviceState(((IotDeviceStateUpdateReqDTO) new IotDeviceStateUpdateReqDTO()
|
||||||
.setRequestId(IdUtil.fastSimpleUUID()).setProcessId(IotPluginCommonUtils.getProcessId()).setReportTime(LocalDateTime.now())
|
.setRequestId(IdUtil.fastSimpleUUID()).setProcessId(IotPluginCommonUtils.getProcessId())
|
||||||
|
.setReportTime(LocalDateTime.now())
|
||||||
.setProductKey(productKey).setDeviceName(deviceName)).setState(IotDeviceStateEnum.ONLINE.getState()));
|
.setProductKey(productKey).setDeviceName(deviceName)).setState(IotDeviceStateEnum.ONLINE.getState()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,11 +144,16 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
* @param body 请求体
|
* @param body 请求体
|
||||||
* @return 属性上报请求 DTO
|
* @return 属性上报请求 DTO
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
private IotDevicePropertyReportReqDTO parsePropertyReportRequest(String productKey, String deviceName,
|
||||||
private IotDevicePropertyReportReqDTO parsePropertyReportRequest(String productKey, String deviceName, String requestId, JsonObject body) {
|
String requestId, JsonObject body) {
|
||||||
// 按照标准 JSON 格式处理属性数据
|
// 使用脚本解析数据
|
||||||
Map<String, Object> properties = new HashMap<>();
|
Map<String, Object> properties = scriptService.parsePropertyData(productKey, deviceName, body);
|
||||||
Map<String, Object> params = body.getJsonObject("params") != null ? body.getJsonObject("params").getMap() : null;
|
|
||||||
|
// 如果脚本解析结果为空,使用默认解析逻辑
|
||||||
|
if (properties.isEmpty()) {
|
||||||
|
properties = new HashMap<>();
|
||||||
|
Map<String, Object> params = body.getJsonObject("params") != null ? body.getJsonObject("params").getMap()
|
||||||
|
: null;
|
||||||
if (params != null) {
|
if (params != null) {
|
||||||
// 将标准格式的 params 转换为平台需要的 properties 格式
|
// 将标准格式的 params 转换为平台需要的 properties 格式
|
||||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||||
|
@ -146,6 +161,7 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
Object valueObj = entry.getValue();
|
Object valueObj = entry.getValue();
|
||||||
// 如果是复杂结构(包含 value 和 time)
|
// 如果是复杂结构(包含 value 和 time)
|
||||||
if (valueObj instanceof Map) {
|
if (valueObj instanceof Map) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, Object> valueMap = (Map<String, Object>) valueObj;
|
Map<String, Object> valueMap = (Map<String, Object>) valueObj;
|
||||||
properties.put(key, valueMap.getOrDefault("value", valueObj));
|
properties.put(key, valueMap.getOrDefault("value", valueObj));
|
||||||
} else {
|
} else {
|
||||||
|
@ -153,6 +169,7 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 构建属性上报请求 DTO
|
// 构建属性上报请求 DTO
|
||||||
return ((IotDevicePropertyReportReqDTO) new IotDevicePropertyReportReqDTO().setRequestId(requestId)
|
return ((IotDevicePropertyReportReqDTO) new IotDevicePropertyReportReqDTO().setRequestId(requestId)
|
||||||
|
@ -170,15 +187,20 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
|
||||||
* @param body 请求体
|
* @param body 请求体
|
||||||
* @return 事件上报请求 DTO
|
* @return 事件上报请求 DTO
|
||||||
*/
|
*/
|
||||||
private IotDeviceEventReportReqDTO parseEventReportRequest(String productKey, String deviceName, String identifier, String requestId, JsonObject body) {
|
private IotDeviceEventReportReqDTO parseEventReportRequest(String productKey, String deviceName, String identifier,
|
||||||
// 按照标准 JSON 格式处理事件参数
|
String requestId, JsonObject body) {
|
||||||
Map<String, Object> params;
|
// 使用脚本解析事件数据
|
||||||
|
Map<String, Object> params = scriptService.parseEventData(productKey, deviceName, identifier, body);
|
||||||
|
|
||||||
|
// 如果脚本解析结果为空,使用默认解析逻辑
|
||||||
|
if (params.isEmpty()) {
|
||||||
if (body.containsKey("params")) {
|
if (body.containsKey("params")) {
|
||||||
params = body.getJsonObject("params").getMap();
|
params = body.getJsonObject("params").getMap();
|
||||||
} else {
|
} else {
|
||||||
// 兼容旧格式
|
// 兼容旧格式
|
||||||
params = new HashMap<>();
|
params = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 构建事件上报请求 DTO
|
// 构建事件上报请求 DTO
|
||||||
return ((IotDeviceEventReportReqDTO) new IotDeviceEventReportReqDTO().setRequestId(requestId)
|
return ((IotDeviceEventReportReqDTO) new IotDeviceEventReportReqDTO().setRequestId(requestId)
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>yudao-module-iot-plugins</artifactId>
|
||||||
|
<groupId>cn.iocoder.boot</groupId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>yudao-module-iot-plugin-script</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>${project.artifactId}</name>
|
||||||
|
<description>IoT 插件脚本模块,提供JS引擎解析等功能</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 引入公共模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.iocoder.boot</groupId>
|
||||||
|
<artifactId>yudao-module-iot-plugin-common</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring相关依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 工具类相关 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- JavaScript 引擎 - 使用标准JSR-223实现 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.nashorn</groupId>
|
||||||
|
<artifactId>nashorn-core</artifactId>
|
||||||
|
<version>15.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 测试相关 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,132 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.context.PluginScriptContext;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本使用示例类
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class ScriptExample {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ScriptExample.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ScriptService scriptService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 示例:执行简单的JavaScript脚本
|
||||||
|
*/
|
||||||
|
public void executeSimpleScript() {
|
||||||
|
String script = "var result = a + b; result;";
|
||||||
|
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("a", 10);
|
||||||
|
params.put("b", 20);
|
||||||
|
|
||||||
|
Object result = scriptService.executeJavaScript(script, params);
|
||||||
|
logger.info("脚本执行结果: {}", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 示例:使用脚本处理设备数据
|
||||||
|
*
|
||||||
|
* @param deviceId 设备ID
|
||||||
|
* @param payload 设备原始数据
|
||||||
|
* @return 处理后的数据
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public Map<String, Object> processDeviceData(String deviceId, String payload) {
|
||||||
|
// 设备数据处理脚本
|
||||||
|
String script = "function process() {\n" +
|
||||||
|
" var data = JSON.parse(payload);\n" +
|
||||||
|
" var result = {};\n" +
|
||||||
|
" // 提取温度信息\n" +
|
||||||
|
" if (data.temp) {\n" +
|
||||||
|
" result.temperature = data.temp;\n" +
|
||||||
|
" }\n" +
|
||||||
|
" // 提取湿度信息\n" +
|
||||||
|
" if (data.hum) {\n" +
|
||||||
|
" result.humidity = data.hum;\n" +
|
||||||
|
" }\n" +
|
||||||
|
" // 计算额外信息\n" +
|
||||||
|
" if (data.temp && data.temp > 30) {\n" +
|
||||||
|
" result.alert = true;\n" +
|
||||||
|
" result.alertMessage = '温度过高警告';\n" +
|
||||||
|
" }\n" +
|
||||||
|
" return result;\n" +
|
||||||
|
"}\n" +
|
||||||
|
"process();";
|
||||||
|
|
||||||
|
// 创建脚本上下文
|
||||||
|
PluginScriptContext context = new PluginScriptContext();
|
||||||
|
context.withDeviceContext(deviceId, null);
|
||||||
|
context.withParameter("payload", payload);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Object result = scriptService.executeJavaScript(script, context);
|
||||||
|
if (result != null) {
|
||||||
|
// 处理结果
|
||||||
|
logger.info("设备数据处理结果: {}", result);
|
||||||
|
|
||||||
|
// 安全地将结果转换为Map
|
||||||
|
if (result instanceof Map) {
|
||||||
|
return (Map<String, Object>) result;
|
||||||
|
} else {
|
||||||
|
logger.warn("脚本返回结果类型不是Map: {}", result.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("处理设备数据失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 示例:生成设备命令
|
||||||
|
*
|
||||||
|
* @param deviceId 设备ID
|
||||||
|
* @param command 命令名称
|
||||||
|
* @param params 命令参数
|
||||||
|
* @return 格式化的命令字符串
|
||||||
|
*/
|
||||||
|
public String generateDeviceCommand(String deviceId, String command, Map<String, Object> params) {
|
||||||
|
// 命令生成脚本
|
||||||
|
String script = "function generateCommand(cmd, params) {\n" +
|
||||||
|
" var result = { 'cmd': cmd };\n" +
|
||||||
|
" if (params) {\n" +
|
||||||
|
" result.params = params;\n" +
|
||||||
|
" }\n" +
|
||||||
|
" result.timestamp = new Date().getTime();\n" +
|
||||||
|
" result.deviceId = deviceId;\n" +
|
||||||
|
" return JSON.stringify(result);\n" +
|
||||||
|
"}\n" +
|
||||||
|
"generateCommand(command, commandParams);";
|
||||||
|
|
||||||
|
// 创建脚本上下文
|
||||||
|
PluginScriptContext context = new PluginScriptContext();
|
||||||
|
context.setParameter("deviceId", deviceId);
|
||||||
|
context.setParameter("command", command);
|
||||||
|
context.setParameter("commandParams", params);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Object result = scriptService.executeJavaScript(script, context);
|
||||||
|
if (result instanceof String) {
|
||||||
|
return (String) result;
|
||||||
|
} else if (result != null) {
|
||||||
|
logger.warn("脚本返回结果类型不是String: {}", result.getClass().getName());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("生成设备命令失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.config;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.engine.ScriptEngineFactory;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptService;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptServiceImpl;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本模块配置类
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class ScriptConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建脚本引擎工厂
|
||||||
|
*
|
||||||
|
* @return 脚本引擎工厂
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public ScriptEngineFactory scriptEngineFactory() {
|
||||||
|
return new ScriptEngineFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建脚本服务
|
||||||
|
*
|
||||||
|
* @param engineFactory 脚本引擎工厂
|
||||||
|
* @return 脚本服务
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public ScriptService scriptService(ScriptEngineFactory engineFactory) {
|
||||||
|
ScriptServiceImpl service = new ScriptServiceImpl();
|
||||||
|
// 如果有其他配置可以在这里设置
|
||||||
|
return service;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,124 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.context;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插件脚本上下文,提供插件执行脚本的上下文环境
|
||||||
|
*/
|
||||||
|
public class PluginScriptContext implements ScriptContext {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上下文参数
|
||||||
|
*/
|
||||||
|
private final Map<String, Object> parameters = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上下文函数
|
||||||
|
*/
|
||||||
|
private final Map<String, Object> functions = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志函数接口
|
||||||
|
*/
|
||||||
|
public interface LogFunction {
|
||||||
|
void log(String message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建插件脚本上下文
|
||||||
|
*/
|
||||||
|
public PluginScriptContext() {
|
||||||
|
// 初始化上下文,注册一些基础函数
|
||||||
|
LogFunction logFunction = message -> System.out.println("[Plugin Script] " + message);
|
||||||
|
registerFunction("log", logFunction);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建插件脚本上下文
|
||||||
|
*
|
||||||
|
* @param parameters 初始参数
|
||||||
|
*/
|
||||||
|
public PluginScriptContext(Map<String, Object> parameters) {
|
||||||
|
this();
|
||||||
|
if (parameters != null) {
|
||||||
|
this.parameters.putAll(parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getParameters() {
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getFunctions() {
|
||||||
|
return functions;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setParameter(String key, Object value) {
|
||||||
|
parameters.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getParameter(String key) {
|
||||||
|
return parameters.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerFunction(String name, Object function) {
|
||||||
|
functions.put(name, function);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量设置参数
|
||||||
|
*
|
||||||
|
* @param params 参数Map
|
||||||
|
* @return 当前上下文对象
|
||||||
|
*/
|
||||||
|
public PluginScriptContext withParameters(Map<String, Object> params) {
|
||||||
|
if (params != null) {
|
||||||
|
parameters.putAll(params);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加设备相关的上下文参数
|
||||||
|
*
|
||||||
|
* @param deviceId 设备ID
|
||||||
|
* @param deviceData 设备数据
|
||||||
|
* @return 当前上下文对象
|
||||||
|
*/
|
||||||
|
public PluginScriptContext withDeviceContext(String deviceId, Map<String, Object> deviceData) {
|
||||||
|
parameters.put("deviceId", deviceId);
|
||||||
|
parameters.put("deviceData", deviceData);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加消息相关的上下文参数
|
||||||
|
*
|
||||||
|
* @param topic 消息主题
|
||||||
|
* @param payload 消息内容
|
||||||
|
* @return 当前上下文对象
|
||||||
|
*/
|
||||||
|
public PluginScriptContext withMessageContext(String topic, Object payload) {
|
||||||
|
parameters.put("topic", topic);
|
||||||
|
parameters.put("payload", payload);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置单个参数
|
||||||
|
*
|
||||||
|
* @param key 参数名
|
||||||
|
* @param value 参数值
|
||||||
|
* @return 当前上下文对象
|
||||||
|
*/
|
||||||
|
public PluginScriptContext withParameter(String key, Object value) {
|
||||||
|
parameters.put(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.context;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本上下文接口,定义脚本执行所需的上下文环境
|
||||||
|
*/
|
||||||
|
public interface ScriptContext {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文参数
|
||||||
|
*
|
||||||
|
* @return 上下文参数
|
||||||
|
*/
|
||||||
|
Map<String, Object> getParameters();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文函数
|
||||||
|
*
|
||||||
|
* @return 上下文函数
|
||||||
|
*/
|
||||||
|
Map<String, Object> getFunctions();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置上下文参数
|
||||||
|
*
|
||||||
|
* @param key 参数名
|
||||||
|
* @param value 参数值
|
||||||
|
*/
|
||||||
|
void setParameter(String key, Object value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上下文参数
|
||||||
|
*
|
||||||
|
* @param key 参数名
|
||||||
|
* @return 参数值
|
||||||
|
*/
|
||||||
|
Object getParameter(String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册函数
|
||||||
|
*
|
||||||
|
* @param name 函数名称
|
||||||
|
* @param function 函数对象
|
||||||
|
*/
|
||||||
|
void registerFunction(String name, Object function);
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.engine;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.context.ScriptContext;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.sandbox.ScriptSandbox;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 抽象脚本引擎基类,定义脚本引擎的基本功能
|
||||||
|
*/
|
||||||
|
public abstract class AbstractScriptEngine {
|
||||||
|
|
||||||
|
protected ScriptSandbox sandbox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化脚本引擎
|
||||||
|
*/
|
||||||
|
public abstract void init();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行脚本
|
||||||
|
*
|
||||||
|
* @param script 脚本内容
|
||||||
|
* @param context 脚本上下文
|
||||||
|
* @return 脚本执行结果
|
||||||
|
*/
|
||||||
|
public abstract Object execute(String script, ScriptContext context);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行脚本
|
||||||
|
*
|
||||||
|
* @param script 脚本内容
|
||||||
|
* @param params 脚本参数
|
||||||
|
* @return 脚本执行结果
|
||||||
|
*/
|
||||||
|
public abstract Object execute(String script, Map<String, Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 销毁脚本引擎,释放资源
|
||||||
|
*/
|
||||||
|
public abstract void destroy();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置脚本沙箱
|
||||||
|
*
|
||||||
|
* @param sandbox 脚本沙箱
|
||||||
|
*/
|
||||||
|
public void setSandbox(ScriptSandbox sandbox) {
|
||||||
|
this.sandbox = sandbox;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,161 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.engine;
|
||||||
|
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.context.ScriptContext;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.sandbox.JsSandbox;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.util.ScriptUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.script.*;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JavaScript脚本引擎实现
|
||||||
|
* 使用JSR-223 Nashorn脚本引擎
|
||||||
|
*/
|
||||||
|
public class JsScriptEngine extends AbstractScriptEngine {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(JsScriptEngine.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认脚本执行超时时间(毫秒)
|
||||||
|
*/
|
||||||
|
private static final long DEFAULT_TIMEOUT_MS = 5000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JavaScript引擎名称
|
||||||
|
*/
|
||||||
|
private static final String JS_ENGINE_NAME = "nashorn";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本引擎管理器
|
||||||
|
*/
|
||||||
|
private ScriptEngineManager engineManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本引擎实例
|
||||||
|
*/
|
||||||
|
private ScriptEngine engine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本缓存
|
||||||
|
*/
|
||||||
|
private final Map<String, Object> cachedScripts = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
logger.info("初始化JavaScript脚本引擎");
|
||||||
|
|
||||||
|
// 创建脚本引擎管理器
|
||||||
|
engineManager = new ScriptEngineManager();
|
||||||
|
|
||||||
|
// 获取JavaScript引擎
|
||||||
|
engine = engineManager.getEngineByName(JS_ENGINE_NAME);
|
||||||
|
if (engine == null) {
|
||||||
|
logger.error("无法创建JavaScript引擎,尝试使用JavaScript名称获取");
|
||||||
|
engine = engineManager.getEngineByName("JavaScript");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (engine == null) {
|
||||||
|
throw new IllegalStateException("无法创建JavaScript引擎,请检查环境配置");
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("成功创建JavaScript引擎: {}", engine.getClass().getName());
|
||||||
|
|
||||||
|
// 默认使用JS沙箱
|
||||||
|
if (sandbox == null) {
|
||||||
|
setSandbox(new JsSandbox());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object execute(String script, ScriptContext context) {
|
||||||
|
if (engine == null) {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建可超时执行的任务
|
||||||
|
Callable<Object> task = () -> {
|
||||||
|
try {
|
||||||
|
// 创建脚本绑定
|
||||||
|
Bindings bindings = new SimpleBindings();
|
||||||
|
if (context != null) {
|
||||||
|
// 添加上下文参数
|
||||||
|
Map<String, Object> contextParams = context.getParameters();
|
||||||
|
if (MapUtil.isNotEmpty(contextParams)) {
|
||||||
|
bindings.putAll(contextParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加上下文函数
|
||||||
|
bindings.putAll(context.getFunctions());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 应用沙箱限制
|
||||||
|
if (sandbox != null) {
|
||||||
|
sandbox.applySandbox(engine, script);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行脚本
|
||||||
|
return engine.eval(script, bindings);
|
||||||
|
} catch (ScriptException e) {
|
||||||
|
logger.error("执行JavaScript脚本异常: {}", e.getMessage());
|
||||||
|
throw new RuntimeException("脚本执行异常: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 使用超时执行器执行脚本
|
||||||
|
return ScriptUtils.executeWithTimeout(task, DEFAULT_TIMEOUT_MS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("执行JavaScript脚本错误: {}", e.getMessage());
|
||||||
|
throw new RuntimeException("脚本执行失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object execute(String script, Map<String, Object> params) {
|
||||||
|
if (engine == null) {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建可超时执行的任务
|
||||||
|
Callable<Object> task = () -> {
|
||||||
|
try {
|
||||||
|
// 创建脚本绑定
|
||||||
|
Bindings bindings = new SimpleBindings();
|
||||||
|
if (MapUtil.isNotEmpty(params)) {
|
||||||
|
bindings.putAll(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 应用沙箱限制
|
||||||
|
if (sandbox != null) {
|
||||||
|
sandbox.applySandbox(engine, script);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行脚本
|
||||||
|
return engine.eval(script, bindings);
|
||||||
|
} catch (ScriptException e) {
|
||||||
|
logger.error("执行JavaScript脚本异常: {}", e.getMessage());
|
||||||
|
throw new RuntimeException("脚本执行异常: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 使用超时执行器执行脚本
|
||||||
|
return ScriptUtils.executeWithTimeout(task, DEFAULT_TIMEOUT_MS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("执行JavaScript脚本错误: {}", e.getMessage());
|
||||||
|
throw new RuntimeException("脚本执行失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
logger.info("销毁JavaScript脚本引擎");
|
||||||
|
cachedScripts.clear();
|
||||||
|
engine = null;
|
||||||
|
engineManager = null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.engine;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本引擎工厂,用于创建不同类型的脚本引擎
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class ScriptEngineFactory {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ScriptEngineFactory.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建JavaScript脚本引擎
|
||||||
|
*
|
||||||
|
* @return JavaScript脚本引擎
|
||||||
|
*/
|
||||||
|
public JsScriptEngine createJsEngine() {
|
||||||
|
logger.debug("创建JavaScript脚本引擎");
|
||||||
|
return new JsScriptEngine();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据脚本类型创建对应的脚本引擎
|
||||||
|
*
|
||||||
|
* @param scriptType 脚本类型
|
||||||
|
* @return 脚本引擎
|
||||||
|
*/
|
||||||
|
public AbstractScriptEngine createEngine(String scriptType) {
|
||||||
|
if (scriptType == null || scriptType.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("脚本类型不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (scriptType.toLowerCase()) {
|
||||||
|
case "js":
|
||||||
|
case "javascript":
|
||||||
|
return createJsEngine();
|
||||||
|
// 可以在这里添加其他类型的脚本引擎
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("不支持的脚本类型: " + scriptType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,97 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.sandbox;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JavaScript脚本沙箱,限制脚本的执行权限
|
||||||
|
*/
|
||||||
|
public class JsSandbox implements ScriptSandbox {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(JsSandbox.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 禁止使用的关键字
|
||||||
|
*/
|
||||||
|
private static final Set<String> FORBIDDEN_KEYWORDS = new HashSet<>(Arrays.asList(
|
||||||
|
"java.lang.System", "java.io", "java.nio", "java.net", "javax.net",
|
||||||
|
"java.security", "java.lang.reflect", "eval(", "Function(", "setTimeout",
|
||||||
|
"setInterval", "exec(", "execSync"));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 正则表达式匹配禁止的关键字
|
||||||
|
*/
|
||||||
|
private static final Pattern FORBIDDEN_PATTERN = Pattern.compile(
|
||||||
|
"(?:import\\s+\\{\\s*.*\\s*\\}\\s+from)|" +
|
||||||
|
"(?:require\\s*\\()|" +
|
||||||
|
"(?:process\\.)|" +
|
||||||
|
"(?:globalThis\\.)|" +
|
||||||
|
"(?:\\bfs\\.)|" +
|
||||||
|
"(?:\\bchild_process\\b)|" +
|
||||||
|
"(?:\\bwindow\\b)");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本执行超时时间(毫秒)
|
||||||
|
*/
|
||||||
|
private static final long SCRIPT_TIMEOUT_MS = 5000;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applySandbox(Object engineContext, String script) {
|
||||||
|
if (!(engineContext instanceof ScriptEngine)) {
|
||||||
|
throw new IllegalArgumentException("引擎上下文类型不正确,无法应用JavaScript沙箱");
|
||||||
|
}
|
||||||
|
|
||||||
|
ScriptEngine engine = (ScriptEngine) engineContext;
|
||||||
|
|
||||||
|
// 在Nashorn引擎中,可以通过以下方式设置安全限制
|
||||||
|
try {
|
||||||
|
// 设置严格模式
|
||||||
|
String securityPrefix = "'use strict';\n";
|
||||||
|
|
||||||
|
// 禁用Java.type等访问系统资源的功能
|
||||||
|
engine.eval("var Java = undefined;");
|
||||||
|
engine.eval("var JavaImporter = undefined;");
|
||||||
|
engine.eval("var Packages = undefined;");
|
||||||
|
|
||||||
|
// 增强安全控制可以在这里添加
|
||||||
|
logger.debug("已应用JavaScript安全沙箱限制");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warn("应用JavaScript沙箱限制失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validateScript(String script) {
|
||||||
|
if (script == null || script.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查禁止的关键字
|
||||||
|
for (String keyword : FORBIDDEN_KEYWORDS) {
|
||||||
|
if (script.contains(keyword)) {
|
||||||
|
logger.warn("脚本包含禁止使用的关键字: {}", keyword);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用正则表达式检查更复杂的模式
|
||||||
|
if (FORBIDDEN_PATTERN.matcher(script).find()) {
|
||||||
|
logger.warn("脚本包含禁止使用的模式");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 脚本长度限制
|
||||||
|
if (script.length() > 1024 * 100) { // 限制100KB
|
||||||
|
logger.warn("脚本太大,超过了限制");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.sandbox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本沙箱接口,提供脚本执行的安全限制
|
||||||
|
*/
|
||||||
|
public interface ScriptSandbox {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用沙箱限制到脚本执行环境
|
||||||
|
*
|
||||||
|
* @param engineContext 引擎上下文
|
||||||
|
* @param script 要执行的脚本内容
|
||||||
|
*/
|
||||||
|
void applySandbox(Object engineContext, String script);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查脚本是否符合安全规则
|
||||||
|
*
|
||||||
|
* @param script 要检查的脚本内容
|
||||||
|
* @return 是否安全
|
||||||
|
*/
|
||||||
|
boolean validateScript(String script);
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.service;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.context.ScriptContext;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本服务接口,定义脚本执行的核心功能
|
||||||
|
*/
|
||||||
|
public interface ScriptService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行脚本
|
||||||
|
*
|
||||||
|
* @param scriptType 脚本类型(如js、groovy等)
|
||||||
|
* @param script 脚本内容
|
||||||
|
* @param context 脚本上下文
|
||||||
|
* @return 脚本执行结果
|
||||||
|
*/
|
||||||
|
Object executeScript(String scriptType, String script, ScriptContext context);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行脚本
|
||||||
|
*
|
||||||
|
* @param scriptType 脚本类型(如js、groovy等)
|
||||||
|
* @param script 脚本内容
|
||||||
|
* @param params 脚本参数
|
||||||
|
* @return 脚本执行结果
|
||||||
|
*/
|
||||||
|
Object executeScript(String scriptType, String script, Map<String, Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行JavaScript脚本
|
||||||
|
*
|
||||||
|
* @param script 脚本内容
|
||||||
|
* @param context 脚本上下文
|
||||||
|
* @return 脚本执行结果
|
||||||
|
*/
|
||||||
|
Object executeJavaScript(String script, ScriptContext context);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行JavaScript脚本
|
||||||
|
*
|
||||||
|
* @param script 脚本内容
|
||||||
|
* @param params 脚本参数
|
||||||
|
* @return 脚本执行结果
|
||||||
|
*/
|
||||||
|
Object executeJavaScript(String script, Map<String, Object> params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证脚本内容是否安全
|
||||||
|
*
|
||||||
|
* @param scriptType 脚本类型
|
||||||
|
* @param script 脚本内容
|
||||||
|
* @return 脚本是否安全
|
||||||
|
*/
|
||||||
|
boolean validateScript(String scriptType, String script);
|
||||||
|
}
|
|
@ -0,0 +1,124 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.service;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.context.PluginScriptContext;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.context.ScriptContext;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.engine.AbstractScriptEngine;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.engine.ScriptEngineFactory;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.sandbox.JsSandbox;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.sandbox.ScriptSandbox;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本服务实现类
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class ScriptServiceImpl implements ScriptService {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ScriptServiceImpl.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ScriptEngineFactory engineFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本引擎缓存,避免重复创建
|
||||||
|
*/
|
||||||
|
private final Map<String, AbstractScriptEngine> engineCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本沙箱缓存
|
||||||
|
*/
|
||||||
|
private final Map<String, ScriptSandbox> sandboxCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
// 初始化常用的脚本引擎和沙箱
|
||||||
|
getEngine("js");
|
||||||
|
sandboxCache.put("js", new JsSandbox());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void destroy() {
|
||||||
|
// 销毁所有引擎
|
||||||
|
for (AbstractScriptEngine engine : engineCache.values()) {
|
||||||
|
try {
|
||||||
|
engine.destroy();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("销毁脚本引擎失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
engineCache.clear();
|
||||||
|
sandboxCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object executeScript(String scriptType, String script, ScriptContext context) {
|
||||||
|
if (scriptType == null || script == null) {
|
||||||
|
throw new IllegalArgumentException("脚本类型和内容不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取脚本引擎
|
||||||
|
AbstractScriptEngine engine = getEngine(scriptType);
|
||||||
|
|
||||||
|
// 验证脚本是否安全
|
||||||
|
if (!validateScript(scriptType, script)) {
|
||||||
|
throw new SecurityException("脚本包含不安全的代码,无法执行");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 执行脚本
|
||||||
|
return engine.execute(script, context);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("执行脚本失败: {}", e.getMessage());
|
||||||
|
throw new RuntimeException("执行脚本失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object executeScript(String scriptType, String script, Map<String, Object> params) {
|
||||||
|
// 创建默认上下文
|
||||||
|
ScriptContext context = new PluginScriptContext(params);
|
||||||
|
return executeScript(scriptType, script, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object executeJavaScript(String script, ScriptContext context) {
|
||||||
|
return executeScript("js", script, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object executeJavaScript(String script, Map<String, Object> params) {
|
||||||
|
return executeScript("js", script, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validateScript(String scriptType, String script) {
|
||||||
|
ScriptSandbox sandbox = sandboxCache.get(scriptType.toLowerCase());
|
||||||
|
if (sandbox == null) {
|
||||||
|
logger.warn("找不到脚本类型[{}]对应的沙箱,使用默认JS沙箱", scriptType);
|
||||||
|
sandbox = new JsSandbox();
|
||||||
|
sandboxCache.put(scriptType.toLowerCase(), sandbox);
|
||||||
|
}
|
||||||
|
return sandbox.validateScript(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取脚本引擎,如果不存在则创建
|
||||||
|
*
|
||||||
|
* @param scriptType 脚本类型
|
||||||
|
* @return 脚本引擎
|
||||||
|
*/
|
||||||
|
private AbstractScriptEngine getEngine(String scriptType) {
|
||||||
|
return engineCache.computeIfAbsent(scriptType.toLowerCase(), type -> {
|
||||||
|
AbstractScriptEngine engine = engineFactory.createEngine(type);
|
||||||
|
engine.init();
|
||||||
|
return engine;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,168 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script.util;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本工具类,提供执行脚本的辅助方法
|
||||||
|
*/
|
||||||
|
public class ScriptUtils {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ScriptUtils.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认脚本执行超时时间(毫秒)
|
||||||
|
*/
|
||||||
|
private static final long DEFAULT_TIMEOUT_MS = 3000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本执行线程池
|
||||||
|
*/
|
||||||
|
private static final ExecutorService SCRIPT_EXECUTOR = new ThreadPoolExecutor(
|
||||||
|
2, 10, 60L, TimeUnit.SECONDS,
|
||||||
|
new LinkedBlockingQueue<>(100),
|
||||||
|
r -> new Thread(r, "script-executor-" + r.hashCode()),
|
||||||
|
new ThreadPoolExecutor.CallerRunsPolicy());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 带超时的执行任务
|
||||||
|
*
|
||||||
|
* @param task 任务
|
||||||
|
* @param timeoutMs 超时时间(毫秒)
|
||||||
|
* @param <T> 返回类型
|
||||||
|
* @return 任务结果
|
||||||
|
* @throws RuntimeException 执行异常
|
||||||
|
*/
|
||||||
|
public static <T> T executeWithTimeout(Callable<T> task, long timeoutMs) {
|
||||||
|
Future<T> future = SCRIPT_EXECUTOR.submit(task);
|
||||||
|
try {
|
||||||
|
return future.get(timeoutMs, TimeUnit.MILLISECONDS);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
future.cancel(true);
|
||||||
|
throw new RuntimeException("脚本执行超时,已终止");
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw new RuntimeException("脚本执行被中断");
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
throw new RuntimeException("脚本执行失败: " + e.getCause().getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 带默认超时的执行任务
|
||||||
|
*
|
||||||
|
* @param task 任务
|
||||||
|
* @param <T> 返回类型
|
||||||
|
* @return 任务结果
|
||||||
|
* @throws RuntimeException 执行异常
|
||||||
|
*/
|
||||||
|
public static <T> T executeWithTimeout(Callable<T> task) {
|
||||||
|
return executeWithTimeout(task, DEFAULT_TIMEOUT_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭工具类的线程池
|
||||||
|
*/
|
||||||
|
public static void shutdown() {
|
||||||
|
SCRIPT_EXECUTOR.shutdown();
|
||||||
|
try {
|
||||||
|
if (!SCRIPT_EXECUTOR.awaitTermination(10, TimeUnit.SECONDS)) {
|
||||||
|
SCRIPT_EXECUTOR.shutdownNow();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
SCRIPT_EXECUTOR.shutdownNow();
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将JSON字符串转换为Map
|
||||||
|
*
|
||||||
|
* @param json JSON字符串
|
||||||
|
* @return Map对象,转换失败则返回null
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static Map<String, Object> parseJson(String json) {
|
||||||
|
try {
|
||||||
|
// 使用hutool的JSONUtil工具类解析JSON
|
||||||
|
return JSONUtil.toBean(json, Map.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("解析JSON失败: {}", e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试将对象转换为整数
|
||||||
|
*
|
||||||
|
* @param obj 需要转换的对象
|
||||||
|
* @return 转换后的整数,如果无法转换则返回null
|
||||||
|
*/
|
||||||
|
public static Integer toInteger(Object obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj instanceof Integer) {
|
||||||
|
return (Integer) obj;
|
||||||
|
} else if (obj instanceof Number) {
|
||||||
|
return ((Number) obj).intValue();
|
||||||
|
} else if (obj instanceof String) {
|
||||||
|
try {
|
||||||
|
return Integer.parseInt((String) obj);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
logger.debug("无法将字符串转换为整数: {}", obj);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug("无法将对象转换为整数: {}", obj.getClass().getName());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试将对象转换为双精度浮点数
|
||||||
|
*
|
||||||
|
* @param obj 需要转换的对象
|
||||||
|
* @return 转换后的双精度浮点数,如果无法转换则返回null
|
||||||
|
*/
|
||||||
|
public static Double toDouble(Object obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obj instanceof Double) {
|
||||||
|
return (Double) obj;
|
||||||
|
} else if (obj instanceof Number) {
|
||||||
|
return ((Number) obj).doubleValue();
|
||||||
|
} else if (obj instanceof String) {
|
||||||
|
try {
|
||||||
|
return Double.parseDouble((String) obj);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
logger.debug("无法将字符串转换为双精度浮点数: {}", obj);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug("无法将对象转换为双精度浮点数: {}", obj.getClass().getName());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 比较两个数值是否相等,忽略其具体类型
|
||||||
|
*
|
||||||
|
* @param a 第一个数值
|
||||||
|
* @param b 第二个数值
|
||||||
|
* @return 如果两个数值相等则返回true,否则返回false
|
||||||
|
*/
|
||||||
|
public static boolean numbersEqual(Number a, Number b) {
|
||||||
|
if (a == null || b == null) {
|
||||||
|
return a == b;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.abs(a.doubleValue() - b.doubleValue()) < 0.0000001;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
cn.iocoder.yudao.module.iot.plugin.script.config.ScriptConfiguration
|
|
@ -0,0 +1,125 @@
|
||||||
|
package cn.iocoder.yudao.module.iot.plugin.script;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.context.PluginScriptContext;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.engine.ScriptEngineFactory;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptService;
|
||||||
|
import cn.iocoder.yudao.module.iot.plugin.script.service.ScriptServiceImpl;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 脚本服务单元测试
|
||||||
|
*/
|
||||||
|
class ScriptServiceTest {
|
||||||
|
|
||||||
|
private ScriptService scriptService;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
ScriptEngineFactory engineFactory = new ScriptEngineFactory();
|
||||||
|
ScriptServiceImpl service = new ScriptServiceImpl();
|
||||||
|
|
||||||
|
// 使用反射设置engineFactory
|
||||||
|
try {
|
||||||
|
java.lang.reflect.Field field = ScriptServiceImpl.class.getDeclaredField("engineFactory");
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(service, engineFactory);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("设置测试依赖失败", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
service.init(); // 手动调用初始化方法
|
||||||
|
this.scriptService = service;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExecuteSimpleScript() {
|
||||||
|
// 准备
|
||||||
|
String script = "var result = a + b; result;";
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("a", 10);
|
||||||
|
params.put("b", 20);
|
||||||
|
|
||||||
|
// 执行
|
||||||
|
Object result = scriptService.executeJavaScript(script, params);
|
||||||
|
|
||||||
|
// 验证 - 使用delta比较,允许浮点数和整数比较
|
||||||
|
assertEquals(30.0, ((Number) result).doubleValue(), 0.001);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExecuteObjectResult() {
|
||||||
|
// 准备
|
||||||
|
String script = "var obj = { name: 'test', value: 123 }; obj;";
|
||||||
|
|
||||||
|
// 执行
|
||||||
|
Object result = scriptService.executeJavaScript(script, new HashMap<>());
|
||||||
|
|
||||||
|
// 验证
|
||||||
|
assertNotNull(result);
|
||||||
|
assertTrue(result instanceof Map);
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, Object> map = (Map<String, Object>) result;
|
||||||
|
assertEquals("test", map.get("name"));
|
||||||
|
|
||||||
|
// 对于数值,先转换为double再比较
|
||||||
|
assertEquals(123.0, ((Number) map.get("value")).doubleValue(), 0.001);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExecuteWithContext() {
|
||||||
|
// 准备
|
||||||
|
String script = "var message = 'Hello, ' + name + '!'; message;";
|
||||||
|
PluginScriptContext context = new PluginScriptContext();
|
||||||
|
context.setParameter("name", "World");
|
||||||
|
|
||||||
|
// 执行
|
||||||
|
Object result = scriptService.executeJavaScript(script, context);
|
||||||
|
|
||||||
|
// 验证
|
||||||
|
assertEquals("Hello, World!", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testScriptWithFunction() {
|
||||||
|
// 准备
|
||||||
|
String script = "function add(x, y) { return x + y; } add(a, b);";
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("a", 15);
|
||||||
|
params.put("b", 25);
|
||||||
|
|
||||||
|
// 执行
|
||||||
|
Object result = scriptService.executeJavaScript(script, params);
|
||||||
|
|
||||||
|
// 验证 - 使用delta比较,允许浮点数和整数比较
|
||||||
|
assertEquals(40.0, ((Number) result).doubleValue(), 0.001);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testExecuteInvalidScript() {
|
||||||
|
// 准备
|
||||||
|
String script = "invalid syntax";
|
||||||
|
|
||||||
|
// 执行和验证
|
||||||
|
assertThrows(RuntimeException.class, () -> {
|
||||||
|
scriptService.executeJavaScript(script, new HashMap<>());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testScriptTimeout() {
|
||||||
|
// 准备 - 一个无限循环的脚本
|
||||||
|
String script = "while(true) { }";
|
||||||
|
|
||||||
|
// 执行和验证
|
||||||
|
assertThrows(RuntimeException.class, () -> {
|
||||||
|
scriptService.executeJavaScript(script, new HashMap<>());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue