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) {
let dv = new DataView(data);
for(let hash_int of hmi_hash) {
if(hash_int != dv.getUint8(i)){
throw new Error("Hash doesn't match")
while(i < data.byteLength){
let index = dv.getUint32(i, true);
console.log("Recv something index is "+index);
let iectype = hmitree_types[index];
if(iectype != undefined){
let [dvgetter, bytesize] = dvgetters[iectype];
let value = dvgetter.call(dv,i,true);
dispatch_value(index, value);
throw new Error("Unknown index "+index)
// 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
function send_blob(data) {
ws.send(new Blob([new Uint8Array(hmi_hash)].concat(data)));
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 < widget.frequency)
maxfreq = widget.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 */
/* 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;
// Once connection established
ws.onopen = function (evt) {
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+".");