aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/PlanConnectionContent.cs
blob: b65af501d92a2cde279801d12112172637b22a87 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System.Globalization;
using System.Text.Json.Serialization;

namespace Costasdev.Busurbano.Sources.OpenTripPlannerGql.Queries;

#pragma warning disable CS8618

public class PlanConnectionContent : IGraphRequest<PlanConnectionContent.Args>
{
    public record Args(
        double StartLatitude,
        double StartLongitude,
        double EndLatitude,
        double EndLongitude,
        DateTimeOffset ReferenceTime,
        bool ReferenceIsArrival = false
    );

    public static string Query(Args args)
    {
        var madridTz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Madrid");

        // Treat incoming DateTime as Madrid local wall-clock time
        var localMadridTime =
            DateTime.SpecifyKind(args.ReferenceTime.UtcDateTime, DateTimeKind.Unspecified);

        var offset = madridTz.GetUtcOffset(localMadridTime);

        var dateTimeToUse = new DateTimeOffset(args.ReferenceTime.DateTime + offset, offset);

        var dateTimeParameter = args.ReferenceIsArrival ? $"latestArrival:\"{dateTimeToUse:O}\"" : $"earliestDeparture:\"{dateTimeToUse:O}\"";

        return string.Create(CultureInfo.InvariantCulture,
            $$"""
              query Query {
                planConnection(
                  first: 4
                  origin: {
                    location:{
                      coordinate:{
                        latitude:{{args.StartLatitude}}
                        longitude:{{args.StartLongitude}}
                      }
                    }
                  }
                  destination:{
                    location:{
                      coordinate:{
                        latitude:{{args.EndLatitude}}
                        longitude:{{args.EndLongitude}}
                      }
                    }
                  }
                  dateTime:{
                    {{dateTimeParameter}}
                  }
                ) {
                  edges {
                    node {
                      duration
                      start
                      end
                      walkTime
                      walkDistance
                      waitingTime
                      legs {
                        start  {
                          scheduledTime
                        }
                        end {
                          scheduledTime
                        }
                        mode
                        route {
                          gtfsId
                         	shortName
                          longName
                          agency {
                            name
                          }
                          color
                          textColor
                        }
                        from {
                          name
                          lat
                          lon
                          stop {
                            gtfsId
                            code
                            name
                            lat
                            lon
                            zoneId
                          }
                        }
                        to {
                          name
                          lat
                          lon
                          stop {
                            gtfsId
                            code
                            name
                            lat
                            lon
                            zoneId
                          }
                        }
                        stopCalls {
                          stopLocation {
                            ... on Stop {
                              gtfsId
                              code
                              name
                              lat
                              lon
                            }
                          }
                        }
                        legGeometry {
                          points
                        }
                        steps {
                          distance
                          relativeDirection
                          streetName
                          absoluteDirection
                          lat
                          lon
                        }
                        headsign
                        distance
                      }
                    }
                  }
                }
              }
              """);
    }
}

public class PlanConnectionResponse : AbstractGraphResponse
{
    public PlanConnectionItem PlanConnection { get; set; }

    public class PlanConnectionItem
    {
        [JsonPropertyName("edges")]
        public Edge[] Edges { get; set; }
    }

    public class Edge
    {
        [JsonPropertyName("node")]
        public Node Node { get; set; }
    }

    public class Node
    {
        [JsonPropertyName("duration")] public int DurationSeconds { get; set; }
        [JsonPropertyName("start")] public string Start8601 { get; set; }
        [JsonPropertyName("end")] public string End8601 { get; set; }
        [JsonPropertyName("walkTime")] public int WalkSeconds { get; set; }
        [JsonPropertyName("walkDistance")] public double WalkDistance { get; set; }
        [JsonPropertyName("waitingTime")] public int WaitingSeconds { get; set; }
        [JsonPropertyName("legs")] public Leg[] Legs { get; set; }
    }

    public class Leg
    {
        [JsonPropertyName("start")] public ScheduledTimeContainer Start { get; set; }
        [JsonPropertyName("end")] public ScheduledTimeContainer End { get; set; }
        [JsonPropertyName("mode")] public string Mode { get; set; } // TODO: Make enum, maybe
        [JsonPropertyName("route")] public TransitRoute? Route { get; set; }
        [JsonPropertyName("from")] public LegPosition From { get; set; }
        [JsonPropertyName("to")] public LegPosition To { get; set; }
        [JsonPropertyName("stopCalls")] public StopCall[] StopCalls { get; set; }
        [JsonPropertyName("legGeometry")] public LegGeometry LegGeometry { get; set; }
        [JsonPropertyName("steps")] public Step[] Steps { get; set; }
        [JsonPropertyName("headsign")] public string? Headsign { get; set; }
        [JsonPropertyName("distance")] public double Distance { get; set; }
    }

    public class TransitRoute
    {
        [JsonPropertyName("gtfsId")] public string GtfsId { get; set; }
        [JsonPropertyName("shortName")] public string ShortName { get; set; }
        [JsonPropertyName("longName")] public string LongName { get; set; }
        [JsonPropertyName("agency")] public AgencyNameContainer Agency { get; set; }
        [JsonPropertyName("color")] public string Color { get; set; }
        [JsonPropertyName("textColor")] public string TextColor { get; set; }
    }

    public class LegPosition
    {
        [JsonPropertyName("name")] public string Name { get; set; }
        [JsonPropertyName("lat")] public double Latitude { get; set; }
        [JsonPropertyName("lon")] public double Longitude { get; set; }
        [JsonPropertyName("stop")] public StopLocation Stop { get; set; }
    }

    public class ScheduledTimeContainer
    {
        [JsonPropertyName("scheduledTime")]
        public string ScheduledTime8601 { get; set; }
    }

    public class AgencyNameContainer
    {
        [JsonPropertyName("name")] public string Name { get; set; }
    }

    public class StopCall
    {
        [JsonPropertyName("stopLocation")]
        public StopLocation StopLocation { get; set; }
    }

    public class StopLocation
    {
        [JsonPropertyName("gtfsId")] public string GtfsId { get; set; }
        [JsonPropertyName("code")] public string Code { get; set; }
        [JsonPropertyName("name")] public string Name { get; set; }
        [JsonPropertyName("lat")] public double Latitude { get; set; }
        [JsonPropertyName("lon")] public double Longitude { get; set; }
        [JsonPropertyName("zoneId")] public string? ZoneId { get; set; }
    }

    public class Step
    {
        [JsonPropertyName("distance")] public double Distance { get; set; }
        [JsonPropertyName("relativeDirection")] public string RelativeDirection { get; set; }
        [JsonPropertyName("streetName")] public string StreetName { get; set; } // TODO: "sidewalk", "path" or actual street name
        [JsonPropertyName("absoluteDirection")] public string AbsoluteDirection { get; set; }
        [JsonPropertyName("lat")] public double Latitude { get; set; }
        [JsonPropertyName("lon")] public double Longitude { get; set; }
    }

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