SVGHI: compute hmitree variables ordered index in xslt svghmi
authorEdouard Tisserant
Sat, 05 Oct 2019 09:45:58 +0200
branchsvghmi
changeset 2790 8fab1886ebec
parent 2789 ba0dd2ec6dc4
child 2791 d022523cb621
SVGHI: compute hmitree variables ordered index in xslt
svghmi/gen_index_xhtml.xslt
svghmi/gen_index_xhtml.ysl2
--- a/svghmi/gen_index_xhtml.xslt	Wed Oct 02 11:31:02 2019 +0200
+++ b/svghmi/gen_index_xhtml.xslt	Sat Oct 05 09:45:58 2019 +0200
@@ -1,8 +1,55 @@
 <?xml version="1.0"?>
-<xsl:stylesheet xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/1999/xhtml" xmlns:str="http://exslt.org/strings" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:exsl="http://exslt.org/common" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" exclude-result-prefixes="ns" extension-element-prefixes="ns" version="1.0">
+<xsl:stylesheet xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:str="http://exslt.org/strings" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:exsl="http://exslt.org/common" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" exclude-result-prefixes="ns" extension-element-prefixes="ns" version="1.0">
   <xsl:output method="xml" cdata-section-elements="script"/>
   <xsl:variable name="geometry" select="ns:GetSVGGeometry()"/>
   <xsl:variable name="hmitree" select="ns:GetHMITree()"/>
+  <xsl:variable name="_categories">
+    <noindex>
+      <xsl:text>HMI_ROOT</xsl:text>
+    </noindex>
+    <noindex>
+      <xsl:text>HMI_LABEL</xsl:text>
+    </noindex>
+    <noindex>
+      <xsl:text>HMI_CLASS</xsl:text>
+    </noindex>
+    <noindex>
+      <xsl:text>HMI_PLC_STATUS</xsl:text>
+    </noindex>
+    <noindex>
+      <xsl:text>HMI_CURRENT_PAGE</xsl:text>
+    </noindex>
+  </xsl:variable>
+  <xsl:variable name="categories" select="exsl:node-set($_categories)"/>
+  <xsl:variable name="indexed_hmitree">
+    <xsl:apply-templates mode="index" select="$hmitree"/>
+  </xsl:variable>
+  <xsl:template mode="index" match="node()">
+    <xsl:param name="index" select="0"/>
+    <xsl:variable name="content">
+      <xsl:choose>
+        <xsl:when test="not(local-name() = $categories/noindex)">
+          <xsl:copy>
+            <xsl:attribute name="index">
+              <xsl:value-of select="$index"/>
+            </xsl:attribute>
+            <xsl:for-each select="@*">
+              <xsl:copy/>
+            </xsl:for-each>
+          </xsl:copy>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:apply-templates mode="index" select="*[1]">
+            <xsl:with-param name="index" select="$index"/>
+          </xsl:apply-templates>
+        </xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+    <xsl:copy-of select="$content"/>
+    <xsl:apply-templates mode="index" select="following-sibling::*[1]">
+      <xsl:with-param name="index" select="$index + count(exsl:node-set($content)/*)"/>
+    </xsl:apply-templates>
+  </xsl:template>
   <xsl:template match="@* | node()">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
@@ -23,25 +70,14 @@
           <xsl:comment>
             <xsl:apply-templates mode="testtree" select="$hmitree"/>
           </xsl:comment>
+          <xsl:comment>
+            <xsl:apply-templates mode="testtree" select="exsl:node-set($indexed_hmitree)"/>
+          </xsl:comment>
           <xsl:apply-templates select="@* | node()"/>
         </xsl:copy>
         <script>
-          <xsl:text>function evaluate_js_from_descriptions() {
-</xsl:text>
-          <xsl:text>    var Page;
-</xsl:text>
-          <xsl:text>    var Input;
-</xsl:text>
-          <xsl:text>    var Display;
-</xsl:text>
-          <xsl:text>    var res = [];
-</xsl:text>
-          <xsl:variable name="midmark">
-            <xsl:text>
-</xsl:text>
-            <xsl:value-of select="$mark"/>
-          </xsl:variable>
-          <xsl:apply-templates mode="code_from_descs" select="//*[contains(child::svg:desc, $midmark) or                                starts-with(child::svg:desc, $mark)]"/>
+          <xsl:text>var subscriptions = {
+</xsl:text>
           <xsl:text>    return res;
 </xsl:text>
           <xsl:text>}
@@ -96,6 +132,66 @@
 </xsl:text>
           <xsl:text>    };
 </xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>    var pending_updates = {};
