# HG changeset patch # User Edouard Tisserant # Date 1651744123 -7200 # Node ID 2716cd8e498d58b224a423c7f2a7d2793ffb0a1e # Parent 0b5ab53007a995ee3bbb789b473afd89f031ab94 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. diff -r 0b5ab53007a9 -r 2716cd8e498d svghmi/parse_labels.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" > «.» diff -r 0b5ab53007a9 -r 2716cd8e498d svghmi/svghmi.js --- 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(); }); }; diff -r 0b5ab53007a9 -r 2716cd8e498d svghmi/widgets_common.ysl2 --- 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)