Skip to content

Commit

Permalink
Change HTML of sensor viewer to Table
Browse files Browse the repository at this point in the history
  • Loading branch information
Sitinut Waisara committed Mar 26, 2020
1 parent 9632105 commit c64119a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 85 deletions.
48 changes: 37 additions & 11 deletions Node main module/src/customPages.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,20 @@ const static char customPageJSON[] PROGMEM = R"raw(

// Live sensor viewing HTML and JS
const char sensorViewerHTML[] PROGMEM = R"rawliteral(
<div id=sensorElement>
<div id=sensorStatus>
<p>Getting sensor information...</p>
</div>
<div class="container">
<table id="sensorDataTable" class="info" style="border:none; font-size: 20px;" >
<tbody>
<tr>

</tr>
</tbody>
</table>
</div>


<script>
loadData(); //Load the data for first time
setInterval(loadData, 1000); //Reload the data every X milliseconds.
Expand All @@ -192,18 +203,33 @@ const char sensorViewerHTML[] PROGMEM = R"rawliteral(
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var sensorElement = document.getElementById("sensorElement");
sensorElement.textContent = ""; //Clear all elements in sensorElement div
var sensorsData = JSON.parse(this.response).data; //Parse the response to JSON object
if (Object.keys(sensorsData).length == 0 ) { //object is empty
sensorElement.innerHTML = "<p> No sensors connected. </p>"
var sensorStatus = document.getElementById("sensorStatus");
var sensorTable = document.getElementById("sensorDataTable");

var sensorsData = JSON.parse(this.response).data; //Parse the response to JSON object
if (Object.keys(sensorsData).length == 0 ) { //object is empty
sensorStatus.style.display = "block"; //Display the status text div
sensorTable.style.display = "none" //Hide the table
sensorStatus.textContent = ""; //Clear all elements in sensorElement div
sensorStatus.innerHTML = "<p> No sensors connected. </p>"
return;
}else{
sensorStatus.style.display = "none"; //Hide the status text div
sensorTable.style.display = "block" //Show the table
sensorTable.innerHTML = "" //Clear the table

for (sensorName of Object.keys(sensorsData)) { //iterate through each element and create table row for each sensor
var row = sensorTable.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.style.fontWeight = "bold"
cell2.style.paddingLeft = "40px";
cell1.innerHTML = sensorName;
cell2.innerHTML = sensorsData[sensorName];
}

}
for (sensorName of Object.keys(sensorsData)) { //iterate through each element and create paragraph for each sensor
sensorElement.innerHTML += "<p><span class=\"sensor-label\">" +
sensorName + "</span> <span id=\"sensor-value\">" +
sensorsData[sensorName] + " </span> <sup class=\"units\">" + ' ' + " </sup> </p>";
}

}
};
xhttp.open("GET", "/getJSON", true); //request JSON data from URI/getJSON
Expand Down
110 changes: 36 additions & 74 deletions Node main module/src/html/sensorviewer.html
Original file line number Diff line number Diff line change
@@ -1,93 +1,55 @@
<!DOCTYPE HTML>
<html>
<div id=sensorStatus>
<p>Getting sensor information...</p>
</div>
<div class="container">
<table id="sensorDataTable" class="info" style="border:none; font-size: 20px;" >
<tbody>
<tr>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css"
integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}

h2 {
font-size: 3.0rem;
}

p {
font-size: 3.0rem;
}

.units {
font-size: 1.2rem;
}

.sensor-label {
font-size: 1.5rem;
vertical-align: middle;
padding-bottom: 15px;
}
</style>
</head>

<body>
<h2>Live connected sensor viewer</h2>
<div id=sensorElement>
<p>Getting sensor information...</p>
</div>
</body>

<footer>
<p style="padding-top:15px;text-align:center">
<a href="/_ac"><img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAApklEQVRYR2NkGGDAOMD2M4w6YDQEkEMgEJggZwCxGI0T5mug+alAvBFkD7IDXtLBcpjfXgEZ4ugOeAETpHEIgIwHeVYC3QH+0CgAS9AQgCwHRcFmdAfQ0E7cRo9mw0EVAqPlAKhwEKVTVsBZDsyiQ2k4Wg6gxPKgyoZ0Sn+o1iCHQBBQaiYQi9DYJTjbAyAJWluOtz0wWg7QOOqxGz+aDUdDYMBDAACA0x4hs/MPrwAAAABJRU5ErkJggg==" border="0" title="AutoConnect menu" alt="AutoConnect menu" />
</a>
</p>

</footer>
</tr>
</tbody>
</table>
</div>


<script>

loadData(); //Load the data for first time
setInterval(loadData, 1000); //Reload the data every X milliseconds.

//Load the JSON data from API, and then replace the HTML element
function loadData(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {

var sensorElement = document.getElementById("sensorElement");

sensorElement.textContent = ""; //Clear all elements in sensorElement div


var sensorsData = JSON.parse(this.response).data; //Parse the response to JSON object


if (Object.keys(sensorsData).length == 0 ) { //object is empty
sensorElement.innerHTML = "<p> No sensors connected. </p>"
var sensorStatus = document.getElementById("sensorStatus");
var sensorTable = document.getElementById("sensorDataTable");

var sensorsData = JSON.parse(this.response).data; //Parse the response to JSON object
if (Object.keys(sensorsData).length == 0 ) { //object is empty
sensorStatus.style.display = "block"; //Display the status text div
sensorTable.style.display = "none" //Hide the table
sensorStatus.textContent = ""; //Clear all elements in sensorElement div
sensorStatus.innerHTML = "<p> No sensors connected. </p>"
return;
}

for (sensorName of Object.keys(sensorsData)) { //iterate through each element and create paragraph for each sensor
sensorElement.innerHTML += "<p><span class=\"sensor-label\">" +
sensorName + "</span> <span id=\"sensor-value\">" +
sensorsData[sensorName] + " </span> <sup class=\"units\">" + ' ' + " </sup> </p>";
}else{
sensorStatus.style.display = "none"; //Hide the status text div
sensorTable.style.display = "block" //Show the table
sensorTable.innerHTML = "" //Clear the table

for (sensorName of Object.keys(sensorsData)) { //iterate through each element and create table row for each sensor
var row = sensorTable.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.style.fontWeight = "bold"
cell2.style.paddingLeft = "40px";
cell1.innerHTML = sensorName;
cell2.innerHTML = sensorsData[sensorName];
}

}

}
};


xhttp.open("GET", "/getJSON", true); //request JSON data from URI/getJSON
xhttp.send();
}

</script>
</html>
</script>

0 comments on commit c64119a

Please sign in to comment.