Route request being made (Needs polling)

This commit is contained in:
luke-else 2021-11-22 13:07:56 +00:00
parent 16543d9c64
commit 0f96994773
7 changed files with 112 additions and 12 deletions

View File

@ -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<T>(resultString);
}catch {
response = resultString;
}
return new ResponseModel{
//Sender should be aware of type T becuase of Generic function
Result = JsonConvert.DeserializeObject<T>(resultString)
Result = response
};
}
catch (System.Exception e)
@ -71,9 +81,16 @@ namespace EFB.Controllers.API
var result = await pendingResult;
string resultString = result.Content.ReadAsStringAsync().Result;
return new ResponseModel{
object response = resultString;
if (typeof(T) != typeof(string))
{//If the user requests string for return type
response = JsonConvert.DeserializeObject<T>(resultString);
}
return new ResponseModel(){
//Sender should be aware of type T becuase of Generic function
Result = JsonConvert.DeserializeObject<T>(resultString)
Result = response
};
}catch(System.Exception e){
return new ResponseModel{Error = e.Message};

View File

@ -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;

View File

@ -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<UserModel>("User");
if (User == null || User.Route != null || User.Token.IsExpired())
UserModel user = HttpContext.Session.GetObject<UserModel>("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<IActionResult> New(string departure, string arrival, string cruise){
UserModel user = HttpContext.Session.GetObject<UserModel>("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<string, string> headerData = new Dictionary<string, string>();
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<string>("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");
}
}
}

View File

@ -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)
}
};

View File

@ -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";
}
}

View File

@ -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;

View File

@ -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(){