aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Backend/Services/Providers/RenfeTransitProvider.cs
blob: 1793adaf61c3c0a3d3e111ad33c8b5f18bb49426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Costasdev.Busurbano.Backend.Configuration;
using Costasdev.Busurbano.Backend.Extensions;
using Costasdev.Busurbano.Backend.Types;
using Microsoft.Extensions.Options;
using SysFile = System.IO.File;

namespace Costasdev.Busurbano.Backend.Services.Providers;

[Obsolete]
public class RenfeTransitProvider : ITransitProvider
{
    private readonly AppConfiguration _configuration;
    private readonly ILogger<RenfeTransitProvider> _logger;

    public RenfeTransitProvider(IOptions<AppConfiguration> options, ILogger<RenfeTransitProvider> logger)
    {
        _configuration = options.Value;
        _logger = logger;
    }

    public async Task<List<ConsolidatedCirculation>> GetCirculationsAsync(string stopId, DateTime nowLocal)
    {
        var todayDate = nowLocal.Date.ToString("yyyy-MM-dd");
        var stopArrivals = await LoadStopArrivalsProto(stopId, todayDate);

        if (stopArrivals == null)
        {
            return [];
        }

        var now = nowLocal.AddSeconds(60 - nowLocal.Second);
        var scopeEnd = now.AddMinutes(8 * 60);

        var scheduledWindow = stopArrivals.Arrivals
            .Where(c => c.CallingDateTime(nowLocal.Date) != null)
            .Where(c => c.CallingDateTime(nowLocal.Date)!.Value >= now && c.CallingDateTime(nowLocal.Date)!.Value <= scopeEnd)
            .OrderBy(c => c.CallingDateTime(nowLocal.Date)!.Value);

        var consolidatedCirculations = new List<ConsolidatedCirculation>();

        foreach (var sched in scheduledWindow)
        {
            var minutes = (int)(sched.CallingDateTime(nowLocal.Date)!.Value - now).TotalMinutes;

            consolidatedCirculations.Add(new ConsolidatedCirculation
            {
                Line = sched.Line,
                Route = sched.Route,
                Schedule = new ScheduleData
                {
                    Running = sched.StartingDateTime(nowLocal.Date)!.Value <= now,
                    Minutes = minutes,
                    TripId = sched.ServiceId[(sched.ServiceId.Length - 6)..(sched.ServiceId.Length - 1)],
                    ServiceId = sched.ServiceId[(sched.ServiceId.Length - 6)..(sched.ServiceId.Length - 1)],
                    ShapeId = sched.ShapeId,
                },
                RealTime = null,
                NextStreets = [.. sched.NextStreets]
            });
        }

        return consolidatedCirculations;
    }

    private async Task<StopArrivals?> LoadStopArrivalsProto(string stopId, string dateString)
    {
        var file = Path.Combine(_configuration.RenfeScheduleBasePath, dateString, stopId + ".pb");
        if (!SysFile.Exists(file))
        {
            _logger.LogWarning("Stop arrivals proto file not found: {File}", file);
            return null;
        }

        var contents = await SysFile.ReadAllBytesAsync(file);
        var stopArrivals = StopArrivals.Parser.ParseFrom(contents);
        return stopArrivals;
    }
}