diff --git a/FS2020PlanePath/FS2020_SQLLiteDB.cs b/FS2020PlanePath/FS2020_SQLLiteDB.cs index da10e13..0cc9b7d 100644 --- a/FS2020PlanePath/FS2020_SQLLiteDB.cs +++ b/FS2020PlanePath/FS2020_SQLLiteDB.cs @@ -1,11 +1,14 @@ using Microsoft.SqlServer.Server; using System; using System.Collections.Generic; +using System.Data.SqlClient; using System.Data.SQLite; using System.Linq; +using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using System.Windows.Forms; namespace FS2020PlanePath { @@ -173,7 +176,7 @@ class FS2020_SQLLiteDB SQLiteConnection sqlite_conn; private const int TblVersion_Flights = 1; private const int TblVersion_FlightSamples = 1; - private const int TblVersion_FlightSampleDetails = 2; + private const int TblVersion_FlightSampleDetails = 3; private const int TblVersion_FlightWaypoints = 1; private const int TblVersion_FlightOptions = 1; @@ -193,9 +196,25 @@ void CreateConnection() catch (Exception ex) { sqlite_conn = null; + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "new SQLiteConnection", null), ex); } } + private string GetDBQueryExtraInfo(string strCallerName, string strQueryType, SQLiteCommand sqlite_cmd) + { + string sAdtlInfo = strCallerName + " " + strQueryType; + if (sqlite_cmd != null) + { + sAdtlInfo += ": " + sqlite_cmd.CommandText.ToString(); + foreach (SQLiteParameter p in sqlite_cmd.Parameters) + { + string isQuted = (p.Value is string) ? "'" : ""; + sAdtlInfo += "\n " + p.ParameterName.ToString() + " = " + isQuted + p.Value.ToString() + isQuted; + } + } + return sAdtlInfo; + } + public void CreateTables() { SQLiteCommand sqlite_cmd; @@ -206,7 +225,15 @@ public void CreateTables() sqlite_cmd = sqlite_conn.CreateCommand(); Createsql = "CREATE TABLE Flights (FlightID INTEGER PRIMARY KEY, aircraft varchar(256), start_datetimestamp long)"; sqlite_cmd.CommandText = Createsql; - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } if (CheckTableExists("TblVersions") == false) @@ -214,7 +241,15 @@ public void CreateTables() sqlite_cmd = sqlite_conn.CreateCommand(); Createsql = "CREATE TABLE TblVersions (tblVersionID INTEGER PRIMARY KEY, tblname varchar(256), tblversion int)"; sqlite_cmd.CommandText = Createsql; - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } @@ -224,7 +259,15 @@ public void CreateTables() Createsql = "CREATE TABLE FlightSamples (FlightSamplesID INTEGER PRIMARY KEY, FlightID integer NOT NULL, latitude double, longitude double, altitude int32, sample_datetimestamp long, "; Createsql += "FOREIGN KEY (FlightID) REFERENCES Flight(FlightID) )"; sqlite_cmd.CommandText = Createsql; - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } if (CheckTableExists("FlightSampleDetails") == false) @@ -234,9 +277,18 @@ public void CreateTables() Createsql += "engine3rpm int32, engine4rpm int32, lightsmask int32, ground_velocity double, plane_pitch double, plane_bank double, plane_heading_true double, "; Createsql += "plane_heading_magnetic double, plane_airspeed_indicated double, airspeed_true double, vertical_speed double, heading_indicator double, flaps_handle_position int32, "; Createsql += "spoilers_handle_position int32, gear_handle_position int32, ambient_wind_velocity double, ambient_wind_direction double, ambient_temperature double, "; - Createsql += "stall_warning int32, overspeed_warning int32, is_gear_retractable int32, spoiler_available int32, FOREIGN KEY (FlightSamplesID) REFERENCES FlightSamples(FlightSamplesID) )"; + Createsql += "stall_warning int32, overspeed_warning int32, is_gear_retractable int32, spoiler_available int32, sim_on_ground int32, FOREIGN KEY (FlightSamplesID) "; + Createsql += "REFERENCES FlightSamples(FlightSamplesID) )"; sqlite_cmd.CommandText = Createsql; - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } if (CheckTableExists("FlightPathOptions") == false) @@ -244,7 +296,15 @@ public void CreateTables() sqlite_cmd = sqlite_conn.CreateCommand(); Createsql = "CREATE TABLE FlightPathOptions (OptionsID INTEGER PRIMARY KEY, optionname varchar(256), optionvalue varchar(512))"; sqlite_cmd.CommandText = Createsql; - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } if (CheckTableExists("FlightWaypoints") == false) @@ -253,7 +313,15 @@ public void CreateTables() Createsql = "CREATE TABLE FlightWaypoints (FlightWaypointsID INTEGER PRIMARY KEY, FlightID integer NOT NULL, latitude double, longitude double, altitude int32, name varchar(256), "; Createsql += "FOREIGN KEY (FlightID) REFERENCES Flight(FlightID) )"; sqlite_cmd.CommandText = Createsql; - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } // Fill in Table Versions if needed @@ -290,7 +358,15 @@ private void LoadUpTableVersions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@tblname", "Flights"); sqlite_cmd.Parameters.AddWithValue("@tblversion", TblVersion_Flights); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumVersionRows++; } if (nNumVersionRows == 1) @@ -298,7 +374,15 @@ private void LoadUpTableVersions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@tblname", "FlightSamples"); sqlite_cmd.Parameters.AddWithValue("@tblversion", TblVersion_FlightSamples); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumVersionRows++; } if (nNumVersionRows == 2) @@ -306,7 +390,15 @@ private void LoadUpTableVersions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@tblname", "FlightSampleDetails"); sqlite_cmd.Parameters.AddWithValue("@tblversion", TblVersion_FlightSampleDetails); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumVersionRows++; } if (nNumVersionRows == 3) @@ -314,7 +406,15 @@ private void LoadUpTableVersions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@tblname", "FlightPathOptions"); sqlite_cmd.Parameters.AddWithValue("@tblversion", TblVersion_FlightOptions); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumVersionRows++; } if (nNumVersionRows == 4) @@ -322,7 +422,15 @@ private void LoadUpTableVersions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@tblname", "FlightWaypoints"); sqlite_cmd.Parameters.AddWithValue("@tblversion", TblVersion_FlightWaypoints); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumVersionRows++; } } @@ -347,9 +455,18 @@ private int GetCurrentTableVersion(string sTable) Selectsql = "SELECT tblVersion FROM TblVersions WHERE tblName = @tblname"; sqlite_cmd.CommandText = Selectsql; sqlite_cmd.Parameters.AddWithValue("@tblname", sTable); - SQLiteDataReader r = sqlite_cmd.ExecuteReader(); - while (r.Read()) - nRetval = r.GetInt32(0); + try + { + SQLiteDataReader r = sqlite_cmd.ExecuteReader(); + + while (r.Read()) + nRetval = r.GetInt32(0); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteReader", sqlite_cmd), ex); + throw ex; + } return (nRetval); } @@ -365,18 +482,110 @@ private void UpdateFlightSampleDetailsTable(int nCurTableVersion) { Updatesql = "ALTER TABLE FlightSampleDetails ADD sim_on_ground int32"; sqlite_cmd.CommandText = Updatesql; - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } sqlite_cmd.Parameters.Clear(); Updatesql = "Update FlightSampleDetails SET sim_on_ground = 0"; sqlite_cmd.CommandText = Updatesql; - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } sqlite_cmd.Parameters.Clear(); Updatesql = "Update TblVersions SET tblVersion = @tblversion WHERE tblName = 'FlightSampleDetails'"; sqlite_cmd.CommandText = Updatesql; - sqlite_cmd.Parameters.AddWithValue("@tblversion", nCurTableVersion + 1); - sqlite_cmd.ExecuteNonQuery(); + nCurTableVersion = nCurTableVersion + 1; + sqlite_cmd.Parameters.AddWithValue("@tblversion", nCurTableVersion); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } + } + if (nCurTableVersion == 2) + { + // 2 was to fix a mistake where create table did not contain sim_on_ground + bool bSimOnGroundInDB = false; + + Updatesql = "Select * from FlightSampleDetails"; + sqlite_cmd.CommandText = Updatesql; + + try + { + using (SQLiteDataReader r = sqlite_cmd.ExecuteReader()) + { + // walk thru results of query and see if sim_on_ground column is there + for (var i = 0; i < r.FieldCount; i++) + if (String.Compare(r.GetName(i), "sim_on_ground") == 0) + bSimOnGroundInDB = true; + } + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteReader", sqlite_cmd), ex); + throw ex; + } + // if sim_on_ground is not in db then we need to add it + if (bSimOnGroundInDB == false) + { + Updatesql = "ALTER TABLE FlightSampleDetails ADD sim_on_ground int32"; + sqlite_cmd.CommandText = Updatesql; + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } + + sqlite_cmd.Parameters.Clear(); + Updatesql = "Update FlightSampleDetails SET sim_on_ground = 0"; + sqlite_cmd.CommandText = Updatesql; + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } + } + + sqlite_cmd.Parameters.Clear(); + Updatesql = "Update TblVersions SET tblVersion = @tblversion WHERE tblName = 'FlightSampleDetails'"; + sqlite_cmd.CommandText = Updatesql; + nCurTableVersion = nCurTableVersion + 1; + sqlite_cmd.Parameters.AddWithValue("@tblversion", nCurTableVersion); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } } @@ -391,7 +600,15 @@ private void LoadUpTableOptions() selectsql = "SELECT COUNT(OptionsID) from FlightPathOptions"; sqlite_cmd.CommandText = selectsql; - nNumOptionRows = Convert.ToInt32(sqlite_cmd.ExecuteScalar()); + try + { + nNumOptionRows = Convert.ToInt32(sqlite_cmd.ExecuteScalar()); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteScalar", sqlite_cmd), ex); + throw ex; + } // since we know the options and the order we have added them to the program over time // then we can use the number of rows to know what options we should add @@ -404,7 +621,15 @@ private void LoadUpTableOptions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@optionname", "AboveThresholdWriteFreq"); sqlite_cmd.Parameters.AddWithValue("@optionvalue", "5"); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumOptionRows++; } if (nNumOptionRows == 1) @@ -412,7 +637,15 @@ private void LoadUpTableOptions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@optionname", "ThresholdMinAltitude"); sqlite_cmd.Parameters.AddWithValue("@optionvalue", "500"); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumOptionRows++; } if (nNumOptionRows == 2) @@ -420,7 +653,15 @@ private void LoadUpTableOptions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@optionname", "KMLFilePath"); sqlite_cmd.Parameters.AddWithValue("@optionvalue", ""); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumOptionRows++; } if (nNumOptionRows == 3) @@ -428,7 +669,15 @@ private void LoadUpTableOptions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@optionname", "GoolgeEarthChoice"); sqlite_cmd.Parameters.AddWithValue("@optionvalue", "Application"); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumOptionRows++; } if (nNumOptionRows == 4) @@ -436,7 +685,15 @@ private void LoadUpTableOptions() sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@optionname", "SpeedUpVideoPlayback"); sqlite_cmd.Parameters.AddWithValue("@optionvalue", "true"); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } nNumOptionRows++; } } @@ -451,9 +708,18 @@ public String GetTableOption(String optionname) Selectsql = "SELECT optionvalue FROM FlightPathOptions WHERE optionname = @optionname"; sqlite_cmd.CommandText = Selectsql; sqlite_cmd.Parameters.AddWithValue("@optionname", optionname); - SQLiteDataReader r = sqlite_cmd.ExecuteReader(); - while (r.Read()) - sRetval = r.GetString(0); + try + { + SQLiteDataReader r = sqlite_cmd.ExecuteReader(); + + while (r.Read()) + sRetval = r.GetString(0); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteReader", sqlite_cmd), ex); + throw ex; + } return (sRetval); } @@ -468,7 +734,15 @@ public void WriteTableOption(String optionname, String optionvalue) sqlite_cmd.CommandText = Updatesql; sqlite_cmd.Parameters.AddWithValue("@optionname", optionname); sqlite_cmd.Parameters.AddWithValue("@optionvalue", optionvalue); - SQLiteDataReader r = sqlite_cmd.ExecuteReader(); + try + { + SQLiteDataReader r = sqlite_cmd.ExecuteReader(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteReader", sqlite_cmd), ex); + throw ex; + } } private bool CheckTableExists(String tblName) @@ -481,9 +755,18 @@ private bool CheckTableExists(String tblName) Selectsql = "SELECT name FROM sqlite_master WHERE type ='table' and name = @tblName"; sqlite_cmd.CommandText = Selectsql; sqlite_cmd.Parameters.AddWithValue("@tblName", tblName); - SQLiteDataReader r = sqlite_cmd.ExecuteReader(); - while (r.Read()) - bRetVal = true; + try + { + SQLiteDataReader r = sqlite_cmd.ExecuteReader(); + + while (r.Read()) + bRetVal = true; + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteReader", sqlite_cmd), ex); + throw ex; + } return (bRetVal); } @@ -501,7 +784,15 @@ public int WriteFlight(string aircraft) sqlite_cmd.CommandText = sqlStr; sqlite_cmd.Parameters.AddWithValue("@aircraft", aircraft); sqlite_cmd.Parameters.AddWithValue("@start_datetimestamp", DateTime.Now.Ticks); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } FlightID = sqlite_conn.LastInsertRowId; transaction.Commit(); @@ -524,7 +815,16 @@ public int WriteFlightPoint(long pk, double latitude, double longitude, Int32 al sqlite_cmd.Parameters.AddWithValue("@longitude", longitude); sqlite_cmd.Parameters.AddWithValue("@altitude", altitude); sqlite_cmd.Parameters.AddWithValue("@sample_datetimestamp", DateTime.Now.Ticks); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } + FlightSampleID = sqlite_conn.LastInsertRowId; transaction.Commit(); @@ -576,7 +876,15 @@ public void WriteFlightPointDetails(long pk, Int32 altitude_above_ground, Int32 sqlite_cmd.Parameters.AddWithValue("@is_gear_retractable", is_gear_retractable); sqlite_cmd.Parameters.AddWithValue("@spoiler_available", spoiler_available); sqlite_cmd.Parameters.AddWithValue("@sim_on_ground", sim_on_ground); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } public void WriteFlightPlan(int pk, List flight_waypoints) @@ -598,7 +906,15 @@ public void WriteFlightPlan(int pk, List flight_waypoints) sqlite_cmd.Parameters.AddWithValue("@longitude", waypoint.gps_wp_longitude); sqlite_cmd.Parameters.AddWithValue("@altitude", waypoint.gps_wp_altitude); sqlite_cmd.Parameters.AddWithValue("@name", waypoint.gps_wp_name); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } } transaction.Commit(); } @@ -612,17 +928,27 @@ public List GetFlightWaypoints(int pk) Selectsql = "Select latitude, longitude, altitude, name FROM FlightWaypoints WHERE FlightID = @FlightID"; sqlite_cmd.CommandText = Selectsql; sqlite_cmd.Parameters.AddWithValue("@FlightID", pk); - SQLiteDataReader r = sqlite_cmd.ExecuteReader(); - while (r.Read()) + try { - FlightWaypointData waypointData = new FlightWaypointData(); - waypointData.gps_wp_latitude = r.GetDouble(0); - waypointData.gps_wp_longitude = r.GetDouble(1); - waypointData.gps_wp_altitude = r.GetInt32(2); - waypointData.gps_wp_name = r.GetString(3); - - FlightWaypoints.Add(waypointData); + SQLiteDataReader r = sqlite_cmd.ExecuteReader(); + + while (r.Read()) + { + FlightWaypointData waypointData = new FlightWaypointData(); + waypointData.gps_wp_latitude = r.GetDouble(0); + waypointData.gps_wp_longitude = r.GetDouble(1); + waypointData.gps_wp_altitude = r.GetInt32(2); + waypointData.gps_wp_name = r.GetString(3); + + FlightWaypoints.Add(waypointData); + } + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteReader", sqlite_cmd), ex); + throw ex; } + return FlightWaypoints; } @@ -640,43 +966,53 @@ public List GetFlightPathData(int pk) Selectsql += "FROM FlightSamples, FlightSampleDetails WHERE FlightSampleDetails.FlightSamplesID = FlightSamples.FlightSamplesID AND FlightID = @FlightID"; sqlite_cmd.CommandText = Selectsql; sqlite_cmd.Parameters.AddWithValue("@FlightID", pk); - SQLiteDataReader r = sqlite_cmd.ExecuteReader(); - while (r.Read()) - { - FlightPathData data = new FlightPathData(); - data.latitude = r.GetDouble(0); - data.longitude = r.GetDouble(1); - data.altitude = r.GetInt32(2); - data.timestamp = r.GetInt64(3); - data.altitudeaboveground = r.GetInt32(4); - data.Eng1Rpm = r.GetInt32(5); - data.Eng2Rpm = r.GetInt32(6); - data.Eng3Rpm = r.GetInt32(7); - data.Eng4Rpm = r.GetInt32(8); - data.LightsMask = r.GetInt32(9); - data.ground_velocity = r.GetDouble(10); - data.plane_pitch = r.GetDouble(11); - data.plane_bank = r.GetDouble(12); - data.plane_heading_true = r.GetDouble(13); - data.plane_heading_magnetic = r.GetDouble(14); - data.plane_airspeed_indicated = r.GetDouble(15); - data.airspeed_true = r.GetDouble(16); - data.vertical_speed = r.GetDouble(17); - data.heading_indicator = r.GetDouble(18); - data.flaps_handle_position = r.GetInt32(19); - data.spoilers_handle_position = r.GetInt32(20); - data.gear_handle_position = r.GetInt32(21); - data.ambient_wind_velocity = r.GetDouble(22); - data.ambient_wind_direction = r.GetDouble(23); - data.ambient_temperature = r.GetDouble(24); - data.stall_warning = r.GetInt32(25); - data.overspeed_warning = r.GetInt32(26); - data.is_gear_retractable = r.GetInt32(27); - data.spoiler_available = r.GetInt32(28); - data.sim_on_ground = r.GetInt32(29); - - FlightPath.Add(data); + try + { + SQLiteDataReader r = sqlite_cmd.ExecuteReader(); + + while (r.Read()) + { + FlightPathData data = new FlightPathData(); + data.latitude = r.GetDouble(0); + data.longitude = r.GetDouble(1); + data.altitude = r.GetInt32(2); + data.timestamp = r.GetInt64(3); + data.altitudeaboveground = r.GetInt32(4); + data.Eng1Rpm = r.GetInt32(5); + data.Eng2Rpm = r.GetInt32(6); + data.Eng3Rpm = r.GetInt32(7); + data.Eng4Rpm = r.GetInt32(8); + data.LightsMask = r.GetInt32(9); + data.ground_velocity = r.GetDouble(10); + data.plane_pitch = r.GetDouble(11); + data.plane_bank = r.GetDouble(12); + data.plane_heading_true = r.GetDouble(13); + data.plane_heading_magnetic = r.GetDouble(14); + data.plane_airspeed_indicated = r.GetDouble(15); + data.airspeed_true = r.GetDouble(16); + data.vertical_speed = r.GetDouble(17); + data.heading_indicator = r.GetDouble(18); + data.flaps_handle_position = r.GetInt32(19); + data.spoilers_handle_position = r.GetInt32(20); + data.gear_handle_position = r.GetInt32(21); + data.ambient_wind_velocity = r.GetDouble(22); + data.ambient_wind_direction = r.GetDouble(23); + data.ambient_temperature = r.GetDouble(24); + data.stall_warning = r.GetInt32(25); + data.overspeed_warning = r.GetInt32(26); + data.is_gear_retractable = r.GetInt32(27); + data.spoiler_available = r.GetInt32(28); + data.sim_on_ground = r.GetInt32(29); + + FlightPath.Add(data); + } + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteReader", sqlite_cmd), ex); + throw ex; } + return FlightPath; } @@ -688,15 +1024,25 @@ public List GetFlightList() sqlite_cmd = sqlite_conn.CreateCommand(); Selectsql = "SELECT FlightID, aircraft, start_datetimestamp FROM Flights ORDER BY FlightID ASC"; sqlite_cmd.CommandText = Selectsql; - SQLiteDataReader r = sqlite_cmd.ExecuteReader(); - while (r.Read()) + try { - FlightListData data = new FlightListData(); - data.FlightID = r.GetInt32(0); - data.aircraft = r.GetString(1); - data.start_flight_timestamp = r.GetInt64(2); - FlightList.Add(data); + SQLiteDataReader r = sqlite_cmd.ExecuteReader(); + + while (r.Read()) + { + FlightListData data = new FlightListData(); + data.FlightID = r.GetInt32(0); + data.aircraft = r.GetString(1); + data.start_flight_timestamp = r.GetInt64(2); + FlightList.Add(data); + } } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteReader", sqlite_cmd), ex); + throw ex; + } + return FlightList; } @@ -713,27 +1059,59 @@ public void DeleteFlight(int nFlightID) sqlite_cmd.CommandText = Deletesql; sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@FlightID", nFlightID); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } Deletesql = "Delete from FlightSampleDetails WHERE FlightSampleDetails.FlightSamplesID IN (select FlightSamplesID From FlightSamples WHERE FlightID = @FlightID)"; sqlite_cmd.CommandText = Deletesql; sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@FlightID", nFlightID); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } sqlite_cmd = sqlite_conn.CreateCommand(); Deletesql = "Delete from FlightSamples WHERE FlightID = @FlightID"; sqlite_cmd.CommandText = Deletesql; sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@FlightID", nFlightID); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } sqlite_cmd = sqlite_conn.CreateCommand(); Deletesql = "Delete from Flights WHERE FlightID = @FlightID"; sqlite_cmd.CommandText = Deletesql; sqlite_cmd.Parameters.Clear(); sqlite_cmd.Parameters.AddWithValue("@FlightID", nFlightID); - sqlite_cmd.ExecuteNonQuery(); + try + { + sqlite_cmd.ExecuteNonQuery(); + } + catch (Exception ex) + { + Program.ErrorLogging(GetDBQueryExtraInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, "ExecuteNonQuery", sqlite_cmd), ex); + throw ex; + } transaction.Commit(); } diff --git a/FS2020PlanePath/MainPage.Designer.cs b/FS2020PlanePath/MainPage.Designer.cs index f1d6691..3d48683 100644 --- a/FS2020PlanePath/MainPage.Designer.cs +++ b/FS2020PlanePath/MainPage.Designer.cs @@ -54,6 +54,7 @@ private void InitializeComponent() this.GoogleEarthWebRB = new System.Windows.Forms.RadioButton(); this.GoogleEarthGB = new System.Windows.Forms.GroupBox(); this.SpeedUpVideoPlaybackCB = new System.Windows.Forms.CheckBox(); + this.ErrorTBRO = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // label2 @@ -300,11 +301,22 @@ private void InitializeComponent() this.SpeedUpVideoPlaybackCB.Text = "Speed Up First Person Flight Playback When Above Threshold Altitude"; this.SpeedUpVideoPlaybackCB.UseVisualStyleBackColor = true; // + // ErrorTBRO + // + this.ErrorTBRO.BorderStyle = System.Windows.Forms.BorderStyle.None; + this.ErrorTBRO.Location = new System.Drawing.Point(13, 30); + this.ErrorTBRO.Multiline = true; + this.ErrorTBRO.Name = "ErrorTBRO"; + this.ErrorTBRO.ReadOnly = true; + this.ErrorTBRO.Size = new System.Drawing.Size(415, 29); + this.ErrorTBRO.TabIndex = 25; + // // MainPage // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(557, 476); + this.Controls.Add(this.ErrorTBRO); this.Controls.Add(this.SpeedUpVideoPlaybackCB); this.Controls.Add(this.GoogleEarthWebRB); this.Controls.Add(this.GoogleEarthAppRB); @@ -366,6 +378,7 @@ private void InitializeComponent() private System.Windows.Forms.RadioButton GoogleEarthWebRB; private System.Windows.Forms.GroupBox GoogleEarthGB; private System.Windows.Forms.CheckBox SpeedUpVideoPlaybackCB; + private System.Windows.Forms.TextBox ErrorTBRO; } } diff --git a/FS2020PlanePath/MainPage.cs b/FS2020PlanePath/MainPage.cs index 83a1c51..4aa9a6d 100644 --- a/FS2020PlanePath/MainPage.cs +++ b/FS2020PlanePath/MainPage.cs @@ -26,12 +26,11 @@ public partial class MainPage : Form int nCurrentFlightID; DateTime dtLastDataRecord; FlightPlan flightPlan; - string sAppVersion = "1.2.1"; - + public MainPage() { InitializeComponent(); - this.Text += sAppVersion; + this.Text += Program.sAppVersion; FlightPathDB = new FS2020_SQLLiteDB(); FlightPathDB.CreateTables(); flightPlan = new FlightPlan(); @@ -75,7 +74,7 @@ private void MainPage_Shown(object sender, EventArgs e) simConnectIntegration.FForm = this; sAppLatestVersion = ReadLatestAppVersionFromWeb(); - if (sAppLatestVersion.Equals(sAppVersion) == false) + if (sAppLatestVersion.Equals(Program.sAppVersion) == false) if (MessageBox.Show("There is a newer version of the application available. Do you wish to download it now?", "New Version Available", MessageBoxButtons.YesNo) == DialogResult.Yes) System.Diagnostics.Process.Start("https://github.com/SAHorowitz/MSFS2020-PilotPathRecorder"); AttemptSimConnection(); @@ -107,11 +106,11 @@ protected override void DefWndProc(ref Message m) { simConnectIntegration.SimConnect.ReceiveMessage(); } - catch + catch (Exception ex) { SimConnectStatusLabel.Text = "Connection lost to SimConnect"; + StopLoggingBtn.PerformClick(); } - } } else @@ -543,6 +542,10 @@ private void StopLoggingBtn_Click(object sender, EventArgs e) StopLoggingBtn.Enabled = false; ContinueLogginBtn.Enabled = false; LoadFlightList(); + if (Program.bLogErrorsWritten == true) + ErrorTBRO.Text = "Errors detected. Please see " + Program.ErrorLogFile() + " for more details"; + else + ErrorTBRO.Text = ""; } // pause and continue logging are as simple as button visibility and setting logging flag @@ -630,7 +633,7 @@ private void MainPage_FormClosing(object sender, FormClosingEventArgs e) private string ReadLatestAppVersionFromWeb() { - string sRetVal; + string sRetVal =""; WebClient client = new WebClient(); try @@ -669,7 +672,7 @@ private string ReadLatestAppVersionFromWeb() } catch (Exception e) { - sRetVal = sAppVersion; + sRetVal = Program.sAppVersion; } return sRetVal; diff --git a/FS2020PlanePath/Program.cs b/FS2020PlanePath/Program.cs index 8391445..0bae3e7 100644 --- a/FS2020PlanePath/Program.cs +++ b/FS2020PlanePath/Program.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using System.Windows.Forms; @@ -11,12 +13,41 @@ static class Program /// /// The main entry point for the application. /// + static public string sAppVersion = "1.2.2"; + static public bool bLogErrorsWritten = false; + [STAThread] + static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainPage()); } + + public static string ErrorLogFile() + { + return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\PilotPathRecorderLog.txt"; + } + + public static void ErrorLogging(string sAdtlMsg, Exception ex) + { + string strPath = ErrorLogFile(); + if (!File.Exists(strPath)) + { + File.Create(strPath).Dispose(); + } + using (StreamWriter sw = File.AppendText(strPath)) + { + sw.WriteLine("=============Error Logging ==========="); + sw.WriteLine("===========Start============= " + DateTime.Now); + sw.WriteLine("Version: " + Program.sAppVersion); + sw.WriteLine("Additional Information: " + sAdtlMsg); + sw.WriteLine("Error Message: " + ex.Message); + sw.WriteLine("Stack Trace: " + ex.StackTrace); + sw.WriteLine("===========End============="); + } + bLogErrorsWritten = true; + } } }