Edouard@2783: // svghmi.js Edouard@2783: Edouard@2798: function dispatch_value(index, value) { Edouard@2798: console.log("dispatch_value("+index+value+")"); Edouard@2798: }; Edouard@2783: 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@2798: for(let hash_int of hmi_hash) { Edouard@2798: if(hash_int != dv.getUint8(i)){ Edouard@2798: console.log("Recv non maching hash. Reload."); Edouard@2798: Edouard@2798: // 1003 is for "Unsupported Data" Edouard@2798: ws.close(1003,"Hash doesn't match"); Edouard@2798: Edouard@2798: // TODO : remove debug alert ? Edouard@2798: alert("HMI will be reloaded."); Edouard@2798: Edouard@2798: // force reload ignoring cache Edouard@2798: location.reload(true); Edouard@2798: }; Edouard@2798: i++; Edouard@2780: }; Edouard@2783: Edouard@2798: while(i < data.length){ Edouard@2798: let index = dv.getUint32(i); Edouard@2798: i += 4; Edouard@2798: let iectype = hmitree_types[index]; Edouard@2798: let [dvgetter, bytesize] = dvgetters[iectypes]; Edouard@2798: value = dvgetter.call(dv,i); Edouard@2798: dispatch_value(index, value); Edouard@2798: i += bytesize; Edouard@2798: }; Edouard@2798: }; Edouard@2783: Edouard@2788: Edouard@2798: function send_blob(data) { Edouard@2798: if(data.length > 0) { Edouard@2798: ws.send(new Blob([ Edouard@2798: new Uint8Array(hmi_hash), Edouard@2798: 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@2798: let new_period; Edouard@2798: if(widgets.size > 0) { Edouard@2798: let maxfreq = 0; Edouard@2798: for(let widget of widgets) Edouard@2798: if(maxfreq < widgets.frequency) Edouard@2798: maxfreq = widgets.frequency; Edouard@2798: Edouard@2798: new_period = 1000/maxfreq; Edouard@2798: } else { Edouard@2798: new_period = 0; Edouard@2798: } Edouard@2798: Edouard@2798: if(previous_period != new_period) { Edouard@2798: subscriptions[index] = new_period; Edouard@2798: delta.push([ Edouard@2798: new Uint8Array([2]), /* subscribe = 2 */ Edouard@2798: new Uint32Array([index]), Edouard@2798: new Uint16Array([new_period])]); Edouard@2798: } Edouard@2798: Edouard@2798: } Edouard@2798: send_blob(delta); Edouard@2798: }; Edouard@2798: Edouard@2798: function update_value(index, value) { Edouard@2798: iectype = hmitree_types[index]; Edouard@2798: jstype = typedarray_types[iectypes]; Edouard@2798: send_blob([ Edouard@2798: new Uint8Array([0]), /* setval = 0 */ Edouard@2798: new jstype([value]) Edouard@2798: ]); Edouard@2798: Edouard@2798: }; Edouard@2798: 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@2798: send_reset(); Edouard@2798: // show main page Edouard@2798: switch_page(default_page); Edouard@2798: Edouard@2798: }; Edouard@2798: