
//================ Setting =========================

const hostname = "localhost:9292"
const clock10Hz = 7
const scaler_name = [
    "Ungated",
    "Gated",
    "coin",
    "single",
    "PPACa",
    "PPACb",
    "SSD-OR",
    "Clock10Hz",
    "D1 FC",
    "pile up",
    "Beam",
    "Tel1 dE1",
    "Tel1 dE2",
    "Tel2,3 dE1,dE2",
    "Tel4,5 dE2",
    "Penetration",
    "SCR16",
    "SCR17",
    "SCR18",
    "SCR19",
    "SCR20",
    "SCR21",
    "SCR22",
    "SCR23",
    "SCR24",
    "SCR25",
    "SCR26",
    "SCR27",
    "SCR28",
    "SCR29",
    "SCR30",
    "SCR31"
]; 


//==================================================


var connection = new WebSocket('ws://'+hostname);
console.log("event01.js");
function initial () {

    connection.onmessage = function(event)
        {
        //var ol = document.getElementById('scaler')
        //ol.innerHTML = '<p>' + event.data + '</p>'
        //ol.innerHTML = '<p>' + data_to_obj(event.data) + '</p>'
        var result = data_to_obj(event.data);
        scrdata = result[0]
        if (result[1]){
        get_frequency (scrdata);
        make_table (scrdata);
        }
        
        }


}

function sleep(waitSec, callbackFunc) {
 
    var spanedSec = 0;
    var id = setInterval(function () {
        spanedSec++;
        if (spanedSec >= waitSec) {
            clearInterval(id);
            if (callbackFunc) callbackFunc();
        }
    }, 1000);
}

function send () {
  setInterval(send_blank, 1000);
}

function send_blank()
{
    connection.send("")
}


// ===================================================================

var data_array = [] 
var data_prev = Array(32).fill(0)
var data_diff = Array(32).fill(0)
var data_cps = Array(32).fill(0)

function data_to_obj (event_data) {
    let array = event_data.split(" ");
    var removals = ["", "\n"]
    array = array.filter(function(v) {
        return ! removals.includes(v);
    } );
    let data1 = [];
    let data2 = [];
    data1.push({ name : "Date", counts : array[0] });
    data1.push({ name : "ID" , counts : array[1] });
    data1.push({ name : "Block", counts : array[2] });
    for ( let i =0; i< 32; ++i) {
        var counts = parseInt(array[i+3])
        if (data_prev[i] != 0) {
            diff = counts - data_prev[i]
        } else {
            diff = 0
        }
        data2.push( { name: scaler_name[i],  counts: array[i+3]} )
        data_diff[i] = diff
        data_prev[i] = counts
    }
    var updated = true;
//    if (!data_prev[0]) updated = false;
   // if (data_diff[clock10Hz]>100) updated = false;
    if (!data_cps[clock10Hz]==10) updated = false;
    //console.log(updated);
    //console.log(data1);
    //console.log(data2);
    //console.log(data_diff);
    //console.log(data_prev);
    return [data2, updated];
}

function get_frequency (scrobj) {
    for ( let i =0; i< 32; ++i) {
        if (data_diff[clock10Hz] == 0 ) {
            scrobj[i].cps = 0
            data_cps = 0
        } else {
            cps =  (data_diff[i] / data_diff[clock10Hz]).toFixed(2) * 10;
            scrobj[i].cps = Math.round(cps*10)/10
            data_cps = Math.round(cps*10)/10
        }
    }
}

// ===================================================================

function make_table (objs) {
    clear_table();
    var table = document.getElementById('scaler-table');
    const tr = document.createElement("tr");
    table.appendChild(tr);
    const td1 = document.createElement("th");
    td1.textContent = "Name"
    tr.appendChild(td1)
    const td2 = document.createElement("th");
    td2.textContent = "Counts"
    tr.appendChild(td2)
    const td3 = document.createElement("th");
    td3.textContent = "Hz"
    tr.appendChild(td3)
    
    console.log(objs);
    objs.forEach( (obj)=> {
        const tr = document.createElement("tr");
        table.appendChild(tr);
        const objArray = Object.entries(obj);
        objArray.forEach( (arr) => {
            const td = document.createElement("td");
            td.textContent = arr[1];
            tr.appendChild(td);
        });
    });
}

function clear_table() {
    var table = document.getElementById("scaler-table");
    while (table.rows.length > 0) table.deleteRow(0);
}


// ===================================================================
