From 7dad43d04522caa74dfe1231ba117de1097b2b70 Mon Sep 17 00:00:00 2001 From: William Moore Date: Sun, 29 May 2022 03:28:25 -0500 Subject: [PATCH] Update to include an early warning mechanism --- src/index.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0a7990c..1d0f0d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,19 +27,37 @@ const USERNAME = process.env.USERNAME; const PASSWORD = process.env.PASSWORD; const NOTIF_URL = process.env.NOTIF_URL; const MESSAGE = process.env.MESSAGE; +const EARLY_WARNING = parseFloat(process.env.EARLY_WARNING ?? process.env.SET_POINT); +const EARLY_WARNING_MESSAGE = process.env.EARLY_WARNING_MESSAGE; const GRAPHQL_URL = process.env.GRAPHQL_URL; const TICK_THRESHOLD = parseInt(process.env.TICK_THRESHOLD ?? '15'); const SLEEP_TIMER = 1000; let numberOfTicks = 0; +let numberOfEarlyTicks = 0; async function temperatoTick() { try { const response = await request(GRAPHQL_URL, getSensorDataQuery); - if (response.getSensorData.temperato >= SET_POINT) { + if (response.getSensorData.temperato < SET_POINT && response.getSensorData.temperato >= EARLY_WARNING) { + numberOfEarlyTicks++; + if (numberOfEarlyTicks == TICK_THRESHOLD) { + numberOfEarlyTicks = 0; + } + + if (numberOfEarlyTicks == 0) { + const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64'); + await fetch(NOTIF_URL, { + method: 'post', + body: `{"message": "${EARLY_WARNING_MESSAGE}"}`, + headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${basicAuth}` }, + }); + } + } + if (response.getSensorData.temperato >= SET_POINT) { numberOfTicks++; - if (numberOfTicks == TICK_THRESHOLD) { + if (numberOfTicks == TICK_THRESHOLD) { numberOfTicks = 0; } @@ -50,7 +68,7 @@ async function temperatoTick() { body: `{"message": "${MESSAGE}"}`, headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${basicAuth}` }, }); - } + } } } catch (e) { console.error(e);