From f1562451cc7ce99251608ae4656c53e89bfe2da2 Mon Sep 17 00:00:00 2001 From: William Moore Date: Sat, 28 May 2022 16:46:29 -0500 Subject: [PATCH] Update to include tick thresholds to avoid spammy messages --- src/index.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 863b295..d7dfbea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,26 +21,33 @@ import fetch from 'node-fetch'; import { request } from 'graphql-request'; import getSensorDataQuery from './queries/getSensorData'; -const PORT = process.env.PORT || 3000; +const PORT = parseInt(process.env.PORT) || 3000; const SET_POINT = parseFloat(process.env.SET_POINT); const USERNAME = process.env.USERNAME; const PASSWORD = process.env.PASSWORD; const NOTIF_URL = process.env.NOTIF_URL; const MESSAGE = process.env.MESSAGE; const GRAPHQL_URL = process.env.GRAPHQL_URL; +const TICK_THRESHOLD = parseInt(process.env.TICK_THRESHOLD ?? '15'); const SLEEP_TIMER = 1000; +let numberOfTicks = 0; + async function temperatoTick() { try { const response = await request(GRAPHQL_URL, getSensorDataQuery); if (response.getSensorData.temperato >= SET_POINT) { - const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64'); - await fetch(NOTIF_URL, { - method: 'post', - body: `{"message": "${MESSAGE}"}`, - headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${basicAuth}` }, - }); + numberOfTicks++; + if (numberOfTicks == TICK_THRESHOLD) { + numberOfTicks = 0; + const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64'); + await fetch(NOTIF_URL, { + method: 'post', + body: `{"message": "${MESSAGE}"}`, + headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${basicAuth}` }, + }); + } } } catch (e) { console.error(e);