From ea87ab6478990d572a4f655b7b81b9cec1b179a6 Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 26 May 2022 20:24:35 -0500 Subject: [PATCH] Update to memoize the sensor reading to prevent overflowing the sensor with requests --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 548fb3a..d76f7b3 100644 --- a/index.js +++ b/index.js @@ -35,9 +35,7 @@ const router = new Router(); router.get('/', async ctx => { try { - const sensor = await fetch(SENSOR_URL); - const reading = await sensor.json(); - ctx.body = reading; + ctx.body = `{"temperature": ${currentSensorReading}}`; } catch (e) { ctx.body = 'There was an error retrieving the sensor data.'; ctx.status = 500; @@ -50,10 +48,13 @@ app app.listen(PORT); +let currentSensorReading=0.0 + async function temperatoTick() { try { const sensor = await fetch(SENSOR_URL); const reading = await sensor.json(); + currentSensorReading = reading.temperature; if (reading.temperature >= SET_POINT) { const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64'); await fetch(NOTIF_URL, {