From 67ba524b2dbd87dad21ac43db820e6c359dac196 Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 19 Jan 2023 20:53:58 -0600 Subject: [PATCH] Update to add teasense --- src/index.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 65160af..b4644be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ const { formatInTimeZone } = dateFnsTz; const RAIN_GAUGE_ENDPOINT = process.env.RAIN_GAUGE_ENDPOINT; const SOIL_SENSOR_ENDPOINT = process.env.SOIL_SENSOR_ENDPOINT; +const TEASENSE_ENDPOINT = process.env.TEASENSE_ENDPOINT; const PORT = process.env.PORT || 3000; const RAIN_GAUGE_SLEEP = 100; @@ -75,7 +76,9 @@ const saveDataPoint = async (name: string, value: number) => { const collectData = async () => { if (RAIN_GAUGE_ENDPOINT && RAIN_GAUGE_ENDPOINT.trim() !== '') { try { - const response = await fetch(RAIN_GAUGE_ENDPOINT); + const response = await fetch(RAIN_GAUGE_ENDPOINT, { + signal: AbortSignal.timeout(5000), + }); const data: any = await response.json(); if (data.rainfall > 0) { saveDataPoint('rainfall', data.rainfall); @@ -87,7 +90,9 @@ const collectData = async () => { if (SOIL_SENSOR_ENDPOINT && SOIL_SENSOR_ENDPOINT.trim() !== '') { try { - const response = await fetch(SOIL_SENSOR_ENDPOINT); + const response = await fetch(SOIL_SENSOR_ENDPOINT, { + signal: AbortSignal.timeout(5000), + }); const data: any = await response.json(); saveDataPoint('soil_temperature', data.temperature); saveDataPoint('soil_capacitence', data.capacitive); @@ -95,6 +100,18 @@ const collectData = async () => { console.log(e); } } + + if (TEASENSE_ENDPOINT && TEASENSE_ENDPOINT.trim() !== '') { + try { + const response = await fetch(TEASENSE_ENDPOINT, { + signal: AbortSignal.timeout(5000), + }); + const data: any = await response.json(); + saveDataPoint('teasense', data.temperature); + } catch (e) { + console.log(e); + } + } setTimeout(() => collectData(), RAIN_GAUGE_SLEEP); }