blob: 2c3478424721c10401aa24b454e654faaf583fd3 (
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
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
|
using System.Globalization;
using System.Text.Json.Serialization;
namespace Costasdev.Busurbano.Backend.GraphClient.App;
public class ArrivalsAtStopContent : IGraphRequest<ArrivalsAtStopContent.Args>
{
public const int PastArrivalMinutesIncluded = -15;
public record Args(string Id, int DepartureCount, bool PastArrivals);
public static string Query(Args args)
{
var startTime = DateTimeOffset.Now;
if (args.PastArrivals)
{
startTime = DateTimeOffset.Now.AddMinutes(PastArrivalMinutesIncluded);
}
var startTimeUnix = startTime.ToUnixTimeSeconds();
return string.Create(CultureInfo.InvariantCulture, $@"
query Query {{
stop(id:""{args.Id}"") {{
code
name
arrivals: stoptimesWithoutPatterns(numberOfDepartures:{args.DepartureCount}, startTime: {startTimeUnix}) {{
headsign
scheduledDeparture
pickupType
trip {{
gtfsId
serviceId
routeShortName
route {{
color
textColor
}}
departureStoptime {{
scheduledDeparture
}}
}}
}}
}}
}}
");
}
}
public class ArrivalsAtStopResponse : AbstractGraphResponse
{
[JsonPropertyName("stop")] public required StopItem Stop { get; set; }
public class StopItem
{
[JsonPropertyName("code")] public required string Code { get; set; }
[JsonPropertyName("name")] public required string Name { get; set; }
[JsonPropertyName("arrivals")] public List<Arrival> Arrivals { get; set; } = [];
}
public class Arrival
{
[JsonPropertyName("headsign")] public required string Headsign { get; set; }
[JsonPropertyName("scheduledDeparture")]
public int ScheduledDepartureSeconds { get; set; }
[JsonPropertyName("pickupType")] public required string PickupTypeOriginal { get; set; }
public PickupType PickupTypeParsed => PickupTypeParsed.Parse(PickupTypeOriginal);
[JsonPropertyName("trip")] public required TripDetails Trip { get; set; }
}
public class TripDetails
{
[JsonPropertyName("gtfsId")] public required string GtfsId { get; set; }
[JsonPropertyName("serviceId")] public required string ServiceId { get; set; }
[JsonPropertyName("routeShortName")] public required string RouteShortName { get; set; }
[JsonPropertyName("departureStoptime")]
public required DepartureStoptime DepartureStoptime { get; set; }
[JsonPropertyName("route")] public required RouteDetails Route { get; set; }
}
public class DepartureStoptime
{
[JsonPropertyName("scheduledDeparture")]
public int ScheduledDeparture { get; set; }
}
public class RouteDetails
{
[JsonPropertyName("color")] public string? Color { get; set; }
[JsonPropertyName("textColor")] public string? TextColor { get; set; }
}
public class PickupType
{
private readonly string _value;
private PickupType(string value)
{
_value = value;
}
public PickupType Parse(string value)
{
return value switch
{
"SCHEDULED" => Scheduled,
"NONE" => None,
"CALL_AGENCY" => CallAgency,
"COORDINATE_WITH_DRIVER" => CoordinateWithDriver,
_ => throw new ArgumentException("Unsupported pickup type ", value)
};
}
public static readonly PickupType Scheduled = new PickupType("SCHEDULED");
public static readonly PickupType None = new PickupType("NONE");
public static readonly PickupType CallAgency = new PickupType("CALL_AGENCY");
public static readonly PickupType CoordinateWithDriver = new PickupType("COORDINATE_WITH_DRIVER");
public override bool Equals(object? other)
{
if (other is not PickupType otherPt)
{
return false;
}
return otherPt._value == _value;
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
}
}
|