# HG changeset patch # User Edouard Tisserant # Date 1598358759 -7200 # Node ID 4930455428df90aeefd771f1feb5002c3bda6635 # Parent d1fc8c55c1d312d21f957a79b27c90490b4a827c SVGHMI: JsonTable now use intermediate variables again to address JSON data without duplicating code or referencing. Using intermediate variables also alows to check for availability of data and stop evaluating early if data is missing. Finally added complete roundtrip example to illustrate use of JSonTable to display "alarms" collected in python from changes on PLC boolean variables. diff -r d1fc8c55c1d3 -r 4930455428df svghmi/widget_jsontable.ysl2 --- a/svghmi/widget_jsontable.ysl2 Mon Aug 24 09:48:35 2020 +0200 +++ b/svghmi/widget_jsontable.ysl2 Tue Aug 25 14:32:39 2020 +0200 @@ -7,7 +7,8 @@ do_http_request() { const query = { args: this.args, - vars: this.cache + vars: this.cache, + visible: this.visible }; const options = { @@ -155,15 +156,37 @@ template "svg:g", mode="json_table_render" { param "expressions"; param "widget_elts"; - /* TODO : use intermediate variables for optimization - foreach "$new_expressions" - | let obj_«@id»_«position()» = «.»; - */ + const "gid", "@id"; + + // use intermediate variables for optimization + const "varprefix" > obj_«$gid»_ + | try { + + foreach "$expressions/expression"{ + | let «$varprefix»«position()» = «@content»; + | if(«$varprefix»«position()» == undefined) { + | console.log("«$varprefix»«position()» = «@content»"); + | throw null; + | } + } + + // because we put values in a variables, we can replace corresponding expression with variable name + const "new_expressions" foreach "$expressions/expression" xsl:copy { + copy "@name"; + attrib "content" > «$varprefix»«position()» + } + + // revert hiding in case it did happen before + | id("«@id»").setAttribute("style", "«@style»"); + const "label", "func:filter_non_widget_label(., $widget_elts)"; apply "*", mode="json_table_render" { - with "expressions", "func:json_expressions($expressions, $label)"; + with "expressions", "func:json_expressions(exsl:node-set($new_expressions), $label)"; with "widget_elts", "$widget_elts"; } + | } catch(err) { + | id("«$gid»").setAttribute("style", "display:none"); + | } } template "widget[@type='JsonTable']", mode="widget_defs" { @@ -171,7 +194,10 @@ labels("data"); optional_labels("forward backward cursor"); const "data_elt", "$result_svg_ns//*[@id = $hmi_element/@id]/*[@inkscape:label = 'data']"; - | spread_json_data: function(jdata) { + | visible: «count($data_elt/*[@inkscape:label])», + | spread_json_data: function(janswer) { + | let [range,position,jdata] = janswer; + | console.log(range,position,jdata); apply "$data_elt/*", mode="json_table_render" { with "expressions","$initexpr_ns"; with "widget_elts","$hmi_element/*[@inkscape:label = 'data']/descendant::svg:*"; diff -r d1fc8c55c1d3 -r 4930455428df tests/svghmi/py_ext_0@py_ext/pyfile.xml --- a/tests/svghmi/py_ext_0@py_ext/pyfile.xml Mon Aug 24 09:48:35 2020 +0200 +++ b/tests/svghmi/py_ext_0@py_ext/pyfile.xml Tue Aug 25 14:32:39 2020 +0200 @@ -1,33 +1,59 @@ - + + + delta else old_position + new_visible = new_range if delta <= 0 else visible + + visible_alarms = [] + for ts, text, status in Alarms[new_position:new_position + new_visible]: + visible_alarms.append({ + "time": time.ctime(ts), + "text": text, # TODO translate text + "status": status + }) -def MyOnChangeFunc(changed_var_name): - print changed_var_name + ": " + getattr(PLCGlobals, changed_var_name) + return new_range, new_position, visible_alarms + ]]> diff -r d1fc8c55c1d3 -r 4930455428df tests/svghmi/svghmi_0@svghmi/svghmi.svg --- a/tests/svghmi/svghmi_0@svghmi/svghmi.svg Mon Aug 24 09:48:35 2020 +0200 +++ b/tests/svghmi/svghmi_0@svghmi/svghmi.svg Tue Aug 25 14:32:39 2020 +0200 @@ -185,9 +185,9 @@ inkscape:current-layer="hmi0" showgrid="false" units="px" - inkscape:zoom="0.43668246" - inkscape:cx="735.62981" - inkscape:cy="-925.76586" + inkscape:zoom="0.61756226" + inkscape:cx="494.4152" + inkscape:cy="527.07406" inkscape:window-width="1800" inkscape:window-height="836" inkscape:window-x="0" @@ -2639,196 +2639,6 @@ - 8888 - - - - dhu - - - - plop - - - - mhoo - - - - yodl - - - - mhe - - - @@ -2849,7 +2659,7 @@ width="100%" height="100%" transform="matrix(0.7609336,0,0,0.7609336,199.15217,164.3798)" - inkscape:label=".sides" /> + inkscape:label=".status" /> + inkscape:label=".status textContent=.time" /> + - - + transform="translate(-186,32)"> + inkscape:label="ack" /> + inkscape:label="alarm" /> + inkscape:label="active" /> - - + inkscape:label="disabled" /> + + 8888 + + + + + + + + send + + + + + sel_0 + +