ソースを参照

增加了groovyShell支持

songwenbin 1 年間 前
コミット
d37bb5c0af

+ 1 - 0
electricity/meter/build.gradle

@@ -47,6 +47,7 @@ dependencies {
     implementation("$cloudGroup:spring-cloud-starter-openfeign")
     implementation("cn.hutool:hutool-all:5.8.18")
     implementation("org.springframework.boot:spring-boot-configuration-processor")
+    implementation('org.codehaus.groovy:groovy-all:2.4.15')
 
 }
 

+ 10 - 0
electricity/meter/src/main/java/com/gyee/gaia/meter/entity/MeterPoint.java

@@ -1,6 +1,7 @@
 package com.gyee.gaia.meter.entity;
 
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.gyee.gaia.common.utils.StringUtil;
 import lombok.Data;
 
 import java.io.Serializable;
@@ -147,5 +148,14 @@ public class MeterPoint implements Serializable {
      */
     private String property;
 
+    /**
+     * 计算公式
+     */
+    private String formula;
+
+
+    public String getNemCode2() {
+        return StringUtil.replace(nemCode,".","_");
+    }
 
 }

+ 39 - 0
electricity/meter/src/main/java/com/gyee/gaia/meter/util/AlarmScript.java

@@ -0,0 +1,39 @@
+package com.gyee.gaia.meter.util;
+
+import groovy.lang.Script;
+
+public class AlarmScript extends Script {
+
+    @Override
+    public Object run() {
+        return null;
+    }
+
+    public double dataSub(double d1, double d2) {
+        return d1 - d2;
+    }
+
+    /**
+     * 数据有变化就报警
+     * @return
+     */
+//    public boolean IsChanged() {
+//        long entityId = (long)getBinding().getVariable(ScriptShell.entityIdName);
+//        String variable = (String)getBinding().getVariable(ScriptShell.variableName);
+//        TsData currentData = (TsData)getBinding().getVariable(ScriptShell.tsDataName);
+//        TsData preData = ScriptShell.getPreTsData(entityId,variable);
+//        if (preData == null) {
+//            return  true;   //如果没有前值,直接触发报警?
+//        }
+//        if (preData.getTs() >= currentData.getTs()) {
+//            return false;   //过期的数据,不触发报警
+//        }
+//        if (preData.getValue().equals(currentData.getValue())) {
+//            return false;
+//        } else {
+//            return true;    //当前值与前值不一样,触发报警
+//        }
+//
+//    }
+//
+}

+ 43 - 0
electricity/meter/src/main/java/com/gyee/gaia/meter/util/ScriptShell.java

@@ -0,0 +1,43 @@
+package com.gyee.gaia.meter.util;
+
+import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
+import groovy.lang.Script;
+import org.codehaus.groovy.control.CompilerConfiguration;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class ScriptShell {
+    private static final GroovyShell shell;
+    private static ConcurrentHashMap<String, Script> cache = new ConcurrentHashMap<String, Script>();
+
+    static {
+        CompilerConfiguration cfg = new CompilerConfiguration();
+        cfg.setScriptBaseClass(AlarmScript.class.getName());
+        shell = new GroovyShell(cfg);
+    }
+
+    public static Object parseExpr(String expr) {
+        Script s = getScriptFromCache(expr);
+        return s.run();
+    }
+
+    public static Object parseExpr(String expr, Map<String, Object> map) {
+        Binding binding = new Binding(map);
+        Script script = getScriptFromCache(expr);
+        script.setBinding(binding);
+        return script.run();
+    }
+
+    private static Script getScriptFromCache(String expr) {
+        if (cache.containsKey(expr)) {
+            return cache.get(expr);
+        }
+        Script script = shell.parse(expr);
+        cache.put(expr, script);
+        return script;
+    }
+
+
+}

+ 121 - 0
electricity/meter/src/main/java/com/gyee/gaia/meter/util/TestScriptShell.java

