Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmaster187 committed Feb 9, 2024
1 parent c6b3ce4 commit 1955832
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
12 changes: 7 additions & 5 deletions TeslaLogger/DBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1672,21 +1672,23 @@ internal void UpdateRefreshToken(string refresh_token)
}
}

internal void UpdateFleetAPIaddress(string url)


internal void UpdateCarColumn(string column, string value)
{
try
{
car.Log("UpdateFleetAPIaddress");
car.Log($"Update {column} / Value: {value}");
using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
{
con.Open();
using (MySqlCommand cmd = new MySqlCommand("update cars set fleetAPIaddress = @fleetAPIaddress where id=@id", con))
using (MySqlCommand cmd = new MySqlCommand($"update cars set {column} = @value where id=@id", con))
{
cmd.Parameters.AddWithValue("@id", car.CarInDB);
cmd.Parameters.AddWithValue("@fleetAPIaddress", url);
cmd.Parameters.AddWithValue("@value", value);
int done = SQLTracer.TraceNQ(cmd, out _);

car.Log("UpdateFleetAPIaddress OK: " + done + " - " + url);
car.Log($"Update {column} OK: " + done + " - " + value);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions TeslaLogger/UpdateTeslalogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,34 @@ PRIMARY KEY (`id`)
AssertAlterDB();
DBHelper.ExecuteSQLQuery(@"ALTER TABLE `cars` ADD `fleetAPIaddress` VARCHAR(200) NULL DEFAULT NULL", 600);
}

if (!DBHelper.ColumnExists("cars", "oldAPIchinaCar"))
{
Logfile.Log("ALTER TABLE cars ADD Column oldAPIchinaCar");
AssertAlterDB();
DBHelper.ExecuteSQLQuery(@"ALTER TABLE `cars` ADD `oldAPIchinaCar` TINYINT UNSIGNED NOT NULL DEFAULT '0'", 600);
}

if (!DBHelper.ColumnExists("cars", "needVirtualKey"))
{
Logfile.Log("ALTER TABLE cars ADD Column needVirtualKey");
AssertAlterDB();
DBHelper.ExecuteSQLQuery(@"ALTER TABLE `cars` ADD `needVirtualKey` TINYINT UNSIGNED NOT NULL DEFAULT '0'", 600);
}

if (!DBHelper.ColumnExists("cars", "needCommandPermission"))
{
Logfile.Log("ALTER TABLE cars ADD Column needCommandPermission");
AssertAlterDB();
DBHelper.ExecuteSQLQuery(@"ALTER TABLE `cars` ADD `needCommandPermission` TINYINT UNSIGNED NOT NULL DEFAULT '0'", 600);
}

if (!DBHelper.ColumnExists("cars", "needFleetAPI"))
{
Logfile.Log("ALTER TABLE cars ADD Column needFleetAPI");
AssertAlterDB();
DBHelper.ExecuteSQLQuery(@"ALTER TABLE `cars` ADD `needFleetAPI` TINYINT UNSIGNED NOT NULL DEFAULT '0'", 600);
}
}

private static void CheckDBSchema_can()
Expand Down
26 changes: 25 additions & 1 deletion TeslaLogger/WebHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Exceptionless;
using Exceptionless.Logging;
using Google.Protobuf.WellKnownTypes;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI;
Expand Down Expand Up @@ -691,7 +692,7 @@ public string GetRegion()

car.FleetApiAddress = fleeturl;
car.Log("FleetApiAddress: " + fleeturl);
car.DbHelper.UpdateFleetAPIaddress(fleeturl);
car.DbHelper.UpdateCarColumn("fleetAPIaddress", fleeturl);
return fleeturl;
}

Expand Down Expand Up @@ -744,6 +745,9 @@ private string UpdateTeslaTokenFromRefreshTokenFromFleetAPI(string refresh_token
string result = response.Content.ReadAsStringAsync().Result;
if (response.IsSuccessStatusCode)
{
if (result.Contains("User revoked consent"))
car.CreateExeptionlessLog("User revoked consent", "Teslalogger won't work anymore!", LogLevel.Warn).Submit();

if (result.Contains("\"error\""))
{
car.Log(result);
Expand Down Expand Up @@ -2055,6 +2059,7 @@ internal void GetAllVehicles(out string resultContent, out Newtonsoft.Json.Linq.
if (resultContent.Contains("user not allowed in region"))
{
car.oldAPIchinaCar = true;
car.DbHelper.UpdateCarColumn("oldAPIchinaCar", "1");
adresse = "https://owner-api.vn.cloud.tesla.cn/api/1/products?orders=true";
DoGetVehiclesRequest(out resultContent, client, adresse, out resultTask, out result);
}
Expand Down Expand Up @@ -4971,6 +4976,25 @@ public async Task<string> PostCommand(string cmd, string data, bool _json = fals

car.Log("Response: " + resultContent);

if (resultContent != null)
{
if (resultContent.Contains("vehicle rejected request: your public key has not been paired with the vehicle"))
{
car.DbHelper.UpdateCarColumn("needVirtualKey", "1");
car.CreateExeptionlessLog("NeedVirtualKey", "", Exceptionless.Logging.LogLevel.Warn).Submit();
}
else if (resultContent.Contains("Tesla Vehicle Command Protocol required"))
{
car.DbHelper.UpdateCarColumn("needFleetAPI", "1");
car.CreateExeptionlessLog("NeedFleetAPI", "", Exceptionless.Logging.LogLevel.Warn).Submit();
}
else if (resultContent.Contains("Unauthorized missing scopes"))
{
car.DbHelper.UpdateCarColumn("needCommandPermission", "1");
car.CreateExeptionlessLog("NeedCommandPermission", "", Exceptionless.Logging.LogLevel.Warn).Submit();
}
}

return resultContent;
}
finally
Expand Down
2 changes: 1 addition & 1 deletion TeslaLogger/WebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2835,7 +2835,7 @@ private static void GetAllCars(HttpListenerRequest request, HttpListenerResponse
{
using (DataTable dt = new DataTable())
{
using (MySqlDataAdapter da = new MySqlDataAdapter("SELECT id, display_name, tasker_hash, model_name, vin, tesla_name, tesla_carid, lastscanmytesla, freesuc, fleetAPI FROM cars order by display_name", DBHelper.DBConnectionstring))
using (MySqlDataAdapter da = new MySqlDataAdapter("SELECT id, display_name, tasker_hash, model_name, vin, tesla_name, tesla_carid, lastscanmytesla, freesuc, fleetAPI, needVirtualKey, needCommandPermission, needFleetAPI FROM cars order by display_name", DBHelper.DBConnectionstring))
{
SQLTracer.TraceDA(dt, da);

Expand Down

0 comments on commit 1955832

Please sign in to comment.