+</xsl:text>
+          <xsl:text>    
+</xsl:text>
+          <xsl:text>    // subscription state, as it should be in hmi server
+</xsl:text>
+          <xsl:text>    // expected {index:period}
+</xsl:text>
+          <xsl:text>    var subscriptions = {};
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>    // subscription state as needed by widget now
+</xsl:text>
+          <xsl:text>    // expected {index:[widgets]};
+</xsl:text>
+          <xsl:text>    var subscribers = {};
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>    // return the diff in between curently subscribed and subscription
+</xsl:text>
+          <xsl:text>    function update_subscriptions() {
+</xsl:text>
+          <xsl:text>        let result = [];
+</xsl:text>
+          <xsl:text>        Object.keys(subscribers).forEach(index =&gt; {
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>            let previous_period = subscriptions[index];
+</xsl:text>
+          <xsl:text>            let new_period = Math.min(...widgets.map(widget =&gt; widget.period));
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>            if(previous_period != new_period) 
+</xsl:text>
+          <xsl:text>                result.push({index: index, period: new_period});
+</xsl:text>
+          <xsl:text>        })
+</xsl:text>
+          <xsl:text>    }
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>    function update_value(index, value) {
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>    };
+</xsl:text>
+          <xsl:text>
+</xsl:text>
           <xsl:text>})();
 </xsl:text>
         </script>
@@ -159,11 +255,12 @@
     <xsl:text> </xsl:text>
     <xsl:value-of select="local-name()"/>
     <xsl:text> </xsl:text>
-    <xsl:value-of select="@name"/>
-    <xsl:text> </xsl:text>
-    <xsl:value-of select="@type"/>
-    <xsl:text> </xsl:text>
-    <xsl:value-of select="@path"/>
+    <xsl:for-each select="@*">
+      <xsl:value-of select="local-name()"/>
+      <xsl:text>=</xsl:text>
+      <xsl:value-of select="."/>
+      <xsl:text> </xsl:text>
+    </xsl:for-each>
     <xsl:text>
 </xsl:text>
     <xsl:apply-templates mode="testtree" select="*">
--- a/svghmi/gen_index_xhtml.ysl2	Wed Oct 02 11:31:02 2019 +0200
+++ b/svghmi/gen_index_xhtml.ysl2	Sat Oct 05 09:45:58 2019 +0200
@@ -10,7 +10,7 @@
             xmlns:svg="http://www.w3.org/2000/svg"
             xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
             xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
-            xmlns="http://www.w3.org/1999/xhtml"
+            xmlns:xhtml="http://www.w3.org/1999/xhtml"
 
             /* Our namespace to invoke python code */
             xmlns:ns="beremiz"
@@ -23,7 +23,43 @@
      */
     variable "geometry", "ns:GetSVGGeometry()";
     variable "hmitree", "ns:GetHMITree()";
-   
+
+    variable "_categories" {
+        noindex > HMI_ROOT
+        noindex > HMI_LABEL
+        noindex > HMI_CLASS
+        noindex > HMI_PLC_STATUS
+        noindex > HMI_CURRENT_PAGE
+    }
+    variable "categories", "exsl:node-set($_categories)";
+    //variable "indexed_hmitree", "$hmitree[not(local-name() = $categories/noindex/text())]";
+    variable "indexed_hmitree" apply "$hmitree", mode="index";
+
+    template "node()", mode="index"{
+        param "index", "0";
+        variable "content" {
+            choose {
+                when "not(local-name() = $categories/noindex)" {
+                    xsl:copy {
+                        attrib "index" > «$index»
+                        foreach "@*" xsl:copy;
+                    }
+                    /* no node expected below value nodes */
+                }
+                otherwise {
+                    apply "*[1]", mode="index"{
+                        with "index", "$index";
+                    }
+                }
+            }
+        }
+
+        copy "$content";
+        apply "following-sibling::*[1]", mode="index" {
+            with "index", "$index + count(exsl:node-set($content)/*)";
+        }
+    }
+
     /* Identity template :
      *  - copy every attributes 
      *  - copy every sub-elements
@@ -47,11 +83,15 @@
                   comment {
                       apply "$hmitree", mode="testtree";
                   }
+                  comment {
+                      apply "exsl:node-set($indexed_hmitree)", mode="testtree";
+                  }
                   apply "@* | node()";
               }
               script{
                   /* TODO : paste hmitree hash stored in hmi tree root node */
 
+                  /* TODO re-enable
                   ||
                   function evaluate_js_from_descriptions() {
                       var Page;
@@ -67,6 +107,7 @@
                       return res;
                   }
                   ||
+                  */
 
                   /*TODO add :
                     - pages content
@@ -74,6 +115,14 @@
                     - widgets parameters
                   */
 
+                  ||
+                  var subscriptions = {
+                  ||
+                  //    apply "$hmitree", mode="subscription_";
+                  ||
+                      return res;
+                  }
+                  ||
                   include text svghmi.js
               }
           }
@@ -116,7 +165,9 @@
 
     template "*", mode="testtree"{
         param "indent", "''";
-        | «$indent» «local-name()» «@name» «@type» «@path»
+        > «$indent» «local-name()» 
+        foreach "@*" > «local-name()»=«.» 
+        > \n
         apply "*", mode="testtree" {
             with "indent" value "concat($indent,'>')"
         };