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);