Update to include an early warning mechanism
This commit is contained in:
parent
9b7b52752f
commit
7dad43d045
18
src/index.ts
18
src/index.ts
@ -27,16 +27,34 @@ 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 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 GRAPHQL_URL = process.env.GRAPHQL_URL;
|
||||||
const TICK_THRESHOLD = parseInt(process.env.TICK_THRESHOLD ?? '15');
|
const TICK_THRESHOLD = parseInt(process.env.TICK_THRESHOLD ?? '15');
|
||||||
const SLEEP_TIMER = 1000;
|
const SLEEP_TIMER = 1000;
|
||||||
|
|
||||||
let numberOfTicks = 0;
|
let numberOfTicks = 0;
|
||||||
|
let numberOfEarlyTicks = 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 && 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) {
|
if (response.getSensorData.temperato >= SET_POINT) {
|
||||||
numberOfTicks++;
|
numberOfTicks++;
|
||||||
if (numberOfTicks == TICK_THRESHOLD) {
|
if (numberOfTicks == TICK_THRESHOLD) {
|
||||||
|
Loading…
Reference in New Issue
Block a user