Update to use data collector results
This commit is contained in:
parent
ad703f6807
commit
5849fda335
18
package-lock.json
generated
18
package-lock.json
generated
@ -9,6 +9,7 @@
|
||||
"version": "2.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"date-fns": "^2.29.3",
|
||||
"dotenv": "^16.0.1",
|
||||
"gotify": "^1.1.0",
|
||||
"node-fetch": "2.6.7"
|
||||
@ -249,6 +250,18 @@
|
||||
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/date-fns": {
|
||||
"version": "2.29.3",
|
||||
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
|
||||
"integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==",
|
||||
"engines": {
|
||||
"node": ">=0.11"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/date-fns"
|
||||
}
|
||||
},
|
||||
"node_modules/decompress-response": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-5.0.0.tgz",
|
||||
@ -842,6 +855,11 @@
|
||||
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
|
||||
"dev": true
|
||||
},
|
||||
"date-fns": {
|
||||
"version": "2.29.3",
|
||||
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
|
||||
"integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA=="
|
||||
},
|
||||
"decompress-response": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-5.0.0.tgz",
|
||||
|
@ -16,6 +16,7 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"date-fns": "^2.29.3",
|
||||
"dotenv": "^16.0.1",
|
||||
"gotify": "^1.1.0",
|
||||
"node-fetch": "2.6.7"
|
||||
|
54
src/index.ts
54
src/index.ts
@ -19,6 +19,7 @@
|
||||
import 'dotenv/config';
|
||||
import fetch from 'node-fetch';
|
||||
import { Gotify, gotify } from 'gotify';
|
||||
import { isBefore } from 'date-fns';
|
||||
|
||||
const PORT = parseInt(process.env.PORT) || 3000;
|
||||
const SET_POINT = parseFloat(process.env.SET_POINT);
|
||||
@ -29,10 +30,7 @@ const ENDPOINT = process.env.ENDPOINT;
|
||||
const GOTIFY_HOST = process.env.GOTIFY_HOST;
|
||||
const GOTIFY_TOKEN = process.env.GOTIFY_TOKEN;
|
||||
const TICK_THRESHOLD = parseInt(process.env.TICK_THRESHOLD ?? '15');
|
||||
const SLEEP_TIMER = 500;
|
||||
|
||||
let numberOfTicks = 0;
|
||||
let numberOfEarlyTicks = 0;
|
||||
const SLEEP_TIMER = 3000;
|
||||
|
||||
async function temperatoTick() {
|
||||
try {
|
||||
@ -40,38 +38,26 @@ async function temperatoTick() {
|
||||
timeout: 5000,
|
||||
});
|
||||
const data: any = await response.json();
|
||||
|
||||
if (data.temperature < SET_POINT && data.temperature >= EARLY_WARNING) {
|
||||
numberOfEarlyTicks++;
|
||||
if (numberOfEarlyTicks == TICK_THRESHOLD) {
|
||||
numberOfEarlyTicks = 0;
|
||||
}
|
||||
const result = data
|
||||
.reduce((accum: any, datum: any) => (((accum && isBefore(new Date(accum?.timestamp), new Date(datum?.timestamp))) || !accum) ? datum : accum));
|
||||
|
||||
if (numberOfEarlyTicks == 0) {
|
||||
await gotify({
|
||||
server: GOTIFY_HOST,
|
||||
app: GOTIFY_TOKEN,
|
||||
title: 'Warning!',
|
||||
message: EARLY_WARNING_MESSAGE,
|
||||
priority: 10,
|
||||
});
|
||||
}
|
||||
if (result.value < SET_POINT && result.value >= EARLY_WARNING) {
|
||||
await gotify({
|
||||
server: GOTIFY_HOST,
|
||||
app: GOTIFY_TOKEN,
|
||||
title: 'Warning!',
|
||||
message: EARLY_WARNING_MESSAGE,
|
||||
priority: 10,
|
||||
});
|
||||
}
|
||||
if (data.temperature >= SET_POINT) {
|
||||
numberOfTicks++;
|
||||
if (numberOfTicks == TICK_THRESHOLD) {
|
||||
numberOfTicks = 0;
|
||||
}
|
||||
|
||||
if (numberOfTicks == 0) {
|
||||
await gotify({
|
||||
server: GOTIFY_HOST,
|
||||
app: GOTIFY_TOKEN,
|
||||
title: 'Alert!',
|
||||
message: MESSAGE,
|
||||
priority: 10,
|
||||
});
|
||||
}
|
||||
if (result.value >= SET_POINT) {
|
||||
await gotify({
|
||||
server: GOTIFY_HOST,
|
||||
app: GOTIFY_TOKEN,
|
||||
title: 'Alert!',
|
||||
message: MESSAGE,
|
||||
priority: 10,
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(new Date(), e);
|
||||
|
Loading…
Reference in New Issue
Block a user