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