From 0f9699477370185c8a03d2969a2cedc5a19b9443 Mon Sep 17 00:00:00 2001 From: luke-else <52086083+luke-else@users.noreply.github.com> Date: Mon, 22 Nov 2021 13:07:56 +0000 Subject: [PATCH] Route request being made (Needs polling) --- Controllers/API/APIInterface.cs | 23 +++++++-- Controllers/Form/FormAuthenticator.cs | 2 +- Controllers/RouteController.cs | 73 +++++++++++++++++++++++++-- Controllers/UserController.cs | 2 +- Models/JSON/RouteRequest.cs | 18 +++++++ Models/RouteModel.cs | 4 +- Models/TokenModel.cs | 2 +- 7 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 Models/JSON/RouteRequest.cs diff --git a/Controllers/API/APIInterface.cs b/Controllers/API/APIInterface.cs index f15712b..9cd8528 100644 --- a/Controllers/API/APIInterface.cs +++ b/Controllers/API/APIInterface.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Text; +using System; using System.Threading.Tasks; using Newtonsoft.Json; using System.Net.Http; @@ -35,9 +36,18 @@ namespace EFB.Controllers.API string resultString = result.Content.ReadAsStringAsync().Result; + object response; + + //Assess return value (object or raw string) + try{ + response = JsonConvert.DeserializeObject(resultString); + }catch { + response = resultString; + } + return new ResponseModel{ //Sender should be aware of type T becuase of Generic function - Result = JsonConvert.DeserializeObject(resultString) + Result = response }; } catch (System.Exception e) @@ -70,10 +80,17 @@ namespace EFB.Controllers.API var pendingResult = this.HttpClient.PostAsync(Endpoint, Body); var result = await pendingResult; string resultString = result.Content.ReadAsStringAsync().Result; + + object response = resultString; - return new ResponseModel{ + if (typeof(T) != typeof(string)) + {//If the user requests string for return type + response = JsonConvert.DeserializeObject(resultString); + } + + return new ResponseModel(){ //Sender should be aware of type T becuase of Generic function - Result = JsonConvert.DeserializeObject(resultString) + Result = response }; }catch(System.Exception e){ return new ResponseModel{Error = e.Message}; diff --git a/Controllers/Form/FormAuthenticator.cs b/Controllers/Form/FormAuthenticator.cs index 28dded6..df42554 100644 --- a/Controllers/Form/FormAuthenticator.cs +++ b/Controllers/Form/FormAuthenticator.cs @@ -37,7 +37,7 @@ namespace EFB.Controllers.Form return false; } - public static bool ValidateCruiseAlt(int CruiseAlt){ + public static bool ValidateCruiseAlt(uint CruiseAlt){ if (CruiseAlt > 0 && CruiseAlt < 50000) { return true; diff --git a/Controllers/RouteController.cs b/Controllers/RouteController.cs index 15f2b84..ba0281a 100644 --- a/Controllers/RouteController.cs +++ b/Controllers/RouteController.cs @@ -1,13 +1,17 @@ using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; using System.Threading.Tasks; +using System.Collections.Generic; +using System.Text; +using System.Net.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; using EFB.Models; +using EFB.Models.JSON; using EFB.Sessions; +using EFB.Controllers.Form; +using EFB.Controllers.API; namespace EFB.Controllers { @@ -23,8 +27,8 @@ namespace EFB.Controllers public IActionResult Index() { //Check the user has a valid login - UserModel User = HttpContext.Session.GetObject("User"); - if (User == null || User.Route != null || User.Token.IsExpired()) + UserModel user = HttpContext.Session.GetObject("User"); + if (user == null || user.Route != null || user.Token.IsExpired()) { return RedirectToAction("Index", "Home"); } @@ -37,5 +41,64 @@ namespace EFB.Controllers { return View("Error!"); } + + public async Task New(string departure, string arrival, string cruise){ + UserModel user = HttpContext.Session.GetObject("User"); + if (!(user == null || user.Token.IsExpired())) + {//If the user is still authenticated + if (FormAuthenticator.ValidateICAOCode(departure) && FormAuthenticator.ValidateICAOCode(arrival)) + {//If the user has entered valid ICAOs + + uint cruiseAlt; + + if (uint.TryParse(cruise, out cruiseAlt) && FormAuthenticator.ValidateCruiseAlt(cruiseAlt)) + {//If the cruise altitude if within limits. + + //Submit route request... + APIInterface API = new APIInterface(); + + //Prepare data to be send off with request (route) + Dictionary headerData = new Dictionary(); + headerData.Add("Authorization", $"Bearer {user.Token.TokenValue}"); + + RouteRequest routeRequest = new RouteRequest(){ + departure = departure, + destination = arrival, + preferredminlevel = cruiseAlt / 1000, + preferredmaxlevel = cruiseAlt / 1000, + }; + StringContent content = new StringContent(JsonConvert.SerializeObject(routeRequest), Encoding.UTF8, "application/json"); + + //Make initial Route Request + var requestRoute = API.Post("https://api.autorouter.aero/v1.0/router", headerData, content); + + ResponseModel responseRoute = await requestRoute; + + if (responseRoute.Error == null) + {//Update User session and add route ID + RouteModel route = new RouteModel(){ + RouteID = responseRoute.Result.ToString() + }; + + user.Route = route; + HttpContext.Session.SetObject("User", user); + + } + + TempData["Error"] = responseRoute.Error; + return RedirectToAction("Index", "Route"); + + } + TempData["Error"] = "Invalid Cruise Altitude"; + TempData["Departure"] = departure; + TempData["Arrival"] = arrival; + return RedirectToAction("Index", "Route"); + + } + TempData["Error"] = "Invalid Departure or Arrival ICAO"; + return RedirectToAction("Index", "Route"); + } + return RedirectToAction("Index", "Home"); + } } } \ No newline at end of file diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index 00a3971..d961576 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -65,7 +65,7 @@ namespace EFB.Controllers UserModel user = new UserModel{ EMail = email, Token = new TokenModel{ - Token = login.access_token, + TokenValue = login.access_token, Expiration = DateTime.UtcNow.AddSeconds(login.expires_in) } }; diff --git a/Models/JSON/RouteRequest.cs b/Models/JSON/RouteRequest.cs new file mode 100644 index 0000000..27cdbc5 --- /dev/null +++ b/Models/JSON/RouteRequest.cs @@ -0,0 +1,18 @@ +using Newtonsoft.Json; + +namespace EFB.Models.JSON +{ + public class RouteRequest + { + [JsonProperty] + public string departure { get; set; } + [JsonProperty] + public string destination { get; set; } + [JsonProperty] + public uint preferredminlevel { get; set; } + [JsonProperty] + public uint preferredmaxlevel { get; set; } + [JsonProperty] + public string optimise { get; } = "preferred"; + } +} \ No newline at end of file diff --git a/Models/RouteModel.cs b/Models/RouteModel.cs index a0468b1..2e874af 100644 --- a/Models/RouteModel.cs +++ b/Models/RouteModel.cs @@ -13,7 +13,9 @@ namespace EFB.Models Route only becomes populated after route is recieved from autorouter API */ - public WaypointModel Departure { get; init; } = null; + 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; diff --git a/Models/TokenModel.cs b/Models/TokenModel.cs index 64ddf38..08faff2 100644 --- a/Models/TokenModel.cs +++ b/Models/TokenModel.cs @@ -10,7 +10,7 @@ namespace EFB.Models /* Auto Router API Token Model */ - public string Token { get; init; } + public string TokenValue { get; init; } public DateTime Expiration { get; init; } public bool IsExpired(){