Update to be single source of truth for teasense alerting

This commit is contained in:
William Moore 2024-04-06 06:01:36 -05:00
parent 3ad4959459
commit bf541b917f
3 changed files with 157 additions and 4928 deletions

5036
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,14 +12,13 @@
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/axios": "^0.14.0", "@types/axios": "^0.14.0",
"@types/node": "^20.12.5",
"typescript": "^5.0.4" "typescript": "^5.0.4"
}, },
"dependencies": { "dependencies": {
"axios": "^1.4.0",
"date-fns": "^2.30.0", "date-fns": "^2.30.0",
"dotenv": "^16.1.3", "dotenv": "^16.1.3",
"firebase": "^9.22.1", "matrix-js-sdk": "^31.6.1",
"firebase-admin": "^11.9.0", "node-fetch": "^3.3.2"
"matrix-js-sdk": "^31.6.1"
} }
} }

View File

@ -2,24 +2,9 @@ import 'dotenv/config'
import * as sdk from "matrix-js-sdk"; import * as sdk from "matrix-js-sdk";
import axios from 'axios'; import axios from 'axios';
import fetch from 'node-fetch';
import { isBefore } from 'date-fns'; import { isBefore } from 'date-fns';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: process.env.apiKey,
authDomain: process.env.authDomain,
databaseURL: process.env.databaseURL,
projectId: process.env.projectId,
storageBucket: process.env.storageBucket,
messagingSenderId: process.env.messagingSenderId,
appId: process.env.appId,
measurementId: process.env.measurementId,
};
let hasAlerted = false; let hasAlerted = false;
export type DataPoint = { export type DataPoint = {
@ -31,26 +16,21 @@ export type DataPoint = {
let tempValue = 0.; let tempValue = 0.;
type TeaSenseResult = {
temperature: number;
};
async function pullTempFromBackend() { async function pullTempFromBackend() {
try { try {
hasAlerted = false; hasAlerted = false;
const response = await axios.get( const response = await fetch(
process.env.TEASENSE_URL ?? '' process.env.TEASENSE_URL ?? '', {
); signal: AbortSignal.timeout(5000),
const result = response.data.reduce( }
(accum: DataPoint, datum: DataPoint) =>
(accum &&
isBefore(
new Date(accum?.timestamp),
new Date(datum?.timestamp),
)) ||
!accum
? datum
: accum,
null,
); );
const result = await response.json() as TeaSenseResult;
if (result?.value && result?.value >= parseInt(process.env.TEASENSE_TEMP ?? '100.0')) { if (result?.temperature && result?.temperature >= parseInt(process.env.TEASENSE_TEMP ?? '100.0')) {
try { try {
const content = { const content = {
body: '♨️', body: '♨️',