<xsl:stylesheet xmlns:func="http://exslt.org/functions" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:svg="http://www.w3.org/2000/svg" xmlns:str="http://exslt.org/strings" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:exsl="http://exslt.org/common" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" extension-element-prefixes="ns func" version="1.0" exclude-result-prefixes="ns str regexp exsl func">
<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="hmi_elements" select="//*[starts-with(@inkscape:label, 'HMI:')]"/>
<xsl:variable name="hmi_geometry" select="$geometry[@Id = $hmi_elements/@id]"/>
<xsl:variable name="hmi_pages" select="$hmi_elements[func:parselabel(@inkscape:label)/widget/@type = 'Page']"/>
<xsl:variable name="default_page">
<xsl:when test="count($hmi_pages) > 1">
<xsl:variable name="Home_page" select="$hmi_pages[func:parselabel(@inkscape:label)/widget/arg[1]/@value = 'Home']"/>
<xsl:when test="$Home_page">
<xsl:text>Home</xsl:text>
<xsl:message terminate="yes">No Home page defined!</xsl:message>
<xsl:when test="count($hmi_pages) = 0">
<xsl:message terminate="yes">No page defined!</xsl:message>
<xsl:value-of select="func:parselabel($hmi_pages/@inkscape:label)/widget/arg[1]/@value"/>
<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:text>Made with SVGHMI. https://beremiz.org</xsl:text>
<html xmlns="http://www.w3.org/1999/xhtml">
<xsl:apply-templates mode="testgeo" select="$hmi_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:call-template name="scripts"/>
<func:function name="func:parselabel">
<xsl:param name="label"/>
<xsl:variable name="description" select="substring-after($label,'HMI:')"/>
<xsl:variable name="_args" select="substring-before($description,'@')"/>
<xsl:variable name="args">
<xsl:value-of select="$_args"/>
<xsl:value-of select="$description"/>
<xsl:variable name="_type" select="substring-before($args,':')"/>
<xsl:variable name="type">
<xsl:value-of select="$_type"/>
<xsl:value-of select="$args"/>
<xsl:variable name="ast">
<xsl:attribute name="type">
<xsl:value-of select="$type"/>
<xsl:for-each select="str:split(substring-after($args, ':'), ':')">
<xsl:attribute name="value">
<xsl:value-of select="."/>
<xsl:variable name="paths" select="substring-after($description,'@')"/>
<xsl:for-each select="str:split($paths, '@')">
<xsl:attribute name="value">
<xsl:value-of select="."/>
<func:result select="exsl:node-set($ast)"/>
<xsl:template name="scripts">
<xsl:text>var hmi_index = {
<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>var page_desc = {
<xsl:for-each select="$hmi_pages">
<xsl:variable name="desc" select="func:parselabel(@inkscape:label)/widget"/>
<xsl:value-of select="$desc/arg[1]/@value"/>
<xsl:text> id: "</xsl:text>
<xsl:value-of select="@id"/>
<xsl:variable name="page" select="."/>
<xsl:variable name="p" select="$hmi_geometry[@Id = $page/@id]"/>
<xsl:for-each select="$hmi_geometry[@Id != $page/@id and @x >= $p/@x and @y >= $p/@y and @w <= $p/@w and @h <= $p/@h]">
<xsl:value-of select="@Id"/>
<xsl:if test="position()!=last()">
<xsl:text>var default_page = "</xsl:text>
<xsl:value-of select="$default_page"/>
<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> const subscriptions = new Map();
<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 delta = [];
<xsl:text> Object.keys(subscribers).forEach(index => {
<xsl:text> let previous_period = subscriptions.get(index);
<xsl:text> delete subscriptions[index];
<xsl:text> let new_period = Math.min(...widgets.map(widget => widget.period));
<xsl:text> if(previous_period != new_period)
<xsl:text> delta.push({index: index, period: new_period});
<xsl:text> return result;
<xsl:text> function update_value(index, value) {
<xsl:text> function switch_page(page_name) {
<xsl:template mode="page_desc" match="*"/>
<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,'>')"/>