Compare commits

..

No commits in common. "eff67b76976ba027b4053b40b31de16449c82b35" and "8ac4a3d6931455cd076a10acd13482c4fb720574" have entirely different histories.

14 changed files with 22 additions and 299 deletions

View File

@ -1,81 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EFB.Sessions;
using Microsoft.Extensions.Logging;
using EFB.Models;
using EFB.MongoData;
using EFB.Models.Route;
namespace EFB.Controllers
{
//[Route("[controller]")]
public class FlightsimController : Controller
{
private readonly ILogger<FlightsimController> _logger;
public FlightsimController(ILogger<FlightsimController> logger)
{
_logger = logger;
}
public async Task<IActionResult> Index()
{
//Retrieve and Check current user status
UserModel user = HttpContext.Session.GetObject<UserModel>("User");
if(user == null) return RedirectToAction("Index", "Home");
//Retrieve the user's latest sim position and construct it into FlightsimModel
if (user.Route == null) return RedirectToAction("Index", "Route");
SimPositionModel latestPositionModel = await Mongo.GetLatestData(user.EMail);
RouteModel route = await RouteModel.StringToRoute(user.Departure, user.Arrival, user.Cruise, user.Route);
IWaypoint closest = DetermineClosest(route, latestPositionModel.LatestPosition);
return View(new FlightsimModel(latestPositionModel, closest));
}
private IWaypoint DetermineClosest(RouteModel route, SimPosition currentPosition){
IWaypoint closest = null;
float closestDistance = float.MaxValue;
//Assuming that we are on the earth for simplicity
IWaypoint waypoint = route.Departure;
while (waypoint.Next != null)
{
int earthRadius = 6371;
float distanceLat = DegreesToRadians(waypoint.Latitude - currentPosition.Latitude);
float distanceLon = DegreesToRadians(waypoint.Longitude - currentPosition.Longitude);
float latitude1 = DegreesToRadians(currentPosition.Latitude);
float latitude2 = DegreesToRadians(waypoint.Latitude);
var a = Math.Sin(distanceLat/2) * Math.Sin(distanceLat/2) + Math.Sin(distanceLon/2) * Math.Sin(distanceLon/2) * Math.Cos(latitude1) * Math.Cos(latitude2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a));
float distance = (float)(earthRadius * c);
if (distance < closestDistance)
{
closest = waypoint;
closestDistance = distance;
}
waypoint = waypoint.Next;
}
return closest;
}
private float DegreesToRadians(float degrees){
return (float)(degrees * Math.PI / 180);
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View("Error!");
}
}
}

View File

@ -144,7 +144,7 @@ namespace EFB.Controllers
foreach (var item in responsePoll.Result)
{
if (item.Command == "notvalid" || item.Command == "solution")
if (item.Command == "fpl" || item.Command == "solution")
{
collected = true;
routeString = item.FlightPlan;
@ -175,9 +175,6 @@ namespace EFB.Controllers
//fill in route
string finalRoute = RouteModel.ParseRoute(routeString);
user.Route = finalRoute;
user.Departure = departure;
user.Arrival = arrival;
user.Cruise = cruise;
HttpContext.Session.SetObject("User", user);
return RedirectToAction("Index", "Route");

View File

@ -5,7 +5,5 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
<PackageReference Include="MySql.Data" Version="*"/>
<PackageReference Include="MongoDB.Driver" Version="*"/>
<PackageReference Include="MongoDB.Bson" Version="*"/>
</ItemGroup>
</Project>

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFB.Models.Route;
namespace EFB.Models
{
public class FlightsimModel
{
public SimPositionModel CurrentPosition { get; set; }
public IWaypoint Closest { get; set; }
public FlightsimModel(SimPositionModel position, IWaypoint closest)
{
CurrentPosition = position;
Closest = closest;
}
}
}

View File

@ -54,11 +54,11 @@ namespace EFB.Models
while (reader.Read())
{
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");
int id = reader.GetInt32(0);
string name = reader.GetString(1);
string type = reader.GetString(2);
string latitude = reader.GetString(4);
string longitude = reader.GetString(5);
if (type == "VOR" || type == "NDB")
{

View File

@ -8,8 +8,8 @@ namespace EFB.Models.Route
public interface IWaypoint
{
public string Name { get; set; }
public float Longitude { get; set; }
public float Latitude { get; set; }
public string Longitude { get; set; }
public string Latitude { get; set; }
public string Airway { get; set; }
public IWaypoint Next { get; set; }

View File

@ -8,8 +8,8 @@ namespace EFB.Models.Route
public class NavaidModel:IWaypoint
{
public string Name { get; set; }
public float Longitude { get; set; }
public float Latitude { get; set; }
public string Longitude { get; set; }
public string Latitude { get; set; }
public int Frequency { get; set; }
public string Airway { get; set; }
@ -17,11 +17,9 @@ namespace EFB.Models.Route
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){
Name = name;
Airway = airway;
Longitude = longitude;
Latitude = latitude;
}
}
}

View File

@ -8,19 +8,17 @@ namespace EFB.Models.Route
public class WaypointModel:IWaypoint
{
public string Name { get; set; }
public float Longitude { get; set; }
public float Latitude { get; set; }
public string Longitude { get; set; }
public string 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){
Name = name;
Airway = airway;
Longitude = longitude;
Latitude = latitude;
}
}

View File

@ -19,15 +19,15 @@ 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);
Departure = new WaypointModel(departure, departureRoute);
}
if (FormAuthenticator.ValidateICAOCode(arrival))
{
Arrival = new WaypointModel(arrival, arrivalRoute, 0, 0);
Arrival = new WaypointModel(arrival, arrivalRoute);
}
if (FormAuthenticator.ValidateCruiseAlt(cruise))
@ -53,8 +53,7 @@ namespace EFB.Models
}
//Generate a route Object
public static async Task<RouteModel> StringToRoute(string departure, string arrival, uint cruise, string routeString){
var navdataFetch = NavdataModel.Populate();
public static RouteModel StringToRoute(string departure, string arrival, uint cruise, string routeString){
string[] routeTemp = routeString.Split(" ");
//Set departure and arrival route
@ -66,34 +65,17 @@ namespace EFB.Models
route.Current = route.Departure;
NavdataModel[] navdata = await navdataFetch;
navdata = NavdataModel.MergeSort(ref navdata, 0, navdata.Length - 1);
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");
}
//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)
);
next = new WaypointModel(routeTemp[i], routeTemp[i+1]);
}else
{//Navaid Type
next = new NavaidModel(
routeTemp[i],
routeTemp[i+1],
float.Parse(currentWaypoint.Longitude),
float.Parse(currentWaypoint.Latitude)
);
next = new NavaidModel(routeTemp[i], routeTemp[i+1]);
}
next.Previous = route.Current;
@ -108,15 +90,6 @@ 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);
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);
return route;
}

