2022-01-26 13:11:04 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MySql.Data.MySqlClient;
|
2022-02-01 20:25:31 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using EFB.Sessions;
|
2022-01-26 13:11:04 +00:00
|
|
|
|
|
|
|
namespace EFB.Models
|
|
|
|
{
|
|
|
|
public class NavdataModel
|
|
|
|
{
|
|
|
|
public int Id { get; set; }
|
|
|
|
public string Name { get; set; }
|
2022-02-01 20:25:31 +00:00
|
|
|
public string Type { get; set; }
|
2022-01-26 13:11:04 +00:00
|
|
|
public int? Frequency { get; set; }
|
|
|
|
public string Latitude { get; set; }
|
|
|
|
public string Longitude { get; set; }
|
|
|
|
|
|
|
|
|
2022-02-01 20:25:31 +00:00
|
|
|
public NavdataModel(int id, string name, string type, string latitude, string longitude){
|
2022-01-26 13:11:04 +00:00
|
|
|
Id = id;
|
|
|
|
Name = name;
|
2022-02-01 20:25:31 +00:00
|
|
|
Type = type;
|
2022-01-26 13:11:04 +00:00
|
|
|
Frequency = null;
|
|
|
|
Latitude = latitude;
|
|
|
|
Longitude = longitude;
|
|
|
|
}
|
|
|
|
|
2022-02-01 20:25:31 +00:00
|
|
|
[JsonConstructor]
|
|
|
|
public NavdataModel(int id, string name, string type, int? frequency, string latitude, string longitude){
|
2022-01-26 13:11:04 +00:00
|
|
|
Id = id;
|
|
|
|
Name = name;
|
2022-02-01 20:25:31 +00:00
|
|
|
Type = type;
|
2022-01-26 13:11:04 +00:00
|
|
|
Frequency = frequency;
|
|
|
|
Latitude = latitude;
|
|
|
|
Longitude = longitude;
|
|
|
|
}
|
|
|
|
|
2022-01-31 15:07:16 +00:00
|
|
|
public static async Task<NavdataModel[]> Populate(){
|
2022-02-09 20:41:39 +00:00
|
|
|
string password = Environment.GetEnvironmentVariable("MySQLPassword", EnvironmentVariableTarget.User);
|
|
|
|
MySqlConnection con = new MySqlConnection($"server=server.luke-else.co.uk;userid=root;password={password};database=EFB");
|
2022-01-26 13:34:49 +00:00
|
|
|
con.Open();
|
|
|
|
|
2022-01-31 15:07:16 +00:00
|
|
|
// Console.WriteLine($"MySQL version : {con.ServerVersion}");
|
|
|
|
|
|
|
|
string query = "SELECT * FROM waypoints";
|
|
|
|
MySqlCommand command = new MySqlCommand(query, con);
|
|
|
|
|
|
|
|
MySqlDataReader reader = (MySqlDataReader) await command.ExecuteReaderAsync();
|
|
|
|
|
|
|
|
List<NavdataModel> navdata = new List<NavdataModel>();
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
{
|
2022-02-17 20:21:50 +00:00
|
|
|
int id = reader.GetInt32("id");
|
|
|
|
string name = reader.GetString("name");
|
|
|
|
string type = reader.GetString("type");
|
|
|
|
string latitude = reader.GetString("latitude");
|
|
|
|
string longitude = reader.GetString("longitude");
|
2022-01-31 15:07:16 +00:00
|
|
|
|
2022-02-01 20:25:31 +00:00
|
|
|
if (type == "VOR" || type == "NDB")
|
2022-01-31 15:07:16 +00:00
|
|
|
{
|
|
|
|
// int? frequency = reader.GetInt32(3);
|
|
|
|
int? frequency = null;
|
|
|
|
navdata.Add(
|
2022-02-01 20:25:31 +00:00
|
|
|
new NavdataModel(id, name, type, frequency, latitude, longitude)
|
2022-01-31 15:07:16 +00:00
|
|
|
);
|
|
|
|
}else{
|
|
|
|
navdata.Add(
|
2022-02-01 20:25:31 +00:00
|
|
|
new NavdataModel(id, name, type, latitude, longitude)
|
2022-01-31 15:07:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return navdata.ToArray<NavdataModel>();
|
2022-01-31 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 (mid == target)
|
|
|
|
{
|
|
|
|
return data[midpoint];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-26 13:34:49 +00:00
|
|
|
|
2022-01-31 15:59:44 +00:00
|
|
|
if (String.Compare(target, mid) < 0)
|
|
|
|
{
|
|
|
|
return BinarySearch(ref data, start, midpoint, target);
|
|
|
|
}
|
|
|
|
return BinarySearch(ref data, midpoint, end, target);
|
2022-01-26 13:11:04 +00:00
|
|
|
}
|
2022-01-31 22:50:09 +00:00
|
|
|
|
|
|
|
public static NavdataModel[] MergeSort(ref NavdataModel[] data, int start, int end)
|
|
|
|
{
|
|
|
|
if (start == end)
|
|
|
|
{//If we have narrowed it down to a single Item
|
|
|
|
return new NavdataModel[] { data[start] };
|
|
|
|
}
|
|
|
|
|
|
|
|
int midpoint = start + ((end - start) / 2);
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
|
|
|
List<NavdataModel> combined = new List<NavdataModel>();
|
|
|
|
|
|
|
|
int leftPointer = 0;
|
|
|
|
int rightPointer = 0;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{//Take a value only from the left (right pointer has reached the end)
|
|
|
|
AddValue(ref combined, left[leftPointer], ref leftPointer);
|
|
|
|
}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)
|
|
|
|
AddValue(ref combined, right[rightPointer], ref rightPointer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return combined.ToArray();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void AddValue(ref List<NavdataModel> data, NavdataModel value, ref int pointer){
|
|
|
|
pointer++;
|
|
|
|
data.Add(value);
|
|
|
|
}
|
2022-01-26 13:11:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|