Code cleanup

This commit is contained in:
2022-02-28 21:53:28 +00:00
parent b1e8bfe8d0
commit 72124e7f15
31 changed files with 250 additions and 312 deletions

View File

@ -1,18 +1,11 @@
using System;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using EFB.Models;
using EFB.Models.JSON;
using EFB.Sessions;
using EFB.Controllers.Form;
using EFB.Controllers.API;
using EFB.Controllers.Form;
using EFB.Models.JSON;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace EFB.Models
{
@ -29,7 +22,8 @@ namespace EFB.Models
public Chart[] PilotBriefing { get; set; }
[JsonConstructor]
public ChartModel(){
public ChartModel()
{
//Empty constructor for JSON Serialisation Purposes
}
public ChartModel(string ICAO, ChartList response)
@ -45,24 +39,26 @@ namespace EFB.Models
PilotBriefing = FillChart(response.PilotBriefing);
}
private Chart[] FillChart(ChartsCollection collection){
private Chart[] FillChart(ChartsCollection collection)
{
if (collection != null)
{
return collection.Charts;
}
return new Chart[]{};
return new Chart[] { };
}
public static async Task<ChartList> FetchAsync(string ICAO){
public static async Task<ChartList> FetchAsync(string ICAO)
{
Console.WriteLine("Start");
if (FormAuthenticator.ValidateICAOCode(ICAO))
{
APIInterface API = new APIInterface();
Dictionary <string, string> headerData = new Dictionary<string, string>();
Dictionary<string, string> headerData = new Dictionary<string, string>();
headerData.Add("referer", "luke-else.co.uk");
Dictionary<string, string> formData = new Dictionary<string, string>();
formData.Add("token", Environment.GetEnvironmentVariable("ChartFoxAPIKey", EnvironmentVariableTarget.User));
FormUrlEncodedContent body = new FormUrlEncodedContent(formData);
@ -71,7 +67,7 @@ namespace EFB.Models
Console.WriteLine("End");
if (requestCharts.Result.Status == "success")
{
return requestCharts.Result.Response;
return requestCharts.Result.Response;
}
}
return null;

View File

@ -1,5 +1,3 @@
using System;
namespace EFB.Models
{
public class ErrorViewModel

View File

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFB.Models.Route;
namespace EFB.Models

View File

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace EFB.Models.JSON
@ -59,6 +55,6 @@ namespace EFB.Models.JSON
[JsonProperty("url")]
public string URL { get; set; }
}
}

View File

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace EFB.Models.JSON
{
@ -14,6 +10,6 @@ namespace EFB.Models.JSON
public string client_id { get; set; }
[JsonProperty]
public string client_secret { get; set; }
}
}

View File

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace EFB.Models.JSON
@ -16,13 +12,13 @@ namespace EFB.Models.JSON
public string token_type { get; set; }
[JsonProperty]
public string scope { get; set; }
[JsonProperty]
public string error { get; set; } = null;
[JsonProperty]
public string error_description { get; set; } = null;
}
}

View File

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace EFB.Models.JSON

View File

