--- a/svghmi/Makefile Fri Oct 11 12:03:14 2019 +0200
+++ b/svghmi/Makefile Tue Oct 15 17:14:48 2019 +0200
@@ -16,7 +16,7 @@
-%.xslt: %.ysl2 ../yslt_noindent.yml2
+%.xslt: %.ysl2 svghmi.js ../yslt_noindent.yml2 $(yml2path)/yml2c -I $(yml2path):../ $< -o $@.tmp
xmlstarlet fo $@.tmp > $@
--- a/svghmi/gen_index_xhtml.xslt Fri Oct 11 12:03:14 2019 +0200
+++ b/svghmi/gen_index_xhtml.xslt Tue Oct 15 17:14:48 2019 +0200
@@ -1,6 +1,6 @@
-<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: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:')]"/>
@@ -179,6 +179,10 @@
<func:result select="exsl:node-set($ast)"/>
<xsl:template name="scripts">
<xsl:text>var hmi_hash = [</xsl:text>
<xsl:value-of select="$hmitree/@hash"/>
@@ -212,7 +216,7 @@
<xsl:for-each select="$widget/path">
<xsl:variable name="hmipath" select="@value"/>
@@ -245,36 +249,16 @@
- <xsl:text>var hmi_index = [
+ <xsl:text>var hmitree_types = [ - <xsl:variable name="svg" select="/"/>
<xsl:for-each select="$indexed_hmitree/*">
- <xsl:text>{ /* </xsl:text>
+ <xsl:text>/* </xsl:text> <xsl:value-of select="@index"/>
<xsl:value-of select="@hmipath"/>
- <xsl:text> type: "</xsl:text>
- <xsl:value-of select="local-name()"/>
- <xsl:variable name="hmipath" select="@hmipath"/>
- <xsl:for-each select="$svg//*[substring-after(@inkscape:label,'@') = $hmipath]">
- <xsl:text> hmi_widgets["</xsl:text>
- <xsl:value-of select="@id"/>
- <xsl:text>"]</xsl:text>
- <xsl:if test="position()!=last()">
+ <xsl:text> */ "</xsl:text> + <xsl:value-of select="substring(local-name(), 5)"/> <xsl:if test="position()!=last()">
@@ -304,9 +288,8 @@
<xsl:for-each select="$page_ids">
- <xsl:text> "</xsl:text>
+ <xsl:text> hmi_widgets.</xsl:text> <xsl:value-of select="."/>
<xsl:if test="position()!=last()">
@@ -315,24 +298,6 @@
- <xsl:text> subscriptions: [
- <xsl:for-each select="$page_elements">
- <xsl:variable name="hmipaths" select="func:parselabel(@inkscape:label)/widget/path/@value"/>
- <xsl:variable name="notlast" select="position()!=last()"/>
- <xsl:for-each select="$hmipaths">
- <xsl:variable name="hmipath" select="."/>
- <xsl:value-of select="$indexed_hmitree/*[@hmipath = $hmipath]/@index"/>
- <xsl:if test="$notlast or position()!=last()">
<xsl:if test="position()!=last()">
@@ -352,147 +317,305 @@
+ <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>// Open WebSocket to relative "/ws" address - <xsl:text> var ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws'));
+ <xsl:text>var ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws')); + <xsl:text>ws.binaryType = 'arraybuffer'; - <xsl:text> // Register message reception handler
+ <xsl:text>const dvgetters = { + <xsl:text> INT: [DataView.prototype.getInt16, 2], + <xsl:text> BOOL: [DataView.prototype.getInt8, 1] - <xsl:text> ws.onmessage = function (evt) {
- <xsl:text> // TODO : dispatch and cache hmi tree updates
+ <xsl:text>// Register message reception handler + <xsl:text>ws.onmessage = function (evt) { - <xsl:text> var received_msg = evt.data;
+ <xsl:text> let data = evt.data; - <xsl:text> // TODO : check for hmitree hash header
+ <xsl:text> let dv = new DataView(data); - <xsl:text> // if not matching, reload page
+ <xsl:text> for(let hash_int of hmi_hash) { - <xsl:text> alert("Message is received..."+received_msg);
+ <xsl:text> if(hash_int != dv.getUint8(i)){
+ <xsl:text> console.log("Recv non maching hash. Reload."); - <xsl:text> // Once connection established
- <xsl:text> ws.onopen = function (evt) {
+ <xsl:text> // 1003 is for "Unsupported Data" - <xsl:text> // TODO : enable the HMI (was previously offline, or just starts)
- <xsl:text> // show main page
+ <xsl:text> ws.close(1003,"Hash doesn't match"); + <xsl:text> // TODO : remove debug alert ? + <xsl:text> alert("HMI will be reloaded."); - <xsl:text> // TODO : prefix with hmitree hash header
+ <xsl:text> // force reload ignoring cache + <xsl:text> location.reload(true); - <xsl:text> ws.send("test");
- <xsl:text> var pending_updates = {};
+ <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> // subscription state, as it should be in hmi server
+ <xsl:text>function send_blob(data) { + <xsl:text> if(data.length > 0) { - <xsl:text> // expected {index:period}
+ <xsl:text> ws.send(new Blob([ + <xsl:text> new Uint8Array(hmi_hash), - <xsl:text> const subscriptions = new Map();
- <xsl:text> // subscription state as needed by widget now
+ <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> // expected {index:[widgets]};
+ <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> var subscribers = new Map();
+ <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> // return the diff in between curently subscribed and subscription
+ <xsl:text> let new_period; - <xsl:text> function update_subscriptions() {
+ <xsl:text> if(widgets.size > 0) { + <xsl:text> let maxfreq = 0; - <xsl:text> let delta = [];
+ <xsl:text> for(let widget of widgets) - <xsl:text> for(let [index, widgets] in subscribers.entries()){
+ <xsl:text> if(maxfreq < widgets.frequency) + <xsl:text> maxfreq = widgets.frequency; - <xsl:text> // periods are in ms
+ <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> delta.push([ - <xsl:text> let previous_period = subscriptions.get(index);
+ <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> let new_period = Math.min(...widgets.map(widget => 1000/widget.frequency));
+ <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> if(previous_period != new_period) {
+ <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> subscriptions.set(index, new_period);
+ <xsl:text> /* TODO move viewport */ - <xsl:text> delta.push({index: index, period: new_period});
+ <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> return result;
+ <xsl:text> for(let index of widget.indexes){ + <xsl:text> subscribers[index].add(widget);
- <xsl:text> function update_value(index, value) {
- <xsl:text> var current_page = default_page;
+ <xsl:text> current_page = page_name; - <xsl:text> function switch_page(page_name) {
- <xsl:text> let new_desc = page_desc[page_name];
- <xsl:text> let old_desc = page_desc[page_name];
- <xsl:text> /* TODO hide / show widgets */
+ <xsl:text> update_subscriptions(); - <xsl:text> /* TODO move viewport */
- <xsl:text> /* Update subscribers */
- <xsl:text> /* Update subscriptions */
+ <xsl:text>// Once connection established + <xsl:text>ws.onopen = function (evt) { + <xsl:text> send_reset(); + <xsl:text> // show main page + <xsl:text> switch_page(default_page); --- a/svghmi/gen_index_xhtml.ysl2 Fri Oct 11 12:03:14 2019 +0200
+++ b/svghmi/gen_index_xhtml.ysl2 Tue Oct 15 17:14:48 2019 +0200
@@ -1,7 +1,7 @@
include yslt_noindent.yml2
// overrides yslt's output function to set CDATA
-decl output(method, cdata-section-elements="script");
+decl output(method, cdata-section-elements="xhtml:script"); @@ -11,6 +11,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:xhtml="http://www.w3.org/1999/xhtml" /* Our namespace to invoke python code */
@@ -163,7 +164,8 @@
- /* paste hmitree hash stored in hmi tree root node */
| var hmi_hash = [«$hmitree/@hash»];
@@ -194,7 +196,7 @@
| "«@value»"`if "position()!=last()" > ,`
const "hmipath","@value";
const "hmitree_match","$indexed_hmitree/*[@hmipath = $hmipath]";
@@ -207,19 +209,10 @@
+ | var hmitree_types = [ - const "svg","/"; /* foreach loses document root */
foreach "$indexed_hmitree/*" {
- | { /* «@index» «@hmipath» */
- | type: "«local-name()»",
- const "hmipath","@hmipath";
- foreach "$svg//*[substring-after(@inkscape:label,'@') = $hmipath]" {
- | hmi_widgets["«@id»"]`if "position()!=last()" > ,`
- | }`if "position()!=last()" > ,`
+ | /* «@index» «@hmipath» */ "«substring(local-name(), 5)»"`if "position()!=last()" > ,` @@ -238,17 +231,7 @@
- | "«.»"`if "position()!=last()" > ,`
- foreach "$page_elements" {
- const "hmipaths", "func:parselabel(@inkscape:label)/widget/path/@value";
- const "notlast", "position()!=last()";
- | «$indexed_hmitree/*[@hmipath = $hmipath]/@index»`if "$notlast or position()!=last()" > ,`
+ | hmi_widgets.«.»`if "position()!=last()" > ,` | }`if "position()!=last()" > ,`
@@ -259,6 +242,7 @@
| var default_page = "«$default_page»";
--- a/svghmi/svghmi.c Fri Oct 11 12:03:14 2019 +0200
+++ b/svghmi/svghmi.c Tue Oct 15 17:14:48 2019 +0200
@@ -242,8 +242,7 @@
int svghmi_recv_dispatch(uint32_t size, const uint8_t *ptr){
@@ -320,20 +319,6 @@
- uint32_t index = *(uint32_t*)(cursor);
- if(index < HMI_ITEM_COUNT)
- hmi_tree_item_t *dsc = &hmi_tree_item[index];
- reset_iterator(index, dsc);
- progress = sizeof(uint32_t) /* index */;
--- a/svghmi/svghmi.js Fri Oct 11 12:03:14 2019 +0200
+++ b/svghmi/svghmi.js Tue Oct 15 17:14:48 2019 +0200
@@ -1,26 +1,153 @@
- // Open WebSocket to relative "/ws" address
- var ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws'));
+function dispatch_value(index, value) { + console.log("dispatch_value("+index+value+")"); +// Open WebSocket to relative "/ws" address +var ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws')); +ws.binaryType = 'arraybuffer'; + INT: [DataView.prototype.getInt16, 2], + BOOL: [DataView.prototype.getInt8, 1] +// Register message reception handler +ws.onmessage = function (evt) { - // Register message reception handler
- ws.onmessage = function (evt) {
- // TODO : dispatch and cache hmi tree updates
+ let dv = new DataView(data); + for(let hash_int of hmi_hash) { + if(hash_int != dv.getUint8(i)){ + console.log("Recv non maching hash. Reload."); - var received_msg = evt.data;
- // TODO : check for hmitree hash header
- // if not matching, reload page
- alert("Message is received..."+received_msg);
+ // 1003 is for "Unsupported Data" + ws.close(1003,"Hash doesn't match"); + // TODO : remove debug alert ? + alert("HMI will be reloaded."); + // force reload ignoring cache - // Once connection established
- ws.onopen = function (evt) {
- // TODO : enable the HMI (was previously offline, or just starts)
+ while(i < data.length){ + let index = dv.getUint32(i); + let iectype = hmitree_types[index]; + let [dvgetter, bytesize] = dvgetters[iectypes]; + value = dvgetter.call(dv,i); + dispatch_value(index, value); - // TODO : prefix with hmitree hash header
+function send_blob(data) { + new Uint8Array(hmi_hash),
+const typedarray_types = { + send_blob(new Uint8Array([1])); /* reset = 1 */ +// subscription state, as it should be in hmi server +// hmitree indexed array of integers +var subscriptions = hmitree_types.map(_ignored => 0); +// subscription state as needed by widget now +// hmitree indexed array of Sets of widgets objects +var subscribers = hmitree_types.map(_ignored => new Set()); +function update_subscriptions() { + for(let index = 0; index < subscribers.length; index++){ + let widgets = subscribers[index]; + let previous_period = subscriptions[index]; + for(let widget of widgets) + if(maxfreq < widgets.frequency) + maxfreq = widgets.frequency; + new_period = 1000/maxfreq; + if(previous_period != new_period) { + subscriptions[index] = new_period; + new Uint8Array([2]), /* subscribe = 2 */ + new Uint32Array([index]), + new Uint16Array([new_period])]); +function update_value(index, value) { + iectype = hmitree_types[index]; + jstype = typedarray_types[iectypes]; + new Uint8Array([0]), /* setval = 0 */ +function switch_page(page_name) { + let old_desc = page_desc[current_page]; + let new_desc = page_desc[page_name]; + /* TODO hide / show widgets */ + /* TODO move viewport */ + /* remove subsribers of previous page if any */ + if(old_desc) for(let widget of old_desc.widgets){ + for(let index of widget.indexes){ + subscribers[index].delete(widget); + /* add new subsribers if any */ + if(new_desc) for(let widget of new_desc.widgets){ + for(let index of widget.indexes){ + subscribers[index].add(widget); + current_page = page_name; + update_subscriptions(); +// Once connection established +ws.onopen = function (evt) { + switch_page(default_page); --- a/svghmi/svghmi_server.py Fri Oct 11 12:03:14 2019 +0200
+++ b/svghmi/svghmi_server.py Tue Oct 15 17:14:48 2019 +0200
@@ -85,7 +85,7 @@
def onMessage(self, msg, isBinary):
self._hmi_session.onMessage(msg)
#self.sendMessage(msg, binary)
class HMIWebSocketServerFactory(WebSocketServerFactory):