From 87a43b8354c0638130a37a4e9d926aef1db0d608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=B5=A9=E6=B5=A9?= <1036606149@qq.com> Date: Sun, 23 Mar 2025 21:03:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E3=80=91IoT:=20=E6=B7=BB=E5=8A=A0=E8=84=9A=E6=9C=AC=E5=BC=95?= =?UTF-8?q?=E6=93=8E=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=94=AF=E6=8C=81=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E6=95=B0=E6=8D=AE=E8=A7=A3=E6=9E=90=E5=92=8C=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao-module-iot-plugins/pom.xml | 1 + .../yudao-module-iot-plugin-http/pom.xml | 63 ++--- .../plugin/http/IotHttpPluginApplication.java | 9 +- .../IotPluginHttpAutoConfiguration.java | 6 +- .../plugin/http/script/HttpScriptService.java | 228 ++++++++++++++++++ .../upstream/IotDeviceUpstreamServer.java | 7 +- .../router/IotDeviceUpstreamVertxHandler.java | 88 ++++--- .../yudao-module-iot-plugin-script/pom.xml | 61 +++++ .../iot/plugin/script/ScriptExample.java | 132 ++++++++++ .../script/config/ScriptConfiguration.java | 37 +++ .../script/context/PluginScriptContext.java | 124 ++++++++++ .../plugin/script/context/ScriptContext.java | 47 ++++ .../script/engine/AbstractScriptEngine.java | 51 ++++ .../plugin/script/engine/JsScriptEngine.java | 161 +++++++++++++ .../script/engine/ScriptEngineFactory.java | 44 ++++ .../iot/plugin/script/sandbox/JsSandbox.java | 97 ++++++++ .../plugin/script/sandbox/ScriptSandbox.java | 23 ++ .../plugin/script/service/ScriptService.java | 58 +++++ .../script/service/ScriptServiceImpl.java | 124 ++++++++++ .../iot/plugin/script/util/ScriptUtils.java | 168 +++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../iot/plugin/script/ScriptServiceTest.java | 125 ++++++++++ 22 files changed, 1589 insertions(+), 66 deletions(-) create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/script/HttpScriptService.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/pom.xml create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/ScriptExample.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/config/ScriptConfiguration.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/context/PluginScriptContext.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/context/ScriptContext.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/AbstractScriptEngine.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/JsScriptEngine.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/ScriptEngineFactory.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/sandbox/JsSandbox.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/sandbox/ScriptSandbox.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/service/ScriptService.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/service/ScriptServiceImpl.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/util/ScriptUtils.java create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/test/java/cn/iocoder/yudao/module/iot/plugin/script/ScriptServiceTest.java diff --git a/yudao-module-iot/yudao-module-iot-plugins/pom.xml b/yudao-module-iot/yudao-module-iot-plugins/pom.xml index d33292527b..d1722a8afc 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/pom.xml +++ b/yudao-module-iot/yudao-module-iot-plugins/pom.xml @@ -9,6 +9,7 @@ yudao-module-iot-plugin-common + yudao-module-iot-plugin-script yudao-module-iot-plugin-http yudao-module-iot-plugin-mqtt yudao-module-iot-plugin-emqx diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml index 88a413ca67..a8e599654c 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml @@ -94,34 +94,34 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -161,5 +161,12 @@ io.vertx vertx-web + + + + cn.iocoder.boot + yudao-module-iot-plugin-script + ${revision} + \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/IotHttpPluginApplication.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/IotHttpPluginApplication.java index a88b34eb31..d569ba3b83 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/IotHttpPluginApplication.java +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/IotHttpPluginApplication.java @@ -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) { diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/IotPluginHttpAutoConfiguration.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/IotPluginHttpAutoConfiguration.java index 63e55f58fe..133d463344 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/IotPluginHttpAutoConfiguration.java +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/IotPluginHttpAutoConfiguration.java @@ -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 diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/script/HttpScriptService.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/script/HttpScriptService.java new file mode 100644 index 0000000000..18a7731acc --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/script/HttpScriptService.java @@ -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 scriptCache = new ConcurrentHashMap<>(); + + /** + * 解析设备属性数据 + * + * @param productKey 产品Key + * @param deviceName 设备名称 + * @param payload 设备上报的原始数据 + * @return 解析后的属性数据 + */ + @SuppressWarnings("unchecked") + public Map 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) 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 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) 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][清除所有脚本缓存]"); + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/IotDeviceUpstreamServer.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/IotDeviceUpstreamServer.java index 67129a4d1c..3752a112b9 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/IotDeviceUpstreamServer.java +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/IotDeviceUpstreamServer.java @@ -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); diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/router/IotDeviceUpstreamVertxHandler.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/router/IotDeviceUpstreamVertxHandler.java index 79d465ea03..c161c3312f 100644 --- a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/router/IotDeviceUpstreamVertxHandler.java +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/upstream/router/IotDeviceUpstreamVertxHandler.java @@ -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 { - // TODO @haohao:要不要类似 IotDeviceConfigSetVertxHandler 写的,把这些 PATH、METHOD 之类的抽走 /** * 属性上报路径 */ @@ -49,8 +48,14 @@ public class IotDeviceUpstreamVertxHandler implements Handler { 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 { 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 { } 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 { 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 { : 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 { */ 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 { * @param body 请求体 * @return 属性上报请求 DTO */ - @SuppressWarnings("unchecked") - private IotDevicePropertyReportReqDTO parsePropertyReportRequest(String productKey, String deviceName, String requestId, JsonObject body) { - // 按照标准 JSON 格式处理属性数据 - Map properties = new HashMap<>(); - Map params = body.getJsonObject("params") != null ? body.getJsonObject("params").getMap() : null; - if (params != null) { - // 将标准格式的 params 转换为平台需要的 properties 格式 - for (Map.Entry entry : params.entrySet()) { - String key = entry.getKey(); - Object valueObj = entry.getValue(); - // 如果是复杂结构(包含 value 和 time) - if (valueObj instanceof Map) { - Map valueMap = (Map) 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 properties = scriptService.parsePropertyData(productKey, deviceName, body); + + // 如果脚本解析结果为空,使用默认解析逻辑 + if (properties.isEmpty()) { + properties = new HashMap<>(); + Map params = body.getJsonObject("params") != null ? body.getJsonObject("params").getMap() + : null; + if (params != null) { + // 将标准格式的 params 转换为平台需要的 properties 格式 + for (Map.Entry entry : params.entrySet()) { + String key = entry.getKey(); + Object valueObj = entry.getValue(); + // 如果是复杂结构(包含 value 和 time) + if (valueObj instanceof Map) { + @SuppressWarnings("unchecked") + Map valueMap = (Map) valueObj; + properties.put(key, valueMap.getOrDefault("value", valueObj)); + } else { + properties.put(key, valueObj); + } } } } @@ -170,14 +187,19 @@ public class IotDeviceUpstreamVertxHandler implements Handler { * @param body 请求体 * @return 事件上报请求 DTO */ - private IotDeviceEventReportReqDTO parseEventReportRequest(String productKey, String deviceName, String identifier, String requestId, JsonObject body) { - // 按照标准 JSON 格式处理事件参数 - Map 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 params = scriptService.parseEventData(productKey, deviceName, identifier, body); + + // 如果脚本解析结果为空,使用默认解析逻辑 + if (params.isEmpty()) { + if (body.containsKey("params")) { + params = body.getJsonObject("params").getMap(); + } else { + // 兼容旧格式 + params = new HashMap<>(); + } } // 构建事件上报请求 DTO diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/pom.xml b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/pom.xml new file mode 100644 index 0000000000..c40bf0b720 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/pom.xml @@ -0,0 +1,61 @@ + + + + yudao-module-iot-plugins + cn.iocoder.boot + ${revision} + + 4.0.0 + + yudao-module-iot-plugin-script + jar + + ${project.artifactId} + IoT 插件脚本模块,提供JS引擎解析等功能 + + + + + cn.iocoder.boot + yudao-module-iot-plugin-common + ${revision} + + + + + org.springframework + spring-context + + + + + cn.hutool + hutool-all + + + org.projectlombok + lombok + true + + + org.slf4j + slf4j-api + + + + + org.openjdk.nashorn + nashorn-core + 15.4 + + + + + org.springframework.boot + spring-boot-starter-test + test + + + \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/ScriptExample.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/ScriptExample.java new file mode 100644 index 0000000000..0c5db114b2 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/ScriptExample.java @@ -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 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 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) 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 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; + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/config/ScriptConfiguration.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/config/ScriptConfiguration.java new file mode 100644 index 0000000000..7f79240b1f --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/config/ScriptConfiguration.java @@ -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; + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/context/PluginScriptContext.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/context/PluginScriptContext.java new file mode 100644 index 0000000000..4bee8d0259 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/context/PluginScriptContext.java @@ -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 parameters = new HashMap<>(); + + /** + * 上下文函数 + */ + private final Map 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 parameters) { + this(); + if (parameters != null) { + this.parameters.putAll(parameters); + } + } + + @Override + public Map getParameters() { + return parameters; + } + + @Override + public Map 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 params) { + if (params != null) { + parameters.putAll(params); + } + return this; + } + + /** + * 添加设备相关的上下文参数 + * + * @param deviceId 设备ID + * @param deviceData 设备数据 + * @return 当前上下文对象 + */ + public PluginScriptContext withDeviceContext(String deviceId, Map 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; + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/context/ScriptContext.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/context/ScriptContext.java new file mode 100644 index 0000000000..7f41855fd4 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/context/ScriptContext.java @@ -0,0 +1,47 @@ +package cn.iocoder.yudao.module.iot.plugin.script.context; + +import java.util.Map; + +/** + * 脚本上下文接口,定义脚本执行所需的上下文环境 + */ +public interface ScriptContext { + + /** + * 获取上下文参数 + * + * @return 上下文参数 + */ + Map getParameters(); + + /** + * 获取上下文函数 + * + * @return 上下文函数 + */ + Map 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); +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/AbstractScriptEngine.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/AbstractScriptEngine.java new file mode 100644 index 0000000000..3401c0cf5b --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/AbstractScriptEngine.java @@ -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 params); + + /** + * 销毁脚本引擎,释放资源 + */ + public abstract void destroy(); + + /** + * 设置脚本沙箱 + * + * @param sandbox 脚本沙箱 + */ + public void setSandbox(ScriptSandbox sandbox) { + this.sandbox = sandbox; + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/JsScriptEngine.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/JsScriptEngine.java new file mode 100644 index 0000000000..79840e5036 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/JsScriptEngine.java @@ -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 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 task = () -> { + try { + // 创建脚本绑定 + Bindings bindings = new SimpleBindings(); + if (context != null) { + // 添加上下文参数 + Map 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 params) { + if (engine == null) { + init(); + } + + // 创建可超时执行的任务 + Callable 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; + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/ScriptEngineFactory.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/ScriptEngineFactory.java new file mode 100644 index 0000000000..86c0d28b51 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/engine/ScriptEngineFactory.java @@ -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); + } + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/sandbox/JsSandbox.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/sandbox/JsSandbox.java new file mode 100644 index 0000000000..55da7ded62 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/sandbox/JsSandbox.java @@ -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 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; + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/sandbox/ScriptSandbox.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/sandbox/ScriptSandbox.java new file mode 100644 index 0000000000..cd8d9cd505 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/sandbox/ScriptSandbox.java @@ -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); +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/service/ScriptService.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/service/ScriptService.java new file mode 100644 index 0000000000..70b3223fc4 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/service/ScriptService.java @@ -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 params); + + /** + * 执行JavaScript脚本 + * + * @param script 脚本内容 + * @param context 脚本上下文 + * @return 脚本执行结果 + */ + Object executeJavaScript(String script, ScriptContext context); + + /** + * 执行JavaScript脚本 + * + * @param script 脚本内容 + * @param params 脚本参数 + * @return 脚本执行结果 + */ + Object executeJavaScript(String script, Map params); + + /** + * 验证脚本内容是否安全 + * + * @param scriptType 脚本类型 + * @param script 脚本内容 + * @return 脚本是否安全 + */ + boolean validateScript(String scriptType, String script); +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/service/ScriptServiceImpl.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/service/ScriptServiceImpl.java new file mode 100644 index 0000000000..ab45b178fb --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/service/ScriptServiceImpl.java @@ -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 engineCache = new ConcurrentHashMap<>(); + + /** + * 脚本沙箱缓存 + */ + private final Map 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 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 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; + }); + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/util/ScriptUtils.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/util/ScriptUtils.java new file mode 100644 index 0000000000..fe294a3d8d --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/java/cn/iocoder/yudao/module/iot/plugin/script/util/ScriptUtils.java @@ -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 返回类型 + * @return 任务结果 + * @throws RuntimeException 执行异常 + */ + public static T executeWithTimeout(Callable task, long timeoutMs) { + Future 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 返回类型 + * @return 任务结果 + * @throws RuntimeException 执行异常 + */ + public static T executeWithTimeout(Callable 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 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; + } +} \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000000..386e03abac --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +cn.iocoder.yudao.module.iot.plugin.script.config.ScriptConfiguration \ No newline at end of file diff --git a/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/test/java/cn/iocoder/yudao/module/iot/plugin/script/ScriptServiceTest.java b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/test/java/cn/iocoder/yudao/module/iot/plugin/script/ScriptServiceTest.java new file mode 100644 index 0000000000..026d84d1f6 --- /dev/null +++ b/yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-script/src/test/java/cn/iocoder/yudao/module/iot/plugin/script/ScriptServiceTest.java @@ -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 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 map = (Map) 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 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<>()); + }); + } +} \ No newline at end of file