From 0e1a00536cbc87c11947d0a7d784582eee9d7c6d Mon Sep 17 00:00:00 2001 From: Luke Else Date: Sun, 9 Jan 2022 17:03:52 +0000 Subject: [PATCH] Route Object Generation Complete --- Controllers/RouteController.cs | 68 ++++++++++---------------- Models/Route/NavaidModel.cs | 5 +- Models/Route/WaypointModel.cs | 2 +- Models/RouteModel.cs | 87 +++++++++++++++++++++++++++++++++- 4 files changed, 112 insertions(+), 50 deletions(-) diff --git a/Controllers/RouteController.cs b/Controllers/RouteController.cs index aa52e1b..392fed0 100644 --- a/Controllers/RouteController.cs +++ b/Controllers/RouteController.cs @@ -29,7 +29,7 @@ namespace EFB.Controllers { //Check the user has a valid login UserModel user = HttpContext.Session.GetObject("User"); - if (user == null || user.Route != null || user.Token.IsExpired()) + if (user == null || user.UserToken.IsExpired()) { return RedirectToAction("Index", "Home"); } @@ -46,7 +46,7 @@ namespace EFB.Controllers public async Task New(string departure, string arrival, string cruise) { UserModel user = HttpContext.Session.GetObject("User"); - if (!(user == null || user.Token.IsExpired())) + if (!(user == null || user.UserToken.IsExpired())) {//If the user is still authenticated if (FormAuthenticator.ValidateICAOCode(departure) && FormAuthenticator.ValidateICAOCode(arrival)) {//If the user has entered valid ICAOs @@ -61,7 +61,7 @@ namespace EFB.Controllers //Prepare data to be send off with request (route) Dictionary headerData = new Dictionary(); - headerData.Add("Authorization", $"Bearer {user.Token.TokenValue}"); + headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}"); RouteRequest routeRequest = new RouteRequest() { @@ -79,15 +79,15 @@ namespace EFB.Controllers if (responseRoute.Error == null) {//Update User session and add route ID - RouteModel route = new RouteModel() + TokenModel routeToken = new TokenModel() { - RouteID = responseRoute.Result.ToString() + TokenValue = responseRoute.Result.ToString() }; - user.Route = route; + user.RouteToken = routeToken; HttpContext.Session.SetObject("User", user); - return await Poll(); + return await Poll(departure, arrival, cruiseAlt); } @@ -108,29 +108,29 @@ namespace EFB.Controllers } - public async Task Poll() + public async Task Poll(string departure, string arrival, uint cruise) { if (HttpContext.Session.GetString("User") != null) {//If the user is currently logged in UserModel user = HttpContext.Session.GetObject("User"); - if (user.Route != null) + if (user.RouteToken != null) {//If the user has a route object (e.g, they have been to the route page) //Make calls to the server to fetch route bool collected = false; - int count = 0; - string route = ""; + int pollCount = 0; + string routeString = ""; APIInterface API = new APIInterface(); Dictionary headerData = new Dictionary(); - headerData.Add("Authorization", $"Bearer {user.Token.TokenValue}"); + headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}"); - while (collected == false && count <= 5) + while (collected == false && pollCount < 3) { //Make Polling Request - var pollingRequest = API.Put>($"https://api.autorouter.aero/v1.0/router/{user.Route.RouteID}/longpoll", headerData, null); + var pollingRequest = API.Put>($"https://api.autorouter.aero/v1.0/router/{user.RouteToken.TokenValue}/longpoll", headerData, null); ResponseModel> responsePoll = await pollingRequest; @@ -139,25 +139,28 @@ namespace EFB.Controllers if (responsePoll.Result[routePos].Command == "solution") { collected = true; - route = responsePoll.Result[routePos].FlightPlan; + routeString = responsePoll.Result[routePos].FlightPlan; break; } - Thread.Sleep(5000); - count++; + Thread.Sleep(3000); + pollCount++; } if (collected) { //fill in route - string finalRoute = ParseRoute(route); + string finalRoute = RouteModel.ParseRoute(routeString); - TempData["Error"] = finalRoute; + RouteModel route = RouteModel.StringToRoute(departure, arrival, cruise, finalRoute); + user.Route = route; + HttpContext.Session.SetObject("User", user); + return RedirectToAction("Index", "Route"); } - TempData["Error"] = "Unable to get route!"; + TempData["Error"] = $"Unable to get route after {pollCount} Attempts!"; return RedirectToAction("Index", "Route"); } @@ -173,29 +176,6 @@ namespace EFB.Controllers } } - private string ParseRoute(string route){ - TempData["Error"] = route; - - route.Replace('/', ' '); - var routeArr = route.Split(' '); - - string finalRoute = ""; - - foreach (var item in routeArr) - { - var waypoint = item.Split('/')[0]; - if (waypoint.Length <= 7 && waypoint.Length >= 3 && !waypoint.Contains('-')) - { - finalRoute += $"{waypoint} "; - - if (waypoint.Length == 7 && finalRoute.Length > 8) - break; - - } - } - - return finalRoute; - - } + } } \ No newline at end of file diff --git a/Models/Route/NavaidModel.cs b/Models/Route/NavaidModel.cs index 95686c3..ed147cd 100644 --- a/Models/Route/NavaidModel.cs +++ b/Models/Route/NavaidModel.cs @@ -17,10 +17,9 @@ namespace EFB.Models.Route public IWaypoint Previous { get; set; } = null; public bool Visited { get; set; } = false; - public NavaidModel(string name, string airway, int frequency){ + public NavaidModel(string name, string airway){ Name = name; - Airway = Airway; - Frequency = frequency; + Airway = airway; } } } \ No newline at end of file diff --git a/Models/Route/WaypointModel.cs b/Models/Route/WaypointModel.cs index abf9c1a..1acc0bf 100644 --- a/Models/Route/WaypointModel.cs +++ b/Models/Route/WaypointModel.cs @@ -18,7 +18,7 @@ namespace EFB.Models.Route public WaypointModel(string name, string airway){ Name = name; - Airway = Airway; + Airway = airway; } } diff --git a/Models/RouteModel.cs b/Models/RouteModel.cs index 2e874af..544aa74 100644 --- a/Models/RouteModel.cs +++ b/Models/RouteModel.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using EFB.Models.Route; +using EFB.Controllers.Form; +using System.Net.Http; namespace EFB.Models { @@ -13,12 +15,93 @@ namespace EFB.Models Route only becomes populated after route is recieved from autorouter API */ - public string RouteID { get; init; } - public WaypointModel Departure { get; set; } = null; public WaypointModel Arrival { get; set; } = null; public IWaypoint Current { get; set; } = null; public uint Cruise { get; set; } = 0; + public RouteModel(string departure, string departureRoute, string arrival, string arrivalRoute, uint cruise){ + if (FormAuthenticator.ValidateICAOCode(departure)) + { + Departure = new WaypointModel(departure, departureRoute); + } + + if (FormAuthenticator.ValidateICAOCode(arrival)) + { + Arrival = new WaypointModel(arrival, arrivalRoute); + } + + if (FormAuthenticator.ValidateCruiseAlt(cruise)) + { + Cruise = cruise; + } + } + + //Generate a route Object + public static RouteModel StringToRoute(string departure, string arrival, uint cruise, string routeString){ + string[] routeTemp = routeString.Split(" "); + + //Set departure and arrival route + string departureRoute = routeTemp[0]; + string arrivalRoute = routeTemp[routeTemp.Length - 2]; + + RouteModel route = new RouteModel(departure, departureRoute, arrival, arrivalRoute, cruise); + route.Departure.Airway = routeTemp[0]; + + route.Current = route.Departure; + + for (var i = 1; i < routeTemp.Length-1; i+=2) + {//Already used first item, continue itterating over every other item + IWaypoint next; + + //Populate 'next' waypoint + if (routeTemp[i].Length > 3) + {//waypoint Type + next = new WaypointModel(routeTemp[i], routeTemp[i+1]); + }else + {//Navaid Type + next = new NavaidModel(routeTemp[i], routeTemp[i+1]); + } + + next.Previous = route.Current; + route.Current.Next = next; + route.Current = next; + } + + //Connect end of route (linked list) + route.Current.Airway = null; + route.Current.Next = route.Arrival; + route.Arrival.Previous = route.Current; + + route.Current = null; + + return route; + } + + + + //Generate a route String + public static string ParseRoute(string route){ + route.Replace('/', ' '); + var routeArr = route.Split(' '); + + string finalRoute = ""; + + foreach (var item in routeArr) + { + var waypoint = item.Split('/')[0]; + if (waypoint.Length <= 7 && waypoint.Length >= 3 && !waypoint.Contains('-')) + { + finalRoute += $"{waypoint} "; + + if (waypoint.Length == 7 && finalRoute.Length > 8) + break; + + } + } + + return finalRoute; + } } + } \ No newline at end of file