EFB/Models/RouteModel.cs

123 lines
3.8 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;
2022-01-09 17:03:52 +00:00
public RouteModel(string departure, string departureRoute, string arrival, string arrivalRoute, uint cruise){
if (FormAuthenticator.ValidateICAOCode(departure))
{
Departure = new WaypointModel(departure, departureRoute);
}
if (FormAuthenticator.ValidateICAOCode(arrival))
{
Arrival = new WaypointModel(arrival, arrivalRoute);
}
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 RouteModel StringToRoute(string departure, string arrival, uint cruise, string routeString){
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;
for (var i = 1; i < routeTemp.Length-1; i+=2)
{//Already used first item, continue itterating over every other item
IWaypoint next;
//Populate 'next' waypoint
if (routeTemp[i].Length > 3)
{//waypoint Type
next = new WaypointModel(routeTemp[i], routeTemp[i+1]);
}else
{//Navaid Type
next = new NavaidModel(routeTemp[i], routeTemp[i+1]);
}
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
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
}