EFB/Controllers/RouteController.cs

197 lines
7.0 KiB
C#
Raw Permalink Normal View History

2021-11-15 07:44:18 +00:00
using System;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
2021-11-15 07:44:18 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
2021-11-15 07:44:18 +00:00
using EFB.Models;
using EFB.Models.JSON;
2021-11-15 07:44:18 +00:00
using EFB.Sessions;
using EFB.Controllers.Form;
using EFB.Controllers.API;
2021-11-15 07:44:18 +00:00
namespace EFB.Controllers
{
public class RouteController : Controller
{
private readonly ILogger<RouteController> _logger;
public RouteController(ILogger<RouteController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
//Check the user has a valid login
UserModel user = HttpContext.Session.GetObject<UserModel>("User");
2022-01-09 17:03:52 +00:00
if (user == null || user.UserToken.IsExpired())
2021-11-15 07:44:18 +00:00
{
return RedirectToAction("Index", "Home");
}
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View("Error!");
}
public async Task<IActionResult> New(string departure, string arrival, string cruise)
{
UserModel user = HttpContext.Session.GetObject<UserModel>("User");
2022-02-18 22:01:53 +00:00
if (user == null || user.UserToken.IsExpired())
{//If the user is still authenticated
2022-02-18 22:01:53 +00:00
return RedirectToAction("Index", "Home");
}
2022-02-18 22:01:53 +00:00
if (!FormAuthenticator.ValidateICAOCode(departure) || !FormAuthenticator.ValidateICAOCode(arrival))
{//If the user has entered valid ICAOs
TempData["Error"] = "Invalid Departure or Arrival ICAO";
return RedirectToAction("Index", "Route");
}
uint cruiseAlt;
2022-02-18 22:01:53 +00:00
if (uint.TryParse(cruise, out cruiseAlt) && FormAuthenticator.ValidateCruiseAlt(cruiseAlt))
{//If the cruise altitude if within limits.
2022-02-18 22:01:53 +00:00
//Submit route request...
APIInterface API = new APIInterface();
2022-02-18 22:01:53 +00:00
//Prepare data to be send off with request (route)
Dictionary<string, string> headerData = new Dictionary<string, string>();
headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}");
2022-02-18 22:01:53 +00:00
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<string> responseRoute = await requestRoute;
if (responseRoute.Error == null)
{//Update User session and add route ID
TokenModel routeToken = new TokenModel()
{
TokenValue = responseRoute.Result.ToString()
};
2022-02-18 22:01:53 +00:00
user.RouteToken = routeToken;
HttpContext.Session.SetObject("User", user);
2022-02-18 22:01:53 +00:00
return await Poll(departure, arrival, cruiseAlt);
}
2022-02-18 22:01:53 +00:00
TempData["Error"] = responseRoute.Error;
return RedirectToAction("Index", "Route");
2022-02-18 22:01:53 +00:00
}
2022-02-18 22:01:53 +00:00
TempData["Error"] = "Invalid Cruise Altitude";
TempData["Departure"] = departure;
TempData["Arrival"] = arrival;
return RedirectToAction("Index", "Route");
}
2022-01-09 17:03:52 +00:00
public async Task<IActionResult> Poll(string departure, string arrival, uint cruise)
{
2022-02-18 22:01:53 +00:00
if (HttpContext.Session.GetString("User") == null)
{//If the user is not currently logged in
TempData["Error"] = "Please login before trying to plan a route";
return RedirectToAction("Index", "Route");
}
2022-02-18 22:01:53 +00:00
UserModel user = HttpContext.Session.GetObject<UserModel>("User");
2022-02-18 22:01:53 +00:00
if (user.RouteToken == null)
{//If the user has a route object (e.g, they have been to the route page)
return RedirectToAction("Index", "Route");
}
//Make calls to the server to fetch route
bool collected = false;
int pollCount = 0;
string routeString = "";
2022-02-18 22:01:53 +00:00
APIInterface API = new APIInterface();
Dictionary<string, string> headerData = new Dictionary<string, string>();
2022-02-18 22:01:53 +00:00
/*-----Chart Fetching Operations--------*/
2022-02-18 22:01:53 +00:00
var requestDepartureCharts = ChartModel.FetchAsync(departure);
var requestArrivalCharts = ChartModel.FetchAsync(arrival);
2022-02-18 22:01:53 +00:00
/*----------------------------------*/
2022-02-18 22:01:53 +00:00
//Run route Polling
headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}");
2022-02-18 22:01:53 +00:00
while (collected == false && pollCount < 15)
{
//Make Polling Request
var pollingRequest = API.Put<List<PollResponse>>($"https://api.autorouter.aero/v1.0/router/{user.RouteToken.TokenValue}/longpoll", headerData, null);
2022-02-18 22:01:53 +00:00
ResponseModel<List<PollResponse>> responsePoll = await pollingRequest;
2022-02-18 22:01:53 +00:00
foreach (var item in responsePoll.Result)
{
if (item.Command == "notvalid" || item.Command == "solution")
{
2022-02-18 22:01:53 +00:00
collected = true;
routeString = item.FlightPlan;
break;
}
2022-02-18 22:01:53 +00:00
}
Thread.Sleep(3000);
pollCount++;
2022-02-18 22:01:53 +00:00
}
2022-02-18 22:01:53 +00:00
if (collected)
{
//Get Response from Charts
ChartList departureCharts = await requestDepartureCharts;
if (departureCharts != null)
{
user.DepartureCharts = new ChartModel(departure, departureCharts);
}
2022-02-18 22:01:53 +00:00
ChartList arrivalCharts = await requestArrivalCharts;
if (arrivalCharts != null)
{
2022-02-18 22:01:53 +00:00
user.ArrivalCharts = new ChartModel(arrival, arrivalCharts);
}
2022-02-18 22:01:53 +00:00
//fill in route
string finalRoute = RouteModel.ParseRoute(routeString);
user.Route = finalRoute;
user.Departure = departure;
user.Arrival = arrival;
user.Cruise = cruise;
HttpContext.Session.SetObject("User", user);
2022-02-18 22:43:01 +00:00
return RedirectToAction("Index", "App");
}
2022-02-18 22:01:53 +00:00
TempData["Error"] = $"Unable to get route after {pollCount} Attempts!";
return RedirectToAction("Index", "Route");
}
2022-01-09 17:03:52 +00:00
2021-11-15 07:44:18 +00:00
}
}