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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
using System.Globalization;
using System.Net.Http.Json;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using NodaTime;
using TransitRealtime;
namespace Enmarcha.Sources.GtfsRealtime;
public partial class GtfsRealtimeEstimatesProvider
{
private HttpClient _http;
private ILogger<GtfsRealtimeEstimatesProvider> _logger;
[GeneratedRegex("^(?<tripId>[0-9]{5})[0-9](?<date>[0-9]{4}-[0-9]{2}-[0-9]{2})$")]
private static partial Regex TripInformationExpression { get; }
public GtfsRealtimeEstimatesProvider(HttpClient http, ILogger<GtfsRealtimeEstimatesProvider> logger)
{
_http = http;
_logger = logger;
}
public async Task<FeedMessage> DownloadFeed(string url)
{
var response = await _http.GetAsync(url);
var body = await response.Content.ReadAsByteArrayAsync();
return FeedMessage.Parser.ParseFrom(body);
}
public async Task<Dictionary<string, int?>> GetRenfeDelays()
{
const string url = "https://gtfsrt.renfe.com/trip_updates_LD.pb";
var feed = await DownloadFeed(url);
var offsetInMadrid = DateTimeZoneProviders.Tzdb["Europe/Madrid"];
var expectedDate = SystemClock.Instance
.GetCurrentInstant()
.InZone(offsetInMadrid).Date
.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
Dictionary<string, int?> delays = new();
foreach (var entity in feed.Entity)
{
if (entity.TripUpdate is null)
{
_logger.LogWarning("Entity {entityId} entity.Id has no trip updates", entity.Id);
continue;
}
if (!entity.TripUpdate.Trip.HasTripId)
{
continue;
}
var tripId = entity.TripUpdate.Trip.TripId!;
var idMatch = TripInformationExpression.Match(tripId);
var trainNumber = idMatch.Groups["tripId"].Value;
if (!idMatch.Success)
{
_logger.LogWarning("Unable to match {tripId} ({entityId}) into trip ID and date",
tripId, entity.Id);
continue;
}
// TODO: Revise this, since apparently some trips appear with the previous day
// if (expectedDate != idMatch.Groups["date"].Value)
// {
// _logger.LogDebug("Entity {entityId} has trip ID {tripId} which is not for today",
// entity.Id, tripId);
// continue;
// }
if (entity.TripUpdate.Trip.HasScheduleRelationship &&
entity.TripUpdate.Trip.ScheduleRelationship == TripDescriptor.Types.ScheduleRelationship.Canceled
)
{
delays.TryAdd(trainNumber, null);
continue;
}
if (!entity.TripUpdate.HasDelay)
{
_logger.LogDebug("Trip {tripId} ({entityId}) has no delay information, and is not cancelled", tripId,
entity.Id);
continue;
}
delays.TryAdd(trainNumber, entity.TripUpdate.Delay);
}
return delays;
}
public async Task<Dictionary<string, Coordinates>> GetRenfePositions()
{
const string url = "https://gtfsrt.renfe.com/vehicle_positions_LD.pb";
var feed = await DownloadFeed(url);
var offsetInMadrid = DateTimeZoneProviders.Tzdb["Europe/Madrid"];
var expectedDate = SystemClock.Instance
.GetCurrentInstant()
.InZone(offsetInMadrid).Date
.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
Dictionary<string, Coordinates> positions = new();
foreach (var entity in feed.Entity)
{
if (entity.Vehicle?.Position is null)
{
_logger.LogWarning("Entity {entityId} entity.Id has no vehicle information", entity.Id);
continue;
}
if (!entity.Vehicle.Trip.HasTripId)
{
continue;
}
var tripId = entity.Vehicle.Trip.TripId!;
var idMatch = TripInformationExpression.Match(tripId);
var trainNumber = idMatch.Groups["tripId"].Value;
if (!idMatch.Success)
{
_logger.LogWarning("Unable to match {tripId} ({entityId}) into trip ID and date",
tripId, entity.Id);
continue;
}
// TODO: Revise this, since apparently some trips appear with the previous day
// if (expectedDate != idMatch.Groups["date"].Value)
// {
// _logger.LogDebug("Entity {entityId} has trip ID {tripId} which is not for today",
// entity.Id, tripId);
// continue;
// }
positions.TryAdd(trainNumber, new Coordinates
{
Latitude = entity.Vehicle.Position.Latitude,
Longitude = entity.Vehicle.Position.Longitude
});
}
return positions;
}
}
public class Coordinates
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
|