SVGHMI: Add support for forcing widget update frequency with period longer than a second. As an example, "HMI:Display|10s@/myvar" updates variable every 10 seconds.
authorEdouard Tisserant
Thu, 05 May 2022 11:48:43 +0200
changeset 3455 2716cd8e498d
parent 3454 0b5ab53007a9
child 3466 eadb3a85ceb7
child 3470 b36754171535
SVGHMI: Add support for forcing widget update frequency with period longer than a second. As an example, "HMI:Display|10s@/myvar" updates variable every 10 seconds.
svghmi/parse_labels.ysl2
svghmi/svghmi.js
svghmi/widgets_common.ysl2
--- a/svghmi/parse_labels.ysl2	Thu May 05 10:37:36 2022 +0200
+++ b/svghmi/parse_labels.ysl2	Thu May 05 11:48:43 2022 +0200
@@ -44,7 +44,12 @@
     if "$type" widget {
         attrib "id" > «$id»
         attrib "type" > «$type»
-        if "$freq" attrib "freq" > «$freq»
+        if "$freq" {
+            if "not(regexp:test($freq,'^[0-9]*(\.[0-9]+)?[smh]'))" {
+                error > Widget id:«$id» label:«$label» has wrong syntax of frequency forcing «$freq»
+            }
+            attrib "freq" > «$freq»
+        }
         foreach "str:split(substring-after($args, ':'), ':')" {
             arg {
                 attrib "value" > «.»
--- a/svghmi/svghmi.js	Thu May 05 10:37:36 2022 +0200
+++ b/svghmi/svghmi.js	Thu May 05 11:48:43 2022 +0200
@@ -18,16 +18,7 @@
 function init_widgets() {
     Object.keys(hmi_widgets).forEach(function(id) {
         let widget = hmi_widgets[id];
-        let init = widget.init;
-        if(typeof(init) == "function"){
-            try {
-                init.call(widget);
-            } catch(err) {
-                console.log(err);
-            }
-        }
-        if(widget.forced_frequency !== undefined)
-            widget.frequency = widget.forced_frequency;
+        widget.do_init();
     });
 };
 
--- a/svghmi/widgets_common.ysl2	Thu May 05 10:37:36 2022 +0200
+++ b/svghmi/widgets_common.ysl2	Thu May 05 11:48:43 2022 +0200
@@ -85,7 +85,7 @@
 
     const "freq" choose {
         when "$widget/@freq"
-            > «$widget/@freq»
+            > "«$widget/@freq»"
         otherwise
             > undefined
     }
@@ -186,6 +186,36 @@
             this.clip = true;
         }
 
+        do_init(){
+            if(widget.forced_frequency !== undefined){
+                let s = widget.forced_frequency;
+                /*
+                once every 10 seconds : 10s
+                once per minute : 1m
+                once per hour : 1h
+                once per day : 1d
+                */
+                let unit = s.slice(-1);
+                let factor = {
+                    "s":1,
+                    "m":60,
+                    "h":3600,
+                    "d":86400}[unit];
+
+                widget.frequency = factor ? 1/(factor * Number(s.slice(0,-1)))
+                                          : Number(s);
+            }
+
+            let init = this.init;
+            if(typeof(init) == "function"){
+                try {
+                    init.call(this);
+                } catch(err) {
+                    console.log(err);
+                }
+            }
+        }
+
         unsub(){
             /* remove subsribers */
             if(!this.unsubscribable)