From 0b7350ec5784a99a96fbf705547ac8930e7ed17a Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 21 Mar 2024 17:39:22 -0500 Subject: [PATCH] Updates --- src/index.ts | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/index.ts b/src/index.ts index fcd02f6..98a431b 100644 --- a/src/index.ts +++ b/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(); })();