From a37f010b76b40244a87feefec12f0cde85c170a7 Mon Sep 17 00:00:00 2001 From: furyhawk Date: Wed, 28 May 2025 15:59:07 +0800 Subject: [PATCH] refactor: Improve Air Temperature data handling and enhance station mapping in readings --- frontend/src/routes/_layout/weather-hub.tsx | 61 +++++++++++++++++---- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/frontend/src/routes/_layout/weather-hub.tsx b/frontend/src/routes/_layout/weather-hub.tsx index 6590c1e..882c150 100644 --- a/frontend/src/routes/_layout/weather-hub.tsx +++ b/frontend/src/routes/_layout/weather-hub.tsx @@ -44,14 +44,31 @@ interface TwoHourForecastPayload { area_metadata?: any[]; // Define AreaMetadata if its structure is known and used } +interface AirTempStation { + id: string; + deviceId: string; + name: string; + location: { + latitude: number; + longitude: number; + }; +} + +interface AirTempDataPoint { + stationId: string; + value: number; +} + interface AirTempReading { - id: string; // Assuming 'id' based on WindDirection component and errors - value: number | null; - timestamp?: string; + timestamp: string; + data: AirTempDataPoint[]; } interface AirTemperaturePayload { + stations?: AirTempStation[]; readings?: AirTempReading[]; + readingType?: string; + readingUnit?: string; } interface OutlookForecast { @@ -209,14 +226,26 @@ function AirTemperature() { if (isLoading) return if (error || !data?.data) return Error loading temperature data - if (!Array.isArray(data.data.readings) || data.data.readings.length === 0) { + const { stations = [], readings = [] } = data.data; + + if (stations.length === 0 || readings.length === 0) { return No temperature data available } - const readings = data.data.readings + // Get the latest reading + const latestReading = readings[0]; + if (!latestReading || !latestReading.data) { + return No temperature data available + } + + // Create a map of station IDs to station names + const stationMap = new Map(stations.map(station => [station.id, station.name])); // Calculate average temperature - const avgTemp = readings.reduce((sum, reading) => sum + (reading.value ?? 0), 0) / readings.length + const validReadings = latestReading.data.filter(reading => reading.value !== null); + const avgTemp = validReadings.length > 0 + ? validReadings.reduce((sum, reading) => sum + reading.value, 0) / validReadings.length + : 0; return ( @@ -225,18 +254,28 @@ function AirTemperature() { Average Temperature {avgTemp.toFixed(1)}°C - Based on {readings.length} station readings + + Based on {validReadings.length} station readings + + + Last updated: {new Date(latestReading.timestamp).toLocaleString()} + Temperature Readings by Station {/* Changed spacing to gap */} - {readings.slice(0, 9).map((reading: AirTempReading) => ( - {/* Changed station_id to id */} + {latestReading.data.slice(0, 9).map((reading: AirTempDataPoint) => ( + - {reading.id} {/* Changed station_id to id */} - {(reading.value ?? 0).toFixed(1)}°C + + {stationMap.get(reading.stationId) || reading.stationId} + + + {reading.stationId} + + {reading.value.toFixed(1)}°C ))}