@ -1,11 +1,9 @@
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc;
using EFB.Sessions;
namespace EFB.Models
{
@ -17,9 +15,10 @@ namespace EFB.Models
public int? Frequency { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public NavdataModel(int id, string name, string type, string latitude, string longitude){
public NavdataModel(int id, string name, string type, string latitude, string longitude)
{
Id = id;
Name = name;
Type = type;
@ -29,7 +28,8 @@ namespace EFB.Models
}
[JsonConstructor]
public NavdataModel(int id, string name, string type, int? frequency, string latitude, string longitude){
public NavdataModel(int id, string name, string type, int? frequency, string latitude, string longitude)
{
Id = id;
Name = name;
Type = type;
@ -38,7 +38,8 @@ namespace EFB.Models
Longitude = longitude;
}
public static async Task<NavdataModel[]> Populate(){
public static async Task<NavdataModel[]> Populate()
{
string password = Environment.GetEnvironmentVariable("MySQLPassword", EnvironmentVariableTarget.User);
MySqlConnection con = new MySqlConnection($"server=server.luke-else.co.uk;userid=root;password={password};database=EFB");
con.Open();
@ -48,7 +49,7 @@ namespace EFB.Models
string query = "SELECT * FROM waypoints";
MySqlCommand command = new MySqlCommand(query, con);
MySqlDataReader reader = (MySqlDataReader) await command.ExecuteReaderAsync();
MySqlDataReader reader = (MySqlDataReader)await command.ExecuteReaderAsync();
List<NavdataModel> navdata = new List<NavdataModel>();
@ -67,7 +68,9 @@ namespace EFB.Models
navdata.Add(
new NavdataModel(id, name, type, frequency, latitude, longitude)
);
}else{
}
else
{
navdata.Add(
new NavdataModel(id, name, type, latitude, longitude)
);
@ -77,13 +80,14 @@ namespace EFB.Models
return navdata.ToArray<NavdataModel>();
}
public static NavdataModel BinarySearch(ref NavdataModel[] data, int start, int end, string target){
public static NavdataModel BinarySearch(ref NavdataModel[] data, int start, int end, string target)
{
int midpoint = start + ((end - start) / 2);
target = target.ToUpper().Trim();
string mid = data[midpoint].Name;
if (start == end-1)
if (start == end - 1)
{
if (mid == target)
{
@ -110,26 +114,31 @@ namespace EFB.Models
//Split the data down to the left and the right portions
NavdataModel[] left = MergeSort(ref data, start, midpoint);
NavdataModel[] right = MergeSort(ref data, midpoint+1, end);
NavdataModel[] right = MergeSort(ref data, midpoint + 1, end);
List<NavdataModel> combined = new List<NavdataModel>();
int leftPointer = 0;
int rightPointer = 0;
while (leftPointer <= left.Length-1 || rightPointer <= right.Length-1)
while (leftPointer <= left.Length - 1 || rightPointer <= right.Length - 1)
{
if (leftPointer == left.Length)
{//Take a value only from the right (left pointer had reached the end)
AddValue(ref combined, right[rightPointer], ref rightPointer);
}else if (rightPointer == right.Length)
}
else if (rightPointer == right.Length)
{//Take a value only from the left (right pointer has reached the end)
AddValue(ref combined, left[leftPointer], ref leftPointer);
}else{
}
else
{
if (String.Compare(left[leftPointer].Name, right[rightPointer].Name) <= 0)
{//Take a value from the left hand side of the list. (Left value is considered 'smaller')
AddValue(ref combined, left[leftPointer], ref leftPointer);
}else{//Take a value from the right (right value is considered smaller)
}
else
{//Take a value from the right (right value is considered smaller)
AddValue(ref combined, right[rightPointer], ref rightPointer);
}
}
@ -139,10 +148,11 @@ namespace EFB.Models
}
private static void AddValue(ref List<NavdataModel> data, NavdataModel value, ref int pointer){
private static void AddValue(ref List<NavdataModel> data, NavdataModel value, ref int pointer)
{
pointer++;
data.Add(value);
}
}
}

View File

@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EFB.Models
{
public class ResponseModel<T>
@ -10,6 +5,6 @@ namespace EFB.Models
//Object should be of known type from sender
public T Result { get; set; } = default(T);
public string Error { get; set; } = null;
}
}

View File

@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EFB.Models.Route
{
public interface IWaypoint
@ -10,11 +5,11 @@ namespace EFB.Models.Route
public string Name { get; set; }
public float Longitude { get; set; }
public float Latitude { get; set; }
public string Airway { get; set; }
public IWaypoint Next { get; set; }
public IWaypoint Previous { get; set; }
public bool Visited { get; set; }
}
}

View File

@ -1,23 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EFB.Models.Route
{
public class NavaidModel:IWaypoint
public class NavaidModel : IWaypoint
{
public string Name { get; set; }
public float Longitude { get; set; }
public float Latitude { get; set; }
public int Frequency { get; set; }
public string Airway { get; set; }
public IWaypoint Next { get; set; } = null;
public IWaypoint Previous { get; set; } = null;
public bool Visited { get; set; } = false;
public NavaidModel(string name, string airway, float longitude, float latitude){
public NavaidModel(string name, string airway, float longitude, float latitude)
{
Name = name;
Airway = airway;
Longitude = longitude;

View File

@ -1,27 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EFB.Models.Route
{
public class WaypointModel:IWaypoint
public class WaypointModel : IWaypoint
{
public string Name { get; set; }
public float Longitude { get; set; }
public float Latitude { get; set; }
public string Airway { get; set; }
public IWaypoint Next { get; set; } = null;
public IWaypoint Previous { get; set; } = null;
public bool Visited { get; set; }
public WaypointModel(string name, string airway, float longitude, float latitude){
public WaypointModel(string name, string airway, float longitude, float latitude)
{
Name = name;
Airway = airway;
Longitude = longitude;
Latitude = latitude;
}
}
}

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFB.Models.Route;
using EFB.Controllers.Form;
using System.Net.Http;
using EFB.Models.Route;
using System.Threading.Tasks;
namespace EFB.Models
{
@ -19,7 +15,8 @@ namespace EFB.Models
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){
public RouteModel(string departure, string departureRoute, string arrival, string arrivalRoute, uint cruise)
{
if (FormAuthenticator.ValidateICAOCode(departure))
{
Departure = new WaypointModel(departure, departureRoute, 0, 0);
@ -36,16 +33,20 @@ namespace EFB.Models
}
}
public IWaypoint Next(){
if(Current.Next != null){
public IWaypoint Next()
{
if (Current.Next != null)
{
Current = Current.Next;
return Current;
}
return null;
}
public IWaypoint Previous(){
if(Current.Previous != null){
public IWaypoint Previous()
{
if (Current.Previous != null)
{
Current = Current.Previous;
return Current;
}
@ -53,7 +54,8 @@ namespace EFB.Models
}
//Generate a route Object
public static async Task<RouteModel> StringToRoute(string departure, string arrival, uint cruise, string routeString){
public static async Task<RouteModel> StringToRoute(string departure, string arrival, uint cruise, string routeString)
{
var navdataFetch = NavdataModel.Populate();
string[] routeTemp = routeString.Split(" ");
@ -69,10 +71,10 @@ namespace EFB.Models
NavdataModel[] navdata = await navdataFetch;
navdata = NavdataModel.MergeSort(ref navdata, 0, navdata.Length - 1);
for (var i = 1; i < routeTemp.Length-1; i+=2)
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]);
NavdataModel currentWaypoint = NavdataModel.BinarySearch(ref navdata, 0, navdata.Length - 1, routeTemp[i]);
if (currentWaypoint == null)
{
currentWaypoint = new NavdataModel(0, routeTemp[i], null, "0", "0");
@ -81,26 +83,27 @@ namespace EFB.Models
if (routeTemp[i].Length > 3)
{//waypoint Type
next = new WaypointModel(
routeTemp[i],
routeTemp[i+1],
float.Parse(currentWaypoint.Longitude),
routeTemp[i],
routeTemp[i + 1],
float.Parse(currentWaypoint.Longitude),
float.Parse(currentWaypoint.Latitude)
);
}else
);
}
else
{//Navaid Type
next = new NavaidModel(
routeTemp[i],
routeTemp[i+1],
float.Parse(currentWaypoint.Longitude),
routeTemp[i],
routeTemp[i + 1],
float.Parse(currentWaypoint.Longitude),
float.Parse(currentWaypoint.Latitude)
);
);
}
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;
@ -109,8 +112,8 @@ namespace EFB.Models
route.Current = route.Departure;
//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);
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);
@ -123,7 +126,8 @@ namespace EFB.Models
//Generate a route String
public static string ParseRoute(string route){
public static string ParseRoute(string route)
{
route.Replace('/', ' ');
var routeArr = route.Split(' ');
@ -138,13 +142,13 @@ namespace EFB.Models
if (waypoint.Length == 7 && finalRoute.Length > 8)
break;
}
}
return finalRoute;
}
}
}

View File

@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization.Attributes;
using System;
namespace EFB.Models
{
@ -16,12 +12,13 @@ namespace EFB.Models
public DateTime LatestPacketUpdate { get; set; }
public SimPosition LatestPosition { get; set; } = null;
public SimPositionModel(string email, SimPosition position){
public SimPositionModel(string email, SimPosition position)
{
EMail = email;
LatestPacketUpdate = DateTime.Now;
LatestPosition = position;
}
}
@ -30,8 +27,9 @@ namespace EFB.Models
public float Latitude { get; set; }
public float Longitude { get; set; }
public int Altitude { get; set; }
public SimPosition(float latitude, float longitude, int altitude){
public SimPosition(float latitude, float longitude, int altitude)
{
Latitude = latitude;
Longitude = longitude;
Altitude = altitude;

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EFB.Models
{
@ -13,7 +10,8 @@ namespace EFB.Models
public string TokenValue { get; init; }
public DateTime Expiration { get; init; }
public bool IsExpired(){
public bool IsExpired()
{
//Check if the current time is beyond expiration
if (DateTime.UtcNow > Expiration)
{
@ -21,6 +19,6 @@ namespace EFB.Models
}
return false;
}
}
}

View File

@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EFB.Models
{
public class UserModel
@ -19,14 +14,14 @@ namespace EFB.Models
public object Id { get; init; }
public string EMail { get; init; }
public TokenModel UserToken { get; set; } = null;
//Contains the most recent route generated by the user through the App
public string Departure { get; set; }
public string Route { get; set; }
public string Arrival { get; set; }
public uint Cruise { get; set; }
public TokenModel RouteToken { get; set; } = null;
//Contains the Departure and Arrival Charts for the user's route
@ -36,6 +31,6 @@ namespace EFB.Models
//Contains the most recently stored position of the user in the simulator
public object SimPosition { get; set; } = null;
}
}

View File

@ -1,14 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Razor;
using Newtonsoft.Json;
using EFB.Models.JSON;
using Microsoft.AspNetCore;
using EFB.Sessions;
using Newtonsoft.Json;
namespace EFB.Models
{
@ -16,8 +7,9 @@ namespace EFB.Models
{
public ChartModel Charts { get; set; }
public Chart Selected { get; set; }
public ViewChartModel(ChartModel current, string selected){
public ViewChartModel(ChartModel current, string selected)
{
Charts = current;
if (selected != null)
{