@@ -0,0 +1,121 @@
+package com.gyee.gaia.meter.util;
+
+import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
+import groovy.lang.Script;
+import org.codehaus.groovy.control.CompilerConfiguration;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import com.gyee.gaia.meter.entity.MeterPoint;
+
+public class TestScriptShell {
+
+    private static final GroovyShell shell;
+
+    private static ConcurrentHashMap<String, Script> cache = new ConcurrentHashMap<String, Script>();
+
+    static {
+        CompilerConfiguration cfg = new CompilerConfiguration();
+        cfg.setScriptBaseClass(AlarmScript.class.getName());
+
+        shell = new GroovyShell(cfg);
+    }
+
+    public static final String entityIdName = "entityId";
+    public static final String variableName = "variable";
+    public static final String tsDataName = "tsData";
+
+    //   private static ConcurrentHashMap<Long,ConcurrentHashMap<String, TsData>> tsDataCache = new ConcurrentHashMap<Long,ConcurrentHashMap<String, TsData>>();
+
+    public static Object parseExpr(String expr) {
+        Script s = getScriptFromCache(expr);
+        return s.run();
+    }
+
+    public static Object parseExpr(String expr, Map<String, Object> map) {
+        Binding binding = new Binding(map);
+        Script script = getScriptFromCache(expr);
+        script.setBinding(binding);
+        return script.run();
+    }
+
+    private static Script getScriptFromCache(String expr) {
+        if (cache.containsKey(expr)) {
+            return cache.get(expr);
+        }
+        Script script = shell.parse(expr);
+        cache.put(expr, script);
+        return script;
+    }
+
+
+    public static void main(String[] args) {
+
+        MeterPoint mp1 = new MeterPoint();
+        mp1.setNemCode("MHSF_ZHCYDL_P1_FJC");
+        MeterPoint mp2 = new MeterPoint();
+        mp2.setNemCode("MHSF_ZHCYDL_P2_FJC");
+        MeterPoint mp3 = new MeterPoint();
+        mp3.setNemCode("MHSF_ZHCYDL_P0_FJC");
+        mp3.setFormula("MHSF_ZHCYDL_P1_FJC + MHSF_ZHCYDL_P2_FJC");
+
+
+        Map<String, Object> row = new HashMap<String, Object>();
+        row.put(mp1.getNemCode(), getMeterpointValue(mp1.getMeterCode()));
+        row.put(mp2.getNemCode(), getMeterpointValue(mp2.getMeterCode()));
+
+        Object obj1 = parseExpr(mp3.getFormula(), row);
+        System.out.println("发电量=" + obj1);
+
+
+    }
+
+    private static double getMeterpointValue(String meterPointCode) {
+        return 19999;
+    }
+
+
+    public static void main1(String[] args) {
+        Map<String, Object> row = new HashMap<String, Object>();
+        row.put("ai1", 13);
+        row.put("ai2", 2.1);
+        row.put("di1", false);
+
+//        DoubleTsData tsd = new DoubleTsData(1,(short)1,335.98);
+//        row.put("AI128", tsd);
+
+        //double a = Math.max()
+
+//        Object obj = parseExpr("AI128",row);
+//        System.out.println(obj);
+        Object obj1 = parseExpr("Math.max(ai1, ai2)", row);
+        System.out.println(obj1);
+        Object obj2 = parseExpr("ai1 > ai2 && di1", row);
+        System.out.println(obj2);
+        Object obj3 = parseExpr("dataSub(ai1,ai2)", row);
+        System.out.println(obj3);
+        Object obj4 = parseExpr("dataSub(ai2,ai1)", row);
+        System.out.println(obj4);
+
+        Map<String, Object> row1 = new HashMap<String, Object>();
+        row1.put("FDLI", 13000);
+        row1.put("FDLII", 5678);
+        //ZFDL = FDLI/1000 + FDLII*0.5
+        Object obj11 = parseExpr("FDLI/1000 + FDLII*0.5", row1);
+        System.out.println(obj11);
+    }
+
+    //eg1: fun1(var1)
+    //eg2: fun2(var1+var2*var3,const1)
+    //eg3: fun3(Math.fun(exp1, exp2...), exp)
+    //eg4: fun4(fun3, fun2,fun1)
+    //eg5: fun5(fun4, fun3)
+
+    //ar: varlist,funlist, scriptMap
+    // 函数内变量全部用引号括起
+    //wt: arlist, taglist
+    //tagInfo:
+
+}