Browse Source

设备详情添加函数计算

lixing 11 months ago
parent
commit
af924457cc

+ 20 - 2
src/views/equipment-center/real-time-data/european-gourd.vue

@@ -238,8 +238,9 @@ const parseData = (payload: any) => {
       updateAlarm(s.trueLight, item.groupName + s.variableName, s.value)
     })
 
-    item.valueVariables.forEach((v: any) => {
-      v.value = toData(byteArray, v.dataType, v.address)
+    item.valueVariables.forEach((e: any) => {
+      const value = toData(byteArray, e.dataType, e.address)
+      e.value = evaluateFormula(formatValue(value, e.translate), e.formula)
     })
 
     item.matchVariables.forEach((m: any) => {
@@ -266,6 +267,23 @@ const parseData = (payload: any) => {
   }
 }
 
+const evaluateFormula = (value: any, formula: any) => {
+  if (formula == null || formula == undefined || formula === '') {
+    return value
+  }
+  //函数模板
+  const array = formula.split(",")
+  //字符串模本
+  var templete = array[0]
+  const formulaArray = array.slice(1)
+  formulaArray.forEach((item: any, index: number) => {
+    const func = item.replace("{Value}", value)
+    const result = eval(func)
+    templete = templete.replace(`{${index}}`, Math.floor(parseFloat(result)))
+  })
+  return templete
+}
+
 
 const base64ToArray = (str: string) => {
   const base64Str = window.atob(str);

+ 23 - 2
src/views/equipment-center/real-time-data/index.vue

@@ -202,9 +202,10 @@ const parseData = (payload: any) => {
       var variable = group.valueVariables.find((f: any) => f.code == mqttCode)
       if (variable != null) {
         if (variable.variableType == 10) {
-          variable.value = mqttValue == true ? '开启' : '闭合'
+          variable.value = mqttValue === 'true' || mqttValue === true ? '开启' : '闭合'
         } else {
-          variable.value = mqttValue
+          let value = formatValue(mqttValue, variable.translate)
+          variable.value = evaluateFormula(value, variable.formula)
         }
         return
       }
@@ -212,6 +213,9 @@ const parseData = (payload: any) => {
   }
 }
 
+
+
+
 //解析档位
 const parseGear = (gear: any, code: any, value: any) => {
   if (gear != null) {
@@ -257,6 +261,23 @@ const updateAlarm = (trueLight: string, name: string, value: any) => {
   }
 }
 
+const evaluateFormula = (value: any, formula: any) => {
+  if (formula == null || formula == undefined || formula === '') {
+    return value
+  }
+  //函数模板
+  const array = formula.split(",")
+  //字符串模本
+  var templete = array[0]
+  const formulaArray = array.slice(1)
+  formulaArray.forEach((item: any, index: number) => {
+    const func = item.replace("{Value}", value)
+    const result = eval(func)
+    templete = templete.replace(`{${index}}`, Math.floor(parseFloat(result)))
+  })
+  return templete
+}
+
 const getColor = (el: any) => {
   const obj: any = {
     green: '#6AD28C',