@Mic
wollte mich mal hier anhängen ;-),
wurde gerne den JSON vom Tankerkönig umwandeln 😉
Leider wird der DP HTML nicht gefüllt ;-(
/******************************************************************************************************
* JSON-Datenpunkt in HTML umwandeln
* --------------------------------------------------------------
* Zweck: Überwacht einen JSON-Datenpunkt und sobald geändert, wird JSON in HTML umgewandelt und
* in einem eigenen Datenpunkt ausgegeben
* Publiziert: https://forum.iobroker.net/topic/32540/json-zu-html-und-in-datei-schreiben-ablegen
* Autor: Mic-M (Github) | Mic (ioBroker)
* --------------------------------------------------------------------------------------
* Change Log:
* 0.0.1 Mic-M * Initial release
******************************************************************************************************/
/*********************************************************************************************
* Einstellungen
*********************************************************************************************/
// JSON-Datenpunkt
const g_jsonState = 'tankerkoenig.0.json';
// Neuer Datenpunkt für HTML-Ausgabe
const g_htmlState = 'javascript.0.html-tables.tankerkoenigHTML';
// Spalte entfernen (für Log Parser Adapter 'ts' nehmen, da dieser autmatisch den timestamp hinzufügt),
// sonst leer lassen
const g_removeColumn = 'ts';
/*********************************************************************************************
* Ab hier nichts mehr ändern
*********************************************************************************************/
main();
function main() {
// Create state for HTML, if not yet existing
createState(g_htmlState, {'name':'HTML Table', 'type':'string', 'read':true, 'write':false, 'role':'html', 'def':'' }, () => {
// State created, so let's subscribe to the given JSON state
on({id: g_jsonState, change:'ne'}, function(obj) {
// JSON state changed
if(obj.state.val && obj.state.val.length > 10) {
// state is not empty
const jsonObject = JSON.parse(obj.state.val);
if(g_removeColumn) {
for (let lpEntry of jsonObject) {
delete lpEntry[g_removeColumn];
}
}
setState(g_htmlState, json2table(jsonObject, ''));
}
});
});
}
/**
* Convert JSON to HTML table
* Source: https://travishorn.com/building-json2table-turn-json-into-an-html-table-a57cf642b84a
*
* @param {object} jsonObject The JSON as object.
* @param {string} [classes] Optional: You can apply one or multiple classes (space separated) to <table>.
* @return {string} The HTML result as string
*/
function json2table(jsonObject, classes = '') {
const cols = Object.keys(jsonObject[0]);
let headerRow = '';
let bodyRows = '';
classes = classes || '';
cols.map(function(col) {
headerRow += '<th>' + capitalizeFirstLetter(col) + '</th>';
});
jsonObject.map(function(row) {
bodyRows += '<tr>';
cols.map(function(colName) {
bodyRows += '<td>' + row[colName] + '</td>';
})
bodyRows += '</tr>';
});
const addClasses = (classes && classes.length > 1) ? ' class="' + classes + '"' : '';
return '<table' + addClasses + '><thead><tr>' +
headerRow +
'</tr></thead><tbody>' +
bodyRows +
'</tbody></table>';
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
}
jemand eine Idee?
javascript.0
2020-06-07 22:19:48.513
error
(30189) at process.topLevelDomainCallback (domain.js:137:15)
javascript.0
2020-06-07 22:19:48.512
error
(30189) at processImmediate (internal/timers.js:456:21)
javascript.0
2020-06-07 22:19:48.512
error
(30189) at Immediate._onImmediate (/opt/iobroker/node_modules/iobroker.js-controller/lib/adapter.js:5384:37)
javascript.0
2020-06-07 22:19:48.511
error
(30189) at Object.stateChange (/opt/iobroker/node_modules/iobroker.javascript/main.js:451:25)
javascript.0
2020-06-07 22:19:48.510
error
(30189) at Object.callback (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1052:38)
javascript.0
2020-06-07 22:19:48.509
error
(30189) at Object.<anonymous> (script.js.Vis-Scripte.HTML_JSON_Tankerkoenig:42:41)
javascript.0
2020-06-07 22:19:48.506
error
(30189) Error in callback: TypeError: jsonObject is not iterable