Update to include tick thresholds to avoid spammy messages

This commit is contained in:
William Moore 2022-05-28 16:46:29 -05:00
parent a9d0fcf728
commit f1562451cc

View File

@ -21,26 +21,33 @@ import fetch from 'node-fetch';
import { request } from 'graphql-request'; import { request } from 'graphql-request';
import getSensorDataQuery from './queries/getSensorData'; 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 SET_POINT = parseFloat(process.env.SET_POINT);
const USERNAME = process.env.USERNAME; const USERNAME = process.env.USERNAME;
const PASSWORD = process.env.PASSWORD; const PASSWORD = process.env.PASSWORD;
const NOTIF_URL = process.env.NOTIF_URL; const NOTIF_URL = process.env.NOTIF_URL;
const MESSAGE = process.env.MESSAGE; const MESSAGE = process.env.MESSAGE;
const GRAPHQL_URL = process.env.GRAPHQL_URL; const GRAPHQL_URL = process.env.GRAPHQL_URL;
const TICK_THRESHOLD = parseInt(process.env.TICK_THRESHOLD ?? '15');
const SLEEP_TIMER = 1000; const SLEEP_TIMER = 1000;
let numberOfTicks = 0;
async function temperatoTick() { async function temperatoTick() {
try { try {
const response = await request(GRAPHQL_URL, getSensorDataQuery); const response = await request(GRAPHQL_URL, getSensorDataQuery);
if (response.getSensorData.temperato >= SET_POINT) { if (response.getSensorData.temperato >= SET_POINT) {
const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64'); numberOfTicks++;
await fetch(NOTIF_URL, { if (numberOfTicks == TICK_THRESHOLD) {
method: 'post', numberOfTicks = 0;
body: `{"message": "${MESSAGE}"}`, const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64');
headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${basicAuth}` }, await fetch(NOTIF_URL, {
}); method: 'post',
body: `{"message": "${MESSAGE}"}`,
headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${basicAuth}` },
});
}
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);