Skip to content

Commit

Permalink
Store FleetAPIAdress in database
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmaster187 committed Feb 2, 2024
1 parent a00e048 commit 002c834
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
8 changes: 4 additions & 4 deletions TeslaLogger/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public double WhTR
private static object initCredentialsLock = new object();
private static object _syncRoot = new object();
internal bool FleetAPI = false;
internal string FleetApiRegionURL = "";
internal string FleetApiAddress = "";

[MethodImpl(MethodImplOptions.Synchronized)]
internal TeslaAPIState GetTeslaAPIState() { return teslaAPIState; }
Expand Down Expand Up @@ -390,8 +390,8 @@ private void InitStage3()
ExitCarThread("DBHelper.DBConnectionstring.Length == 0");
}

webhelper.GetRegion();

if (!DbHelper.GetRegion())
webhelper.GetRegion();

if (webhelper.GetVehicles() == "NULL")
{
Expand Down Expand Up @@ -973,7 +973,7 @@ private void HandleState_Start()
{
//Log(res);
SetCurrentState(TeslaState.Online);
if (FleetAPI && String.IsNullOrEmpty(FleetApiRegionURL))
if (FleetAPI && String.IsNullOrEmpty(FleetApiAddress))
webhelper.GetRegion();

webhelper.IsDriving(true);
Expand Down
66 changes: 64 additions & 2 deletions TeslaLogger/DBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,32 @@ internal void UpdateRefreshToken(string refresh_token)
cmd.Parameters.AddWithValue("@refresh_token", refresh_token);
int done = SQLTracer.TraceNQ(cmd, out _);

car.Log("UpdateRefreshToken OK: " + done + " - " + refresh_token.Substring(0,20) + "xxxxxxxx");
car.Log("UpdateRefreshToken OK: " + done + " - " + refresh_token.Substring(0, 20) + "xxxxxxxx");
}
}
}
catch (Exception ex)
{
car.CreateExceptionlessClient(ex).Submit();
car.Log(ex.ToString());
}
}

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

car.Log("UpdateFleetAPIaddress OK: " + done + " - " + url);
}
}
}
Expand Down Expand Up @@ -6696,6 +6721,43 @@ id ASC
ex.ToExceptionless().FirstCarUserID().Submit();
Logfile.Log(ex.ToString());
}
}
}

internal bool GetRegion()
{
try
{
using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
{
con.Open();
using (MySqlCommand cmd = new MySqlCommand("SELECT fleetAPIaddress FROM cars where id = @CarID", con))
{
cmd.Parameters.AddWithValue("@CarID", car.CarInDB);

MySqlDataReader dr = SQLTracer.TraceDR(cmd);
if (dr.Read())
{
if (dr[0] == DBNull.Value)
return false;

string url = dr[0].ToString();
if (String.IsNullOrEmpty(url))
return false;

car.FleetApiAddress = url;
car.Log("FleetApiAddress: " + url);
return true;
}
}
}
}
catch (Exception ex)
{
ex.ToExceptionless().FirstCarUserID().Submit();
Logfile.Log(ex.ToString());
}

return false;
}
}
}
9 changes: 5 additions & 4 deletions TeslaLogger/WebHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public string apiaddress
get
{
if (car.FleetAPI)
if (String.IsNullOrEmpty(car.FleetApiRegionURL))
if (String.IsNullOrEmpty(car.FleetApiAddress))
return "https://fleet-api.prd.eu.vn.cloud.tesla.com/";
else
return car.FleetApiRegionURL;
return car.FleetApiAddress;

else
return "https://owner-api.teslamotors.com/";
Expand Down Expand Up @@ -687,8 +687,9 @@ public string GetRegion()
if (!fleeturl.EndsWith("/"))
fleeturl += "/";

car.FleetApiRegionURL = fleeturl;
car.Log("Fleet URL: " + fleeturl);
car.FleetApiAddress = fleeturl;
car.Log("FleetApiAddress: " + fleeturl);
car.DbHelper.UpdateFleetAPIaddress(fleeturl);
return fleeturl;
}

Expand Down

0 comments on commit 002c834

Please sign in to comment.