2021-11-15 07:44:18 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
2022-01-05 15:24:15 +00:00
|
|
|
using System.Threading;
|
2021-11-22 13:07:56 +00:00
|
|
|
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;
|
2021-11-22 13:07:56 +00:00
|
|
|
using Newtonsoft.Json;
|
2021-11-15 07:44:18 +00:00
|
|
|
using EFB.Models;
|
2021-11-22 13:07:56 +00:00
|
|
|
using EFB.Models.JSON;
|
2021-11-15 07:44:18 +00:00
|
|
|
using EFB.Sessions;
|
2021-11-22 13:07:56 +00:00
|
|
|
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
|
2021-11-22 13:07:56 +00:00
|
|
|
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!");
|
|
|
|
}
|
2021-11-22 13:07:56 +00:00
|
|
|
|
2022-01-05 20:56:28 +00:00
|
|
|
public async Task<IActionResult> New(string departure, string arrival, string cruise)
|
|
|
|
{
|
2021-11-22 13:07:56 +00:00
|
|
|
UserModel user = HttpContext.Session.GetObject<UserModel>("User");
|
2022-02-18 22:01:53 +00:00
|
|
|
if (user == null || user.UserToken.IsExpired())
|
2021-11-22 13:07:56 +00:00
|
|
|
{//If the user is still authenticated
|
2022-02-18 22:01:53 +00:00
|
|
|
return RedirectToAction("Index", "Home");
|
|
|
|
}
|
2021-11-22 13:07:56 +00:00
|
|
|
|
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;
|
2021-11-22 13:07:56 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
if (uint.TryParse(cruise, out cruiseAlt) && FormAuthenticator.ValidateCruiseAlt(cruiseAlt))
|
|
|
|
{//If the cruise altitude if within limits.
|
2021-11-22 13:07:56 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
//Submit route request...
|
|
|
|
APIInterface API = new APIInterface();
|
2021-11-22 13:07:56 +00:00
|
|
|
|
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}");
|
2021-12-28 21:53:31 +00:00
|
|
|
|
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()
|
|
|
|
};
|
2021-11-22 13:07:56 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
user.RouteToken = routeToken;
|
|
|
|
HttpContext.Session.SetObject("User", user);
|
2021-11-22 13:07:56 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
return await Poll(departure, arrival, cruiseAlt);
|
2021-11-22 13:07:56 +00:00
|
|
|
|
|
|
|
}
|
2022-02-18 22:01:53 +00:00
|
|
|
|
|
|
|
TempData["Error"] = responseRoute.Error;
|
2021-11-22 13:07:56 +00:00
|
|
|
return RedirectToAction("Index", "Route");
|
2022-02-18 22:01:53 +00:00
|
|
|
|
2021-11-22 13:07:56 +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");
|
2021-11-22 13:07:56 +00:00
|
|
|
}
|
2021-12-27 21:15:55 +00:00
|
|
|
|
|
|
|
|
2022-01-09 17:03:52 +00:00
|
|
|
public async Task<IActionResult> Poll(string departure, string arrival, uint cruise)
|
2022-01-05 20:56:28 +00:00
|
|
|
{
|
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-09 20:19:32 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
UserModel user = HttpContext.Session.GetObject<UserModel>("User");
|
2022-02-09 20:19:32 +00:00
|
|
|
|
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-09 20:19:32 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
APIInterface API = new APIInterface();
|
|
|
|
Dictionary<string, string> headerData = new Dictionary<string, string>();
|
2022-02-09 20:19:32 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
/*-----Chart Fetching Operations--------*/
|
2021-12-28 21:53:31 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
var requestDepartureCharts = ChartModel.FetchAsync(departure);
|
|
|
|
var requestArrivalCharts = ChartModel.FetchAsync(arrival);
|
2022-01-05 20:56:28 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
/*----------------------------------*/
|
2021-12-28 21:53:31 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
//Run route Polling
|
|
|
|
headerData.Add("Authorization", $"Bearer {user.UserToken.TokenValue}");
|
2022-01-05 15:24:15 +00:00
|
|
|
|
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-01-05 20:56:28 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
ResponseModel<List<PollResponse>> responsePoll = await pollingRequest;
|
2021-12-28 21:53:31 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
foreach (var item in responsePoll.Result)
|
|
|
|
{
|
|
|
|
if (item.Command == "notvalid" || item.Command == "solution")
|
2022-01-05 20:56:28 +00:00
|
|
|
{
|
2022-02-18 22:01:53 +00:00
|
|
|
collected = true;
|
|
|
|
routeString = item.FlightPlan;
|
|
|
|
break;
|
2022-01-05 20:56:28 +00:00
|
|
|
}
|
2022-02-18 22:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
pollCount++;
|
2022-01-05 15:24:15 +00:00
|
|
|
|
2022-02-18 22:01:53 +00:00
|
|
|
}
|
2021-12-27 21:15:55 +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-01-05 20:56:28 +00:00
|
|
|
}
|
2022-02-18 22:01:53 +00:00
|
|
|
|
|
|
|
ChartList arrivalCharts = await requestArrivalCharts;
|
|
|
|
if (arrivalCharts != null)
|
2022-01-05 20:56:28 +00:00
|
|
|
{
|
2022-02-18 22:01:53 +00:00
|
|
|
user.ArrivalCharts = new ChartModel(arrival, arrivalCharts);
|
2021-12-27 21:15:55 +00:00
|
|
|
}
|
2022-01-05 20:56:28 +00:00
|
|
|
|
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");
|
2021-12-27 21:15:55 +00:00
|
|
|
}
|
2022-02-18 22:01:53 +00:00
|
|
|
|
|
|
|
TempData["Error"] = $"Unable to get route after {pollCount} Attempts!";
|
|
|
|
return RedirectToAction("Index", "Route");
|
2021-12-27 21:15:55 +00:00
|
|
|
}
|
2022-01-05 20:56:28 +00:00
|
|
|
|
2022-01-09 17:03:52 +00:00
|
|
|
|
2021-11-15 07:44:18 +00:00
|
|
|
}
|
|
|
|
}
|