merge svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Thu, 11 Mar 2021 09:13:51 +0100
branchsvghmi
changeset 3184 1d3408e46ab1
parent 3181 50d0fef791d5 (diff)
parent 3183 ca9774c0f6a7 (current diff)
child 3185 9038655c1b18
merge
--- a/svghmi/gen_index_xhtml.xslt	Wed Mar 10 10:01:05 2021 +0100
+++ b/svghmi/gen_index_xhtml.xslt	Thu Mar 11 09:13:51 2021 +0100
@@ -1,6 +1,6 @@
 <?xml version="1.0"?>
-<xsl:stylesheet xmlns:ns="beremiz" xmlns:definitions="definitions" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:func="http://exslt.org/functions" xmlns:epilogue="epilogue" xmlns:preamble="preamble" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:svg="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:str="http://exslt.org/strings" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:exsl="http://exslt.org/common" xmlns:declarations="declarations" xmlns:debug="debug" exclude-result-prefixes="ns func exsl regexp str dyn debug preamble epilogue declarations definitions" extension-element-prefixes="ns func exsl regexp str dyn" version="1.0">
-  <xsl:output method="xml" cdata-section-elements="xhtml:script"/>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:str="http://exslt.org/strings" xmlns:func="http://exslt.org/functions" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:debug="debug" xmlns:preamble="preamble" xmlns:declarations="declarations" xmlns:definitions="definitions" xmlns:epilogue="epilogue" xmlns:ns="beremiz" version="1.0" extension-element-prefixes="ns func exsl regexp str dyn" exclude-result-prefixes="ns func exsl regexp str dyn debug preamble epilogue declarations definitions">
+  <xsl:output cdata-section-elements="xhtml:script" method="xml"/>
   <xsl:variable name="svg" select="/svg:svg"/>
   <xsl:variable name="hmi_elements" select="//svg:*[starts-with(@inkscape:label, 'HMI:')]"/>
   <xsl:variable name="hmitree" select="ns:GetHMITree()"/>
@@ -1693,7 +1693,7 @@
 </xsl:text>
   </xsl:template>
   <xsl:variable name="excluded_types" select="str:split('Page VarInit VarInitPersistent')"/>
-  <xsl:key use="@type" name="TypesKey" match="widget"/>
+  <xsl:key name="TypesKey" match="widget" use="@type"/>
   <declarations:hmi-classes/>
   <xsl:template match="declarations:hmi-classes">
     <xsl:text>
@@ -4535,7 +4535,7 @@
 </xsl:text>
     <xsl:text>    cache = [0,100,50];
 </xsl:text>
