Commit 724512399a5f434f9fa5272b37800a361259b1b4
1 parent
4919439b
【功能新增】IoT:动态脚本 js、groovy demo
Showing
2 changed files
with
82 additions
and
0 deletions
yudao-module-iot/yudao-module-iot-biz/pom.xml
| ... | ... | @@ -81,6 +81,27 @@ |
| 81 | 81 | <artifactId>pf4j-spring</artifactId> |
| 82 | 82 | </dependency> |
| 83 | 83 | |
| 84 | + <!-- TODO @芋艿:bom 管理 --> | |
| 85 | + <dependency> | |
| 86 | + <groupId>org.apache.groovy</groupId> | |
| 87 | + <artifactId>groovy-all</artifactId> | |
| 88 | + <version>4.0.25</version> | |
| 89 | + <type>pom</type> | |
| 90 | + </dependency> | |
| 91 | + | |
| 92 | + <!-- TODO @芋艿:bom 管理 --> | |
| 93 | + <dependency> | |
| 94 | + <groupId>org.graalvm.js</groupId> | |
| 95 | + <artifactId>js</artifactId> | |
| 96 | + <version>24.1.2</version> | |
| 97 | + <type>pom</type> | |
| 98 | + </dependency> | |
| 99 | + <dependency> | |
| 100 | + <groupId>org.graalvm.js</groupId> | |
| 101 | + <artifactId>js-scriptengine</artifactId> | |
| 102 | + <version>24.1.2</version> | |
| 103 | + </dependency> | |
| 104 | + | |
| 84 | 105 | </dependencies> |
| 85 | 106 | |
| 86 | 107 | </project> | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/ScriptTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot; | |
| 2 | + | |
| 3 | +import cn.hutool.script.ScriptUtil; | |
| 4 | +import javax.script.Bindings; | |
| 5 | +import javax.script.ScriptEngine; | |
| 6 | +import javax.script.ScriptException; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * TODO 芋艿:测试脚本的接入 | |
| 10 | + */ | |
| 11 | +public class ScriptTest { | |
| 12 | + | |
| 13 | + public static void main2(String[] args) { | |
| 14 | + // 创建一个 Groovy 脚本引擎 | |
| 15 | + ScriptEngine engine = ScriptUtil.createGroovyEngine(); | |
| 16 | + | |
| 17 | + // 创建绑定参数 | |
| 18 | + Bindings bindings = engine.createBindings(); | |
| 19 | + bindings.put("name", "Alice"); | |
| 20 | + bindings.put("age", 30); | |
| 21 | + | |
| 22 | + // 定义一个稍微复杂的 Groovy 脚本 | |
| 23 | + String script = "def greeting = 'Hello, ' + name + '!';\n" + | |
| 24 | + "def ageInFiveYears = age + 5;\n" + | |
| 25 | + "def message = greeting + ' In five years, you will be ' + ageInFiveYears + ' years old.';\n" + | |
| 26 | + "return message.toUpperCase();\n"; | |
| 27 | + | |
| 28 | + try { | |
| 29 | + // 执行脚本并获取结果 | |
| 30 | + Object result = engine.eval(script, bindings); | |
| 31 | + System.out.println(result); // 输出: HELLO, ALICE! IN FIVE YEARS, YOU WILL BE 35 YEARS OLD. | |
| 32 | + } catch (ScriptException e) { | |
| 33 | + e.printStackTrace(); | |
| 34 | + } | |
| 35 | + } | |
| 36 | + | |
| 37 | + public static void main(String[] args) { | |
| 38 | + // 创建一个 JavaScript 脚本引擎 | |
| 39 | + ScriptEngine jsEngine = ScriptUtil.createJsEngine(); | |
| 40 | + | |
| 41 | + // 创建绑定参数 | |
| 42 | + Bindings jsBindings = jsEngine.createBindings(); | |
| 43 | + jsBindings.put("name", "Bob"); | |
| 44 | + jsBindings.put("age", 25); | |
| 45 | + | |
| 46 | + // 定义一个简单的 JavaScript 脚本 | |
| 47 | + String jsScript = "var greeting = 'Hello, ' + name + '!';\n" + | |
| 48 | + "var ageInTenYears = age + 10;\n" + | |
| 49 | + "var message = greeting + ' In ten years, you will be ' + ageInTenYears + ' years old.';\n" + | |
| 50 | + "message.toUpperCase();\n"; | |
| 51 | + | |
| 52 | + try { | |
| 53 | + // 执行脚本并获取结果 | |
| 54 | + Object jsResult = jsEngine.eval(jsScript, jsBindings); | |
| 55 | + System.out.println(jsResult); // 输出: HELLO, BOB! IN TEN YEARS, YOU WILL BE 35 YEARS OLD. | |
| 56 | + } catch (ScriptException e) { | |
| 57 | + e.printStackTrace(); | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | +} | ... | ... |