- 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>
88 lines
3.4 KiB
C#
88 lines
3.4 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Options;
|
|
using Schaad.OpenData.Weather.Models;
|
|
|
|
namespace Schaad.OpenData.Weather.Storage;
|
|
|
|
public class WeatherCacheStorage(IOptions<WeatherOptions> options)
|
|
{
|
|
private const string DayFormat = "yyyy-MM-dd";
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
|
|
|
|
private string RootPath => Path.Combine(Path.GetFullPath(options.Value.DataPath), "weather", "thurgau");
|
|
|
|
private string DaysPath => Path.Combine(RootPath, "days");
|
|
|
|
public virtual async Task<WeatherSnapshot> LoadAsync(CancellationToken cancellationToken)
|
|
{
|
|
var meta = await ReadAsync<WeatherCacheMeta>(Path.Combine(RootPath, "meta.json"), cancellationToken);
|
|
var stations = await ReadAsync<List<WeatherStation>>(Path.Combine(RootPath, "stations.json"), cancellationToken) ?? [];
|
|
var readings = new List<WeatherReading>();
|
|
if (Directory.Exists(DaysPath))
|
|
{
|
|
foreach (var file in Directory.EnumerateFiles(DaysPath, "*.json").Order(StringComparer.Ordinal))
|
|
{
|
|
readings.AddRange(await ReadAsync<List<WeatherReading>>(file, cancellationToken) ?? []);
|
|
}
|
|
}
|
|
|
|
return new WeatherSnapshot(stations, readings, meta?.LastRefresh, meta?.StationsFetched);
|
|
}
|
|
|
|
public virtual async Task SaveAsync(WeatherSnapshot snapshot, IReadOnlyCollection<DateOnly> changedDays, CancellationToken cancellationToken)
|
|
{
|
|
var byDay = snapshot.Readings.ToLookup(r => DateOnly.FromDateTime(r.Time.UtcDateTime));
|
|
foreach (var day in changedDays)
|
|
{
|
|
await WriteAsync(DayPath(day), byDay[day].ToList(), cancellationToken);
|
|
}
|
|
|
|
await WriteAsync(Path.Combine(RootPath, "stations.json"), snapshot.Stations, cancellationToken);
|
|
await WriteAsync(Path.Combine(RootPath, "meta.json"), new WeatherCacheMeta(snapshot.LastRefresh, snapshot.StationsFetched), cancellationToken);
|
|
}
|
|
|
|
public virtual void DeleteDaysBefore(DateOnly firstKeptDay)
|
|
{
|
|
if (!Directory.Exists(DaysPath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var file in Directory.EnumerateFiles(DaysPath, "*.json"))
|
|
{
|
|
if (DateOnly.TryParseExact(Path.GetFileNameWithoutExtension(file), DayFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var day)
|
|
&& day < firstKeptDay)
|
|
{
|
|
File.Delete(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
private string DayPath(DateOnly day) =>
|
|
Path.Combine(DaysPath, day.ToString(DayFormat, CultureInfo.InvariantCulture) + ".json");
|
|
|
|
private static async Task<T?> ReadAsync<T>(string path, CancellationToken cancellationToken)
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
return default;
|
|
}
|
|
|
|
await using var stream = File.OpenRead(path);
|
|
return await JsonSerializer.DeserializeAsync<T>(stream, JsonOptions, cancellationToken);
|
|
}
|
|
|
|
private static async Task WriteAsync<T>(string path, T value, CancellationToken cancellationToken)
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
var tempPath = path + ".tmp";
|
|
await using (var stream = File.Create(tempPath))
|
|
{
|
|
await JsonSerializer.SerializeAsync(stream, value, JsonOptions, cancellationToken);
|
|
}
|
|
|
|
File.Move(tempPath, path, true);
|
|
}
|
|
}
|