Kaynağa Gözat

update 脚本引擎

jay 2 yıl önce
ebeveyn
işleme
a6c9df4a24

+ 6 - 0
iot-components/iot-component-server/pom.xml

@@ -96,6 +96,12 @@
             <groupId>cc.iotkit</groupId>
             <groupId>cc.iotkit</groupId>
             <artifactId>iot-data-cache</artifactId>
             <artifactId>iot-data-cache</artifactId>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>cc.iotkit</groupId>
+            <artifactId>iot-components-engine</artifactId>
+            <version>0.4.2-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
 
 
     </dependencies>
     </dependencies>
 
 

+ 27 - 20
iot-components/iot-component-server/src/main/java/cc/iotkit/comps/DeviceMessageHandler.java

@@ -21,16 +21,17 @@ import cc.iotkit.converter.DeviceMessage;
 import cc.iotkit.comp.model.DeviceState;
 import cc.iotkit.comp.model.DeviceState;
 import cc.iotkit.comps.service.DeviceBehaviourService;
 import cc.iotkit.comps.service.DeviceBehaviourService;
 import cc.iotkit.converter.IConverter;
 import cc.iotkit.converter.IConverter;
+import cc.iotkit.engine.IScriptEngine;
+import cc.iotkit.engine.IScriptException;
+import cc.iotkit.engine.JsNashornScriptEngine;
 import cc.iotkit.model.device.message.ThingModelMessage;
 import cc.iotkit.model.device.message.ThingModelMessage;
-import jdk.nashorn.api.scripting.NashornScriptEngine;
-import jdk.nashorn.api.scripting.ScriptObjectMirror;
 import lombok.Data;
 import lombok.Data;
 import lombok.SneakyThrows;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.beanutils.BeanUtils;
 import org.apache.commons.beanutils.BeanUtils;
 
 
-import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import javax.script.ScriptException;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 import java.util.Map;
 import java.util.concurrent.*;
 import java.util.concurrent.*;
 import java.util.function.Consumer;
 import java.util.function.Consumer;
@@ -38,9 +39,9 @@ import java.util.function.Consumer;
 @Slf4j
 @Slf4j
 @Data
 @Data
 public class DeviceMessageHandler implements IMessageHandler {
 public class DeviceMessageHandler implements IMessageHandler {
-    private final NashornScriptEngine engine = (NashornScriptEngine) (new ScriptEngineManager()).getEngineByName("nashorn");
 
 
-    private final Object scriptObj;
+
+    private final IScriptEngine scriptEngine;
 
 
     private final IConverter converter;
     private final IConverter converter;
 
 
@@ -68,9 +69,9 @@ public class DeviceMessageHandler implements IMessageHandler {
         this.converter = converter;
         this.converter = converter;
         this.deviceBehaviourService = deviceBehaviourService;
         this.deviceBehaviourService = deviceBehaviourService;
         this.deviceRouter = deviceRouter;
         this.deviceRouter = deviceRouter;
-
-        engine.put("component", component);
-        scriptObj = engine.eval(String.format("new (function () {\n%s})()", script));
+        this.scriptEngine = new JsNashornScriptEngine();
+        scriptEngine.putScriptEnv("component", component);
+        scriptEngine.setScript(script);
     }
     }
 
 
     public void onReceive(Map<String, Object> head, String type, String msg) {
     public void onReceive(Map<String, Object> head, String type, String msg) {
@@ -81,22 +82,22 @@ public class DeviceMessageHandler implements IMessageHandler {
     public void onReceive(Map<String, Object> head, String type, String msg, Consumer<ReceiveResult> onResult) {
     public void onReceive(Map<String, Object> head, String type, String msg, Consumer<ReceiveResult> onResult) {
         executorService.submit(() -> {
         executorService.submit(() -> {
             try {
             try {
-                ScriptObjectMirror result = (ScriptObjectMirror) invokeMethod("onReceive", head, type, msg);
+                DeviceMsgScriptResult result = invokeMethodWithResult("onReceive", head, type, msg);
                 log.info("onReceive script result:{}", JsonUtil.toJsonString(result));
                 log.info("onReceive script result:{}", JsonUtil.toJsonString(result));
-                Object rstType = result.get("type");
+                Object rstType = result.getType();
                 if (rstType == null) {
                 if (rstType == null) {
                     onResult.accept(null);
                     onResult.accept(null);
                     return;
                     return;
                 }
                 }
                 //取脚本执行后返回的数据
                 //取脚本执行后返回的数据
-                Object data = JsonUtil.toObject((ScriptObjectMirror) result.get("data"));
+                Object data = result.getData();
                 if (!(data instanceof Map)) {
                 if (!(data instanceof Map)) {
                     throw new BizException("script result data is incorrect");
                     throw new BizException("script result data is incorrect");
                 }
                 }
 
 
                 Map<String, Object> dataMap = (Map) data;
                 Map<String, Object> dataMap = (Map) data;
                 //获取动作数据
                 //获取动作数据
-                Action action = getAction(result.get("action"));
+                Action action = getAction(result.getAction());
 
 
                 if ("register".equals(rstType)) {
                 if ("register".equals(rstType)) {
                     //注册数据
                     //注册数据
@@ -145,7 +146,7 @@ public class DeviceMessageHandler implements IMessageHandler {
         });
         });
     }
     }
 
 
-    private void doRegister(RegisterInfo reg) throws ScriptException, NoSuchMethodException {
+    private void doRegister(RegisterInfo reg) throws IScriptException {
         try {
         try {
             deviceBehaviourService.register(reg);
             deviceBehaviourService.register(reg);
         } catch (Throwable e) {
         } catch (Throwable e) {
@@ -155,7 +156,7 @@ public class DeviceMessageHandler implements IMessageHandler {
         }
         }
     }
     }
 
 
-    private void doAuth(AuthInfo auth) throws ScriptException, NoSuchMethodException {
+    private void doAuth(AuthInfo auth) throws IScriptException {
         try {
         try {
             deviceBehaviourService.deviceAuth(auth.getProductKey(),
             deviceBehaviourService.deviceAuth(auth.getProductKey(),
                     auth.getDeviceName(),
                     auth.getDeviceName(),
@@ -168,11 +169,16 @@ public class DeviceMessageHandler implements IMessageHandler {
         }
         }
     }
     }
 
 
-    private Object invokeMethod(String name, Object... args) throws ScriptException, NoSuchMethodException {
-        if (((ScriptObjectMirror) scriptObj).get(name) != null) {
-            return engine.invokeMethod(scriptObj, name, args);
-        }
-        return null;
+    private void invokeMethod(String name, Object... args) throws IScriptException {
+        scriptEngine.invokeMethod(name, args);
+
+    }
+
+    private DeviceMsgScriptResult invokeMethodWithResult(String name, Object... args) throws InvocationTargetException, IllegalAccessException, IScriptException {
+        Object o = scriptEngine.invokeMethod(name, args);
+        DeviceMsgScriptResult result = new DeviceMsgScriptResult();
+        BeanUtils.copyProperties(result, o);
+        return result;
     }
     }
 
 
     private void doStateChange(DeviceState state) {
     private void doStateChange(DeviceState state) {
@@ -239,7 +245,8 @@ public class DeviceMessageHandler implements IMessageHandler {
 
 
     @Override
     @Override
     public void putScriptEnv(String key, Object value) {
     public void putScriptEnv(String key, Object value) {
-        engine.put(key, value);
+
+        scriptEngine.putScriptEnv(key, value);
     }
     }
 
 
     @Data
     @Data

+ 17 - 0
iot-components/iot-component-server/src/main/java/cc/iotkit/comps/DeviceMsgScriptResult.java

@@ -0,0 +1,17 @@
+package cc.iotkit.comps;
+
+import lombok.Data;
+
+import java.util.Map;
+
+@Data
+public class DeviceMsgScriptResult {
+
+    private String type;
+
+    private Object data;
+
+
+    private Map action;
+
+}

+ 20 - 0
iot-components/iot-components-engine/pom.xml

@@ -0,0 +1,20 @@
+<?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">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>cc.iotkit</groupId>
+        <artifactId>iot-components</artifactId>
+        <version>0.4.2-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>iot-components-engine</artifactId>
+
+    <properties>
+        <maven.compiler.source>11</maven.compiler.source>
+        <maven.compiler.target>11</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+</project>

+ 12 - 0
iot-components/iot-components-engine/src/main/java/cc/iotkit/engine/IScriptEngine.java

@@ -0,0 +1,12 @@
+package cc.iotkit.engine;
+
+public interface IScriptEngine {
+
+    void setScript(String key) throws  IScriptException;
+
+    void putScriptEnv(String key, Object val);
+
+    Object invokeMethod(String methodName, Object ...args) throws IScriptException;
+
+
+}

+ 7 - 0
iot-components/iot-components-engine/src/main/java/cc/iotkit/engine/IScriptException.java

@@ -0,0 +1,7 @@
+package cc.iotkit.engine;
+
+public class IScriptException extends Exception{
+    public IScriptException(String message) {
+        super(message);
+    }
+}

+ 41 - 0
iot-components/iot-components-engine/src/main/java/cc/iotkit/engine/JsNashornScriptEngine.java

@@ -0,0 +1,41 @@
+package cc.iotkit.engine;
+
+
+import jdk.nashorn.api.scripting.NashornScriptEngine;
+import jdk.nashorn.api.scripting.ScriptObjectMirror;
+
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+public class JsNashornScriptEngine implements IScriptEngine{
+
+    private final NashornScriptEngine engine = (NashornScriptEngine) (new ScriptEngineManager()).getEngineByName("nashorn");
+    private  Object scriptObj;
+
+
+    @Override
+    public void setScript(String script) throws IScriptException {
+        try {
+            scriptObj = engine.eval(String.format("new (function () {\n%s})()", script));
+        } catch (ScriptException e) {
+            throw new IScriptException(e.getMessage());
+        }
+    }
+
+    @Override
+    public void putScriptEnv(String key, Object val) {
+        engine.put(key, val);
+    }
+
+    @Override
+    public Object invokeMethod(String methodName, Object... args) throws IScriptException{
+        if (((ScriptObjectMirror) scriptObj).get(methodName) != null) {
+            try {
+                return engine.invokeMethod(scriptObj, methodName, args);
+            } catch (Throwable e) {
+                throw new IScriptException(e.getMessage());
+            }
+        }
+        return null;
+    }
+}

Dosya farkı çok büyük olduğundan ihmal edildi
+ 0 - 0
iot-components/iot-mqtt-component/src/main/resources/ali-component.js


+ 2 - 1
iot-components/pom.xml

@@ -19,7 +19,8 @@
         <module>iot-component-base</module>
         <module>iot-component-base</module>
         <module>iot-http-biz-component</module>
         <module>iot-http-biz-component</module>
         <module>iot-component-tcp</module>
         <module>iot-component-tcp</module>
-<!--        <module>iot-ctwing-component</module>-->
+        <module>iot-components-engine</module>
+        <!--        <module>iot-ctwing-component</module>-->
     </modules>
     </modules>
 
 
 </project>
 </project>

Bu fark içinde çok fazla dosya değişikliği olduğu için bazı dosyalar gösterilmiyor