1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
// convert epochtime to JavaScripte Date object
function epochToJsDate(epochTime){
return new Date(epochTime*1000);
}
// convert time to human-readable format YYYY/MM/DD HH:MM:SS
function epochToDateTime(epochTime){
var epochDate = new Date(epochToJsDate(epochTime));
var dateTime = epochDate.getFullYear() + "/" +
("00" + (epochDate.getMonth() + 1)).slice(-2) + "/" +
("00" + epochDate.getDate()).slice(-2) + " " +
("00" + epochDate.getHours()).slice(-2) + ":" +
("00" + epochDate.getMinutes()).slice(-2) + ":" +
("00" + epochDate.getSeconds()).slice(-2);
return dateTime;
}
// function to plot values on charts
function plotValues(chart, timestamp, value){
var x = epochToJsDate(timestamp).getTime();
var y = Number (value);
if(chart.series[0].data.length > 40) {
chart.series[0].addPoint([x, y], true, true, true);
} else {
chart.series[0].addPoint([x, y], true, false, true);
}
}
// DOM elements
const loginElement = document.querySelector('#login-form');
const contentElement = document.querySelector("#content-sign-in");
const userDetailsElement = document.querySelector('#user-details');
const authBarElement = document.querySelector('#authentication-bar');
const deleteButtonElement = document.getElementById('delete-button');
const deleteModalElement = document.getElementById('delete-modal');
const deleteDataFormElement = document.querySelector('#delete-data-form');
const viewDataButtonElement = document.getElementById('view-data-button');
const hideDataButtonElement = document.getElementById('hide-data-button');
const tableContainerElement = document.querySelector('#table-container');
const chartsRangeInputElement = document.getElementById('charts-range');
const loadDataButtonElement = document.getElementById('load-data');
const cardsCheckboxElement = document.querySelector('input[name=cards-checkbox]');
const gaugesCheckboxElement = document.querySelector('input[name=gauges-checkbox]');
const chartsCheckboxElement = document.querySelector('input[name=charts-checkbox]');
// DOM elements for sensor readings
const cardsReadingsElement = document.querySelector("#cards-div");
const gaugesReadingsElement = document.querySelector("#gauges-div");
const chartsDivElement = document.querySelector('#charts-div');
const tempElement = document.getElementById("temp");
const humElement = document.getElementById("hum");
const updateElement = document.getElementById("lastUpdate")
// MANAGE LOGIN/LOGOUT UI
const setupUI = (user) => {
if (user) {
//toggle UI elements
loginElement.style.display = 'none';
contentElement.style.display = 'block';
authBarElement.style.display ='block';
userDetailsElement.style.display ='block';
userDetailsElement.innerHTML = user.email;
// get user UID to get data from database
var uid = user.uid;
console.log(uid);
// Database paths (with user UID)
var dbPath = 'UsersData/' + uid.toString() + '/readings';
var chartPath = 'UsersData/' + uid.toString() + '/charts/range';
// Database references
var dbRef = firebase.database().ref(dbPath);
var chartRef = firebase.database().ref(chartPath);
// CHARTS
// Number of readings to plot on charts
var chartRange = 0;
// Get number of readings to plot saved on database (runs when the page first loads and whenever there's a change in the database)
chartRef.on('value', snapshot =>{
chartRange = Number(snapshot.val());
console.log(chartRange);
// Delete all data from charts to update with new values when a new range is selected
chartT.destroy();
chartH.destroy();
// Render new charts to display new range of data
chartT = createTemperatureChart();
chartH = createHumidityChart();
// Update the charts with the new range
// Get the latest readings and plot them on charts (the number of plotted readings corresponds to the chartRange value)
dbRef.orderByKey().limitToLast(chartRange).on('child_added', snapshot =>{
var jsonData = snapshot.toJSON(); // example: {temperature: 25.02, humidity: 50.20, pressure: 1008.48, timestamp:1641317355}
// Save values on variables
var temperature = jsonData.temperature;
var humidity = jsonData.humidity;
var timestamp = jsonData.timestamp;
// Plot the values on the charts
plotValues(chartT, timestamp, temperature);
plotValues(chartH, timestamp, humidity);
});
});
// Update database with new range (input field)
chartsRangeInputElement.onchange = () =>{
chartRef.set(chartsRangeInputElement.value);
};
//CHECKBOXES
// Checbox (cards for sensor readings)
cardsCheckboxElement.addEventListener('change', (e) =>{
if (cardsCheckboxElement.checked) {
cardsReadingsElement.style.display = 'block';
}
else{
cardsReadingsElement.style.display = 'none';
}
});
// Checbox (gauges for sensor readings)
gaugesCheckboxElement.addEventListener('change', (e) =>{
if (gaugesCheckboxElement.checked) {
gaugesReadingsElement.style.display = 'block';
}
else{
gaugesReadingsElement.style.display = 'none';
}
});
// Checbox (charta for sensor readings)
chartsCheckboxElement.addEventListener('change', (e) =>{
if (chartsCheckboxElement.checked) {
chartsDivElement.style.display = 'block';
}
else{
chartsDivElement.style.display = 'none';
}
});
// CARDS
// Get the latest readings and display on cards
dbRef.orderByKey().limitToLast(1).on('child_added', snapshot =>{
var jsonData = snapshot.toJSON(); // example: {temperature: 25.02, humidity: 50.20, pressure: 1008.48, timestamp:1641317355}
var temperature = jsonData.temperature;
var humidity = jsonData.humidity;
var timestamp = jsonData.timestamp;
// Update DOM elements
tempElement.innerHTML = temperature;
humElement.innerHTML = humidity;
updateElement.innerHTML = epochToDateTime(timestamp);
});
// GAUGES
// Get the latest readings and display on gauges
dbRef.orderByKey().limitToLast(1).on('child_added', snapshot =>{
var jsonData = snapshot.toJSON(); // example: {temperature: 25.02, humidity: 50.20, pressure: 1008.48, timestamp:1641317355}
var temperature = jsonData.temperature;
var humidity = jsonData.humidity;
var timestamp = jsonData.timestamp;
// Update DOM elements
var gaugeT = createTemperatureGauge();
var gaugeH = createHumidityGauge();
gaugeT.draw();
gaugeH.draw();
gaugeT.value = temperature;
gaugeH.value = humidity;
updateElement.innerHTML = epochToDateTime(timestamp);
});
// DELETE DATA
// Add event listener to open modal when click on "Delete Data" button
deleteButtonElement.addEventListener('click', e =>{
console.log("Remove data");
e.preventDefault;
deleteModalElement.style.display="block";
});
// Add event listener when delete form is submited
deleteDataFormElement.addEventListener('submit', (e) => {
// delete data (readings)
dbRef.remove();
});
// TABLE
var lastReadingTimestamp; //saves last timestamp displayed on the table
// Function that creates the table with the first 100 readings
function createTable(){
// append all data to the table
var firstRun = true;
dbRef.orderByKey().limitToLast(100).on('child_added', function(snapshot) {
if (snapshot.exists()) {
var jsonData = snapshot.toJSON();
console.log(jsonData);
var temperature = jsonData.temperature;
var humidity = jsonData.humidity;
var timestamp = jsonData.timestamp;
var content = '';
content += '<tr>';
content += '<td>' + epochToDateTime(timestamp) + '</td>';
content += '<td>' + temperature + '</td>';
content += '<td>' + humidity + '</td>';
content += '</tr>';
$('#tbody').prepend(content);
// Save lastReadingTimestamp --> corresponds to the first timestamp on the returned snapshot data
if (firstRun){
lastReadingTimestamp = timestamp;
firstRun=false;
console.log(lastReadingTimestamp);
}
}
});
};
// append readings to table (after pressing More results... button)
function appendToTable(){
var dataList = []; // saves list of readings returned by the snapshot (oldest-->newest)
var reversedList = []; // the same as previous, but reversed (newest--> oldest)
console.log("APEND");
dbRef.orderByKey().limitToLast(100).endAt(lastReadingTimestamp).once('value', function(snapshot) {
// convert the snapshot to JSON
if (snapshot.exists()) {
snapshot.forEach(element => {
var jsonData = element.toJSON();
dataList.push(jsonData); // create a list with all data
});
lastReadingTimestamp = dataList[0].timestamp; //oldest timestamp corresponds to the first on the list (oldest --> newest)
reversedList = dataList.reverse(); // reverse the order of the list (newest data --> oldest data)
var firstTime = true;
// loop through all elements of the list and append to table (newest elements first)
reversedList.forEach(element =>{
if (firstTime){ // ignore first reading (it's already on the table from the previous query)
firstTime = false;
}
else{
var temperature = element.temperature;
var humidity = element.humidity;
var timestamp = element.timestamp;
var content = '';
content += '<tr>';
content += '<td>' + epochToDateTime(timestamp) + '</td>';
content += '<td>' + temperature + '</td>';
content += '<td>' + humidity + '</td>';
content += '</tr>';
$('#tbody').append(content);
}
});
}
});
}
viewDataButtonElement.addEventListener('click', (e) =>{
// Toggle DOM elements
tableContainerElement.style.display = 'block';
viewDataButtonElement.style.display ='none';
hideDataButtonElement.style.display ='inline-block';
loadDataButtonElement.style.display = 'inline-block'
createTable();
});
loadDataButtonElement.addEventListener('click', (e) => {
appendToTable();
});
hideDataButtonElement.addEventListener('click', (e) => {
tableContainerElement.style.display = 'none';
viewDataButtonElement.style.display = 'inline-block';
hideDataButtonElement.style.display = 'none';
});
// IF USER IS LOGGED OUT
} else{
// toggle UI elements
loginElement.style.display = 'block';
authBarElement.style.display ='none';
userDetailsElement.style.display ='none';
contentElement.style.display = 'none';
}
} |
Partager