EFB/Models/RouteModel.cs

150 lines
5.2 KiB
C#
Raw Normal View History

2021-11-07 20:19:24 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFB.Models.Route;
2022-01-09 17:03:52 +00:00
using EFB.Controllers.Form;
using System.Net.Http;
2021-11-07 20:19:24 +00:00
namespace EFB.Models
{
public class RouteModel
{
/*
Route Model - This model contains implementations for different points along the Route
Route only becomes populated after route is recieved from autorouter API
*/
public WaypointModel Departure { get; set; } = null;
2021-11-07 20:19:24 +00:00
public WaypointModel Arrival { get; set; } = null;
public IWaypoint Current { get; set; } = null;
public uint Cruise { get; set; } = 0;
public RouteModel(string departure, string departureRoute, string arrival, string arrivalRoute, uint cruise){
2022-01-09 17:03:52 +00:00
if (FormAuthenticator.ValidateICAOCode(departure))
{
Departure = new WaypointModel(departure, departureRoute, 0, 0);
2022-01-09 17:03:52 +00:00
}
if (FormAuthenticator.ValidateICAOCode(arrival))
{
Arrival = new WaypointModel(arrival, arrivalRoute, 0, 0);
2022-01-09 17:03:52 +00:00
}
if (FormAuthenticator.ValidateCruiseAlt(cruise))
{
Cruise = cruise;
}
}
2022-01-11 18:43:08 +00:00
public IWaypoint Next(){
if(Current.Next != null){
2022-01-11 18:44:04 +00:00
Current = Current.Next;
2022-01-11 18:43:08 +00:00
return Current;
}
return null;
}
public IWaypoint Previous(){
if(Current.Previous != null){
2022-01-11 18:44:04 +00:00
Current = Current.Previous;
2022-01-11 18:43:08 +00:00
return Current;
}
return null;
}
2022-01-09 17:03:52 +00:00
//Generate a route Object
public static async Task<RouteModel> StringToRoute(string departure, string arrival, uint cruise, string routeString){
var navdataFetch = NavdataModel.Populate();
2022-01-09 17:03:52 +00:00
string[] routeTemp = routeString.Split(" ");
//Set departure and arrival route
string departureRoute = routeTemp[0];
string arrivalRoute = routeTemp[routeTemp.Length - 2];
RouteModel route = new RouteModel(departure, departureRoute, arrival, arrivalRoute, cruise);
route.Departure.Airway = routeTemp[0];
route.Current = route.Departure;
NavdataModel[] navdata = await navdataFetch;
navdata = NavdataModel.MergeSort(ref navdata, 0, navdata.Length - 1);
2022-01-09 17:03:52 +00:00
for (var i = 1; i < routeTemp.Length-1; i+=2)
{//Already used first item, continue itterating over every other item
IWaypoint next;
NavdataModel currentWaypoint = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length-1, routeTemp[i]);
if (currentWaypoint == null)
{
currentWaypoint = new NavdataModel(0, routeTemp[i], null, "0", "0");
}
2022-01-09 17:03:52 +00:00
//Populate 'next' waypoint
if (routeTemp[i].Length > 3)
{//waypoint Type
next = new WaypointModel(
routeTemp[i],
routeTemp[i+1],
float.Parse(currentWaypoint.Longitude),
float.Parse(currentWaypoint.Latitude)
);
2022-01-09 17:03:52 +00:00
}else
{//Navaid Type
next = new NavaidModel(
routeTemp[i],
routeTemp[i+1],
float.Parse(currentWaypoint.Longitude),
float.Parse(currentWaypoint.Latitude)
);
2022-01-09 17:03:52 +00:00
}
next.Previous = route.Current;
route.Current.Next = next;
route.Current = next;
}
//Connect end of route (linked list)
route.Current.Airway = null;
route.Current.Next = route.Arrival;
route.Arrival.Previous = route.Current;
route.Current = route.Departure;
2022-01-09 17:03:52 +00:00
//Assign departure and arrival coordinate positions
NavdataModel departureNav = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, departure);
NavdataModel arrivalNav = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, arrival);
route.Departure.Latitude = float.Parse(departureNav.Latitude);
route.Departure.Longitude = float.Parse(departureNav.Longitude);
route.Arrival.Latitude = float.Parse(arrivalNav.Latitude);
route.Arrival.Latitude = float.Parse(arrivalNav.Longitude);
2022-01-09 17:03:52 +00:00
return route;
}
//Generate a route String
public static string ParseRoute(string 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;
}
2021-11-07 20:19:24 +00:00
}
2022-01-09 17:03:52 +00:00
2021-11-07 20:19:24 +00:00
}