Edouard@2783: // svghmi.js Edouard@2783: Edouard@2798: function dispatch_value(index, value) { Edouard@2800: let widgets = subscribers[index]; Edouard@2800: Edouard@2801: // TODO : value cache Edouard@2801: Edouard@2800: if(widgets.size > 0) { Edouard@2800: for(let widget of widgets){ Edouard@2800: let idxidx = widget.indexes.indexOf(index); Edouard@2800: if(idxidx == -1){ Edouard@2800: throw new Error("Dispatching to widget not interested, should not happen."); Edouard@2800: } Edouard@2800: let d = widget.dispatch; Edouard@2800: if(typeof(d) == "function" && idxidx == 0){ Edouard@2800: return d.call(widget,value); Edouard@2800: }else if(typeof(d) == "object" && d.length >= idxidx){ Edouard@2800: d[idxidx].call(widget,value); Edouard@2800: }/* else dispatch_0, ..., dispatch_n ? */ Edouard@2800: /*else { Edouard@2800: throw new Error("Dunno how to dispatch to widget at index = " + index); Edouard@2800: }*/ Edouard@2800: } Edouard@2800: } Edouard@2798: }; Edouard@2783: Edouard@2801: function init_widgets() { Edouard@2801: Object.keys(hmi_widgets).forEach(function(id) { Edouard@2801: let widget = hmi_widgets[id]; Edouard@2801: let init = widget.init; Edouard@2801: if(typeof(init) == "function"){ Edouard@2801: return init.call(widget); Edouard@2801: } Edouard@2801: }); Edouard@2801: }; Edouard@2801: Edouard@2798: // Open WebSocket to relative "/ws" address Edouard@2798: var ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws')); Edouard@2798: ws.binaryType = 'arraybuffer'; Edouard@2783: Edouard@2798: const dvgetters = { Edouard@2798: INT: [DataView.prototype.getInt16, 2], Edouard@2798: BOOL: [DataView.prototype.getInt8, 1] Edouard@2798: /* TODO */ Edouard@2798: }; Edouard@2798: Edouard@2798: // Register message reception handler Edouard@2798: ws.onmessage = function (evt) { Edouard@2798: Edouard@2798: let data = evt.data; Edouard@2798: let dv = new DataView(data); Edouard@2798: let i = 0; Edouard@2799: try { Edouard@2799: for(let hash_int of hmi_hash) { Edouard@2799: if(hash_int != dv.getUint8(i)){ Edouard@2800: throw new Error("Hash doesn't match"); Edouard@2799: }; Edouard@2799: i++; Edouard@2799: }; Edouard@2798: Edouard@2799: while(i < data.byteLength){ Edouard@2799: let index = dv.getUint32(i, true); Edouard@2799: i += 4; Edouard@2799: let iectype = hmitree_types[index]; Edouard@2799: if(iectype != undefined){ Edouard@2799: let [dvgetter, bytesize] = dvgetters[iectype]; Edouard@2799: let value = dvgetter.call(dv,i,true); Edouard@2799: dispatch_value(index, value); Edouard@2799: i += bytesize; Edouard@2799: } else { Edouard@2799: throw new Error("Unknown index "+index) Edouard@2799: } Edouard@2799: }; Edouard@2799: } catch(err) { Edouard@2799: // 1003 is for "Unsupported Data" Edouard@2799: // ws.close(1003, err.message); Edouard@2798: Edouard@2799: // TODO : remove debug alert ? Edouard@2799: alert("Error : "+err.message+"\\\\nHMI will be reloaded."); Edouard@2798: Edouard@2799: // force reload ignoring cache Edouard@2799: location.reload(true); Edouard@2799: } Edouard@2798: }; Edouard@2783: Edouard@2788: Edouard@2798: function send_blob(data) { Edouard@2798: if(data.length > 0) { Edouard@2799: ws.send(new Blob([new Uint8Array(hmi_hash)].concat(data))); Edouard@2780: }; Edouard@2798: }; Edouard@2798: Edouard@2798: const typedarray_types = { Edouard@2798: INT: Int16Array, Edouard@2798: BOOL: Uint8Array Edouard@2798: /* TODO */ Edouard@2798: }; Edouard@2798: Edouard@2798: function send_reset() { Edouard@2798: send_blob(new Uint8Array([1])); /* reset = 1 */ Edouard@2798: }; Edouard@2798: Edouard@2798: // subscription state, as it should be in hmi server Edouard@2798: // hmitree indexed array of integers Edouard@2798: var subscriptions = hmitree_types.map(_ignored => 0); Edouard@2798: Edouard@2798: // subscription state as needed by widget now Edouard@2798: // hmitree indexed array of Sets of widgets objects Edouard@2798: var subscribers = hmitree_types.map(_ignored => new Set()); Edouard@2798: Edouard@2798: function update_subscriptions() { Edouard@2798: let delta = []; Edouard@2798: for(let index = 0; index < subscribers.length; index++){ Edouard@2798: let widgets = subscribers[index]; Edouard@2798: Edouard@2798: // periods are in ms Edouard@2798: let previous_period = subscriptions[index]; Edouard@2798: Edouard@2799: let new_period = 0; Edouard@2798: if(widgets.size > 0) { Edouard@2798: let maxfreq = 0; Edouard@2798: for(let widget of widgets) Edouard@2799: if(maxfreq < widget.frequency) Edouard@2799: maxfreq = widget.frequency; Edouard@2798: Edouard@2799: if(maxfreq != 0) Edouard@2799: new_period = 1000/maxfreq; Edouard@2798: } Edouard@2798: Edouard@2798: if(previous_period != new_period) { Edouard@2798: subscriptions[index] = new_period; Edouard@2802: delta.push( Edouard@2798: new Uint8Array([2]), /* subscribe = 2 */ Edouard@2798: new Uint32Array([index]), Edouard@2802: new Uint16Array([new_period])); Edouard@2798: } Edouard@2798: Edouard@2798: } Edouard@2798: send_blob(delta); Edouard@2798: }; Edouard@2798: Edouard@2801: function send_hmi_value(index, value) { Edouard@2802: let iectype = hmitree_types[index]; Edouard@2802: let jstype = typedarray_types[iectype]; Edouard@2798: send_blob([ Edouard@2798: new Uint8Array([0]), /* setval = 0 */ Edouard@2802: new Uint32Array([index]), Edouard@2802: new jstype([value])]); Edouard@2798: Edouard@2798: }; Edouard@2798: Edouard@2801: function change_hmi_value(index, opstr) { Edouard@2801: let op = opstr[0]; Edouard@2801: if(op == "=") Edouard@2801: return send_hmi_value(index, Number(opstr.slice(1))); Edouard@2801: Edouard@2801: alert('Change '+opstr+" TODO !!! (index :"+index+")"); Edouard@2801: } Edouard@2801: Edouard@2798: var current_page; Edouard@2798: Edouard@2798: function switch_page(page_name) { Edouard@2798: let old_desc = page_desc[current_page]; Edouard@2798: let new_desc = page_desc[page_name]; Edouard@2798: /* TODO hide / show widgets */ Edouard@2798: /* TODO move viewport */ Edouard@2798: Edouard@2798: /* remove subsribers of previous page if any */ Edouard@2798: if(old_desc) for(let widget of old_desc.widgets){ Edouard@2798: for(let index of widget.indexes){ Edouard@2798: subscribers[index].delete(widget); Edouard@2798: } Edouard@2798: } Edouard@2798: /* add new subsribers if any */ Edouard@2798: if(new_desc) for(let widget of new_desc.widgets){ Edouard@2798: for(let index of widget.indexes){ Edouard@2798: subscribers[index].add(widget); Edouard@2798: } Edouard@2798: } Edouard@2798: Edouard@2798: current_page = page_name; Edouard@2798: Edouard@2798: update_subscriptions(); Edouard@2798: }; Edouard@2798: Edouard@2798: Edouard@2798: // Once connection established Edouard@2798: ws.onopen = function (evt) { Edouard@2801: init_widgets(); Edouard@2798: send_reset(); Edouard@2798: // show main page Edouard@2798: switch_page(default_page); Edouard@2799: }; Edouard@2799: Edouard@2799: ws.onclose = function (evt) { Edouard@2799: // TODO : add visible notification while waiting for reload Edouard@2799: console.log("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+" Reload in 10s."); Edouard@2799: // TODO : re-enable auto reload when not in debug Edouard@2799: //window.setTimeout(() => location.reload(true), 10000); Edouard@2799: alert("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+"."); Edouard@2798: Edouard@2798: };