Update to be single source of truth for teasense alerting
This commit is contained in:
parent
3ad4959459
commit
bf541b917f
5032
package-lock.json
generated
5032
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -12,14 +12,13 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/axios": "^0.14.0",
|
||||
"@types/node": "^20.12.5",
|
||||
"typescript": "^5.0.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.4.0",
|
||||
"date-fns": "^2.30.0",
|
||||
"dotenv": "^16.1.3",
|
||||
"firebase": "^9.22.1",
|
||||
"firebase-admin": "^11.9.0",
|
||||
"matrix-js-sdk": "^31.6.1"
|
||||
"matrix-js-sdk": "^31.6.1",
|
||||
"node-fetch": "^3.3.2"
|
||||
}
|
||||
}
|
||||
|
42
src/index.ts
42
src/index.ts
@ -2,24 +2,9 @@ import 'dotenv/config'
|
||||
|
||||
import * as sdk from "matrix-js-sdk";
|
||||
import axios from 'axios';
|
||||
import fetch from 'node-fetch';
|
||||
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;
|
||||
|
||||
export type DataPoint = {
|
||||
@ -31,26 +16,21 @@ export type DataPoint = {
|
||||
|
||||
let tempValue = 0.;
|
||||
|
||||
type TeaSenseResult = {
|
||||
temperature: number;
|
||||
};
|
||||
|
||||
async function pullTempFromBackend() {
|
||||
try {
|
||||
hasAlerted = false;
|
||||
const response = await axios.get(
|
||||
process.env.TEASENSE_URL ?? ''
|
||||
);
|
||||
const result = response.data.reduce(
|
||||
(accum: DataPoint, datum: DataPoint) =>
|
||||
(accum &&
|
||||
isBefore(
|
||||
new Date(accum?.timestamp),
|
||||
new Date(datum?.timestamp),
|
||||
)) ||
|
||||
!accum
|
||||
? datum
|
||||
: accum,
|
||||
null,
|
||||
const response = await fetch(
|
||||
process.env.TEASENSE_URL ?? '', {
|
||||
signal: AbortSignal.timeout(5000),
|
||||
}
|
||||
);
|
||||
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 {
|
||||
const content = {
|
||||
body: '♨️',
|
||||
|
Loading…
Reference in New Issue
Block a user