2019-01-19 16:08:47 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// import CreateElement from 'dom-create-element-query-selector';
|
|
|
|
// We're using the git repo for now until an update is released, and rollup doesn't like that apparently
|
|
|
|
import CreateElement from '../../node_modules/dom-create-element-query-selector/src/index.js';
|
|
|
|
|
2019-01-20 21:11:45 +00:00
|
|
|
import moment from 'moment';
|
2019-01-20 00:17:54 +00:00
|
|
|
// import Chart from 'chart.js';
|
2019-01-20 15:18:29 +00:00
|
|
|
// Chart.js pollutes the global scope, but the main entry point is going to change soon in v2.8 - which should fix our issue here
|
2019-01-20 21:11:45 +00:00
|
|
|
import Chart from '../../node_modules/chart.js/dist/Chart.bundle.min.js';
|
2019-01-19 16:08:47 +00:00
|
|
|
|
|
|
|
import GetFromUrl from './Helpers/GetFromUrl.mjs';
|
2019-02-10 14:45:15 +00:00
|
|
|
import GetContainingElement from './Helpers/GetContainingElement.mjs';
|
2019-01-19 22:04:51 +00:00
|
|
|
import Postify from './Helpers/Postify.mjs';
|
2019-01-19 16:08:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceReadingDisplay {
|
2019-01-20 20:09:20 +00:00
|
|
|
constructor(in_config, in_device_id) {
|
2019-01-19 16:08:47 +00:00
|
|
|
this.config = in_config;
|
2019-01-20 20:09:20 +00:00
|
|
|
/** The ID of the device to display a graph for. @type {int} */
|
2019-01-19 22:04:51 +00:00
|
|
|
this.device_id = in_device_id;
|
2019-01-20 20:09:20 +00:00
|
|
|
/** The current reading type to display a graph for. @type {Object} */
|
|
|
|
this.reading_type = null;
|
2019-01-20 21:31:16 +00:00
|
|
|
|
2019-02-10 14:29:16 +00:00
|
|
|
// The number of points to display at a time.
|
|
|
|
this.points_resolution = 50;
|
|
|
|
|
|
|
|
this.start_time = moment().subtract(1, "days");
|
|
|
|
this.end_time = moment();
|
|
|
|
|
|
|
|
|
2019-01-20 21:54:40 +00:00
|
|
|
this.default_colours = {
|
|
|
|
borderColor: "hsla(0, 0%, 50%, 1)",
|
|
|
|
backgroundColor: "hsla(0, 0%, 65%, 0.5)"
|
|
|
|
};
|
2019-01-20 22:32:34 +00:00
|
|
|
this.reading_type_defs = {
|
2019-01-20 21:31:16 +00:00
|
|
|
"PM10": {
|
2019-01-20 22:32:34 +00:00
|
|
|
suggestedMin: 0,
|
|
|
|
suggestedMax: 110,
|
2019-01-20 21:31:16 +00:00
|
|
|
borderColor: "hsla(0, 82%, 56%, 1)",
|
|
|
|
backgroundColor: "hsla(14, 94%, 71%, 0.57)"
|
|
|
|
},
|
|
|
|
"PM25": {
|
2019-01-20 22:32:34 +00:00
|
|
|
suggestedMin: 0,
|
|
|
|
suggestedMax: 75,
|
2019-01-20 21:31:16 +00:00
|
|
|
borderColor: "hsla(33, 70%, 51%, 1)",
|
|
|
|
backgroundColor: "hsla(28, 77%, 58%, 0.63)"
|
|
|
|
},
|
|
|
|
"temperature": {
|
2019-01-21 00:03:29 +00:00
|
|
|
suggestedMin: 0,
|
|
|
|
suggestedMax: 20,
|
2019-01-20 21:31:16 +00:00
|
|
|
borderColor: "hsla(0, 77%, 45%, 1)",
|
|
|
|
backgroundColor: "hsla(0, 61%, 58%, 0.59)"
|
|
|
|
},
|
|
|
|
"humidity": {
|
2019-01-20 22:32:34 +00:00
|
|
|
suggestedMin: 0,
|
|
|
|
suggestedMax: 100,
|
2019-01-20 21:31:16 +00:00
|
|
|
borderColor: "hsla(184, 69%, 40%, 1)",
|
|
|
|
backgroundColor: "hsla(188, 53%, 46%, 0.58)"
|
|
|
|
},
|
|
|
|
"pressure": {
|
|
|
|
borderColor: "hsla(258, 67%, 40%, 1)",
|
|
|
|
backgroundColor: "hsla(249, 56%, 40%, 0.66)"
|
|
|
|
}
|
2019-02-10 14:29:16 +00:00
|
|
|
};
|
2019-01-19 16:08:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 20:09:20 +00:00
|
|
|
async setup(default_reading_type) {
|
|
|
|
// Create the display element first, as we need it to be immediately available for inclusion in the popup window
|
|
|
|
|
2019-01-19 22:04:51 +00:00
|
|
|
/** @type {HTMLElement} */
|
2019-02-10 14:45:15 +00:00
|
|
|
this.display = CreateElement("div.chart-device-data.working",
|
2019-01-19 16:08:47 +00:00
|
|
|
CreateElement("canvas.canvas-chart"),
|
2019-02-10 14:29:16 +00:00
|
|
|
CreateElement("ul.reading-types.button-array"),
|
|
|
|
CreateElement("ul.quick-time-selector.button-array",
|
|
|
|
CreateElement("li", CreateElement("button[data-timelength=1h]", "1 hour")),
|
|
|
|
CreateElement("li", CreateElement("button[data-timelength=6h]", "6 hours")),
|
|
|
|
CreateElement("li", CreateElement("button[data-timelength=1d].selected", "1 day")),
|
|
|
|
CreateElement("li", CreateElement("button[data-timelength=1w]", "1 week")),
|
|
|
|
CreateElement("li", CreateElement("button[data-timelength=1M]", "1 month")),
|
|
|
|
CreateElement("li", CreateElement("button[data-timelength=3M]", "3 months")),
|
|
|
|
CreateElement("li", CreateElement("button[data-timelength=1y]", "1 year"))
|
|
|
|
)
|
2019-01-19 16:08:47 +00:00
|
|
|
);
|
2019-01-19 22:04:51 +00:00
|
|
|
|
2019-02-10 14:29:16 +00:00
|
|
|
|
2019-01-20 20:09:20 +00:00
|
|
|
await this.fetch_reading_types();
|
|
|
|
this.reading_type = this.reading_types.find((type) => type.id == default_reading_type);
|
2019-02-10 13:40:23 +00:00
|
|
|
// Default to the 1st reading type if we can't find the default
|
|
|
|
if(typeof this.reading_type == "undefined")
|
|
|
|
this.reading_type = this.reading_types[0].id;
|
2019-01-20 20:09:20 +00:00
|
|
|
|
2019-01-20 22:32:34 +00:00
|
|
|
// Create the reading type buttons
|
2019-01-20 20:09:20 +00:00
|
|
|
let reading_type_list = this.display.querySelector(".reading-types");
|
|
|
|
for(let reading_type of this.reading_types) {
|
2019-01-20 20:53:12 +00:00
|
|
|
let new_element = CreateElement("li",
|
|
|
|
CreateElement("button", reading_type.friendly_text)
|
|
|
|
);
|
2019-01-20 22:32:34 +00:00
|
|
|
let button = new_element.querySelector("button");
|
2019-01-20 20:53:12 +00:00
|
|
|
if(reading_type.id == this.reading_type.id)
|
2019-01-20 22:32:34 +00:00
|
|
|
button.classList.add("selected");
|
|
|
|
button.addEventListener("click", this.switch_graph_type_handler.bind(this));
|
2019-01-20 20:53:12 +00:00
|
|
|
|
2019-01-20 20:09:20 +00:00
|
|
|
new_element.dataset.id = reading_type.id;
|
|
|
|
reading_type_list.appendChild(new_element);
|
|
|
|
}
|
|
|
|
|
2019-02-10 14:29:16 +00:00
|
|
|
this.display.querySelector(".quick-time-selector")
|
|
|
|
.addEventListener("click", (this.timelength_button_click_handler).bind(this));
|
|
|
|
|
|
|
|
|
2019-01-20 20:09:20 +00:00
|
|
|
// ----------------------------------------------------
|
|
|
|
|
2019-01-20 22:32:34 +00:00
|
|
|
// Setup the chart itself
|
2019-02-10 14:45:15 +00:00
|
|
|
await this.setup_chart();
|
|
|
|
|
|
|
|
this.display.classList.remove("working");
|
2019-01-20 20:09:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 14:29:16 +00:00
|
|
|
async timelength_button_click_handler(event) {
|
|
|
|
let timelength = event.target.dataset.timelength;
|
|
|
|
if(typeof timelength == "undefined")
|
|
|
|
return;
|
|
|
|
|
|
|
|
let time_unit = timelength.replace(/[0-9]+/g, "");
|
|
|
|
let time_length = timelength.replace(/[^0-9]+/g, "");
|
|
|
|
|
|
|
|
this.start_time = moment().subtract(time_length, time_unit);
|
|
|
|
this.end_time = moment();
|
|
|
|
|
2019-02-10 14:45:15 +00:00
|
|
|
let popup_container = GetContainingElement(event.target, "div");
|
2019-02-10 14:29:16 +00:00
|
|
|
|
2019-02-10 14:45:15 +00:00
|
|
|
popup_container.classList.add("working");
|
2019-02-10 14:29:16 +00:00
|
|
|
|
|
|
|
await this.update_chart();
|
|
|
|
|
|
|
|
// Show the new button to be selected
|
|
|
|
this.display.querySelectorAll(".quick-time-selector button").forEach((button) => button.classList.remove("selected"));
|
|
|
|
event.target.classList.add("selected");
|
2019-02-10 14:45:15 +00:00
|
|
|
|
|
|
|
popup_container.classList.remove("working");
|
2019-02-10 14:29:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 14:45:15 +00:00
|
|
|
async setup_chart() {
|
2019-01-20 21:54:40 +00:00
|
|
|
this.chart = new Chart(this.display.querySelector("canvas").getContext("2d"), {
|
|
|
|
type: "line",
|
|
|
|
data: {
|
|
|
|
// We need to define an initial dataset here because otherwise
|
|
|
|
// Chart.js gets confused 'cause it has nothing to animate from
|
|
|
|
labels: [],
|
|
|
|
datasets: []
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
scales: {
|
|
|
|
xAxes: [{
|
|
|
|
type: "time",
|
|
|
|
time: {
|
|
|
|
format: "YYYY-MM-DD HH:mm",
|
|
|
|
tooltipFormat: 'll HH:mm'
|
|
|
|
},
|
|
|
|
scaleLabel: {
|
|
|
|
display: true,
|
|
|
|
labelString: "Time"
|
|
|
|
}
|
|
|
|
}],
|
|
|
|
yAxes: [{
|
2019-01-20 22:32:34 +00:00
|
|
|
ticks: { },
|
2019-01-20 21:54:40 +00:00
|
|
|
scaleLabel: {
|
|
|
|
display: true,
|
|
|
|
labelString: "Value"
|
|
|
|
}
|
2019-01-19 22:04:51 +00:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
2019-01-20 21:54:40 +00:00
|
|
|
});
|
|
|
|
|
2019-02-10 14:45:15 +00:00
|
|
|
await this.update_chart();
|
2019-01-19 16:08:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:04:51 +00:00
|
|
|
async get_data() {
|
2019-01-20 21:54:40 +00:00
|
|
|
let new_data = null;
|
2019-02-10 14:29:16 +00:00
|
|
|
// Dividing by 1000: ms -> s
|
|
|
|
let average_seconds = (this.end_time.diff(this.start_time) / 1000) / this.points_resolution;
|
|
|
|
console.info("Requesting data with start", this.start_time.toString(), ", end", this.end_time.toString(), ", average-seconds", average_seconds);
|
2019-01-20 21:54:40 +00:00
|
|
|
try {
|
|
|
|
new_data = JSON.parse(await GetFromUrl(`${this.config.api_root}?` + Postify({
|
|
|
|
action: "device-data",
|
|
|
|
"device-id": this.device_id,
|
|
|
|
"reading-type": this.reading_type.id,
|
2019-02-10 14:29:16 +00:00
|
|
|
start: this.start_time.toISOString(),
|
|
|
|
end: this.end_time.toISOString(),
|
|
|
|
"average-seconds": average_seconds
|
2019-01-20 21:54:40 +00:00
|
|
|
})));
|
|
|
|
} catch(error) {
|
|
|
|
// TODO: Display a nice error message here instead of an alert()
|
|
|
|
alert(error);
|
|
|
|
console.error(error);
|
2019-01-20 22:47:05 +00:00
|
|
|
return null;
|
2019-01-20 21:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log("[marker/popup/device-graph] Fetched data:", new_data);
|
2019-02-10 14:29:16 +00:00
|
|
|
console.log("[marker/popup/device-graph] Point count:", new_data.length);
|
2019-01-19 22:04:51 +00:00
|
|
|
|
|
|
|
return new_data.map((data_point) => { return {
|
2019-01-20 21:11:45 +00:00
|
|
|
t: moment(data_point.datetime),
|
2019-01-20 21:23:44 +00:00
|
|
|
y: Math.round(data_point.value*10000)/10000
|
2019-01-19 22:04:51 +00:00
|
|
|
}});
|
2019-01-19 16:08:47 +00:00
|
|
|
}
|
2019-01-20 20:09:20 +00:00
|
|
|
|
|
|
|
async fetch_reading_types() {
|
2019-02-07 19:27:21 +00:00
|
|
|
this.reading_types = JSON.parse(await GetFromUrl(`${this.config.api_root}?action=list-reading-types&device-id=${this.device_id}`));
|
2019-01-20 20:09:20 +00:00
|
|
|
}
|
2019-01-20 21:54:40 +00:00
|
|
|
|
|
|
|
async switch_graph_type_handler(event) {
|
|
|
|
// Figure out what the new reading type is
|
2019-01-20 22:32:34 +00:00
|
|
|
this.reading_type = this.reading_types.find((type) => type.id == event.target.parentNode.dataset.id);
|
|
|
|
|
|
|
|
console.log("[marker/device-graph] Reading type is now", this.reading_type);
|
2019-01-20 21:54:40 +00:00
|
|
|
|
2019-02-10 14:45:15 +00:00
|
|
|
let popup_container = GetContainingElement(event.target, "div");
|
|
|
|
popup_container.classList.add("working");
|
|
|
|
|
2019-01-20 22:47:05 +00:00
|
|
|
// Update the button list to highlight the newly-selected reading type, but only if we managed to update the chart successfully
|
|
|
|
if(await this.update_chart()) {
|
|
|
|
// Remove the selected class from the old one(s?)
|
|
|
|
event.target.parentNode.parentNode.querySelectorAll(`button`)
|
|
|
|
.forEach((next_button) => next_button.classList.remove("selected"));
|
|
|
|
|
|
|
|
// Add the selected class to the new one
|
|
|
|
event.target.classList.add("selected");
|
|
|
|
}
|
2019-02-10 14:45:15 +00:00
|
|
|
|
|
|
|
popup_container.classList.remove("working");
|
2019-01-20 21:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async update_chart() {
|
|
|
|
this.chart.data.datasets.length = 0;
|
|
|
|
|
|
|
|
// Get the Chart.js data object
|
|
|
|
let new_data_obj = {
|
|
|
|
label: this.reading_type.friendly_text,
|
|
|
|
data: await this.get_data()
|
|
|
|
};
|
|
|
|
|
2019-01-20 22:47:05 +00:00
|
|
|
if(new_data_obj.data == null) return false;
|
|
|
|
|
2019-01-20 22:32:34 +00:00
|
|
|
// Update the colour & suggested min/max values
|
|
|
|
let def = this.reading_type_defs[this.reading_type.id],
|
|
|
|
y_axis = this.chart.options.scales.yAxes[0];
|
|
|
|
if(typeof def !== "undefined") {
|
|
|
|
new_data_obj.borderColor = def.borderColor;
|
|
|
|
new_data_obj.backgroundColor = def.backgroundColor;
|
2019-01-20 21:54:40 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_data_obj.borderColor = this.default_colours.borderColor;
|
|
|
|
new_data_obj.backgroundColor = this.default_colours.backgroundColor;
|
|
|
|
}
|
2019-01-20 22:32:34 +00:00
|
|
|
if(typeof def !== "undefined" && typeof def.suggestedMin !== "undefined") {
|
|
|
|
y_axis.ticks.suggestedMin = def.suggestedMin;
|
|
|
|
y_axis.ticks.suggestedMax = def.suggestedMax;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete y_axis.ticks.suggestedMin;
|
|
|
|
delete y_axis.ticks.suggestedMax;
|
|
|
|
}
|
2019-01-20 21:54:40 +00:00
|
|
|
|
|
|
|
this.chart.data.datasets.push(new_data_obj);
|
|
|
|
|
|
|
|
// Update the x axis labels
|
|
|
|
this.chart.data.labels = new_data_obj.data.map((point) => point.t);
|
|
|
|
|
|
|
|
// Update the chart
|
|
|
|
this.chart.update();
|
2019-01-20 22:47:05 +00:00
|
|
|
|
|
|
|
return true;
|
2019-01-20 21:54:40 +00:00
|
|
|
}
|
2019-01-19 16:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default DeviceReadingDisplay;
|