【功能新增】IoT: 添加脚本引擎模块,支持设备数据解析和命令生成

This commit is contained in:
安浩浩 2025-03-23 21:03:07 +08:00
parent 2f9d760327
commit 87a43b8354
22 changed files with 1589 additions and 66 deletions

View File

@ -9,6 +9,7 @@
</parent>
<modules>
<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-mqtt</module>
<module>yudao-module-iot-plugin-emqx</module>

View File

@ -94,34 +94,34 @@
</archive>
</configuration>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-shade-plugin</artifactId>-->
<!-- <version>3.6.0</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>package</phase>-->
<!-- <goals>-->
<!-- <goal>shade</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <minimizeJar>true</minimizeJar>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <configuration>-->
<!-- <archive>-->
<!-- <manifestEntries>-->
<!-- <Plugin-Id>${plugin.id}</Plugin-Id>-->
<!-- <Plugin-Class>${plugin.class}</Plugin-Class>-->
<!-- <Plugin-Version>${plugin.version}</Plugin-Version>-->
<!-- <Plugin-Provider>${plugin.provider}</Plugin-Provider>-->
<!-- <Plugin-Description>${plugin.description}</Plugin-Description>-->
<!-- <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>-->
<!-- </manifestEntries>-->
<!-- </archive>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-shade-plugin</artifactId>-->
<!-- <version>3.6.0</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>package</phase>-->
<!-- <goals>-->
<!-- <goal>shade</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <minimizeJar>true</minimizeJar>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <configuration>-->
<!-- <archive>-->
<!-- <manifestEntries>-->
<!-- <Plugin-Id>${plugin.id}</Plugin-Id>-->
<!-- <Plugin-Class>${plugin.class}</Plugin-Class>-->
<!-- <Plugin-Version>${plugin.version}</Plugin-Version>-->
<!-- <Plugin-Provider>${plugin.provider}</Plugin-Provider>-->
<!-- <Plugin-Description>${plugin.description}</Plugin-Description>-->
<!-- <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>-->
<!-- </manifestEntries>-->
<!-- </archive>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- 独立模式 -->
<plugin>
@ -161,5 +161,12 @@
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<!-- 添加脚本引擎模块依赖 -->
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-iot-plugin-script</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</project>

View File

@ -9,7 +9,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* 独立运行入口
*/
@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 static void main(String[] args) {

View File

@ -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.upstream.IotDeviceUpstreamServer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -19,8 +20,9 @@ public class IotPluginHttpAutoConfiguration {
@Bean(initMethod = "start", destroyMethod = "stop")
public IotDeviceUpstreamServer deviceUpstreamServer(IotDeviceUpstreamApi deviceUpstreamApi,
IotPluginHttpProperties properties) {
return new IotDeviceUpstreamServer(properties, deviceUpstreamApi);
IotPluginHttpProperties properties,
ApplicationContext applicationContext) {
return new IotDeviceUpstreamServer(properties, deviceUpstreamApi, applicationContext);
}
@Bean

View File

@ -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][清除所有脚本缓存]");
}
}

View File