-    <xsl:text>    init() {
+    <xsl:text>    init_common() {
 </xsl:text>
     <xsl:text>        this.spread_json_data_bound = this.spread_json_data.bind(this);
 </xsl:text>
@@ -4940,13 +4940,6 @@
         <xsl:text>data</xsl:text>
       </xsl:with-param>
     </xsl:call-template>
-    <xsl:call-template name="defs_by_labels">
-      <xsl:with-param name="hmi_element" select="$hmi_element"/>
-      <xsl:with-param name="labels">
-        <xsl:text>forward backward cursor</xsl:text>
-      </xsl:with-param>
-      <xsl:with-param name="mandatory" select="'no'"/>
-    </xsl:call-template>
     <xsl:variable name="data_elt" select="$result_svg_ns//*[@id = $hmi_element/@id]/*[@inkscape:label = 'data']"/>
     <xsl:text>    visible: </xsl:text>
     <xsl:value-of select="count($data_elt/*[@inkscape:label])"/>
@@ -4968,6 +4961,20 @@
       <xsl:with-param name="expressions" select="$initexpr_ns"/>
       <xsl:with-param name="widget_elts" select="$hmi_element/*[@inkscape:label = 'data']/descendant::svg:*"/>
     </xsl:apply-templates>
+    <xsl:text>    },
+</xsl:text>
+    <xsl:text>    init() {
+</xsl:text>
+    <xsl:text>       this.init_common();
+</xsl:text>
+    <xsl:for-each select="$hmi_element/*[starts-with(@inkscape:label,'action_')]">
+      <xsl:text>        id("</xsl:text>
+      <xsl:value-of select="@id"/>
+      <xsl:text>").onclick = this.make_on_click("</xsl:text>
+      <xsl:value-of select="func:escape_quotes(@inkscape:label)"/>
+      <xsl:text>");
+</xsl:text>
+    </xsl:for-each>
     <xsl:text>    }
 </xsl:text>
   </xsl:template>
@@ -6801,7 +6808,7 @@
     <xsl:comment>
       <xsl:apply-templates select="document('')/*/debug:*"/>
     </xsl:comment>
-    <html xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/1999/xhtml">
+    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
       <head/>
       <body style="margin:0;overflow:hidden;user-select:none;touch-action:none;">
         <xsl:copy-of select="$result_svg"/>
--- a/svghmi/svghmi.py	Wed Mar 10 10:01:05 2021 +0100
+++ b/svghmi/svghmi.py	Thu Mar 11 09:13:51 2021 +0100
@@ -362,7 +362,7 @@
 
         # Backup HMI Tree in XML form so that it can be loaded without building
         hmitree_backup_path = os.path.join(buildpath, "hmitree.xml")
-        hmitree_backup_file = open(hmitree_backup_path, 'w')
+        hmitree_backup_file = open(hmitree_backup_path, 'wb')
         hmitree_backup_file.write(etree.tostring(hmi_tree_root.etree()))
         hmitree_backup_file.close()
 
@@ -449,7 +449,7 @@
             buildpath = self.Controler.GetCTRoot()._getBuildPath()
             hmitree_backup_path = os.path.join(buildpath, "hmitree.xml")
             if os.path.exists(hmitree_backup_path):
-                hmitree_backup_file = open(hmitree_backup_path, 'r')
+                hmitree_backup_file = open(hmitree_backup_path, 'rb')
                 hmi_tree_root = HMITreeNode.from_etree(etree.parse(hmitree_backup_file).getroot())
 
         return HMITreeSelector(parent)
@@ -602,51 +602,74 @@
 
         target_fname = "svghmi_"+location_str+".xhtml"
 
-        target_path = os.path.join(self._getBuildPath(), target_fname)
-        target_file = open(target_path, 'wb')
+        build_path = self._getBuildPath()
+        target_path = os.path.join(build_path, target_fname)
+        hash_path = os.path.join(build_path, "svghmi.md5")
 
         self.GetCTRoot().logger.write("SVGHMI:\n")
 
         if os.path.exists(svgfile):
 
-            # TODO : move to __init__
-            transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
-                          [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry()),
-                           ("GetHMITree", lambda *_ignored:self.GetHMITree()),
-                           ("GetTranslations", self.GetTranslations),
-                           ("ProgressStart", lambda _ign,k,m:self.ProgressStart(str(k),str(m))),
-                           ("ProgressEnd", lambda _ign,k:self.ProgressEnd(str(k)))])
-
-            self.ProgressStart("svg", "source SVG parsing")
-
-            # load svg as a DOM with Etree
-            svgdom = etree.parse(svgfile)
-
-            self.ProgressEnd("svg")
-
-            # call xslt transform on Inkscape's SVG to generate XHTML
-            try: 
-                self.ProgressStart("xslt", "XSLT transform")
-                result = transform.transform(svgdom)  # , profile_run=True)
-                self.ProgressEnd("xslt")
-            except XSLTApplyError as e:
-                self.FatalError("SVGHMI " + view_name  + ": " + e.message)
-            finally:
-                for entry in transform.get_error_log():
-                    message = "SVGHMI: "+ entry.message + "\n" 
-                    self.GetCTRoot().logger.write_warning(message)
-
-            result.write(target_file, encoding="utf-8")
-            # print(str(result))
-            # print(transform.xslt.error_log)
-            # print(etree.tostring(result.xslt_profile,pretty_print=True))
-
-            # TODO
-            #   - Errors on HMI semantics
-            #   - ... maybe something to have a global view of what is declared in SVG.
+            hasher = hashlib.md5()
+            hmi_tree_root._hash(hasher)
+            with open(svgfile, 'rb') as afile:
+                while True:
+                    buf = afile.read(65536)
+                    if len(buf) > 0:
+                        hasher.update(buf)
+                    else:
+                        break
+            digest = hasher.hexdigest()
+
+            if os.path.exists(hash_path):
+                with open(hash_path, 'rb') as digest_file:
+                    last_digest = digest_file.read()
+            else:
+                last_digest = None
+            
+            if digest != last_digest:
+
+                transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
+                              [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry()),
+                               ("GetHMITree", lambda *_ignored:self.GetHMITree()),
+                               ("GetTranslations", self.GetTranslations),
+                               ("ProgressStart", lambda _ign,k,m:self.ProgressStart(str(k),str(m))),
+                               ("ProgressEnd", lambda _ign,k:self.ProgressEnd(str(k)))])
+
+                self.ProgressStart("svg", "source SVG parsing")
+
+                # load svg as a DOM with Etree
+                svgdom = etree.parse(svgfile)
+
+                self.ProgressEnd("svg")
+
+                # call xslt transform on Inkscape's SVG to generate XHTML
+                try: 
+                    self.ProgressStart("xslt", "XSLT transform")
+                    result = transform.transform(svgdom)  # , profile_run=True)
+                    self.ProgressEnd("xslt")
+                except XSLTApplyError as e:
+                    self.FatalError("SVGHMI " + view_name  + ": " + e.message)
+                finally:
+                    for entry in transform.get_error_log():
+                        message = "SVGHMI: "+ entry.message + "\n" 
+                        self.GetCTRoot().logger.write_warning(message)
+
+                target_file = open(target_path, 'wb')
+                result.write(target_file, encoding="utf-8")
+                target_file.close()
+
+                # print(str(result))
+                # print(transform.xslt.error_log)
+                # print(etree.tostring(result.xslt_profile,pretty_print=True))
+
+                with open(hash_path, 'wb') as digest_file:
+                    digest_file.write(digest)
+            else:
+                self.GetCTRoot().logger.write("    No changes - XSLT transformation skipped\n")
 
         else:
