Skip to content

Commit

Permalink
BF: missing trip because of Duplicate entry 'xxx' for key 'ix_startpos'
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmaster187 committed Sep 18, 2024
1 parent 842945a commit c4e00b2
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions TeslaLogger/DBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit c4e00b2

Please sign in to comment.