@Daniele Your code looks super cool and awesome. I just added few HTML template style (you may not like 😉 ) and minor tune. Thanks for the awesome code!!
var myImports = new JavaImporter(java.io, java.lang, java.util,java.lang.Object, org.mycontroller.standalone.utils.McUtils);
with(myImports) {
var options = new HashMap();
options.put("orderBy", "Eui");
options.put("order", "asc");
options.put("pageLimit", new Long(-1)); // -1 to list all the nodes
var nodes = mcApi.node().getAll(options);
var nodesVar = mcApi.variable().get("nodes_state"); // It is good to have key in lowercase and no space
var nodesOU = JSON.parse(nodesVar.value);
var nodesOD = JSON.parse(nodesVar.value2);
var nodesU = [];
var nodesD = [];
var subject = "[MyController] Nodes status";
var tableBody = "";
for (i = 0; i < nodes.data.length; i++) {
var nodeStatus = { };
var _nodeId = nodes.data[i].getId(); // nodeId is unique and internal
// but still if you want to refer node EUI and gateway id, use the following line and comment above line.
//var _nodeId = nodes.data[i].getGatewayTable().getId() + '-' + nodes.data[i].getEui();
var _nodeState = nodes.data[i].getState();
nodeStatus[_nodeId] = _nodeState; // update in to our common map
if (_nodeState == "UP") {
nodesU.push(_nodeId);
var prevState = "";
for (j = 0; j < nodesOU.length; j++){
if (nodesOU[j] == _nodeId) prevState = "UP";
}
if (prevState != "UP")
tableBody += "<tr style='color: green;'><td>" + _nodeId + "</td><td>" + nodes.data[i].getName() + "</td><td>" + nodes.data[i].getEui() + "</td><td>" + _nodeState + "</td><td>" + mcApi.utils().friendlyTime(nodes.data[i].getLastSeen()) + "</td></tr>";
}
if (_nodeState == "DOWN") {
nodesD.push(_nodeId);
var prevState = "";
for (j = 0; j < nodesOD.length; j++){
if (nodesOD[j] == nodeId) prevState = "DOWN";
}
if (prevState != "DOWN")
tableBody += "<tr style='color: red;'><td>" + _nodeId + "</td><td>" + nodes.data[i].getName() + "</td><td>" + nodes.data[i].getEui() + "</td><td>" + _nodeState + "</td><td>" + mcApi.utils().friendlyTime(nodes.data[i].getLastSeen()) + "</td></tr>";
}
}
if (tableBody.length > 0){
var pageStart = "Hello,<BR><BR>";
var tableStyle = '<table border = "1" cellpadding = "5" style="border-collapse: collapse;border: none;"><tbody><tr><th align="left">Id</th><th align="left">Name</th><th align="left">EUI</th><th align="left">State</th><th align="left">Since</th></tr>';
var tableEnd = '</tbody></table>';
var pageEnd = "<BR><BR>--MyController.org<BR>";
// mcApi.operation().sendPushbulletNote(null, "abc@xyz.com", null, subject, message);
mcApi.operation().sendEmail("abc@xyz.com", subject, pageStart + tableStyle + tableBody + tableEnd + pageEnd);
}
nodesVar.value = JSON.stringify(nodesU);
nodesVar.value2 = JSON.stringify(nodesD);
nodesVar.save();
}
Email will be looking like this,
310032a4-3e1b-4191-9c0e-c42d7a1ff5f0-image.png