svghmi/gen_index_xhtml.xslt
branchsvghmi
changeset 2799 f5da343b9b63
parent 2798 ddb2c4668a6b
child 2800 68cee1366b9c
equal deleted inserted replaced
2798:ddb2c4668a6b 2799:f5da343b9b63
   177       </xsl:if>
   177       </xsl:if>
   178     </xsl:variable>
   178     </xsl:variable>
   179     <func:result select="exsl:node-set($ast)"/>
   179     <func:result select="exsl:node-set($ast)"/>
   180   </func:function>
   180   </func:function>
   181   <xsl:template name="scripts">
   181   <xsl:template name="scripts">
   182     <xsl:text>(function(){
   182     <xsl:text>//(function(){
   183 </xsl:text>
   183 </xsl:text>
   184     <xsl:text>
   184     <xsl:text>
   185 </xsl:text>
   185 </xsl:text>
   186     <xsl:text>var hmi_hash = [</xsl:text>
   186     <xsl:text>var hmi_hash = [</xsl:text>
   187     <xsl:value-of select="$hmitree/@hash"/>
   187     <xsl:value-of select="$hmitree/@hash"/>
   317 </xsl:text>
   317 </xsl:text>
   318     <xsl:text>
   318     <xsl:text>
   319 </xsl:text>
   319 </xsl:text>
   320     <xsl:text>function dispatch_value(index, value) {
   320     <xsl:text>function dispatch_value(index, value) {
   321 </xsl:text>
   321 </xsl:text>
   322     <xsl:text>    console.log("dispatch_value("+index+value+")");
   322     <xsl:text>    console.log("dispatch_value("+index+", "+value+")");
   323 </xsl:text>
   323 </xsl:text>
   324     <xsl:text>};
   324     <xsl:text>};
   325 </xsl:text>
   325 </xsl:text>
   326     <xsl:text>
   326     <xsl:text>
   327 </xsl:text>
   327 </xsl:text>
   355 </xsl:text>
   355 </xsl:text>
   356     <xsl:text>    let dv = new DataView(data);
   356     <xsl:text>    let dv = new DataView(data);
   357 </xsl:text>
   357 </xsl:text>
   358     <xsl:text>    let i = 0;
   358     <xsl:text>    let i = 0;
   359 </xsl:text>
   359 </xsl:text>
   360     <xsl:text>    for(let hash_int of hmi_hash) {
   360     <xsl:text>    console.log("Recv something.");
   361 </xsl:text>
   361 </xsl:text>
   362     <xsl:text>        if(hash_int != dv.getUint8(i)){
   362     <xsl:text>    try {
   363 </xsl:text>
   363 </xsl:text>
   364     <xsl:text>            console.log("Recv non maching hash. Reload.");
   364     <xsl:text>        for(let hash_int of hmi_hash) {
   365 </xsl:text>
   365 </xsl:text>
   366     <xsl:text>
   366     <xsl:text>            if(hash_int != dv.getUint8(i)){
   367 </xsl:text>
   367 </xsl:text>
   368     <xsl:text>            // 1003 is for "Unsupported Data"
   368     <xsl:text>                throw new Error("Hash doesn't match")
   369 </xsl:text>
   369 </xsl:text>
   370     <xsl:text>            ws.close(1003,"Hash doesn't match");
   370     <xsl:text>            };
   371 </xsl:text>
   371 </xsl:text>
   372     <xsl:text>
   372     <xsl:text>            i++;
   373 </xsl:text>
       
   374     <xsl:text>            // TODO : remove debug alert ?
       
   375 </xsl:text>
       
   376     <xsl:text>            alert("HMI will be reloaded.");
       
   377 </xsl:text>
       
   378     <xsl:text>
       
   379 </xsl:text>
       
   380     <xsl:text>            // force reload ignoring cache
       
   381 </xsl:text>
       
   382     <xsl:text>            location.reload(true);
       
   383 </xsl:text>
   373 </xsl:text>
   384     <xsl:text>        };
   374     <xsl:text>        };
   385 </xsl:text>
   375 </xsl:text>
   386     <xsl:text>        i++;
   376     <xsl:text>
       
   377 </xsl:text>
       
   378     <xsl:text>        console.log("Recv something GOOD.");
       
   379 </xsl:text>
       
   380     <xsl:text>
       
   381 </xsl:text>
       
   382     <xsl:text>        while(i &lt; data.byteLength){
       
   383 </xsl:text>
       
   384     <xsl:text>            let index = dv.getUint32(i, true);
       
   385 </xsl:text>
       
   386     <xsl:text>            console.log("Recv something index is "+index);
       
   387 </xsl:text>
       
   388     <xsl:text>            i += 4;
       
   389 </xsl:text>
       
   390     <xsl:text>            let iectype = hmitree_types[index];
       
   391 </xsl:text>
       
   392     <xsl:text>            if(iectype != undefined){
       
   393 </xsl:text>
       
   394     <xsl:text>                let [dvgetter, bytesize] = dvgetters[iectype];
       
   395 </xsl:text>
       
   396     <xsl:text>                let value = dvgetter.call(dv,i,true);
       
   397 </xsl:text>
       
   398     <xsl:text>                dispatch_value(index, value);
       
   399 </xsl:text>
       
   400     <xsl:text>                i += bytesize;
       
   401 </xsl:text>
       
   402     <xsl:text>            } else {
       
   403 </xsl:text>
       
   404     <xsl:text>                throw new Error("Unknown index "+index)
       
   405 </xsl:text>
       
   406     <xsl:text>            }
       
   407 </xsl:text>
       
   408     <xsl:text>        };
       
   409 </xsl:text>
       
   410     <xsl:text>    } catch(err) {
       
   411 </xsl:text>
       
   412     <xsl:text>        // 1003 is for "Unsupported Data"
       
   413 </xsl:text>
       
   414     <xsl:text>        // ws.close(1003, err.message);
       
   415 </xsl:text>
       
   416     <xsl:text>
       
   417 </xsl:text>
       
   418     <xsl:text>        // TODO : remove debug alert ?
       
   419 </xsl:text>
       
   420     <xsl:text>        alert("Error : "+err.message+"\nHMI will be reloaded.");
       
   421 </xsl:text>
       
   422     <xsl:text>
       
   423 </xsl:text>
       
   424     <xsl:text>        // force reload ignoring cache
       
   425 </xsl:text>
       
   426     <xsl:text>        location.reload(true);
       
   427 </xsl:text>
       
   428     <xsl:text>    }
       
   429 </xsl:text>
       
   430     <xsl:text>};
       
   431 </xsl:text>
       
   432     <xsl:text>
       
   433 </xsl:text>
       
   434     <xsl:text>
       
   435 </xsl:text>
       
   436     <xsl:text>function send_blob(data) {
       
   437 </xsl:text>
       
   438     <xsl:text>    if(data.length &gt; 0) {
       
   439 </xsl:text>
       
   440     <xsl:text>        ws.send(new Blob([new Uint8Array(hmi_hash)].concat(data)));
   387 </xsl:text>
   441 </xsl:text>
   388     <xsl:text>    };
   442     <xsl:text>    };
   389 </xsl:text>
   443 </xsl:text>
   390     <xsl:text>
       
   391 </xsl:text>
       
   392     <xsl:text>    while(i &lt; data.length){
       
   393 </xsl:text>
       
   394     <xsl:text>        let index = dv.getUint32(i);
       
   395 </xsl:text>
       
   396     <xsl:text>        i += 4;
       
   397 </xsl:text>
       
   398     <xsl:text>        let iectype = hmitree_types[index];
       
   399 </xsl:text>
       
   400     <xsl:text>        let [dvgetter, bytesize] = dvgetters[iectypes];
       
   401 </xsl:text>
       
   402     <xsl:text>        value = dvgetter.call(dv,i);
       
   403 </xsl:text>
       
   404     <xsl:text>        dispatch_value(index, value);
       
   405 </xsl:text>
       
   406     <xsl:text>        i += bytesize;
       
   407 </xsl:text>
       
   408     <xsl:text>    };
       
   409 </xsl:text>
       
   410     <xsl:text>};
       
   411 </xsl:text>
       
   412     <xsl:text>
       
   413 </xsl:text>
       
   414     <xsl:text>
       
   415 </xsl:text>
       
   416     <xsl:text>function send_blob(data) {
       
   417 </xsl:text>
       
   418     <xsl:text>    if(data.length &gt; 0) {
       
   419 </xsl:text>
       
   420     <xsl:text>        ws.send(new Blob([
       
   421 </xsl:text>
       
   422     <xsl:text>            new Uint8Array(hmi_hash), 
       
   423 </xsl:text>
       
   424     <xsl:text>            data]));
       
   425 </xsl:text>
       
   426     <xsl:text>    };
       
   427 </xsl:text>
       
   428     <xsl:text>};
   444     <xsl:text>};
   429 </xsl:text>
   445 </xsl:text>
   430     <xsl:text>
   446     <xsl:text>
   431 </xsl:text>
   447 </xsl:text>
   432     <xsl:text>const typedarray_types = {
   448     <xsl:text>const typedarray_types = {
   479 </xsl:text>
   495 </xsl:text>
   480     <xsl:text>        let previous_period = subscriptions[index];
   496     <xsl:text>        let previous_period = subscriptions[index];
   481 </xsl:text>
   497 </xsl:text>
   482     <xsl:text>
   498     <xsl:text>
   483 </xsl:text>
   499 </xsl:text>
   484     <xsl:text>        let new_period;
   500     <xsl:text>        let new_period = 0;
   485 </xsl:text>
   501 </xsl:text>
   486     <xsl:text>        if(widgets.size &gt; 0) {
   502     <xsl:text>        if(widgets.size &gt; 0) {
   487 </xsl:text>
   503 </xsl:text>
   488     <xsl:text>            let maxfreq = 0;
   504     <xsl:text>            let maxfreq = 0;
   489 </xsl:text>
   505 </xsl:text>
   490     <xsl:text>            for(let widget of widgets)
   506     <xsl:text>            for(let widget of widgets)
   491 </xsl:text>
   507 </xsl:text>
   492     <xsl:text>                if(maxfreq &lt; widgets.frequency)
   508     <xsl:text>                if(maxfreq &lt; widget.frequency)
   493 </xsl:text>
   509 </xsl:text>
   494     <xsl:text>                    maxfreq = widgets.frequency;
   510     <xsl:text>                    maxfreq = widget.frequency;
   495 </xsl:text>
   511 </xsl:text>
   496     <xsl:text>
   512     <xsl:text>
   497 </xsl:text>
   513 </xsl:text>
   498     <xsl:text>            new_period = 1000/maxfreq;
   514     <xsl:text>            if(maxfreq != 0)
   499 </xsl:text>
   515 </xsl:text>
   500     <xsl:text>        } else {
   516     <xsl:text>                new_period = 1000/maxfreq;
   501 </xsl:text>
       
   502     <xsl:text>            new_period = 0;
       
   503 </xsl:text>
   517 </xsl:text>
   504     <xsl:text>        }
   518     <xsl:text>        }
   505 </xsl:text>
   519 </xsl:text>
   506     <xsl:text>
   520     <xsl:text>         
   507 </xsl:text>
   521 </xsl:text>
   508     <xsl:text>        if(previous_period != new_period) {
   522     <xsl:text>        if(previous_period != new_period) {
   509 </xsl:text>
   523 </xsl:text>
   510     <xsl:text>            subscriptions[index] = new_period;
   524     <xsl:text>            subscriptions[index] = new_period;
   511 </xsl:text>
   525 </xsl:text>
   512     <xsl:text>            delta.push([
   526     <xsl:text>            delta.push(new Blob([
   513 </xsl:text>
   527 </xsl:text>
   514     <xsl:text>                new Uint8Array([2]), /* subscribe = 2 */
   528     <xsl:text>                new Uint8Array([2]), /* subscribe = 2 */
   515 </xsl:text>
   529 </xsl:text>
   516     <xsl:text>                new Uint32Array([index]), 
   530     <xsl:text>                new Uint32Array([index]), 
   517 </xsl:text>
   531 </xsl:text>
   518     <xsl:text>                new Uint16Array([new_period])]);
   532     <xsl:text>                new Uint16Array([new_period])]));
   519 </xsl:text>
   533 </xsl:text>
   520     <xsl:text>        }
   534     <xsl:text>        }
   521 </xsl:text>
   535 </xsl:text>
   522     <xsl:text>        
   536     <xsl:text>        
   523 </xsl:text>
   537 </xsl:text>
   611 </xsl:text>
   625 </xsl:text>
   612     <xsl:text>    // show main page
   626     <xsl:text>    // show main page
   613 </xsl:text>
   627 </xsl:text>
   614     <xsl:text>    switch_page(default_page);
   628     <xsl:text>    switch_page(default_page);
   615 </xsl:text>
   629 </xsl:text>
   616     <xsl:text>
   630     <xsl:text>};
   617 </xsl:text>
   631 </xsl:text>
   618     <xsl:text>};
   632     <xsl:text>
   619 </xsl:text>
   633 </xsl:text>
   620     <xsl:text>
   634     <xsl:text>ws.onclose = function (evt) {
   621 </xsl:text>
   635 </xsl:text>
   622     <xsl:text>})();
   636     <xsl:text>    // TODO : add visible notification while waiting for reload
       
   637 </xsl:text>
       
   638     <xsl:text>    console.log("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+" Reload in 10s.");
       
   639 </xsl:text>
       
   640     <xsl:text>    // TODO : re-enable auto reload when not in debug
       
   641 </xsl:text>
       
   642     <xsl:text>    //window.setTimeout(() =&gt; location.reload(true), 10000);
       
   643 </xsl:text>
       
   644     <xsl:text>    alert("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+".");
       
   645 </xsl:text>
       
   646     <xsl:text>
       
   647 </xsl:text>
       
   648     <xsl:text>};
       
   649 </xsl:text>
       
   650     <xsl:text>//})();
   623 </xsl:text>
   651 </xsl:text>
   624   </xsl:template>
   652   </xsl:template>
   625   <xsl:template mode="page_desc" match="*"/>
   653   <xsl:template mode="page_desc" match="*"/>
   626   <xsl:template mode="code_from_descs" match="*">
   654   <xsl:template mode="code_from_descs" match="*">
   627     <xsl:text>{
   655     <xsl:text>{