Update to memoize the sensor reading to prevent overflowing the sensor with requests
This commit is contained in:
parent
25e5a63d46
commit
ea87ab6478
7
index.js
7
index.js
@ -35,9 +35,7 @@ const router = new Router();
|
|||||||
|
|
||||||
router.get('/', async ctx => {
|
router.get('/', async ctx => {
|
||||||
try {
|
try {
|
||||||
const sensor = await fetch(SENSOR_URL);
|
ctx.body = `{"temperature": ${currentSensorReading}}`;
|
||||||
const reading = await sensor.json();
|
|
||||||
ctx.body = reading;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ctx.body = 'There was an error retrieving the sensor data.';
|
ctx.body = 'There was an error retrieving the sensor data.';
|
||||||
ctx.status = 500;
|
ctx.status = 500;
|
||||||
@ -50,10 +48,13 @@ app
|
|||||||
|
|
||||||
app.listen(PORT);
|
app.listen(PORT);
|
||||||
|
|
||||||
|
let currentSensorReading=0.0
|
||||||
|
|
||||||
async function temperatoTick() {
|
async function temperatoTick() {
|
||||||
try {
|
try {
|
||||||
const sensor = await fetch(SENSOR_URL);
|
const sensor = await fetch(SENSOR_URL);
|
||||||
const reading = await sensor.json();
|
const reading = await sensor.json();
|
||||||
|
currentSensorReading = reading.temperature;
|
||||||
if (reading.temperature >= SET_POINT) {
|
if (reading.temperature >= SET_POINT) {
|
||||||
const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64');
|
const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64');
|
||||||
await fetch(NOTIF_URL, {
|
await fetch(NOTIF_URL, {
|
||||||
|
Loading…
Reference in New Issue
Block a user