Update to include an early warning mechanism

This commit is contained in:
William Moore 2022-05-29 03:28:25 -05:00
parent 9b7b52752f
commit 7dad43d045

View File

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