From c4e00b264a494462624cf1cb7372c75d7be892a5 Mon Sep 17 00:00:00 2001 From: Christian Pogea Date: Wed, 18 Sep 2024 22:33:39 +0200 Subject: [PATCH] BF: missing trip because of Duplicate entry 'xxx' for key 'ix_startpos' --- TeslaLogger/DBHelper.cs | 74 +++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/TeslaLogger/DBHelper.cs b/TeslaLogger/DBHelper.cs index 1d5a10e0..6a064b9d 100644 --- a/TeslaLogger/DBHelper.cs +++ b/TeslaLogger/DBHelper.cs @@ -4454,7 +4454,7 @@ public void StartDriveState(DateTime now) // driving means that charging must be over UpdateUnplugDate(); int posID = 0; - + if (car.FleetAPI) // maxpos in Fleetapi is useless because lat & lng = 0 { posID = car.telemetry.lastposid; @@ -4467,7 +4467,33 @@ public void StartDriveState(DateTime now) if (posID == 0) posID = GetMaxPosid(); + + if (!InsertDrivestate(now, posID)) + { + posID = (int)DBHelper.DuplicatePos(posID); + car.Log("DuplicatePos: " + posID); + if (posID > 0) + InsertDrivestate(now, posID); + } + + Insert_active_route_energy_at_arrival(posID, true); + + car.CurrentJSON.current_driving = true; + car.CurrentJSON.current_charge_energy_added = 0; + car.CurrentJSON.current_trip_start = DateTime.Now; + car.CurrentJSON.current_trip_end = DateTime.MinValue; + car.CurrentJSON.current_trip_km_start = 0; + car.CurrentJSON.current_trip_km_end = 0; + car.CurrentJSON.current_trip_max_speed = 0; + car.CurrentJSON.current_trip_max_power = 0; + car.CurrentJSON.current_trip_start_range = 0; + car.CurrentJSON.current_trip_end_range = 0; + car.CurrentJSON.CreateCurrentJSON(); + } + + private bool InsertDrivestate(DateTime now, int posID) + { try { using (MySqlConnection con = new MySqlConnection(DBConnectionstring)) @@ -4480,28 +4506,22 @@ public void StartDriveState(DateTime now) cmd.Parameters.AddWithValue("@CarID", car.CarInDB); cmd.Parameters.AddWithValue("@wheel_type", car.wheel_type); _ = SQLTracer.TraceNQ(cmd, out _); + return true; } } - } catch (Exception ex) + } + catch (MySqlException ex) { + if (ex.ErrorCode == -2147467259) // Duplicate entry + { + car.Log(ex.Message); + return false; + } + car.Log(ex.ToString()); car.SendException2Exceptionless(ex); } - - Insert_active_route_energy_at_arrival(posID, true); - - car.CurrentJSON.current_driving = true; - car.CurrentJSON.current_charge_energy_added = 0; - car.CurrentJSON.current_trip_start = DateTime.Now; - car.CurrentJSON.current_trip_end = DateTime.MinValue; - car.CurrentJSON.current_trip_km_start = 0; - car.CurrentJSON.current_trip_km_end = 0; - car.CurrentJSON.current_trip_max_speed = 0; - car.CurrentJSON.current_trip_max_power = 0; - car.CurrentJSON.current_trip_start_range = 0; - car.CurrentJSON.current_trip_end_range = 0; - - car.CurrentJSON.CreateCurrentJSON(); + return false; } private void UpdatePosFromCurrentJSON(int posID) @@ -7245,5 +7265,25 @@ AND T2.date BETWEEN @start AND @end ex.ToExceptionless().FirstCarUserID().Submit(); } } + + internal static long DuplicatePos(int id) + { + string sql = @"INSERT INTO `pos` (`Datum`,`lat`,`lng`,`speed`,`power`,`odometer`,`ideal_battery_range_km`,`address`,`outside_temp`,`altitude`,`battery_level`,`inside_temp`,`battery_heater`,`is_preconditioning`,`sentry_mode`,`import`,`battery_range_km`,`CarID`,`AP`) + select now() ,`lat`,`lng`,`speed`,`power`,`odometer`,`ideal_battery_range_km`,`address`,`outside_temp`,`altitude`,`battery_level`,`inside_temp`,`battery_heater`,`is_preconditioning`,`sentry_mode`,`import`,`battery_range_km`,`CarID`,`AP` + from pos + where id = " + id; + + using (MySqlConnection con = new MySqlConnection(DBConnectionstring)) + { + con.Open(); + using (MySqlCommand cmd = new MySqlCommand(sql, con)) + { + cmd.ExecuteNonQuery(); + return cmd.LastInsertedId; + } + } + + return 0; + } } }