Andrew Boyson / web

Dependents:   oldheating gps motorhome heating

base/net/web-net-script.js

Committer:
andrewboyson
Date:
2020-03-11
Revision:
132:5b2df69a4f17
Child:
133:98c6bf14bc37

File content as of revision 132:5b2df69a4f17:

//Net script
'use strict';

let mac = '';
let tcp = '';

function parseTcpLine(line)
{
    if (line.length == 0) return;
    let fields = line.split('\t');
    let minutes  = parseInt(fields[0], 16) / 1000 / 60;
    let ipType   = parseInt(fields[1], 16);
    let arIndex  = parseInt(fields[2], 16);
    let locPort  = parseInt(fields[3], 16);
    
    
    tcp += Math.floor(minutes).toString().padStart(4, ' ');
    tcp += ' ';
    
    if      (ipType == 0x0800) tcp += "IPv4";                   
    else if (ipType == 0x86DD) tcp += "IPv6";
    else                       tcp += fields[1];
    tcp += ' ';
    
    tcp += arIndex.toString();                                  
    tcp += ' ';
    
    tcp += locPort.toString();                                     
    tcp += '\r\n';
}
function parseTcpLines(text)
{
    tcp = '';
    text.split('\n').forEach(parseTcpLine);
}
function parseGenLines(text)
{
    let lines = text.split('\n');
    mac = Net.makeMac(lines[ 0]);
}
function parse()
{
    let topics = Ajax.response.split('\f');
    parseGenLines(topics[0]);
    parseTcpLines(topics[1]);
}
function display()
{
    let elem;

    elem = Ajax.getElementOrNull('ajax-tcp'         ); if (elem) elem.textContent = tcp;
    elem = Ajax.getElementOrNull('ajax-mac'         ); if (elem) elem.textContent = mac;
}

Ajax.server     = '/net-ajax';
Ajax.onResponse = function() { parse(); display(); };
Ajax.init();