<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:xhtml="http://www.w3.org/1999/xhtml" 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="xhtml:script"/>
<xsl:variable name="geometry" select="ns:GetSVGGeometry()"/>
<xsl:variable name="hmitree" select="ns:GetHMITree()"/>
<xsl:variable name="hmi_elements" select="//svg:*[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_hash = [</xsl:text>
<xsl:value-of select="$hmitree/@hash"/>
<xsl:text>var hmi_widgets = {
<xsl:for-each select="$hmi_elements">
<xsl:variable name="widget" select="func:parselabel(@inkscape:label)/widget"/>
<xsl:value-of select="@id"/>
<xsl:text> type: "</xsl:text>
<xsl:value-of select="$widget/@type"/>
<xsl:text> frequency: </xsl:text>
<xsl:apply-templates mode="refresh_frequency" select="$widget"/>
<xsl:for-each select="$widget/arg">
<xsl:value-of select="@value"/>
<xsl:if test="position()!=last()">
<xsl:for-each select="$widget/path">
<xsl:variable name="hmipath" select="@value"/>
<xsl:variable name="hmitree_match" select="$indexed_hmitree/*[@hmipath = $hmipath]"/>
<xsl:if test="count($hmitree_match) = 0">
<xsl:message terminate="yes">
<xsl:text>No match for HMI </xsl:text>
<xsl:value-of select="$hmipath"/>
<xsl:value-of select="$hmitree_match/@index"/>
<xsl:if test="position()!=last()">
<xsl:if test="position()!=last()">
<xsl:text>var hmitree_types = [
<xsl:for-each select="$indexed_hmitree/*">
<xsl:value-of select="@index"/>
<xsl:value-of select="@hmipath"/>
<xsl:text> */ "</xsl:text>
<xsl:value-of select="substring(local-name(), 5)"/>
<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:variable name="page" select="."/>
<xsl:variable name="p" select="$hmi_geometry[@Id = $page/@id]"/>
<xsl:variable name="page_ids" select="$hmi_geometry[@Id != $page/@id and @x >= $p/@x and @y >= $p/@y and @x+@w <= $p/@x+$p/@w and @y+@h <= $p/@y+$p/@h]/@Id"/>
<xsl:variable name="page_elements" select="$hmi_elements[@id = $page_ids]"/>
<xsl:value-of select="$desc/arg[1]/@value"/>
<xsl:text> id: "</xsl:text>
<xsl:value-of select="@id"/>
<xsl:for-each select="$page_ids">
<xsl:text> hmi_widgets.</xsl:text>
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:if test="position()!=last()">
<xsl:text>var default_page = "</xsl:text>
<xsl:value-of select="$default_page"/>
<xsl:text>function dispatch_value(index, value) {
<xsl:text> console.log("dispatch_value("+index+value+")");
<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>ws.binaryType = 'arraybuffer';
<xsl:text>const dvgetters = {
<xsl:text> INT: [DataView.prototype.getInt16, 2],
<xsl:text> BOOL: [DataView.prototype.getInt8, 1]
<xsl:text>// Register message reception handler
<xsl:text>ws.onmessage = function (evt) {
<xsl:text> let data = evt.data;
<xsl:text> let dv = new DataView(data);
<xsl:text> for(let hash_int of hmi_hash) {
<xsl:text> if(hash_int != dv.getUint8(i)){
<xsl:text> console.log("Recv non maching hash. Reload.");
<xsl:text> // 1003 is for "Unsupported Data"
<xsl:text> ws.close(1003,"Hash doesn't match");
<xsl:text> // TODO : remove debug alert ?
<xsl:text> alert("HMI will be reloaded.");
<xsl:text> // force reload ignoring cache
<xsl:text> location.reload(true);
<xsl:text> while(i < data.length){
<xsl:text> let index = dv.getUint32(i);
<xsl:text> let iectype = hmitree_types[index];
<xsl:text> let [dvgetter, bytesize] = dvgetters[iectypes];
<xsl:text> value = dvgetter.call(dv,i);
<xsl:text> dispatch_value(index, value);
<xsl:text> i += bytesize;
<xsl:text>function send_blob(data) {
<xsl:text> if(data.length > 0) {
<xsl:text> ws.send(new Blob([
<xsl:text> new Uint8Array(hmi_hash),
<xsl:text>const typedarray_types = {
<xsl:text> INT: Int16Array,
<xsl:text> BOOL: Uint8Array
<xsl:text>function send_reset() {
<xsl:text> send_blob(new Uint8Array([1])); /* reset = 1 */
<xsl:text>// subscription state, as it should be in hmi server
<xsl:text>// hmitree indexed array of integers
<xsl:text>var subscriptions = hmitree_types.map(_ignored => 0);
<xsl:text>// subscription state as needed by widget now
<xsl:text>// hmitree indexed array of Sets of widgets objects
<xsl:text>var subscribers = hmitree_types.map(_ignored => new Set());
<xsl:text>function update_subscriptions() {
<xsl:text> let delta = [];
<xsl:text> for(let index = 0; index < subscribers.length; index++){
<xsl:text> let widgets = subscribers[index];
<xsl:text> // periods are in ms
<xsl:text> let previous_period = subscriptions[index];
<xsl:text> let new_period;
<xsl:text> if(widgets.size > 0) {
<xsl:text> let maxfreq = 0;
<xsl:text> for(let widget of widgets)
<xsl:text> if(maxfreq < widgets.frequency)
<xsl:text> maxfreq = widgets.frequency;
<xsl:text> new_period = 1000/maxfreq;
<xsl:text> new_period = 0;
<xsl:text> if(previous_period != new_period) {
<xsl:text> subscriptions[index] = new_period;
<xsl:text> new Uint8Array([2]), /* subscribe = 2 */
<xsl:text> new Uint32Array([index]),
<xsl:text> new Uint16Array([new_period])]);
<xsl:text> send_blob(delta);
<xsl:text>function update_value(index, value) {
<xsl:text> iectype = hmitree_types[index];
<xsl:text> jstype = typedarray_types[iectypes];
<xsl:text> new Uint8Array([0]), /* setval = 0 */
<xsl:text> new jstype([value])
<xsl:text>var current_page;
<xsl:text>function switch_page(page_name) {
<xsl:text> let old_desc = page_desc[current_page];
<xsl:text> let new_desc = page_desc[page_name];
<xsl:text> /* TODO hide / show widgets */
<xsl:text> /* TODO move viewport */
<xsl:text> /* remove subsribers of previous page if any */
<xsl:text> if(old_desc) for(let widget of old_desc.widgets){
<xsl:text> for(let index of widget.indexes){
<xsl:text> subscribers[index].delete(widget);
<xsl:text> /* add new subsribers if any */
<xsl:text> if(new_desc) for(let widget of new_desc.widgets){
<xsl:text> for(let index of widget.indexes){
<xsl:text> subscribers[index].add(widget);
<xsl:text> current_page = page_name;
<xsl:text> update_subscriptions();
<xsl:text>// Once connection established
<xsl:text>ws.onopen = function (evt) {
<xsl:text> // show main page
<xsl:text> switch_page(default_page);
<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,'>')"/>
<xsl:template mode="refresh_frequency" match="widget">
<xsl:template mode="refresh_frequency" match="widget[@type='Meter']">
<xsl:template mode="refresh_frequency" match="widget[@type='Display']">
<xsl:template mode="refresh_frequency" match="widget[@type='Input']">