// get all slaves recently connected to master // ask master for slaves setTimeout(function() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { var slaveData = JSON.parse(xmlhttp.responseText); var HTML = "

Current connections:

" for(i=0;i'; HTML += "

ID: " + slaveData[key].unique + "

Last seen: "+seenDate+"

Port: "+slaveData[key].serverPort+"

Host: "+slaveData[key].mac+"

"; HTML += ""; } } document.querySelector("#slaves > #display").innerHTML = HTML let graphs = document.querySelectorAll(".productionGraph"); for(let i = 0; i < graphs.length; i++) { // execute functions to make graphs // ID of slave, ID of canvasjs div without # makeGraph(graphs[i].id, graphs[i].id) } } } xmlhttp.open("GET", "slaves", true); xmlhttp.send(); }, 0) // ID of slave, ID of canvasjs div without # function makeGraph(slaveID, selector) { post("getStats", {slaveID: slaveID}, function(data){ console.log("Building chart " + slaveID + " with this data:") console.log(data) if(data.length > 0) { // find keys let itemNames = []; for(let key in data[data.length - 1].data) { itemNames[itemNames.length] = key } let chartData = []; for(let o = 0; o < itemNames.length; o++) { if(itemNames[o] != "water"){ chartData[chartData.length] = generateLineChartArray(data, itemNames[o]); } } console.log(chartData) drawChart(selector, chartData) } // callback(data); }) } // production info // string, object, function(object) function post(url, data, callback) { console.log("POST " + url + JSON.stringify(data)) var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); xhr.setRequestHeader("Content-type", "application/json"); xhr.onreadystatechange = function () { console.log(xhr.readyState + "WOW") if (xhr.readyState == 4 && xhr.status == 200) { var json = JSON.parse(xhr.responseText); callback(json); } } xhr.send(JSON.stringify(data)); } function generateLineChartArray(data, nameKey) { let chartData = []; for(let i = 0; i < data.length; i++) { // only show recent data if(data[i].timestamp > Date.now() - (24*60*60*1000)){ let y = data[i].data[nameKey] if(!data[i].data[nameKey]) { y = 0 } chartData[chartData.length] = { x: new Date(data[i].timestamp), y: Number(y) } } } let xyz = {}; xyz.name = nameKey; xyz.type = "spline"; xyz.dataPoints = chartData; return xyz; } function drawChart(selector, chartData) { // selector is ID of element, ex "chartContainer" var chart = new CanvasJS.Chart(selector, { title:{ text: "Production graph" }, toolTip:{ content: "{name}: {y}" }, zoomEnabled: true, axisY:{ includeZero: true }, data: chartData }); // console.log(chart) chart.render(); }