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 00:17:54 +00:00
|
|
|
// import Chart from 'chart.js';
|
2019-01-19 16:08:47 +00:00
|
|
|
|
|
|
|
import GetFromUrl from './Helpers/GetFromUrl.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-19 22:04:51 +00:00
|
|
|
constructor(in_config, in_device_id, in_reading_type) {
|
2019-01-19 16:08:47 +00:00
|
|
|
this.config = in_config;
|
2019-01-19 22:04:51 +00:00
|
|
|
/** @type {int} */
|
|
|
|
this.device_id = in_device_id;
|
|
|
|
// TODO: Allow the user to change this
|
|
|
|
/** @type {Object} */
|
|
|
|
this.reading_type = in_reading_type;
|
|
|
|
|
|
|
|
this.setup_display();
|
2019-01-19 16:08:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:04:51 +00:00
|
|
|
async setup_display() {
|
|
|
|
/** @type {HTMLElement} */
|
2019-01-19 16:08:47 +00:00
|
|
|
this.display = CreateElement("div.chart-device-data",
|
|
|
|
CreateElement("canvas.canvas-chart"),
|
|
|
|
CreateElement("ul.reading-types")
|
|
|
|
);
|
2019-01-19 22:04:51 +00:00
|
|
|
|
|
|
|
this.chart = new Chart(
|
|
|
|
this.display.querySelector("canvas").getContext("2d"), {
|
|
|
|
type: "line",
|
|
|
|
data: {
|
|
|
|
datasets: [{
|
|
|
|
label: this.reading_type.friendly_text,
|
|
|
|
data: await this.get_data
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-01-19 16:08:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:04:51 +00:00
|
|
|
async get_data() {
|
|
|
|
let 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,
|
|
|
|
start: (new Date()).toISOString(),
|
|
|
|
end: new Date(new Date - 60*60*24),
|
|
|
|
"average-seconds": 3600
|
|
|
|
})));
|
|
|
|
|
|
|
|
return new_data.map((data_point) => { return {
|
|
|
|
x: new Date(data_point.datetime),
|
|
|
|
y: data_point.value
|
|
|
|
}});
|
2019-01-19 16:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DeviceReadingDisplay;
|