-            # TODO : use default svg that expose the HMI tree as-is
+            target_file = open(target_path, 'wb')
             target_file.write("""<!DOCTYPE html>
 <html>
 <body>
@@ -654,8 +677,7 @@
 </body>
 </html>
 """)
-
-        target_file.close()
+            target_file.close()
 
         res += ((target_fname, open(target_path, "rb")),)
 
--- a/svghmi/widget_jsontable.ysl2	Wed Mar 10 10:01:05 2021 +0100
+++ b/svghmi/widget_jsontable.ysl2	Thu Mar 11 09:13:51 2021 +0100
@@ -5,7 +5,7 @@
     class JsonTableWidget extends Widget{
         // arbitrary defaults to avoid missing entries in query
         cache = [0,100,50];
-        init() {
+        init_common() {
             this.spread_json_data_bound = this.spread_json_data.bind(this);
             this.handle_http_response_bound = this.handle_http_response.bind(this);
             this.fetch_error_bound = this.fetch_error.bind(this);
@@ -254,7 +254,6 @@
 template "widget[@type='JsonTable']", mode="widget_defs" {
     param "hmi_element";
     labels("data");
-    optional_labels("forward backward cursor");
     const "data_elt", "$result_svg_ns//*[@id = $hmi_element/@id]/*[@inkscape:label = 'data']";
     |     visible: «count($data_elt/*[@inkscape:label])»,
     |     spread_json_data: function(janswer) {
@@ -267,5 +266,12 @@
         with "expressions","$initexpr_ns";
         with "widget_elts","$hmi_element/*[@inkscape:label = 'data']/descendant::svg:*";
     }
+    |     },
+    |     init() {
+    |        this.init_common();
+    foreach "$hmi_element/*[starts-with(@inkscape:label,'action_')]" {
+    |         id("«@id»").onclick = this.make_on_click("«func:escape_quotes(@inkscape:label)»");
+    }
     |     }
-}
+
+}
--- a/tests/svghmi/py_ext_0@py_ext/pyfile.xml	Wed Mar 10 10:01:05 2021 +0100
+++ b/tests/svghmi/py_ext_0@py_ext/pyfile.xml	Thu Mar 11 09:13:51 2021 +0100
@@ -37,7 +37,12 @@
         extra = newdata[u'extra']
         options = newdata[u'options']
 
-        if len(options) == 2 :
+        if len(options) == 1 :
+            action, = options
+            if action == "action_reset":
+                del Alarms[:]
+                AlarmIndex.clear()
+        elif len(options) == 2 :
             action, alarmid = options
             if action == "onClick[acknowledge]":
                 AlarmIndex[int(alarmid)][2] = "ack"
--- a/tests/svghmi/svghmi_0@svghmi/svghmi.svg	Wed Mar 10 10:01:05 2021 +0100
+++ b/tests/svghmi/svghmi_0@svghmi/svghmi.svg	Thu Mar 11 09:13:51 2021 +0100
@@ -128,12 +128,12 @@
      inkscape:current-layer="hmi0"
      showgrid="false"
      units="px"
-     inkscape:zoom="0.42177816"
-     inkscape:cx="-871.66754"
-     inkscape:cy="136.19693"
-     inkscape:window-width="1600"
-     inkscape:window-height="836"
-     inkscape:window-x="0"
+     inkscape:zoom="1.6871126"
+     inkscape:cx="-820.55411"
+     inkscape:cy="162.70697"
+     inkscape:window-width="3840"
+     inkscape:window-height="2096"
+     inkscape:window-x="1600"
      inkscape:window-y="27"
      inkscape:window-maximized="1"
      showguides="true"
@@ -5214,6 +5214,44 @@
          transform="translate(0,-377.48864)"
          inkscape:label="[0]" />
     </g>
+    <g
+       style="stroke-width:1.04184687"
+       inkscape:label="action_reset"
+       id="g1839-6"
+       transform="matrix(2,0,0,2,-181.39997,-864.49004)">
+      <g
+         style="stroke-width:1.04184687"
+         inkscape:label="bg"
+         id="g945-7">
+        <rect
+           rx="26.820074"
+           inkscape:label="button"
+           ry="23.177595"
+           y="594.82263"
+           x="971.96545"
+           height="95.723877"
+           width="245.44583"
+           id="rect943-5"
+           style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#e6e6e6;fill-opacity:1;fill-rule:nonzero;stroke:#ff6600;stroke-width:5.20923424;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;marker:none;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate" />
+      </g>
+      <g
+         style="stroke-width:1.04184687"
+         inkscape:label="text"
+         id="g951-3">
+        <text
+           xml:space="preserve"
+           style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;text-align:center;letter-spacing:0px;word-spacing:0px;text-anchor:middle;display:inline;fill:#ff6600;fill-opacity:1;stroke:none;stroke-width:1.04184675px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
+           x="1090.7626"
+           y="656.98151"
+           id="text949-5"
+           inkscape:label="setting_jmp"><tspan
+             sodipodi:role="line"
+             id="tspan947-6"
+             x="1090.7626"
+             y="656.98151"
+             style="text-align:center;text-anchor:middle;fill:#ff6600;stroke-width:1.04184675px">reset</tspan></text>
+      </g>
+    </g>
   </g>
   <g
      id="g1332"