Brultech Dashbox - List of active circuits - (json to html table)

Here is a plugin that pulls the list of circuits from a Dashbox, removes the ones that haven’t been used today, and lists the active ones sorted by current consumption, descendant.

It includes a simple function to generate an html table from json, and send that html to a bound webbrowser.
With this function you can show on screen any json data of unknown/variable number of rows.

function onPoll() {
    console.log("polling");
    var mainDevice = plugin.Devices["gemlist"];
   
    var http = new HTTPClient();
    var response = http.get("http://xxx.xxx.xxx.xxx/index.php/pages/search/getLive/" );
    var json = response.data.chanGraph; 

    Object.keys(json).forEach(function(key) {
  	//console.log('Key : ' + key + ', Value : ' + json[key].value)
  	delete json[key].ctype;
  	delete json[key].colour;
  	if (((json[key].value == 0) && (json[key].kwh == 0)) || (json[key].ch_name.indexOf('Voltage') > 0))
      		delete json[key];
	})

    mainDevice.out = json2table(json);  

   
   function json2table(json) {
    var cols = Object.keys(json[0]);
    var BkgCol = '#F2F2F2';
    var BrdCol = 'C0C0C0';
    var CelCol = 'FFFFFF';
    var BrdWid = '4';
    var headerRow = '';
    var bodyRows = '';
    function capitalizeFirstLetter(string) {     return string.charAt(0).toUpperCase() + string.slice(1);   }
    cols.map(function(col) {     headerRow += '<th style="border: '+ BrdWid+'px solid #'+BrdCol+';">' + capitalizeFirstLetter(col) + '</th>';   });
    json.map(function(row) {
         bodyRows += '<tr>';
         cols.map(function(colName) {   bodyRows += '<td style="border: '+ BrdWid+'px solid #'+BrdCol+';">' + row[colName] + '</td>';      })
         bodyRows += '</tr>';
  });
  return '<body style="background-color:'+BkgCol+'"><table style="background-color: #'+CelCol+';border: '+ BrdWid+'px solid #'+BrdCol+';border-collapse: collapse"><thead><tr>' +     headerRow +
         '</tr></thead><tbody>' +    bodyRows +
         '</tbody></table>';
} // end json2table function
  
} //end onPoll
2 Likes