2019-06-10 23:19:37 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-06-11 12:28:47 +00:00
|
|
|
import chroma from 'chroma-js';
|
|
|
|
|
|
|
|
import Config from '../Config.mjs';
|
|
|
|
|
2019-06-10 23:19:37 +00:00
|
|
|
import VoronoiOverlay from './VoronoiOverlay.mjs';
|
|
|
|
import VoronoiCell from './VoronoiCell.mjs';
|
|
|
|
|
2019-06-11 11:42:59 +00:00
|
|
|
import Guage from '../Guage.mjs';
|
|
|
|
import Specs from './OverlaySpecs.mjs';
|
|
|
|
|
2019-06-10 23:19:37 +00:00
|
|
|
import Vector2 from '../Helpers/Vector2.mjs';
|
2019-06-11 11:42:59 +00:00
|
|
|
import GetFromUrl from '../Helpers/GetFromUrl.mjs';
|
2019-06-10 23:19:37 +00:00
|
|
|
|
|
|
|
class VoronoiManager {
|
2019-06-11 11:22:01 +00:00
|
|
|
|
2019-06-11 12:45:26 +00:00
|
|
|
constructor(in_device_data, in_map) {
|
2019-06-10 23:19:37 +00:00
|
|
|
this.device_data = in_device_data;
|
2019-06-11 12:45:26 +00:00
|
|
|
this.map = in_map;
|
|
|
|
|
|
|
|
this.layer = null;
|
2019-06-11 11:42:59 +00:00
|
|
|
|
|
|
|
this.setup_guage();
|
2019-06-11 12:45:26 +00:00
|
|
|
this.setup_overlay();
|
2019-06-11 11:42:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:45:26 +00:00
|
|
|
setup_overlay() {
|
2019-06-10 23:19:37 +00:00
|
|
|
this.overlay = new VoronoiOverlay();
|
2019-06-11 12:45:26 +00:00
|
|
|
this.set_data(new Date(), "PM25"); // TODO: Make this customisable
|
2019-06-10 23:19:37 +00:00
|
|
|
}
|
2019-06-11 11:42:59 +00:00
|
|
|
|
|
|
|
setup_guage() {
|
|
|
|
this.guage = new Guage(document.getElementById("canvas-guage"));
|
|
|
|
}
|
|
|
|
|
|
|
|
async set_data(datetime, reading_type) {
|
2019-06-11 12:28:47 +00:00
|
|
|
this.spec = Specs[reading_type];
|
|
|
|
if(typeof this.spec.chroma == "undefined")
|
|
|
|
this.spec.chroma = chroma.scale(Object.values(this.spec.gradient))
|
|
|
|
.domain(Object.keys(this.spec.gradient));
|
|
|
|
this.guage.set_spec(this.spec);
|
2019-06-11 11:42:59 +00:00
|
|
|
this.guage.render();
|
|
|
|
|
2019-06-11 12:28:47 +00:00
|
|
|
let dataset = JSON.parse(await GetFromUrl(
|
2019-06-11 11:42:59 +00:00
|
|
|
`${Config.api_root}?action=fetch-data&datetime=${encodeURIComponent(datetime.toISOString())}&reading_type=${encodeURIComponent(reading_type)}`
|
|
|
|
));
|
|
|
|
|
2019-06-11 12:28:47 +00:00
|
|
|
let result = [];
|
|
|
|
for(let row of dataset) {
|
|
|
|
let device = this.device_data.get_by_id(row.device_id);
|
|
|
|
if(typeof device.latitude != "number" || typeof device.longitude != "number")
|
|
|
|
continue;
|
|
|
|
result.push(new VoronoiCell(
|
|
|
|
new Vector2(
|
|
|
|
device.longitude,
|
|
|
|
device.latitude
|
|
|
|
),
|
2019-06-11 12:45:26 +00:00
|
|
|
// See https://gka.github.io/chroma.js/
|
2019-06-11 12:28:47 +00:00
|
|
|
this.spec.chroma(row.value).toString()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:45:26 +00:00
|
|
|
this.overlay.set_cells(result);
|
|
|
|
|
|
|
|
if(this.layer !== null)
|
|
|
|
this.layer.remove(); // Remove the old layer if it exists
|
|
|
|
// Generate & add the new layer
|
|
|
|
this.layer = this.overlay.generate_layer();
|
|
|
|
this.layer.addTo(this.map);
|
|
|
|
|
2019-06-11 11:42:59 +00:00
|
|
|
console.log(result);
|
|
|
|
}
|
2019-06-10 23:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default VoronoiManager;
|