tempestas

A REST API for processing sensor.community data
git clone https://git.bracken.jp/tempestas.git
Log | Files | Refs | README | LICENSE

commit 37a74914f4cbe39a9f292482f60d2b23fea0cf10
parent aeba88e67eec07f946cbf562b710d3d900ce0c7d
Author: Chris Bracken <chris@bracken.jp>
Date:   Fri, 12 Nov 2021 23:53:19 -0800

Eliminate hardcoded sensors list

We now fetch the sensors and descriptions from the backend.

Diffstat:
Mstatic/tempestas.js | 24+++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/static/tempestas.js b/static/tempestas.js @@ -4,11 +4,9 @@ // The JSON API server base URL. const API_SERVER = 'http://minerva' -var sensors = [ - {id: '8558074', name: 'Back yard', color: 'rgb(75, 192, 192)'}, - {id: '10714323', name: 'Master bedroom', color: 'rgb(255, 99, 132)'}, - {id: '6497060', name: 'Dining room', color: 'rgb(255, 159, 64)'}, -]; +// TODO: automate colour generation. +var colors = ['rgb(75, 192, 192)', 'rgb(255, 99, 132)', 'rgb(255, 159, 64)']; +var sensors = []; var charts = [ {id: 'chart-temperature', title: 'Temperature (°C)', readingType: 'BME280_temperature'}, {id: 'chart-humidity', title: 'Humidity (%)', readingType: 'BME280_humidity'}, @@ -16,10 +14,18 @@ var charts = [ ]; var sensorReadings = {}; -charts.forEach((chart, i) => sensorReadings[chart.id] = {}); -sensors.forEach((sensor, i) => fetchSensorData(sensor.id, charts[0])); -sensors.forEach((sensor, i) => fetchSensorData(sensor.id, charts[1])); -sensors.forEach((sensor, i) => fetchSensorData(sensor.id, charts[2])); +var sensorUrl = API_SERVER + '/sensor/sensors/'; +fetch(sensorUrl) + .then((response) => { return response.json(); }) + .then((data) => { + data.forEach((sensor, i) => { + sensors.push({id: sensor.sensor_id, name: sensor.name, color: colors[i]}); + }); + charts.forEach((chart, i) => sensorReadings[chart.id] = {}); + sensors.forEach((sensor, i) => fetchSensorData(sensor.id, charts[0])); + sensors.forEach((sensor, i) => fetchSensorData(sensor.id, charts[1])); + sensors.forEach((sensor, i) => fetchSensorData(sensor.id, charts[2])); +}); function fetchSensorData(sensorId, chart) { var url = API_SERVER + '/sensor/reading/' + sensorId + '/' + chart.readingType;