- New Schaad.OpenData.Weather project: MeteoSwissClient reads the
10-minute CSV of the five SwissMetNet stations in Thurgau every 10
minutes (station list once a day) and backfills the last 7 days from
the yearly file when the cache has less than 6 days
- History kept as JSON per day (60 days); a failing station does not
stop the others
- API: /api/weather/meta, /stations (current values, 1 h/24 h rain,
24 h min/max, strongest gust), /stations/{abbr}/history
- Combined map: "Wetter (live)" layer with station badges (temperature,
24 h rain or wind), wind arrows, rain marker, station panel with
hourly 7-day chart, overview and municipality context entries
- Docs: docs/datasets/wetter.md, overview in docs/datasets.md,
CLAUDE.md; Docker and appsettings wiring
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
208 lines
6.7 KiB
TypeScript
208 lines
6.7 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
||
import {
|
||
BarController,
|
||
BarElement,
|
||
CategoryScale,
|
||
Chart as ChartJS,
|
||
Legend,
|
||
LinearScale,
|
||
LineController,
|
||
LineElement,
|
||
PointElement,
|
||
Tooltip,
|
||
type ChartData,
|
||
type ChartOptions,
|
||
} from 'chart.js';
|
||
import { useMemo } from 'react';
|
||
import { Chart } from 'react-chartjs-2';
|
||
import { fetchWeatherHistory, type WeatherStation } from '../../api/weather';
|
||
import { compassDirection, formatDecimal, formatTime, toHourly } from '../../weather/format';
|
||
|
||
ChartJS.register(
|
||
CategoryScale,
|
||
LinearScale,
|
||
BarController,
|
||
BarElement,
|
||
LineController,
|
||
LineElement,
|
||
PointElement,
|
||
Tooltip,
|
||
Legend,
|
||
);
|
||
|
||
type MixedType = 'bar' | 'line';
|
||
|
||
const HISTORY_DAYS = 7;
|
||
const TEMPERATURE_COLOR = '#eb6834';
|
||
const PRECIPITATION_COLOR = '#2a78d6';
|
||
const GRID_COLOR = '#e2e8f0';
|
||
|
||
interface WeatherStationPanelProps {
|
||
station: WeatherStation;
|
||
lastRefresh: string | null;
|
||
onClose: () => void;
|
||
}
|
||
|
||
export function WeatherStationPanel({ station, lastRefresh, onClose }: WeatherStationPanelProps) {
|
||
const historyQuery = useQuery({
|
||
queryKey: ['weather-history', station.abbr, lastRefresh],
|
||
queryFn: () => fetchWeatherHistory(station.abbr, HISTORY_DAYS),
|
||
staleTime: Infinity,
|
||
});
|
||
const hourly = useMemo(() => toHourly(historyQuery.data ?? []), [historyQuery.data]);
|
||
const hasTemperature = hourly.some((h) => h.temperature !== null);
|
||
const hasPrecipitation = hourly.some((h) => h.precipitation !== null);
|
||
|
||
const first = hourly[0]?.time;
|
||
const coversFullRange =
|
||
first !== undefined && Date.now() - first.getTime() >= (HISTORY_DAYS - 1) * 24 * 3_600_000;
|
||
const chartTitle =
|
||
coversFullRange || first === undefined
|
||
? `Verlauf der letzten ${HISTORY_DAYS} Tage (Stundenwerte)`
|
||
: `Verlauf seit ${first.toLocaleString('de-CH', { day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit' })}`;
|
||
|
||
const data: ChartData<MixedType, (number | null)[], string> = {
|
||
labels: hourly.map((h) =>
|
||
h.time.toLocaleString('de-CH', {
|
||
weekday: 'short',
|
||
day: '2-digit',
|
||
month: '2-digit',
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
}),
|
||
),
|
||
datasets: [
|
||
...(hasTemperature
|
||
? [
|
||
{
|
||
type: 'line' as const,
|
||
label: 'Temperatur (°C)',
|
||
data: hourly.map((h) => h.temperature),
|
||
borderColor: TEMPERATURE_COLOR,
|
||
backgroundColor: TEMPERATURE_COLOR,
|
||
borderWidth: 2,
|
||
spanGaps: true,
|
||
yAxisID: 'y',
|
||
},
|
||
]
|
||
: []),
|
||
...(hasPrecipitation
|
||
? [
|
||
{
|
||
type: 'bar' as const,
|
||
label: 'Niederschlag (mm/h)',
|
||
data: hourly.map((h) => h.precipitation),
|
||
backgroundColor: PRECIPITATION_COLOR,
|
||
yAxisID: 'y1',
|
||
},
|
||
]
|
||
: []),
|
||
],
|
||
};
|
||
|
||
const options: ChartOptions<MixedType> = {
|
||
responsive: true,
|
||
maintainAspectRatio: false,
|
||
animation: false,
|
||
interaction: { mode: 'index', intersect: false },
|
||
elements: { point: { radius: 0 }, line: { tension: 0.2 } },
|
||
plugins: { legend: { position: 'bottom', labels: { boxWidth: 10, boxHeight: 10 } } },
|
||
scales: {
|
||
x: { grid: { display: false }, ticks: { maxTicksLimit: 7, autoSkip: true } },
|
||
y: {
|
||
display: hasTemperature,
|
||
grid: { color: GRID_COLOR },
|
||
title: { display: true, text: '°C' },
|
||
},
|
||
y1: {
|
||
display: hasPrecipitation,
|
||
position: 'right',
|
||
beginAtZero: true,
|
||
grid: { display: false },
|
||
title: { display: true, text: 'mm/h' },
|
||
},
|
||
},
|
||
};
|
||
|
||
return (
|
||
<aside className="station-panel">
|
||
<header className="station-panel-header">
|
||
<div>
|
||
<h2>{station.name}</h2>
|
||
<p>
|
||
Wetterstation {station.abbr} · {station.heightMasl ?? '–'} m ü. M. · Stand{' '}
|
||
{formatTime(station.time)}
|
||
</p>
|
||
</div>
|
||
<button type="button" className="close-button" onClick={onClose} aria-label="Schliessen">
|
||
×
|
||
</button>
|
||
</header>
|
||
|
||
<section className="stat-tiles">
|
||
<div className="stat-tile">
|
||
<span className="stat-tile-value">
|
||
{station.temperature === null ? '–' : `${formatDecimal(station.temperature, 1)} °C`}
|
||
</span>
|
||
<span className="stat-tile-label">
|
||
Lufttemperatur
|
||
{station.temperatureMin24h !== null &&
|
||
` · 24 h: ${formatDecimal(station.temperatureMin24h, 1)} bis ${formatDecimal(station.temperatureMax24h, 1)} °C`}
|
||
</span>
|
||
</div>
|
||
<div className="stat-tile">
|
||
<span className="stat-tile-value">
|
||
{station.precipitation24h === null
|
||
? '–'
|
||
: `${formatDecimal(station.precipitation24h, 1)} mm`}
|
||
</span>
|
||
<span className="stat-tile-label">
|
||
Niederschlag 24 h
|
||
{station.precipitation1h !== null &&
|
||
` · letzte Stunde ${formatDecimal(station.precipitation1h, 1)} mm`}
|
||
</span>
|
||
</div>
|
||
<div className="stat-tile">
|
||
<span className="stat-tile-value">
|
||
{station.windSpeed === null ? '–' : `${Math.round(station.windSpeed)} km/h`}
|
||
</span>
|
||
<span className="stat-tile-label">
|
||
Wind aus {compassDirection(station.windDirection)}
|
||
{station.windGust !== null && ` · Böen ${Math.round(station.windGust)} km/h`}
|
||
{station.windGustMax24h !== null &&
|
||
` · max. ${Math.round(station.windGustMax24h)} in 24 h`}
|
||
</span>
|
||
</div>
|
||
<div className="stat-tile">
|
||
<span className="stat-tile-value">
|
||
{station.sunshine24h === null ? '–' : `${Math.round(station.sunshine24h)} min`}
|
||
</span>
|
||
<span className="stat-tile-label">
|
||
Sonnenschein 24 h
|
||
{station.humidity !== null && ` · Feuchte ${Math.round(station.humidity)} %`}
|
||
{station.pressure !== null && ` · ${formatDecimal(station.pressure, 0)} hPa`}
|
||
</span>
|
||
</div>
|
||
</section>
|
||
|
||
<section>
|
||
<h3>{chartTitle}</h3>
|
||
{hourly.length > 1 ? (
|
||
<div className="chart">
|
||
<Chart<MixedType, (number | null)[], string> type="bar" options={options} data={data} />
|
||
</div>
|
||
) : (
|
||
<p className="panel-note">Noch zu wenige Messwerte für einen Verlauf.</p>
|
||
)}
|
||
{!hasTemperature && (
|
||
<p className="panel-note">
|
||
Diese Station misst keine Temperatur und keinen Niederschlag.
|
||
</p>
|
||
)}
|
||
<p className="panel-note">
|
||
Zehnminutenwerte von MeteoSchweiz (SwissMetNet), im Diagramm zu Stunden zusammengefasst.
|
||
</p>
|
||
</section>
|
||
</aside>
|
||
);
|
||
}
|