Route Polling Complete

App now fetched a route, still needs to be moved into a Linked List
This commit is contained in:
Luke Else 2022-01-05 20:56:28 +00:00
parent 18bf369a1b
commit f2e852fcf0

View File

@ -43,7 +43,8 @@ namespace EFB.Controllers
return View("Error!");
}
public async Task<IActionResult> New(string departure, string arrival, string cruise){
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
@ -62,7 +63,8 @@ namespace EFB.Controllers
Dictionary<string, string> headerData = new Dictionary<string, string>();
headerData.Add("Authorization", $"Bearer {user.Token.TokenValue}");
RouteRequest routeRequest = new RouteRequest(){
RouteRequest routeRequest = new RouteRequest()
{
departure = departure,
destination = arrival,
preferredminlevel = cruiseAlt / 1000,
@ -77,7 +79,8 @@ namespace EFB.Controllers
if (responseRoute.Error == null)
{//Update User session and add route ID
RouteModel route = new RouteModel(){
RouteModel route = new RouteModel()
{
RouteID = responseRoute.Result.ToString()
};
@ -105,7 +108,8 @@ namespace EFB.Controllers
}
public async Task<IActionResult> Poll(){
public async Task<IActionResult> Poll()
{
if (HttpContext.Session.GetString("User") != null)
{//If the user is currently logged in
UserModel user = HttpContext.Session.GetObject<UserModel>("User");
@ -116,7 +120,7 @@ namespace EFB.Controllers
//Make calls to the server to fetch route
bool collected = false;
int count = 0;
string route;
string route = "";
APIInterface API = new APIInterface();
@ -131,10 +135,11 @@ namespace EFB.Controllers
ResponseModel<List<PollResponse>> responsePoll = await pollingRequest;
if (responsePoll.Result[count].Command == "solution")
int routePos = responsePoll.Result.Count - 1;
if (responsePoll.Result[routePos].Command == "solution")
{
collected = true;
route = responsePoll.Result[count].FlightPlan;
route = responsePoll.Result[routePos].FlightPlan;
break;
}
@ -143,16 +148,54 @@ namespace EFB.Controllers
}
if (collected)
{
//fill in route
string finalRoute = ParseRoute(route);
return RedirectToAction("Index", "Route");
}else{
TempData["Error"] = finalRoute;
return RedirectToAction("Index", "Route");
}
}else{
TempData["Error"] = "Unable to get route!";
return RedirectToAction("Index", "Route");
}
else
{
return RedirectToAction("Index", "Route");
}
}
else
{
return RedirectToAction("Index", "Route");
}
}
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;
}
}
}