beremiz

SVGHMI: Added simple HMI Tree View.
SVGHMI_compat
2019-11-15, Edouard Tisserant
d51b75cf6d95
SVGHMI: Added simple HMI Tree View.
// svghmi.js
var ws;
var cache = hmitree_types.map(function(_ignored) {return undefined});
function dispatch_value_to_widget(widget, index, value, oldval) {
var idxidx = widget.indexes.indexOf(index);
if(idxidx == -1){
throw new Error("Dispatching to widget not interested, should not happen.");
}
var d = widget.dispatch;
if(typeof(d) == "function" && idxidx == 0){
return d.call(widget, value, oldval);
}else if(typeof(d) == "object" && d.length >= idxidx){
return d[idxidx].call(widget, value, oldval);
}/* else dispatch_0, ..., dispatch_n ? */
/*else {
throw new Error("Dunno how to dispatch to widget at index = " + index);
}*/
}
function dispatch_value(index, value) {
var destwidgets = subscribers[index];
var oldval = cache[index];
cache[index] = value;
if(destwidgets.size > 0) {
destwidgets.forEach(function(widget) {
dispatch_value_to_widget(widget, index, value, oldval);
});
}
};
function init_widgets() {
Object.keys(hmi_widgets).forEach(function(id) {
var widget = hmi_widgets[id];
var init = widget.init;
if(typeof(init) == "function"){
return init.call(widget);
}
});
};
const dvgetters = {
INT: [DataView.prototype.getInt16, 2],
BOOL: [DataView.prototype.getInt8, 1]
/* TODO */
};
function send_blob(data) {
if(data.length > 0) {
ws.send(new Blob([new Uint8Array(hmi_hash)].concat(data)));
};
};
const typedarray_types = {
INT: Int16Array,
BOOL: Uint8Array
/* TODO */
};
function send_reset() {
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(function(_ignored) {return 0;});
/*! (c) Andrea Giammarchi - ISC */
var self = this || /* istanbul ignore next */ {};
try { self.Set = Set; }
catch (Set) {
(function (i, dPs) {'use strict';
var proto = dPs(Set.prototype, {size: {
configurable: true,
get: function () {
return this._v.length;
}
}});
proto.add = function (value) {
if (!has(this, value))
this._v.push(value);
return this;
};
proto.clear = function () {
var length = this._v.length;
this._v.splice(0, length);
};
proto.delete = function (value) {
return has(this, value) && !!this._v.splice(i, 1);
};
proto.entries = function () {
return this._v.map(pair);
};
proto.forEach = function (callback, context) {
this._v.forEach(
function (value, i) {
callback.call(context, value, value, this);
},
this
);
};
proto.has = function (key) {
return has(this, key);
};
proto.keys = proto.values = function () {
return this._v.slice(0);
};
self.Set = Set;
function Set(iterable) {
dPs(this, {_v: {value: []}});
if (iterable)
iterable.forEach(this.add, this);
}
function has(self, value) {
i = self._v.indexOf(value);
return -1 < i;
}
function pair(value) {
return [value, value];
}
}(0, Object.defineProperties));
}
// subscription state as needed by widget now
// hmitree indexed array of Sets of widgets objects
var subscribers = hmitree_types.map(function(_ignored) {return new Set();});
function update_subscriptions() {
var delta = [];
var index;
for(index = 0; index < subscribers.length; index++){
var subscribed = subscribers[index];
// periods are in ms
var previous_period = subscriptions[index];
var new_period = 0;
if(subscribed.size > 0) {
var maxfreq = 0;
subscribed.forEach(function(widget) {
if(maxfreq < widget.frequency)
maxfreq = widget.frequency;
});
if(maxfreq != 0)
new_period = 1000/maxfreq;
}
if(previous_period != new_period) {
subscriptions[index] = new_period;
delta.push(
new Uint8Array([2]), /* subscribe = 2 */
new Uint32Array([index]),
new Uint16Array([new_period]));
}
}
send_blob(delta);
};
function send_hmi_value(index, value) {
var iectype = hmitree_types[index];
var jstype = typedarray_types[iectype];
send_blob([
new Uint8Array([0]), /* setval = 0 */
new Uint32Array([index]),
new jstype([value])]);
cache[index] = value;
};
function change_hmi_value(index, opstr) {
var op = opstr[0];
var given_val = opstr.slice(1);
var old_val = cache[index]
var new_val;
switch(op){
case "=":
eval("new_val"+opstr);
break;
case "+":
case "-":
case "*":
case "/":
if(old_val != undefined)
new_val = eval("old_val"+opstr);
break;
}
if(new_val != undefined && old_val != new_val)
send_hmi_value(index, new_val);
return new_val;
}
var current_page;
function switch_page(page_name) {
var old_desc = page_desc[current_page];
var new_desc = page_desc[page_name];
/* TODO hide / show widgets */
/* remove subsribers of previous page if any */
if(old_desc) {
old_desc.widgets.forEach(function(widget) {
widget.indexes.forEach(function(index) {
subscribers[index].delete(widget);
});
});
}
if(new_desc) {
/* add new subsribers if any */
new_desc.widgets.forEach(function(widget) {
widget.indexes.forEach(function(index) {
subscribers[index].add(widget);
var cached_val = cache[index];
if(cached_val != undefined)
dispatch_value_to_widget(widget, index, cached_val, cached_val);
});
});
svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
// TODO dispatch current cache in newly opened page
}
current_page = page_name;
update_subscriptions();
};
// Open WebSocket to relative "/ws" address
ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws'));
ws.binaryType = 'arraybuffer';
// Once connection established
ws.onopen = function (evt) {
init_widgets();
send_reset();
// show main page
switch_page(default_page);
};
ws.onclose = function (evt) {
// TODO : add visible notification while waiting for reload
console.log("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+" Reload in 10s.");
// TODO : re-enable auto reload when not in debug
//window.setTimeout(() => location.reload(true), 10000);
alert("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+".");
};
// Register message reception handler
ws.onmessage = function (evt) {
var data = evt.data;
var dv = new DataView(data);
var i = 0;
try {
hmi_hash.forEach(function(hash_int) {
if(hash_int != dv.getUint8(i)){
throw new Error("Hash doesn't match");
};
i++;
});
while(i < data.byteLength){
var index = dv.getUint32(i, true);
i += 4;
var iectype = hmitree_types[index];
if(iectype != undefined){
var _dvgetter = dvgetters[iectype];
var dvgetter = _dvgetter[0];
var bytesize = _dvgetter[1];
var value = dvgetter.call(dv,i,true);
dispatch_value(index, value);
i += bytesize;
} else {
throw new Error("Unknown index "+index)
}
};
} catch(err) {
// 1003 is for "Unsupported Data"
// ws.close(1003, err.message);
// TODO : remove debug alert ?
alert("Error : "+err.message+"\\\\nHMI will be reloaded.");
// force reload ignoring cache
location.reload(true);
}
};