mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2024-11-25 06:53:00 +00:00
44 lines
968 B
JavaScript
44 lines
968 B
JavaScript
|
"use strict";
|
||
|
|
||
|
import Config from './Config.mjs';
|
||
|
|
||
|
import GetFromUrl from './Helpers/GetFromUrl.mjs';
|
||
|
|
||
|
/**
|
||
|
* Handles and caches data about devices.
|
||
|
*/
|
||
|
class DeviceData {
|
||
|
/**
|
||
|
* Creates a new DeviceData class instance.
|
||
|
*/
|
||
|
constructor() {
|
||
|
this.devices = [];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Fetches the device data from the server.
|
||
|
* @return {Promise} A promise that resolves when the data has been fetched from the server.
|
||
|
*/
|
||
|
async setup() {
|
||
|
this.devices = JSON.parse(await GetFromUrl(
|
||
|
`${Config.api_root}?action=list-devices`
|
||
|
));
|
||
|
|
||
|
// Create a map to help us lookup ids faster
|
||
|
this.device_map = new Map();
|
||
|
for(let device of this.devices)
|
||
|
this.device_map.set(device.id, device);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Looks up a device by its id.
|
||
|
* @param {Number} device_id The ID of the device to return
|
||
|
* @return {object} The info about the device with the specified id.
|
||
|
*/
|
||
|
get_by_id(device_id) {
|
||
|
return this.device_map.get(device_id);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default DeviceData;
|