aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2025-12-26 15:56:09 +0100
committerAriel Costas Guerrero <ariel@costas.dev>2025-12-26 15:56:09 +0100
commitef2df90ffb195edcddd701511dc5953c7baa63af (patch)
tree68ab850068e686647beccec8036e6905ecbab242 /src/Costasdev.Busurbano.Sources.OpenTripPlannerGql
parent70b5788269845bbf368af5b13b495c70a08927f2 (diff)
Move OpenTripPlanner source to separate package
Diffstat (limited to 'src/Costasdev.Busurbano.Sources.OpenTripPlannerGql')
-rw-r--r--src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Costasdev.Busurbano.Sources.OpenTripPlannerGql.csproj13
-rw-r--r--src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/OpenTripPlannerClient.cs47
-rw-r--r--src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/ArrivalsAtStop.cs199
-rw-r--r--src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/StopTile.cs76
-rw-r--r--src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/ResponseTypes.cs36
5 files changed, 371 insertions, 0 deletions
diff --git a/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Costasdev.Busurbano.Sources.OpenTripPlannerGql.csproj b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Costasdev.Busurbano.Sources.OpenTripPlannerGql.csproj
new file mode 100644
index 0000000..6d78b55
--- /dev/null
+++ b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Costasdev.Busurbano.Sources.OpenTripPlannerGql.csproj
@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net10.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.Extensions.Logging" />
+ </ItemGroup>
+
+</Project>
diff --git a/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/OpenTripPlannerClient.cs b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/OpenTripPlannerClient.cs
new file mode 100644
index 0000000..eed78d6
--- /dev/null
+++ b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/OpenTripPlannerClient.cs
@@ -0,0 +1,47 @@
+using System.Net.Http.Json;
+using Costasdev.Busurbano.Sources.OpenTripPlannerGql.Queries;
+using Microsoft.Extensions.Logging;
+
+namespace Costasdev.Busurbano.Sources.OpenTripPlannerGql;
+
+public class OpenTripPlannerClient
+{
+ private readonly HttpClient _httpClient;
+ private readonly string _baseUrl;
+ private readonly ILogger<OpenTripPlannerClient> _logger;
+
+ public OpenTripPlannerClient(
+ HttpClient httpClient,
+ string baseUrl,
+ ILogger<OpenTripPlannerClient> logger
+ )
+ {
+ _httpClient = httpClient;
+ _baseUrl = baseUrl;
+ _logger = logger;
+ }
+
+ public async Task GetStopsInBbox(double minLat, double minLon, double maxLat, double maxLon)
+ {
+ var requestContent =
+ StopTileRequestContent.Query(new StopTileRequestContent.Bbox(minLon, minLat, maxLon, maxLat));
+
+ var request = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/gtfs/v1");
+ request.Content = JsonContent.Create(new GraphClientRequest
+ {
+ Query = requestContent
+ });
+
+ var response = await _httpClient.SendAsync(request);
+ var responseBody = await response.Content.ReadFromJsonAsync<GraphClientResponse<StopTileResponse>>();
+
+ if (responseBody is not { IsSuccess: true })
+ {
+ _logger.LogError(
+ "Error fetching stop data, received {StatusCode} {ResponseBody}",
+ response.StatusCode,
+ await response.Content.ReadAsStringAsync()
+ );
+ }
+ }
+}
diff --git a/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/ArrivalsAtStop.cs b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/ArrivalsAtStop.cs
new file mode 100644
index 0000000..bbf2c08
--- /dev/null
+++ b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/ArrivalsAtStop.cs
@@ -0,0 +1,199 @@
+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
+ }}
+ {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("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 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();
+ }
+ }
+}
diff --git a/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/StopTile.cs b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/StopTile.cs
new file mode 100644
index 0000000..792d19e
--- /dev/null
+++ b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/Queries/StopTile.cs
@@ -0,0 +1,76 @@
+using System.Globalization;
+using System.Text.Json.Serialization;
+
+namespace Costasdev.Busurbano.Sources.OpenTripPlannerGql.Queries;
+
+public class StopTileRequestContent : IGraphRequest<StopTileRequestContent.Bbox>
+{
+ public record Bbox(double MinLon, double MinLat, double MaxLon, double MaxLat);
+
+ public static string Query(Bbox bbox)
+ {
+ return string.Create(CultureInfo.InvariantCulture, $@"
+ query Query {{
+ stopsByBbox(
+ minLat: {bbox.MinLat:F6}
+ minLon: {bbox.MinLon:F6}
+ maxLon: {bbox.MaxLon:F6}
+ maxLat: {bbox.MaxLat:F6}
+ ) {{
+ gtfsId
+ code
+ name
+ lat
+ lon
+ routes {{
+ gtfsId
+ shortName
+ color
+ textColor
+ }}
+ }}
+ }}
+ ");
+ }
+}
+
+public class StopTileResponse : AbstractGraphResponse
+{
+ [JsonPropertyName("stopsByBbox")]
+ public List<Stop>? StopsByBbox { get; set; }
+
+ public record Stop
+ {
+ [JsonPropertyName("gtfsId")]
+ public required string GtfsId { get; set; }
+
+ [JsonPropertyName("code")]
+ public string? Code { get; set; }
+
+ [JsonPropertyName("name")]
+ public required string Name { get; set; }
+
+ [JsonPropertyName("lat")]
+ public required double Lat { get; set; }
+
+ [JsonPropertyName("lon")]
+ public required double Lon { get; set; }
+
+ [JsonPropertyName("routes")]
+ public List<Route>? Routes { get; set; }
+ }
+
+ public record Route
+ {
+ [JsonPropertyName("gtfsId")]
+ public required string GtfsId { get; set; }
+ [JsonPropertyName("shortName")]
+ public required string ShortName { get; set; }
+
+ [JsonPropertyName("color")]
+ public string? Color { get; set; }
+
+ [JsonPropertyName("textColor")]
+ public string? TextColor { get; set; }
+ }
+}
diff --git a/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/ResponseTypes.cs b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/ResponseTypes.cs
new file mode 100644
index 0000000..237537f
--- /dev/null
+++ b/src/Costasdev.Busurbano.Sources.OpenTripPlannerGql/ResponseTypes.cs
@@ -0,0 +1,36 @@
+using System.Text.Json.Serialization;
+
+namespace Costasdev.Busurbano.Sources.OpenTripPlannerGql;
+
+public class GraphClientRequest
+{
+ public string OperationName { get; set; } = "Query";
+ public required string Query { get; set; }
+}
+
+public class GraphClientResponse<T> where T : AbstractGraphResponse
+{
+ [JsonPropertyName("data")]
+ public T? Data { get; set; }
+
+ [JsonPropertyName("errors")]
+ public List<GraphClientError>? Errors { get; set; }
+
+ public bool IsSuccess => Errors == null || Errors.Count == 0;
+}
+
+public interface IGraphRequest<T>
+{
+ static abstract string Query(T parameters);
+}
+
+public class AbstractGraphResponse
+{
+}
+
+public class GraphClientError
+{
+ [JsonPropertyName("message")]
+ public required string Message { get; set; }
+}
+