Skip to content

Commit

Permalink
Fetch schedules.
Browse files Browse the repository at this point in the history
  • Loading branch information
angelinn committed Jul 19, 2024
1 parent 04b1ca1 commit 5af50bf
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 70 deletions.
10 changes: 10 additions & 0 deletions src/TramlineFive/SkgtService/Models/Json/Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace SkgtService.Models.Json
Expand All @@ -27,5 +28,14 @@ public class Line
{
public string Name { get; set; }
public TransportType Type { get; set; }
public string Color { get; set; }
[JsonProperty("ext_id")]
public string ExtId { get; set; }
public string Icon { get; set; }
public int IsWeekend { get; set; }
[JsonProperty("line_id")]
public int LineID { get; set; }


}
}
231 changes: 231 additions & 0 deletions src/TramlineFive/SkgtService/Models/Json/Schedule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SkgtService.Models.Json
{
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Details
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("route_id")]
public int RouteId { get; set; }

[JsonProperty("type")]
public int Type { get; set; }

[JsonProperty("is_active")]
public int IsActive { get; set; }

[JsonProperty("polyline")]
public string Polyline { get; set; }

[JsonProperty("description")]
public object Description { get; set; }

[JsonProperty("continious_pickup")]
public object ContiniousPickup { get; set; }

[JsonProperty("continious_drop_off")]
public object ContiniousDropOff { get; set; }
}

public class EndStop
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("ext_id")]
public string ExtId { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("code")]
public string Code { get; set; }

[JsonProperty("type")]
public int Type { get; set; }

[JsonProperty("is_active")]
public int IsActive { get; set; }

[JsonProperty("longitude")]
public string Longitude { get; set; }

[JsonProperty("latitude")]
public string Latitude { get; set; }

[JsonProperty("description")]
public object Description { get; set; }
}

public class LineResponse
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("ext_id")]
public string ExtId { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("is_active")]
public int IsActive { get; set; }

[JsonProperty("has_single_direction")]
public int HasSingleDirection { get; set; }

[JsonProperty("type")]
public int Type { get; set; }

[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }

[JsonProperty("updated_at")]
public DateTime UpdatedAt { get; set; }

[JsonProperty("tr_name")]
public string TrName { get; set; }

[JsonProperty("tr_icon")]
public string TrIcon { get; set; }

[JsonProperty("tr_color")]
public string TrColor { get; set; }
}

public class ScheduleResponse
{
[JsonProperty("line")]
public LineResponse Line { get; set; }

[JsonProperty("routes")]
public List<RouteResponse> Routes { get; set; }
}

public class RouteResponse
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("line_id")]
public int LineId { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("type")]
public int Type { get; set; }

[JsonProperty("ext_id")]
public string ExtId { get; set; }

[JsonProperty("route_ref")]
public int RouteRef { get; set; }

[JsonProperty("details")]
public Details Details { get; set; }

[JsonProperty("segments")]
public List<Segment> Segments { get; set; }
}

public class Segment
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("route_id")]
public int RouteId { get; set; }

[JsonProperty("sequence")]
public int Sequence { get; set; }

[JsonProperty("start_stop_id")]
public int StartStopId { get; set; }

[JsonProperty("end_stop_id")]
public int EndStopId { get; set; }

[JsonProperty("polyline")]
public string Polyline { get; set; }

[JsonProperty("length")]
public string Length { get; set; }

[JsonProperty("stop")]
public StopSchResponse Stop { get; set; }

[JsonProperty("end_stop")]
public EndStop EndStop { get; set; }

[JsonProperty("priority")]
public int? Priority { get; set; }
}

public class StopSchResponse
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("ext_id")]
public string ExtId { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("code")]
public string Code { get; set; }

[JsonProperty("type")]
public int Type { get; set; }

[JsonProperty("is_active")]
public int IsActive { get; set; }

[JsonProperty("longitude")]
public string Longitude { get; set; }

[JsonProperty("latitude")]
public string Latitude { get; set; }

[JsonProperty("description")]
public object Description { get; set; }

[JsonProperty("times")]
public List<TimeResponse> Times { get; set; }
}

public class TimeResponse
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("weekend")]
public int Weekend { get; set; }

[JsonProperty("time")]
public string Time { get; set; }

[JsonProperty("item_id")]
public int ItemId { get; set; }

[JsonProperty("route_id")]
public int RouteId { get; set; }

[JsonProperty("stop_id")]
public int StopId { get; set; }

