SVGHMI: decoupled operation string evaluation from HMI variable uptdate in change_hmi_variable(), paving the way for min/max boundaries enforcement svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Thu, 24 Dec 2020 17:00:43 +0100
branchsvghmi
changeset 3098 5823b73b132f
parent 3097 a098b2dd9dff
child 3099 c7d14130401f
SVGHMI: decoupled operation string evaluation from HMI variable uptdate in change_hmi_variable(), paving the way for min/max boundaries enforcement
svghmi/svghmi.js
svghmi/widgets_common.ysl2
--- a/svghmi/svghmi.js	Thu Dec 24 16:56:19 2020 +0100
+++ b/svghmi/svghmi.js	Thu Dec 24 17:00:43 2020 +0100
@@ -267,21 +267,20 @@
 
 const quotes = {"'":null, '"':null};
 
-function change_hmi_value(index, opstr) {
+function eval_operation_string(old_val, opstr) {
     let op = opstr[0];
     let given_val;
     if(opstr.length < 2) 
-        return undefined; // TODO raise
+        return undefined;
     if(opstr[1] in quotes){
         if(opstr.length < 3) 
-            return undefined; // TODO raise
+            return undefined;
         if(opstr[opstr.length-1] == opstr[1]){
             given_val = opstr.slice(2,opstr.length-1);
         }
     } else {
         given_val = Number(opstr.slice(1));
     }
-    let old_val = cache[index];
     let new_val;
     switch(op){
       case "=":
@@ -300,11 +299,19 @@
         new_val = old_val / given_val;
         break;
     }
+    return new_val;
+}
+
+/*
+function change_hmi_value(index, opstr) {
+    let old_val = cache[index];
+    let new_val = eval_operation_string(old_val, opstr);
     if(new_val != undefined && old_val != new_val)
         send_hmi_value(index, new_val);
     // TODO else raise
     return new_val;
 }
+*/
 
 var current_visible_page;
 var current_subscribed_page;
--- a/svghmi/widgets_common.ysl2	Thu Dec 24 16:56:19 2020 +0100
+++ b/svghmi/widgets_common.ysl2	Thu Dec 24 17:00:43 2020 +0100
@@ -182,10 +182,13 @@
             }
             return index;
         }
+
         change_hmi_value(index, opstr) {
             let realindex = this.get_variable_index(index);
             if(realindex == undefined) return undefined;
-            return change_hmi_value(realindex, opstr);
+            let old_val = cache[realindex];
+            let new_val = eval_operation_string(old_val, opstr);
+            return apply_hmi_value(realindex, new_val);
         }
 
         apply_hmi_value(index, new_val) {