Explorar o código

实现公式配置处理,二汽水厂设备录入

欧阳劲驰 hai 1 mes
pai
achega
7214e8d08f

+ 1 - 0
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/constants/LogFlagBusiType.java

@@ -41,6 +41,7 @@ public enum LogFlagBusiType {
     BUSI_GIS_SURVEY(37,"Gis Survey Biz"),
     BUSI_PIPE_BURST(38,"Pipe Burst Biz"),
     ZAO_YANG_DMA(39,"Zao Yang Dma"),
+    ZHONG_HUAN_SACADA(40,"Zhong Huan Sacada"),
 
     BUSI_INTERNAL(99,"Internal Busi"),
 

+ 50 - 0
custom-gateway-core/src/main/java/com/shkpr/service/customgateway/core/utils/ExpressionUtil.java

@@ -0,0 +1,50 @@
+package com.shkpr.service.customgateway.core.utils;
+
+import com.global.base.log.LogLevelFlag;
+import com.global.base.log.LogPrintMgr;
+import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
+import org.springframework.expression.Expression;
+import org.springframework.expression.ExpressionParser;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+import org.springframework.expression.spel.support.StandardEvaluationContext;
+
+/**
+ * 公式工具类
+ *
+ * @author 欧阳劲驰
+ * @serial 1.0.0
+ */
+public class ExpressionUtil {
+    /**
+     * log
+     */
+    private static final String CLASS_NAME = "ExpressionUtil";
+    private static final String BIZ_TYPE = LogFlagBusiType.ZHONG_HUAN_SACADA.toStrValue();
+
+    //公式解析器
+    private final static ExpressionParser parser = new SpelExpressionParser();
+    //值name
+    private final static String VALUE_NAME = "v";
+
+    /**
+     * 计算公式
+     *
+     * @param value   原始值
+     * @param formula 公式
+     * @return 计算后的值
+     */
+    public static Double evaluateFormula(Double value, String formula) {
+        //解析公式
+        Expression expression = parser.parseExpression(formula);
+        //设置计算上下文
+        StandardEvaluationContext context = new StandardEvaluationContext();
+        context.setVariable(VALUE_NAME, value);
+
+        //计算
+        Double result = (Double) expression.getValue(context);
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, BIZ_TYPE, CLASS_NAME
+                , String.format("应用公式计算: 原始值=%f, 公式=%s, 计算结果=%f",
+                        value, formula, result));
+        return result;
+    }
+}

+ 18 - 1
custom-gateway-zhscada/src/main/java/com/shkpr/service/customgateway/zhscada/domain/ScadaPlatformData.java

@@ -3,8 +3,10 @@ package com.shkpr.service.customgateway.zhscada.domain;
 import com.fasterxml.jackson.annotation.JsonAlias;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.shkpr.service.customgateway.core.constants.InfluxdbMetadata;
+import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
 import com.shkpr.service.customgateway.core.domain.Device;
 import com.shkpr.service.customgateway.core.domain.DeviceTag;
+import com.shkpr.service.customgateway.core.utils.ExpressionUtil;
 import lombok.Data;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.math.NumberUtils;
@@ -28,6 +30,11 @@ import java.util.concurrent.TimeUnit;
 @Data
 public class ScadaPlatformData {
     /**
+     * log
+     */
+    private final static String mStrClassName = "PersonnelInfoServiceImpl";
+    private final static String mBizType = LogFlagBusiType.ZAO_YANG_DMA.toStrValue();
+    /**
      * 采集标签
      */
     @JsonAlias("N")
@@ -68,6 +75,16 @@ public class ScadaPlatformData {
                 .filter(t -> Objects.equals(n, t.getTag()))
                 .findFirst().orElse(null);
         if (tag == null) return null;
+
+        //值
+        Double value = Double.parseDouble(v);
+        //应用公式计算
+        if (StringUtils.isNotBlank(tag.getCalcFormula()))
+            value = ExpressionUtil.evaluateFormula(value, tag.getCalcFormula());
+        //数值范围校验
+        if (tag.getMinValue() != null && value < tag.getMinValue()) value = tag.getMinValue();
+        if (tag.getMaxValue() != null && value > tag.getMaxValue()) value = tag.getMaxValue();
+
         //获取时间戳
         long timestamp = LocalDateTime.now()
                 .truncatedTo(ChronoUnit.MINUTES)
@@ -83,7 +100,7 @@ public class ScadaPlatformData {
                 //时间
                 .time(timestamp, TimeUnit.MILLISECONDS)
                 //值
-                .addField(tag.getField(), Double.parseDouble(v))
+                .addField(tag.getField(), value)
                 .build();
     }
 }

+ 34 - 0
dev_zhscada.yml

@@ -2064,6 +2064,40 @@
       maxValue: null
       syncField: null
       calcFormula: null
+- deviceId: "710022511130069"
+  deviceName: "二汽水厂-中环水务供给"
+  deviceSn: null
+  deviceKind: "flow"
+  platform: "zhonghuan-scada"
+  mfrs: null
+  createTime: "2025-11-03 17:45:44"
+  tags:
+    - tag: "gw_zhgsljll"
+      protocol: "json"
+      measurement: "WaterMeter"
+      field: "flow_total_pos"
+      valueType: "double"
+      minValue: null
+      maxValue: null
+      syncField: null
+      calcFormula: "(#v < 0) ? -#v : #v"
+- deviceId: "710022511130070"
+  deviceName: "二汽水厂-中广核供给"
+  deviceSn: null
+  deviceKind: "flow"
+  platform: "zhonghuan-scada"
+  mfrs: null
+  createTime: "2025-11-03 17:45:44"
+  tags:
+    - tag: "gw_eqgsljll"
+      protocol: "json"
+      measurement: "WaterMeter"
+      field: "flow_total_pos"
+      valueType: "double"
+      minValue: null
+      maxValue: null
+      syncField: null
+      calcFormula: "(#v < 0) ? -#v : #v"
 - deviceId: "710032511130001"
   deviceName: "云湾泵站"
   deviceSn: null