svghmi/svghmi.js
branchsvghmi
changeset 2800 68cee1366b9c
parent 2799 f5da343b9b63
child 2801 390acff12755
equal deleted inserted replaced
2799:f5da343b9b63 2800:68cee1366b9c
     1 // svghmi.js
     1 // svghmi.js
     2 
     2 
     3 function dispatch_value(index, value) {
     3 function dispatch_value(index, value) {
     4     console.log("dispatch_value("+index+", "+value+")");
     4     let widgets = subscribers[index];
       
     5 
       
     6     if(widgets.size > 0) {
       
     7         for(let widget of widgets){
       
     8             let idxidx = widget.indexes.indexOf(index);
       
     9             if(idxidx == -1){
       
    10                 throw new Error("Dispatching to widget not interested, should not happen.");
       
    11             }
       
    12             let d = widget.dispatch;
       
    13             if(typeof(d) == "function" && idxidx == 0){
       
    14                 return d.call(widget,value);
       
    15             }else if(typeof(d) == "object" && d.length >= idxidx){
       
    16                 d[idxidx].call(widget,value);
       
    17             }/* else dispatch_0, ..., dispatch_n ? */
       
    18             /*else {
       
    19                 throw new Error("Dunno how to dispatch to widget at index = " + index);
       
    20             }*/
       
    21         }
       
    22     }
     5 };
    23 };
     6 
    24 
     7 // Open WebSocket to relative "/ws" address
    25 // Open WebSocket to relative "/ws" address
     8 var ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws'));
    26 var ws = new WebSocket(window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws'));
     9 ws.binaryType = 'arraybuffer';
    27 ws.binaryType = 'arraybuffer';
    21     let dv = new DataView(data);
    39     let dv = new DataView(data);
    22     let i = 0;
    40     let i = 0;
    23     try {
    41     try {
    24         for(let hash_int of hmi_hash) {
    42         for(let hash_int of hmi_hash) {
    25             if(hash_int != dv.getUint8(i)){
    43             if(hash_int != dv.getUint8(i)){
    26                 throw new Error("Hash doesn't match")
    44                 throw new Error("Hash doesn't match");
    27             };
    45             };
    28             i++;
    46             i++;
    29         };
    47         };
    30 
    48 
    31         while(i < data.byteLength){
    49         while(i < data.byteLength){
    32             let index = dv.getUint32(i, true);
    50             let index = dv.getUint32(i, true);
    33             console.log("Recv something index is "+index);
       
    34             i += 4;
    51             i += 4;
    35             let iectype = hmitree_types[index];
    52             let iectype = hmitree_types[index];
    36             if(iectype != undefined){
    53             if(iectype != undefined){
    37                 let [dvgetter, bytesize] = dvgetters[iectype];
    54                 let [dvgetter, bytesize] = dvgetters[iectype];
    38                 let value = dvgetter.call(dv,i,true);
    55                 let value = dvgetter.call(dv,i,true);