|
@@ -1,5 +1,6 @@
|
|
package cc.iotkit.plugin.main.script;
|
|
package cc.iotkit.plugin.main.script;
|
|
|
|
|
|
|
|
+import cc.iotkit.common.utils.CodecUtil;
|
|
import cc.iotkit.common.utils.StringUtils;
|
|
import cc.iotkit.common.utils.StringUtils;
|
|
import cc.iotkit.data.manager.IPluginInfoData;
|
|
import cc.iotkit.data.manager.IPluginInfoData;
|
|
import cc.iotkit.model.plugin.PluginInfo;
|
|
import cc.iotkit.model.plugin.PluginInfo;
|
|
@@ -15,12 +16,14 @@ import io.vertx.core.parsetools.RecordParser;
|
|
import lombok.Setter;
|
|
import lombok.Setter;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@Component
|
|
@Component
|
|
@@ -30,6 +33,8 @@ public class ScriptVerticle extends AbstractVerticle {
|
|
|
|
|
|
private static final Map<String, IScriptEngine> PLUGIN_SCRIPT_ENGINES = new HashMap<>();
|
|
private static final Map<String, IScriptEngine> PLUGIN_SCRIPT_ENGINES = new HashMap<>();
|
|
|
|
|
|
|
|
+ private final Map<String, String> pluginScripts = new HashMap<>();
|
|
|
|
+
|
|
@Setter
|
|
@Setter
|
|
private long keepAliveTimeout = Duration.ofSeconds(30).toMillis();
|
|
private long keepAliveTimeout = Duration.ofSeconds(30).toMillis();
|
|
|
|
|
|
@@ -46,6 +51,9 @@ public class ScriptVerticle extends AbstractVerticle {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //缓存脚本md5,用于判断是否需要更新脚本
|
|
|
|
+ pluginScripts.put(pluginId, CodecUtil.md5Str(script));
|
|
|
|
+
|
|
IScriptEngine jsEngine = ScriptEngineFactory.getJsEngine(script);
|
|
IScriptEngine jsEngine = ScriptEngineFactory.getJsEngine(script);
|
|
PLUGIN_SCRIPT_ENGINES.put(pluginId, ScriptEngineFactory.getJsEngine(script));
|
|
PLUGIN_SCRIPT_ENGINES.put(pluginId, ScriptEngineFactory.getJsEngine(script));
|
|
return jsEngine;
|
|
return jsEngine;
|
|
@@ -58,6 +66,27 @@ public class ScriptVerticle extends AbstractVerticle {
|
|
return PLUGIN_SCRIPT_ENGINES.get(pluginId);
|
|
return PLUGIN_SCRIPT_ENGINES.get(pluginId);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
|
|
|
|
+ public void checkScriptUpdate() {
|
|
|
|
+ //定时检查脚本是否需要更新
|
|
|
|
+ pluginScripts.forEach((k, v) -> {
|
|
|
|
+ PluginInfo pluginInfo = pluginInfoData.findByPluginId(k);
|
|
|
|
+ if (pluginInfo == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String md5 = CodecUtil.md5Str(pluginInfo.getScript());
|
|
|
|
+ if (v.equals(md5)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ IScriptEngine scriptEngine = PLUGIN_SCRIPT_ENGINES.get(k);
|
|
|
|
+ if (scriptEngine == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ pluginScripts.put(k, md5);
|
|
|
|
+ scriptEngine.setScript(pluginInfo.getScript());
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void start() {
|
|
public void start() {
|
|
initTcpServer();
|
|
initTcpServer();
|
|
@@ -112,9 +141,9 @@ public class ScriptVerticle extends AbstractVerticle {
|
|
String pluginId = data.getPluginId();
|
|
String pluginId = data.getPluginId();
|
|
clientMap.put(pluginId, client);
|
|
clientMap.put(pluginId, client);
|
|
IScriptEngine scriptEngine = getScriptEngine(pluginId);
|
|
IScriptEngine scriptEngine = getScriptEngine(pluginId);
|
|
- if(scriptEngine==null){
|
|
|
|
|
|
+ if (scriptEngine == null) {
|
|
data.setResult("");
|
|
data.setResult("");
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
//调用执行脚本方法返回结果
|
|
//调用执行脚本方法返回结果
|
|
String result = scriptEngine.invokeMethod(data.getMethod(), data.getArgs());
|
|
String result = scriptEngine.invokeMethod(data.getMethod(), data.getArgs());
|
|
data.setResult(result);
|
|
data.setResult(result);
|