ASP.NET Core 10 API and React client that visualize the canton of Thurgau's hourly traffic counts by vehicle class (data.tg.ch, CC0). - Background refresh from data.tg.ch every 6 h into a per-day JSON cache, so history grows beyond the source's two-week window - Road geometry per station direction from swisstopo swissTLM3D, oriented by geocoding the direction's target town; roundabout arms follow the road outward only - Leaflet map with canvas animation: dots drive along the road in both directions, density by hourly count, colour by vehicle class, right-hand traffic; play/pause, time slider and day selection - Station panel with per-class counts and Chart.js charts (hourly, weekday/weekend profile, daily totals, class mix) - swisstopo base map switcher (grey, colour, aerial) - Dockerfile and docker-compose with a data volume - xUnit tests for mapping, cache, geometry, geocoding and analysis Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Schaad.OpenData.Traffic;
|
|
|
|
public sealed class TrafficRefreshService(
|
|
TrafficRefresher refresher,
|
|
IOptions<TrafficOptions> options,
|
|
ILogger<TrafficRefreshService> logger) : BackgroundService
|
|
{
|
|
private static readonly TimeSpan CheckInterval = TimeSpan.FromMinutes(15);
|
|
|
|
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 traffic cache");
|
|
}
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
if (refresher.IsRefreshDue(options.Value.RefreshInterval))
|
|
{
|
|
await refresher.RefreshAsync(stoppingToken);
|
|
}
|
|
|
|
await refresher.BuildMissingGeometryAsync(stoppingToken);
|
|
}
|
|
catch (Exception exception) when (exception is not OperationCanceledException)
|
|
{
|
|
logger.LogError(exception, "Traffic refresh failed");
|
|
}
|
|
|
|
await Task.Delay(CheckInterval, stoppingToken);
|
|
}
|
|
}
|
|
}
|