svghmi/svghmi.js
branchsvghmi
changeset 2829 4c2c50f60730
parent 2827 af32e80d108f
child 2834 6ac6a9dff594
equal deleted inserted replaced
2828:be947a338760 2829:4c2c50f60730
    57                 size /* size of string */
    57                 size /* size of string */
    58             )), size + 1]; /* total increment */
    58             )), size + 1]; /* total increment */
    59     }
    59     }
    60 };
    60 };
    61 
    61 
    62 // Register message reception handler 
    62 // Register message reception handler
    63 ws.onmessage = function (evt) {
    63 ws.onmessage = function (evt) {
    64 
    64 
    65     let data = evt.data;
    65     let data = evt.data;
    66     let dv = new DataView(data);
    66     let dv = new DataView(data);
    67     let i = 0;
    67     let i = 0;
   104         ws.send(new Blob([new Uint8Array(hmi_hash)].concat(data)));
   104         ws.send(new Blob([new Uint8Array(hmi_hash)].concat(data)));
   105     };
   105     };
   106 };
   106 };
   107 
   107 
   108 const typedarray_types = {
   108 const typedarray_types = {
   109     INT: Int16Array,
   109     INT: (number) => new Int16Array([number]),
   110     BOOL: Uint8Array
   110     BOOL: (truth) => new Int16Array([truth]),
       
   111     STRING: (str) => {
       
   112         // beremiz default string max size is 128
       
   113         str = str.slice(0,128);
       
   114         binary = new Uint8Array(str.length + 1);
       
   115         binary[0] = str.length;
       
   116         for(var i = 0; i < str.length; i++){
       
   117             binary[i+1] = str.charCodeAt(i);
       
   118         }
       
   119         return binary;
       
   120     }
   111     /* TODO */
   121     /* TODO */
   112 };
   122 };
   113 
   123 
   114 function send_reset() {
   124 function send_reset() {
   115     send_blob(new Uint8Array([1])); /* reset = 1 */
   125     send_blob(new Uint8Array([1])); /* reset = 1 */
   122 // subscription state as needed by widget now
   132 // subscription state as needed by widget now
   123 // hmitree indexed array of Sets of widgets objects
   133 // hmitree indexed array of Sets of widgets objects
   124 var subscribers = hmitree_types.map(_ignored => new Set());
   134 var subscribers = hmitree_types.map(_ignored => new Set());
   125 
   135 
   126 // artificially subscribe the watchdog widget to "/heartbeat" hmi variable
   136 // artificially subscribe the watchdog widget to "/heartbeat" hmi variable
   127 // Since dispatch directly calls change_hmi_value, 
   137 // Since dispatch directly calls change_hmi_value,
   128 // PLC will periodically send variable at given frequency
   138 // PLC will periodically send variable at given frequency
   129 subscribers[heartbeat_index].add({
   139 subscribers[heartbeat_index].add({
   130     /* type: "Watchdog", */
   140     /* type: "Watchdog", */
   131     frequency: 1,
   141     frequency: 1,
   132     indexes: [heartbeat_index],
   142     indexes: [heartbeat_index],
   133     dispatch: function(value) {
   143     dispatch: function(value) {
   134         // console.log("Heartbeat" + value);
   144         // console.log("Heartbeat" + value);
   135         change_hmi_value(this.indexes[0], "+1");
   145         change_hmi_value(heartbeat_index, "+1");
   136     }
   146     }
   137 });
   147 });
   138 
   148 
   139 function update_subscriptions() {
   149 function update_subscriptions() {
   140     let delta = [];
   150     let delta = [];
   167     send_blob(delta);
   177     send_blob(delta);
   168 };
   178 };
   169 
   179 
   170 function send_hmi_value(index, value) {
   180 function send_hmi_value(index, value) {
   171     let iectype = hmitree_types[index];
   181     let iectype = hmitree_types[index];
   172     let jstype = typedarray_types[iectype];
   182     let tobinary = typedarray_types[iectype];
   173     send_blob([
   183     send_blob([
   174         new Uint8Array([0]),  /* setval = 0 */
   184         new Uint8Array([0]),  /* setval = 0 */
   175         new Uint32Array([index]), 
   185         new Uint32Array([index]),
   176         new jstype([value])]);
   186         tobinary(value)]);
   177 
   187 
   178     cache[index] = value;
   188     cache[index] = value;
   179 };
   189 };
   180 
   190 
   181 function change_hmi_value(index, opstr) {
   191 function change_hmi_value(index, opstr) {
   220             for(let index of widget.indexes){
   230             for(let index of widget.indexes){
   221                 subscribers[index].add(widget);
   231                 subscribers[index].add(widget);
   222                 let cached_val = cache[index];
   232                 let cached_val = cache[index];
   223                 if(cached_val != undefined)
   233                 if(cached_val != undefined)
   224                     dispatch_value_to_widget(widget, index, cached_val, cached_val);
   234                     dispatch_value_to_widget(widget, index, cached_val, cached_val);
   225                 
       
   226             }
   235             }
   227         }
   236         }
   228         svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
   237         svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
   229         // TODO dispatch current cache in newly opened page
   238         // TODO dispatch current cache in newly opened page
   230     }
   239     }