From 795a887678f750a2c81dddb0229432a2c3ab9b86 Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 15 Jun 2023 15:02:51 -0500 Subject: [PATCH] Updates --- Dockerfile | 2 +- src/index.ts | 46 +++++++++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index 47165f7..a7fa107 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:18-alpine3.14 +FROM node:18-alpine3.18 RUN apk update && apk upgrade && sync EXPOSE 3000 WORKDIR /usr/app diff --git a/src/index.ts b/src/index.ts index 9f9a57a..fcd02f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,15 +33,12 @@ export type DataPoint = { value: number; }; +let tempValue = 0.; + async function pullTempFromBackend() { try { const response = await axios.get( - 'https://sensors.mooreforge.com/api/teasense/', - { - headers: { - Authorization: 'Basic Y2FyYW5tZWdpbDplOHl0cjl1dQ==', - }, - }, + process.env.TEASENSE_URL ?? '' ); const result = response.data.reduce( @@ -57,25 +54,32 @@ async function pullTempFromBackend() { null, ); - if (result && result.value >= 78.0) { - try { - const resultStr = await messaging.send({ - data: { - value: `${result?.value}` - }, - topic: 'teasense', - }); - console.log(resultStr); - } catch (e) { - console.error(e); - } + if (result?.value && result?.value >= parseInt(process.env.TEASENSE_TEMP ?? '100.0')) { + tempValue = result?.value; } } catch (e) { - console.log(e); + console.error(e); } finally { - setTimeout(() => pullTempFromBackend(), 1000); + setTimeout(() => pullTempFromBackend(), 2000); } } -pullTempFromBackend(); +(async function () { + await pullTempFromBackend(); + setInterval(async () => { + if (tempValue > 0.) { + try { + await messaging.send({ + notification: { + body: `♨️ ${tempValue} C`, + }, + topic: 'teasense', + }); + } catch (e) { + console.error(e); + } + tempValue = 0.; + } + }, 5000); +})();