@ -8,6 +8,7 @@ import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
/**
* IoT 设备下行服务端接收来自 device 设备的请求转发给 server 服务器
@ -24,7 +25,8 @@ public class IotDeviceUpstreamServer {
private final IotPluginHttpProperties properties;
public IotDeviceUpstreamServer(IotPluginHttpProperties properties,
IotDeviceUpstreamApi deviceUpstreamApi) {
IotDeviceUpstreamApi deviceUpstreamApi,
ApplicationContext applicationContext) {
this.properties = properties;
// 创建 Vertx 实例
this.vertx = Vertx.vertx();
@ -33,7 +35,8 @@ public class IotDeviceUpstreamServer {
router.route().handler(BodyHandler.create()); // 处理 Body
// 使用统一的 Handler 处理所有上行请求
IotDeviceUpstreamVertxHandler upstreamHandler = new IotDeviceUpstreamVertxHandler(deviceUpstreamApi);
IotDeviceUpstreamVertxHandler upstreamHandler = new IotDeviceUpstreamVertxHandler(deviceUpstreamApi,
applicationContext);
router.post(IotDeviceUpstreamVertxHandler.PROPERTY_PATH).handler(upstreamHandler);
router.post(IotDeviceUpstreamVertxHandler.EVENT_PATH).handler(upstreamHandler);

View File

@ -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.plugin.common.pojo.IotStandardResponse;
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.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import java.time.LocalDateTime;
import java.util.HashMap;
@ -30,11 +31,9 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC
*
* @author haohao
*/
@RequiredArgsConstructor
@Slf4j
public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
// TODO @haohao要不要类似 IotDeviceConfigSetVertxHandler 写的把这些 PATHMETHOD 之类的抽走
/**
* 属性上报路径
*/
@ -49,8 +48,14 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
private static final String EVENT_METHOD_SUFFIX = ".post";
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
public void handle(RoutingContext routingContext) {
String path = routingContext.request().path();
@ -68,7 +73,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
String method;
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());
@ -79,7 +85,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
} else if (path.matches(".*/thing/event/.+/post")) {
// 处理事件上报
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());
@ -89,7 +96,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
method = EVENT_METHOD_PREFIX + identifier + EVENT_METHOD_SUFFIX;
} else {
// 不支持的请求路径
IotStandardResponse errorResponse = IotStandardResponse.error(requestId, "unknown", BAD_REQUEST.getCode(), "不支持的请求路径");
IotStandardResponse errorResponse = IotStandardResponse.error(requestId, "unknown",
BAD_REQUEST.getCode(), "不支持的请求路径");
IotPluginCommonUtils.writeJsonResponse(routingContext, errorResponse);
return;
}
@ -108,7 +116,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
: EVENT_METHOD_PREFIX + (routingContext.pathParams().containsKey("identifier")
? routingContext.pathParam("identifier")
: "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);
}
}
@ -121,7 +130,8 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
*/
private void updateDeviceState(String productKey, String deviceName) {
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()));
}
@ -134,22 +144,29 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
* @param body 请求体
* @return 属性上报请求 DTO
*/
@SuppressWarnings("unchecked")
private IotDevicePropertyReportReqDTO parsePropertyReportRequest(String productKey, String deviceName, String requestId, JsonObject body) {
// 按照标准 JSON 格式处理属性数据
Map<String, Object> properties = new HashMap<>();
Map<String, Object> params = body.getJsonObject("params") != null ? body.getJsonObject("params").getMap() : null;
if (params != null) {
// 将标准格式的 params 转换为平台需要的 properties 格式
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
Object valueObj = entry.getValue();
// 如果是复杂结构包含 value time
if (valueObj instanceof Map) {
Map<String, Object> valueMap = (Map<String, Object>) valueObj;
properties.put(key, valueMap.getOrDefault("value", valueObj));
} else {
properties.put(key, valueObj);
private IotDevicePropertyReportReqDTO parsePropertyReportRequest(String productKey, String deviceName,
String requestId, JsonObject body) {
// 使用脚本解析数据
Map<String, Object> properties = scriptService.parsePropertyData(productKey, deviceName, body);
// 如果脚本解析结果为空使用默认解析逻辑
if (properties.isEmpty()) {
properties = new HashMap<>();
Map<String, Object> params = body.getJsonObject("params") != null ? body.getJsonObject("params").getMap()
: null;
if (params != null) {
// 将标准格式的 params 转换为平台需要的 properties 格式
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
Object valueObj = entry.getValue();
// 如果是复杂结构包含 value time
if (valueObj instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> valueMap = (Map<String, Object>) valueObj;
properties.put(key, valueMap.getOrDefault("value", valueObj));
} else {
properties.put(key, valueObj);
}
}
}
}
@ -170,14 +187,19 @@ public class IotDeviceUpstreamVertxHandler implements Handler<RoutingContext> {
* @param body 请求体
* @return 事件上报请求 DTO
*/
private IotDeviceEventReportReqDTO parseEventReportRequest(String productKey, String deviceName, String identifier, String requestId, JsonObject body) {
// 按照标准 JSON 格式处理事件参数
Map<String, Object> params;
if (body.containsKey("params")) {
params = body.getJsonObject("params").getMap();
} else {
// 兼容旧格式
params = new HashMap<>();
private IotDeviceEventReportReqDTO parseEventReportRequest(String productKey, String deviceName, String identifier,
String requestId, JsonObject body) {
// 使用脚本解析事件数据
Map<String, Object> params = scriptService.parseEventData(productKey, deviceName, identifier, body);
// 如果脚本解析结果为空使用默认解析逻辑
if (params.isEmpty()) {
if (body.containsKey("params")) {
params = body.getJsonObject("params").getMap();
} else {
// 兼容旧格式
params = new HashMap<>();
}
}
// 构建事件上报请求 DTO

View File

@ -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>

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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 脚本类型如jsgroovy等
* @param script 脚本内容
* @param context 脚本上下文
* @return 脚本执行结果
*/
Object executeScript(String scriptType, String script, ScriptContext context);
/**
* 执行脚本
*
* @param scriptType 脚本类型如jsgroovy等
* @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);
}

View File

@ -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;
});
}
}

View File

@ -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;
}
}

View File

@ -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<>());
});
}
}