Updates
This commit is contained in:
parent
9e929daa9a
commit
0b7350ec57
41
src/index.ts
41
src/index.ts
@ -1,8 +1,5 @@
|
||||
import 'dotenv/config'
|
||||
|
||||
// Import the functions you need from the SDKs you need
|
||||
import { initializeApp } from "firebase-admin/app";
|
||||
import { getMessaging } from "firebase-admin/messaging";
|
||||
import axios from 'axios';
|
||||
import { isBefore } from 'date-fns';
|
||||
|
||||
@ -22,9 +19,7 @@ const firebaseConfig = {
|
||||
measurementId: process.env.measurementId,
|
||||
};
|
||||
|
||||
// Initialize Firebase
|
||||
const app = initializeApp(firebaseConfig);
|
||||
const messaging = getMessaging(app);
|
||||
let hasAlerted = false;
|
||||
|
||||
export type DataPoint = {
|
||||
id: string;
|
||||
@ -37,10 +32,10 @@ let tempValue = 0.;
|
||||
|
||||
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 &&
|
||||
@ -55,31 +50,21 @@ async function pullTempFromBackend() {
|
||||
);
|
||||
|
||||
if (result?.value && result?.value >= parseInt(process.env.TEASENSE_TEMP ?? '100.0')) {
|
||||
tempValue = result?.value;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
} finally {
|
||||
setTimeout(() => pullTempFromBackend(), 2000);
|
||||
}
|
||||
}
|
||||
|
||||
(async function () {
|
||||
await pullTempFromBackend();
|
||||
|
||||
setInterval(async () => {
|
||||
if (tempValue > 0.) {
|
||||
try {
|
||||
await messaging.send({
|
||||
notification: {
|
||||
body: `♨️ ${tempValue} C`,
|
||||
},
|
||||
topic: 'teasense',
|
||||
});
|
||||
axios.get(process.env.TEASENSE_ALERT_URL ?? '');
|
||||
hasAlerted = true;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
tempValue = 0.;
|
||||
}
|
||||
}, 5000);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
} finally {
|
||||
setTimeout(() => pullTempFromBackend(), hasAlerted ? 60000 : 5000);
|
||||
}
|
||||
}
|
||||
|
||||
(async function () {
|
||||
await pullTempFromBackend();
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user