aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/ArrivalsAtStop.cs
blob: bce35a21fe31021db2c01173a3bf5eeee67a26ba (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.Globalization;
using System.Text.Json.Serialization;
using Costasdev.Busurbano.Sources.OpenTripPlannerGql;

namespace Costasdev.Busurbano.Sources.OpenTripPlannerGql.Queries;

public class ArrivalsAtStopContent : IGraphRequest<ArrivalsAtStopContent.Args>
{
    public record Args(string Id, bool Reduced);

    public static string Query(Args args)
    {
        var startTime = DateTimeOffset.UtcNow.AddMinutes(-75);
        var startTimeUnix = startTime.ToUnixTimeSeconds();
        var geometryField = args.Reduced ? "" : @"tripGeometry { points }";

        return string.Create(CultureInfo.InvariantCulture, $@"
        query Query {{
            stop(id:""{args.Id}"") {{
                code
                name
                lat
                lon
                routes {{
                    gtfsId
                    shortName
                    color
                    textColor
                }}
                arrivals: stoptimesWithoutPatterns(numberOfDepartures: 100, startTime: {startTimeUnix}, timeRange: 14400) {{
                    headsign
                    scheduledDeparture
                    serviceDay
                    pickupType

                    trip {{
                        gtfsId
                        serviceId
                        routeShortName
                        route {{
                            gtfsId
                            color
                            textColor
                            longName
                        }}
                        departureStoptime {{
                            scheduledDeparture
                        }}
                        arrivalStoptime {{
                            stop {{
                                gtfsId
                            }}
                        }}
                        {geometryField}
                        stoptimes {{
                            stop {{
                                name
                                lat
                                lon
                            }}
                            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("lat")] public double Lat { get; set; }

        [JsonPropertyName("lon")] public double Lon { get; set; }

        [JsonPropertyName("routes")] public List<RouteDetails> Routes { 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("serviceDay")]
        public long ServiceDay { get; set; }

        [JsonPropertyName("pickupType")] public required string PickupTypeOriginal { get; set; }

        public PickupType PickupTypeParsed => PickupType.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("arrivalStoptime")]
        public required ArrivalStoptime ArrivalStoptime { get; set; }

        [JsonPropertyName("route")] public required RouteDetails Route { get; set; }

        [JsonPropertyName("tripGeometry")] public GeometryDetails? Geometry { get; set; }

        [JsonPropertyName("stoptimes")] public List<StoptimeDetails> Stoptimes { get; set; } = [];
    }

    public class GeometryDetails
    {
        [JsonPropertyName("points")] public string? Points { get; set; }
    }

    public class StoptimeDetails
    {
        [JsonPropertyName("stop")] public required StopDetails Stop { get; set; }
        [JsonPropertyName("scheduledDeparture")] public int ScheduledDeparture { get; set; }
    }

    public class StopDetails
    {
        [JsonPropertyName("name")] public required string Name { get; set; }
        [JsonPropertyName("lat")] public double Lat { get; set; }
        [JsonPropertyName("lon")] public double Lon { get; set; }
    }

    public class DepartureStoptime
    {
        [JsonPropertyName("scheduledDeparture")]
        public int ScheduledDeparture { get; set; }
    }

    public class ArrivalStoptime
    {
        [JsonPropertyName("stop")] public ArrivalStoptimeStop Stop { get; set; }
    }

    public class ArrivalStoptimeStop
    {
        [JsonPropertyName("gtfsId")] public required string GtfsId { get; set; }
    }

    public class RouteDetails
    {
        [JsonPropertyName("gtfsId")] public required string GtfsId { get; set; }
        public string GtfsIdValue => GtfsId.Split(':', 2)[1];

        [JsonPropertyName("shortName")] public string? ShortName { get; set; }

        [JsonPropertyName("color")] public string? Color { get; set; }

        [JsonPropertyName("textColor")] public string? TextColor { get; set; }

        [JsonPropertyName("longName")] public string? LongName { get; set; }
    }

    public class PickupType
    {
        private readonly string _value;

        private PickupType(string value)
        {
            _value = value;
        }

        public static 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();
        }
    }
}