From 43b64c32a15091724c6a697252d4bc0f7b5f956f Mon Sep 17 00:00:00 2001 From: Chin Yi Han Date: Thu, 4 Jul 2024 11:37:20 +0200 Subject: [PATCH] Implemented formula equation for virtual plugin Signed-off-by: Chin Yi Han --- blueprint/designer.html | 2 +- js_plugins/virtual.js | 80 +++++++++++++++++++++++++++++++++-------- template/virtual.html | 1 + typedef/virtual.yaml | 5 +++ 4 files changed, 72 insertions(+), 16 deletions(-) diff --git a/blueprint/designer.html b/blueprint/designer.html index 5f837e0..e57d8da 100644 --- a/blueprint/designer.html +++ b/blueprint/designer.html @@ -126,7 +126,7 @@ {% end %} {% if data['style'][key]['type'] == "virtual" %} -
{{ data['style'][key]['div']['text-name'] }} XXX.XX {{ data['style'][key]['div']['unit-name'] }}
diff --git a/js_plugins/virtual.js b/js_plugins/virtual.js index b6388a6..c32c97c 100644 --- a/js_plugins/virtual.js +++ b/js_plugins/virtual.js @@ -26,38 +26,88 @@ function parse_virtual(id, response) { var data_smaller_than = $("#" + id).attr("data-smaller-than"); var data_larger_than = $("#" + id).attr("data-larger-than"); var data_exp = $("#" + id).attr("data-exp"); + var data_formula = $("#" + id).attr("data-formula"); + var delta = (currentTimeMillis - timestamp) / 1000.0; // since delta is converted to seconds, we need to convert invalid to seconds as well invalid = invalid / 1000.0; - if (data_decimal_numbers != undefined && data_decimal_numbers > 0) { - value = (Math.round(value * 100) / 100).toFixed(data_decimal_numbers); + var myformula = data_formula.trim(); + var data = myformula; + var re= /\[(.*?)\]/g; + var buffer = [] + for(m = re.exec(data); m; m = re.exec(data)){ + buffer.push(m[1]); } - if (data_smaller_than != undefined && value < data_smaller_than) { - $("#" + id + "> .value").css('color', 'red'); - $("#" + id + "> .unit-name").css('color', 'red'); - } else { - $("#" + id + "> .value").css('color', 'green'); - $("#" + id + "> .unit-name").css('color', 'green'); - } + var finalstring = ""; + var pattern = /\[(.*?)\]/g; + var teststring = myformula.split(/[\[\]]+/); - if (data_larger_than != undefined && value > data_larger_than) { - $("#" + id + "> .value").css('color', 'red'); - $("#" + id + "> .unit-name").css('color', 'red'); - } else { - $("#" + id + "> .value").css('color', 'green'); - $("#" + id + "> .unit-name").css('color', 'green'); + for (var i = 0; i < teststring.length; i++) { + var val = ""; + if (teststring[i] in response) { + val = response[teststring[i]]["value"]; + } else { + val = teststring[i]; + } + finalstring += val; + finalstring += " "; + } + + finalstring = finalstring.trim(); + console.log("(line code delay) suppress error from math.js"); + console.log("(line code delay) suppress error from math.js"); + + var value = math.round(math.eval(finalstring),parseInt(data_decimal_numbers)); + + + if (data_smaller_than != undefined && data_larger_than != undefined) { + + if (value > data_smaller_than && value < data_larger_than) { + $("#" + key + "> .value").css('color', 'green'); + $("#" + key + "> .unit-name").css('color', 'green'); + } else if (data_smaller_than == '' && data_larger_than == '') { + $("#" + key + "> .value").css('color', 'green'); + $("#" + key + "> .unit-name").css('color', 'green'); + } else if (value < data_smaller_than && data_larger_than == '') { + $("#" + key + "> .value").css('color', 'red'); + $("#" + key + "> .unit-name").css('color', 'red'); + } else if (value > data_smaller_than && data_larger_than == '') { + $("#" + key + "> .value").css('color', 'green'); + $("#" + key + "> .unit-name").css('color', 'green'); + } else if (value > data_larger_than && data_smaller_than == '') { + $("#" + key + "> .value").css('color', 'red'); + $("#" + key + "> .unit-name").css('color', 'red'); + } else if (value < data_larger_than && data_smaller_than == '') { + $("#" + key + "> .value").css('color', 'green'); + $("#" + key + "> .unit-name").css('color', 'green'); + } else { + // value < data_smaller_than && value > data_larger_than + $("#" + key + "> .value").css('color', 'red'); + $("#" + key + "> .unit-name").css('color', 'red'); + } } + if (data_exp != undefined && data_exp == "true") { if (data_decimal_numbers != undefined && data_decimal_numbers > 0) { value = parseFloat(value).toExponential(data_decimal_numbers); } else { value = parseFloat(value).toExponential(); } + } else { + // when data_exp == false + if (data_decimal_numbers != undefined && data_decimal_numbers >= 0) { + if (value >= 0) { + value = (Math.round(value * 100) / 100).toFixed(data_decimal_numbers); + } else { + // negative value + value = ((value * 100) / 100).toFixed(data_decimal_numbers); + } + } } if (invalid != undefined && invalid < delta) { diff --git a/template/virtual.html b/template/virtual.html index b739e70..cf6456e 100644 --- a/template/virtual.html +++ b/template/virtual.html @@ -6,6 +6,7 @@ tooltip="" data-virtual-id="{{ data['style'][key]['div']['data-virtual-id'] }}" data-type="{{ data['style'][key]['type'] }}" + data-formula="{{ data['style'][key]['div']['data-formula'] }}" data-decimal-numbers="{{ data['style'][key]['div']['data-decimal-numbers'] }}" data-smaller-than="{{ data['style'][key]['div']['data-smaller-than'] }}" data-larger-than="{{ data['style'][key]['div']['data-larger-than'] }}" diff --git a/typedef/virtual.yaml b/typedef/virtual.yaml index bb5bd3e..31e4885 100644 --- a/typedef/virtual.yaml +++ b/typedef/virtual.yaml @@ -9,6 +9,11 @@ "type": "string" "optional": "Yes" "data": "No" + "formula": + "value": "" + "type": "string" + "optional": "Yes" + "data": "Yes" "virtual_id": "value": "" "type": "string"