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); }