[JsonProperty("secondary")]
public bool? Secondary { get; set; }
}


}
9 changes: 9 additions & 0 deletions src/TramlineFive/SkgtService/PublicTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using static System.Formats.Asn1.AsnWriter;
Expand All @@ -18,6 +19,7 @@ public class PublicTransport
private Dictionary<string, StopInformation> stopsHash = new();
private Dictionary<string, Dictionary<string, Line>> lines = new();
private Dictionary<string, List<Route>> routes = new();
private Dictionary<Line, ScheduleResponse> schedules = new();

private List<StopInformation> stops;

Expand All @@ -33,6 +35,7 @@ public List<StopInformation> Stops
}

public Dictionary<string, Dictionary<string, Line>> Lines => lines;
public Dictionary<Line, ScheduleResponse> Schedules => schedules;


public async Task LoadLinesAsync()
Expand Down Expand Up @@ -190,4 +193,10 @@ public Line FindStopsByLine(string line, string type)

throw new TramlineFiveException($"Could not find type {type}");
}

public async Task LoadSchedule(Line line)
{
ScheduleResponse schedule = await StopsLoader.GetSchedule(line);
schedules.Add(line, schedule);
}
}
49 changes: 30 additions & 19 deletions src/TramlineFive/SkgtService/StopsLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public static class StopsLoader
{
private const string STOPS_URL = "https://sofiatraffic.bg/bg/trip/getAllStops";
private const string LINES_URL = "https://sofiatraffic.bg/bg/trip/getLines";
private const string ROUTES_URL = "https://routes.sofiatraffic.bg/resources/routes.json";
//private const string ROUTES_URL = "https://routes.sofiatraffic.bg/resources/routes.json";
private const string GET_SCHEDULE_URL = "https://sofiatraffic.bg/bg/trip/getSchedule";
private static string PATH = String.Empty;
private static string ROUTES_PATH = String.Empty;
private static string LINES_PATH = String.Empty;
Expand Down Expand Up @@ -53,18 +54,18 @@ public static async Task<List<StopLocation>> LoadStopsAsync()

}

public static async Task<List<Routes>> LoadRoutesAsync()
{
if (!File.Exists(ROUTES_PATH))
{
await UpdateRoutesAsync();
}
//public static async Task<List<Routes>> LoadRoutesAsync()
//{
// if (!File.Exists(ROUTES_PATH))
// {
// await UpdateRoutesAsync();
// }

string json = File.ReadAllText(ROUTES_PATH);
List<Routes> routes = JsonConvert.DeserializeObject<List<Routes>>(json);
// string json = File.ReadAllText(ROUTES_PATH);
// List<Routes> routes = JsonConvert.DeserializeObject<List<Routes>>(json);

return routes;
}
// return routes;
//}

public static List<StopLocation> LoadStops(Stream stream)
{
Expand All @@ -88,22 +89,22 @@ public static async Task UpdateStopsAsync()
OnStopsUpdated?.Invoke(null, new EventArgs());
}

public static async Task UpdateRoutesAsync()
{
using HttpClient client = new HttpClient();
//public static async Task UpdateRoutesAsync()
//{
// using HttpClient client = new HttpClient();

byte[] stops = await client.GetByteArrayAsync(ROUTES_URL);
File.WriteAllBytes(ROUTES_PATH, stops);
// byte[] stops = await client.GetByteArrayAsync(ROUTES_URL);
// File.WriteAllBytes(ROUTES_PATH, stops);

//OnStopsUpdated?.Invoke(null, new EventArgs());
}
// //OnStopsUpdated?.Invoke(null, new EventArgs());
//}

public static async Task GetLinesAsync()
{
HttpResponseMessage response = await sofiaHttpClient.PostAsync(LINES_URL, new StringContent("", null, "application/json"));
string lines = await response.Content.ReadAsStringAsync();
SentrySdk.CaptureMessage($"update lines: {response.StatusCode}, length: {lines.Length}");

File.WriteAllText(LINES_PATH, lines);
}

Expand All @@ -121,6 +122,16 @@ public static async Task<List<Line>> LoadLinesAsync()
}


public static async Task<ScheduleResponse> GetSchedule(Line line)
{
string payload = JsonConvert.SerializeObject(line);
HttpResponseMessage response = await sofiaHttpClient.PostAsync(GET_SCHEDULE_URL, new StringContent(payload, Encoding.UTF8, "application/json"));

string responseJson = await response.Content.ReadAsStringAsync();
ScheduleResponse schedule = JsonConvert.DeserializeObject<ScheduleResponse>(responseJson);
return schedule;
}

public static void ClearData()
{
if (File.Exists(PATH))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace TramlineFive.Common.Services;
public interface INavigationService
{
void ChangePage(string pageName);
void GoToDetails(LineViewModel line, string stop = "");
void GoToDetails(Line line, string stop = "");
void GoToDetails(ArrivalInformation line, string stop);
}
Loading

0 comments on commit 5af50bf

Please sign in to comment.