Skip to content

Commit

Permalink
v1.4.1 - Fixed waypoint of zero altitude causing exception. Rewrote w…
Browse files Browse the repository at this point in the history
…aypoint algorithm. Added logger for general exception that would cause disconnect from SimConnect.
  • Loading branch information
SAHorowitz committed Aug 24, 2021
1 parent 10472fa commit 617ad5c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 88 deletions.
122 changes: 35 additions & 87 deletions FS2020PlanePath/FS2020_SQLLiteDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class FlightWaypointData
public double gps_wp_longitude;
public Int32 gps_wp_altitude;
public string gps_wp_name;
public bool waypoint_valid;

public FlightWaypointData()
{
Expand All @@ -111,89 +112,33 @@ public FlightWaypointData(double gps_wp_lat, double gps_wp_long, Int32 gps_wp_al
gps_wp_longitude = gps_wp_long;
gps_wp_altitude = gps_wp_alt;
gps_wp_name = gps_wp_id;

if ((gps_wp_altitude >= 0) &&
(gps_wp_latitude != 0) &&
(gps_wp_longitude != 0) &&
(gps_wp_name.Length > 0))
waypoint_valid = true;
else
waypoint_valid = false;
}
}

class FlightPlan
{
public List<FlightWaypointData> flight_waypoints;
private int flightplan_index;
private int flightplan_count;

public FlightPlan()
{
flight_waypoints = new List<FlightWaypointData>();
}

public bool IsWaypointValid(FlightWaypointData waypointData)
{
if ((waypointData.gps_wp_altitude > 0) &&
(waypointData.gps_wp_latitude != 0) &&
(waypointData.gps_wp_longitude != 0) &&
(waypointData.gps_wp_name.Length > 0))
return true;
else
return false;
}

public void AddFlightPlanWaypoint(FlightWaypointData prevWaypoint, FlightWaypointData nextWaypoint, int iflightplan_index, int iflightplan_count)
{
// if there is already at least one item in the list then compare last item to see if it matches
if (flight_waypoints.Count > 0)
{
FlightWaypointData fwdLastWaypointinList;

fwdLastWaypointinList = flight_waypoints[flight_waypoints.Count - 1];

// if the next waypoint is not equal to last in the list then something has changed
if (String.Compare(fwdLastWaypointinList.gps_wp_name, nextWaypoint.gps_wp_name) != 0)
// if last waypoint in list is equal to the prev waypoint then we can add next waypoint to the list
// because it means user crossed the waypoint and possibly heading to next or is done with flightplan
if (String.Compare(fwdLastWaypointinList.gps_wp_name, prevWaypoint.gps_wp_name) == 0)
{
if (IsWaypointValid(nextWaypoint) == true)
flight_waypoints.Add(nextWaypoint);
flightplan_index = iflightplan_index;
flightplan_count = iflightplan_count;
}
else
{
FlightWaypointData fwdSecondToLastWaypointinList;

fwdSecondToLastWaypointinList = flight_waypoints[flight_waypoints.Count - 2];

// if second to last is same as previous then user just changed next waypoint so just replace it
if (String.Compare(fwdSecondToLastWaypointinList.gps_wp_name, prevWaypoint.gps_wp_name) == 0)
{
flight_waypoints.RemoveAt(flight_waypoints.Count - 1);
if (IsWaypointValid(nextWaypoint) == true)
flight_waypoints.Add(nextWaypoint);
}
else
{
// these new waypoints don't match anything we have so user changed flightplan?
flight_waypoints.Clear();
if (IsWaypointValid(prevWaypoint) == true)
flight_waypoints.Add(prevWaypoint);
if (iflightplan_index < iflightplan_count)
if (IsWaypointValid(nextWaypoint) == true)
flight_waypoints.Add(nextWaypoint);
}
flightplan_index = iflightplan_index;
flightplan_count = iflightplan_count;
}
}
else // add new items that are valid
{
if (IsWaypointValid(prevWaypoint) == true)
flight_waypoints.Add(prevWaypoint);
// if not at end of flightplan then there should be a next waypoint
if (iflightplan_index < iflightplan_count)
if (IsWaypointValid(nextWaypoint) == true)
flight_waypoints.Add(nextWaypoint);
flightplan_index = iflightplan_index;
flightplan_count = iflightplan_count;
}
// if no waypoints yet or if last waypoint does not equal waypoint we just passed then add it to waypoint list. Note this does not add the last waypoint of flight
// to the waypoint list since very last waypoint is never past but StopLoggingAction will add last waypoint if it is an airport and within 2 miles of where flight ended
if ((flight_waypoints.Count == 0) ||
((flight_waypoints.Count > 0) && (String.Compare(flight_waypoints.Last().gps_wp_name, prevWaypoint.gps_wp_name) != 0)))
flight_waypoints.Add(prevWaypoint);
}
}

Expand Down Expand Up @@ -1067,26 +1012,29 @@ public void WriteFlightPlan(int pk, List<FlightWaypointData> flight_waypoints)

foreach (FlightWaypointData waypoint in flight_waypoints)
{
sqlite_cmd.Parameters.Clear();
sqlite_cmd.Parameters.AddWithValue("@FlightID", pk);
sqlite_cmd.Parameters.AddWithValue("@latitude", waypoint.gps_wp_latitude);
sqlite_cmd.Parameters.AddWithValue("@longitude", waypoint.gps_wp_longitude);
sqlite_cmd.Parameters.AddWithValue("@altitude", waypoint.gps_wp_altitude);
if (waypoint.waypoint_valid == true)
{
sqlite_cmd.Parameters.Clear();
sqlite_cmd.Parameters.AddWithValue("@FlightID", pk);
sqlite_cmd.Parameters.AddWithValue("@latitude", waypoint.gps_wp_latitude);
sqlite_cmd.Parameters.AddWithValue("@longitude", waypoint.gps_wp_longitude);
sqlite_cmd.Parameters.AddWithValue("@altitude", waypoint.gps_wp_altitude);

// verify that waypoint name is a valid string
Regex r = new Regex(@"[^\w\.@-]");
if (r.IsMatch(waypoint.gps_wp_name))
waypoint.gps_wp_name = Program.sInvalidWaypointName;
// verify that waypoint name is a valid string
Regex r = new Regex(@"[^\w\.@-]");
if (r.IsMatch(waypoint.gps_wp_name))
waypoint.gps_wp_name = Program.sInvalidWaypointName;

sqlite_cmd.Parameters.AddWithValue("@name", waypoint.gps_wp_name);
try
{
sqlite_cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex);
throw ex;
sqlite_cmd.Parameters.AddWithValue("@name", waypoint.gps_wp_name);
try
{
sqlite_cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex);
throw ex;
}
}
}
transaction.Commit();
Expand Down
1 change: 1 addition & 0 deletions FS2020PlanePath/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ protected override void DefWndProc(ref Message m)
catch (Exception ex)
{
SimConnectStatusLabel.Text = "Connection lost to SimConnect";
Program.ErrorLogging("Error: Connection lost to SimConnect - any recordings occurring during this time have automatically stopped and been saved.", ex);
StopLoggingBtn.PerformClick();
}
}
Expand Down
2 changes: 1 addition & 1 deletion FS2020PlanePath/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static class Program
/// <summary>
/// The main entry point for the application.
/// </summary>
static public string sAppVersion = "1.4.0";
static public string sAppVersion = "1.4.1";
static public string sInvalidWaypointName = "Unknown Waypoint";
static public bool bLogErrorsWritten = false;

Expand Down
Binary file added docs/images/PPRv14xx.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 617ad5c

Please sign in to comment.