View File

@ -1,53 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
using MongoDB.Driver;
namespace EFB.Models
{
public class SimPositionModel
{
[BsonId]
public ObjectId Id { get; set; }
public string EMail { get; set; } = "";
public DateTime LatestPacketUpdate { get; set; }
public SimPosition LatestPosition { get; set; } = null;
public SimPositionModel(string email, SimPosition position){
EMail = email;
LatestPacketUpdate = DateTime.Now;
LatestPosition = position;
}
}
public class SimPosition
{
public float Latitude { get; set; }
public float Longitude { get; set; }
public int Altitude { get; set; }
public SimPosition(float latitude, float longitude, int altitude){
Latitude = latitude;
Longitude = longitude;
Altitude = altitude;
}
//**Packet Processing not required**//
// public SimPosition(Packet[] data){
// if (data[0].Data != null)
// {
// //Use Linq to search through the packets for a given id and use that data
// Latitude = (data.Where(x => x.Id == 22).Select(x => x.Data[0]).ToArray())[0];
// Longitude = (data.Where(x => x.Id == 23).Select(x => x.Data[0]).ToArray())[0];
// Altitude = Convert.ToInt32((data.Where(x => x.Id == 24).Select(x => x.Data[0]).ToArray())[0]);
// }
// }
}
}

View File

@ -21,12 +21,7 @@ namespace EFB.Models
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

View File

@ -1,30 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;
using EFB.Models;
namespace EFB.MongoData
{
public class Mongo
{
//function that is responsible to getting the user's latest sim position from the MongoDB
public static async Task<SimPositionModel> GetLatestData(string email){
MongoClient client = new MongoClient(
Environment.GetEnvironmentVariable("MongoDBConnectionString", EnvironmentVariableTarget.User)
);
MongoDatabaseBase database = (MongoDatabaseBase)client.GetDatabase("EFB");
MongoCollectionBase<SimPositionModel> collection = (MongoCollectionBase<SimPositionModel>)database.GetCollection<SimPositionModel>("Simdata");
FilterDefinition<SimPositionModel> filter = Builders<SimPositionModel>.Filter.Eq(x => x.EMail, email);
var data = await collection.FindAsync<SimPositionModel>(filter).Result.ToListAsync();
if (data.Count > 0)
{
return data[0];
}
return null;
}
}
}

View File

@ -1,53 +0,0 @@
@model EFB.Models.FlightsimModel;
@{
ViewData["Title"] = "Welcome";
}
<div class="row d-flex">
<div class="card-body col-md-6 bg-primary">
<div class="container jumbotron">
<h3>Current Position</h3>
<br />
<br />
<h5 style="color: gray;">Last Updated at: @Model.CurrentPosition.LatestPacketUpdate.ToString()</h5>
<div class="row d-flex">
<div class="col-md-6">
<h4>Latitude</h4>
@Model.CurrentPosition.LatestPosition.Latitude
<h4>Longitude</h4>
@Model.CurrentPosition.LatestPosition.Longitude
</div>
<div class="col-md-6">
<h4>Altitude</h4>
@Model.CurrentPosition.LatestPosition.Altitude ft
</div>
</div>
</div>
</div>
<div class="card-body col-md-6 bg-success">
<div class="container jumbotron">
<h3>Closest Waypoint</h3>
<br />
<br />
<h4>@Model.Closest.Name -> @Model.Closest.Airway</h4>
<h4>Latitude</h4>
@Model.Closest.Latitude
<h4>Longitude</h4>
@Model.Closest.Longitude
</div>
</div>
</div>

View File

@ -35,7 +35,7 @@
<a class="nav-link text-light" asp-area="" asp-controller="Charts" asp-action="Index">Charts</a>
</li>
<li class="nav-item">
<a class="nav-link text-light" asp-area="" asp-controller="Flightsim" asp-action="Index">FlightSim</a>
<a class="nav-link text-light" asp-area="" asp-controller="" asp-action="Index">FlightSim</a>
</li>
@{