OpenDataVisualizer/Schaad.OpenData.Weather/WeatherRefreshService.cs
Claudio Schaad 05f4fa78f6 Add live weather layer from MeteoSwiss stations
- 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>
2026-11-19 06:04:15 +01:00

42 lines
1.3 KiB
C#

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Schaad.OpenData.Weather;
public sealed class WeatherRefreshService(
WeatherRefresher refresher,
IOptions<WeatherOptions> options,
ILogger<WeatherRefreshService> logger) : BackgroundService
{
private static readonly TimeSpan CheckInterval = TimeSpan.FromMinutes(1);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
await refresher.LoadCacheAsync(stoppingToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Could not load the weather cache");
}
while (!stoppingToken.IsCancellationRequested)
{
try
{
if (refresher.IsRefreshDue(options.Value.RefreshInterval))
{
await refresher.RefreshAsync(stoppingToken);
}
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Weather refresh failed");
}
await Task.Delay(CheckInterval, stoppingToken);
}
}
}