<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">
<xsl:text>HMI_ROOT</xsl:text>
<xsl:text>HMI_LABEL</xsl:text>
<xsl:text>HMI_CLASS</xsl:text>
<xsl:text>HMI_PLC_STATUS</xsl:text>
<xsl:text>HMI_CURRENT_PAGE</xsl:text>
<xsl:variable name="categories" select="exsl:node-set($_categories)"/>
<xsl:variable name="_indexed_hmitree">
<xsl:apply-templates mode="index" select="$hmitree"/>
<xsl:variable name="indexed_hmitree" select="exsl:node-set($_indexed_hmitree)"/>
<xsl:template mode="index" match="*">
<xsl:param name="index" select="0"/>
<xsl:param name="parentpath" select="''"/>
<xsl:variable name="content">
<xsl:variable name="path">
<xsl:when test="local-name() = 'HMI_ROOT'">
<xsl:value-of select="$parentpath"/>
<xsl:value-of select="$parentpath"/>
<xsl:value-of select="@name"/>
<xsl:when test="not(local-name() = $categories/noindex)">
<xsl:attribute name="index">
<xsl:value-of select="$index"/>
<xsl:attribute name="hmipath">
<xsl:value-of select="$path"/>
<xsl:for-each select="@*">
<xsl:apply-templates mode="index" select="*[1]">
<xsl:with-param name="index" select="$index"/>
<xsl:with-param name="parentpath">
<xsl:value-of select="$path"/>
<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:with-param name="parentpath">
<xsl:value-of select="$parentpath"/>
<xsl:template mode="identity_svg" match="@* | node()">
<xsl:apply-templates mode="identity_svg" select="@* | node()"/>
<xsl:variable name="mark">
<html xmlns="http://www.w3.org/1999/xhtml">
<xsl:apply-templates mode="testgeo" select="$geometry"/>
<xsl:apply-templates mode="testtree" select="$hmitree"/>
<xsl:apply-templates mode="testtree" select="$indexed_hmitree"/>
<xsl:apply-templates mode="identity_svg" select="@* | node()"/>
<xsl:text>var subscriptions = {
<xsl:variable name="svg" select="/"/>
<xsl:for-each select="$indexed_hmitree/*">
<xsl:value-of select="@index"/>
<xsl:text> name: "</xsl:text>
<xsl:value-of select="@name"/>
<xsl:text> hmipath: "</xsl:text>
<xsl:value-of select="@hmipath"/>
<xsl:variable name="hmipath" select="@hmipath"/>
<xsl:for-each select="$svg//*[substring-after(@inkscape:label,'@') = $hmipath]">
<xsl:value-of select="@id"/>
<xsl:if test="position()!=last()">
<xsl:if test="position()!=last()">
<xsl:text> // Open WebSocket to relative "/ws" address
<xsl:text> var ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws'));
<xsl:text> // Register message reception handler
<xsl:text> ws.onmessage = function (evt) {
<xsl:text> // TODO : dispatch and cache hmi tree updates
<xsl:text> var received_msg = evt.data;
<xsl:text> // TODO : check for hmitree hash header
<xsl:text> // if not matching, reload page
<xsl:text> alert("Message is received..."+received_msg);
<xsl:text> // Once connection established
<xsl:text> ws.onopen = function (evt) {
<xsl:text> // TODO : enable the HMI (was previously offline, or just starts)
<xsl:text> // show main page
<xsl:text> // TODO : prefix with hmitree hash header
<xsl:text> ws.send("test");
<xsl:text> var pending_updates = {};
<xsl:text> // subscription state, as it should be in hmi server
<xsl:text> // expected {index:period}
<xsl:text> var subscriptions = {};
<xsl:text> // subscription state as needed by widget now
<xsl:text> // expected {index:[widgets]};
<xsl:text> var subscribers = {};
<xsl:text> // return the diff in between curently subscribed and subscription
<xsl:text> function update_subscriptions() {
<xsl:text> let result = [];
<xsl:text> Object.keys(subscribers).forEach(index => {
<xsl:text> let previous_period = subscriptions[index];
<xsl:text> let new_period = Math.min(...widgets.map(widget => widget.period));
<xsl:text> if(previous_period != new_period)
<xsl:text> result.push({index: index, period: new_period});
<xsl:text> function update_value(index, value) {
<xsl:template mode="code_from_descs" match="*">
<xsl:text> var path, role, name, priv;
<xsl:text> var id = "</xsl:text>
<xsl:value-of select="@id"/>
<xsl:if test="@inkscape:label">
<xsl:text>name = "</xsl:text>
<xsl:value-of select="@inkscape:label"/>
<xsl:text>/* -------------- */
<xsl:value-of select="substring-after(svg:desc, $mark)"/>
<xsl:text> /* -------------- */
<xsl:template mode="testgeo" match="bbox">
<xsl:text>ID: </xsl:text>
<xsl:value-of select="@Id"/>
<xsl:text> x: </xsl:text>
<xsl:value-of select="@x"/>
<xsl:text> y: </xsl:text>
<xsl:value-of select="@y"/>
<xsl:text> w: </xsl:text>
<xsl:value-of select="@w"/>
<xsl:text> h: </xsl:text>
<xsl:value-of select="@h"/>
<xsl:template mode="testtree" match="*">
<xsl:param name="indent" select="''"/>
<xsl:value-of select="$indent"/>
<xsl:value-of select="local-name()"/>
<xsl:for-each select="@*">
<xsl:value-of select="local-name()"/>
<xsl:value-of select="."/>
<xsl:apply-templates mode="testtree" select="*">
<xsl:with-param name="indent">
<xsl:value-of select="concat($indent,'>')"/>