Update to use data collector results

This commit is contained in:
William Moore 2023-01-20 08:44:12 -06:00
parent ad703f6807
commit 5849fda335
3 changed files with 39 additions and 34 deletions

18
package-lock.json generated
View File

@ -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",

View File

@ -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"

View File

@ -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,14 +38,10 @@ async function temperatoTick() {
timeout: 5000,
});
const data: any = await response.json();
const result = data
.reduce((accum: any, datum: any) => (((accum && isBefore(new Date(accum?.timestamp), new Date(datum?.timestamp))) || !accum) ? datum : accum));
if (data.temperature < SET_POINT && data.temperature >= EARLY_WARNING) {
numberOfEarlyTicks++;
if (numberOfEarlyTicks == TICK_THRESHOLD) {
numberOfEarlyTicks = 0;
}
if (numberOfEarlyTicks == 0) {
if (result.value < SET_POINT && result.value >= EARLY_WARNING) {
await gotify({
server: GOTIFY_HOST,
app: GOTIFY_TOKEN,
@ -56,14 +50,7 @@ async function temperatoTick() {
priority: 10,
});
}
}
if (data.temperature >= SET_POINT) {
numberOfTicks++;
if (numberOfTicks == TICK_THRESHOLD) {
numberOfTicks = 0;
}
if (numberOfTicks == 0) {
if (result.value >= SET_POINT) {
await gotify({
server: GOTIFY_HOST,
app: GOTIFY_TOKEN,
@ -72,7 +59,6 @@ async function temperatoTick() {
priority: 10,
});
}
}
} catch (e) {
console.error(new Date(), e);
} finally {