- Thurgau hourly traffic counts (data.tg.ch) refreshed every 6 h into a per-day JSON cache - Road geometry per station direction from swisstopo swissTLM3D; roundabout arms run outward from the roundabout centre - Leaflet map with animated vehicles by class, time controls and base map switcher (grey, colour, aerial) - Station panel with per-class counts and Chart.js charts - Start page with map tiles; traffic map at /verkehr/thurgau - Dockerfile and docker-compose, xUnit tests 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>
74 lines
3.2 KiB
C#
74 lines
3.2 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Options;
|
|
using Schaad.OpenData.Traffic.Models;
|
|
|
|
namespace Schaad.OpenData.Traffic.Storage;
|
|
|
|
public class TrafficCacheStorage(IOptions<TrafficOptions> options)
|
|
{
|
|
private const string DayFormat = "yyyy-MM-dd";
|
|
private const string GeometryFileName = "geometry-v7.json";
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web) { WriteIndented = true };
|
|
|
|
private string RootPath => Path.Combine(Path.GetFullPath(options.Value.DataPath), "traffic", "thurgau");
|
|
|
|
private string CountsPath => Path.Combine(RootPath, "counts");
|
|
|
|
public virtual async Task<TrafficSnapshot> LoadAsync(CancellationToken cancellationToken)
|
|
{
|
|
var stations = await ReadAsync<List<Station>>(Path.Combine(RootPath, "stations.json"), cancellationToken) ?? [];
|
|
var geometry = await ReadAsync<List<StationGeometry>>(Path.Combine(RootPath, GeometryFileName), cancellationToken) ?? [];
|
|
var meta = await ReadAsync<TrafficCacheMeta>(Path.Combine(RootPath, "meta.json"), cancellationToken);
|
|
|
|
var counts = new List<HourlyCount>();
|
|
if (Directory.Exists(CountsPath))
|
|
{
|
|
foreach (var file in Directory.EnumerateFiles(CountsPath, "*.json").Order(StringComparer.Ordinal))
|
|
{
|
|
counts.AddRange(await ReadAsync<List<HourlyCount>>(file, cancellationToken) ?? []);
|
|
}
|
|
}
|
|
|
|
return new TrafficSnapshot(stations, counts, geometry, meta?.LastRefresh);
|
|
}
|
|
|
|
public virtual async Task SaveAsync(TrafficSnapshot snapshot, IReadOnlyCollection<DateOnly> changedDays, CancellationToken cancellationToken)
|
|
{
|
|
await WriteAsync(Path.Combine(RootPath, "stations.json"), snapshot.Stations.Values.OrderBy(s => s.Code).ToList(), cancellationToken);
|
|
foreach (var day in changedDays)
|
|
{
|
|
var path = Path.Combine(CountsPath, day.ToString(DayFormat, CultureInfo.InvariantCulture) + ".json");
|
|
await WriteAsync(path, snapshot.CountsByDate.GetValueOrDefault(day).ToList(), cancellationToken);
|
|
}
|
|
|
|
await WriteAsync(Path.Combine(RootPath, "meta.json"), new TrafficCacheMeta(snapshot.LastRefresh), cancellationToken);
|
|
}
|
|
|
|
public virtual Task SaveGeometryAsync(TrafficSnapshot snapshot, CancellationToken cancellationToken) =>
|
|
WriteAsync(Path.Combine(RootPath, GeometryFileName), snapshot.Geometry.Values.OrderBy(g => g.StationCode).ToList(), cancellationToken);
|
|
|
|
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);
|
|
}
|
|
}
|