Skip to content

Commit

Permalink
Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bassmaster187 committed Feb 15, 2024
1 parent 89c7448 commit 6d9d1f5
Showing 1 changed file with 83 additions and 2 deletions.
85 changes: 83 additions & 2 deletions TeslaLogger/WebHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Exceptionless;
using Exceptionless.Logging;
using Exceptionless.Models.Data;
using Google.Protobuf.WellKnownTypes;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI;
Expand All @@ -19,6 +20,7 @@
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.Caching;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -750,7 +752,20 @@ private string UpdateTeslaTokenFromRefreshTokenFromFleetAPI(string refresh_token

if (result.Contains("\"error\""))
{
string error = result;

try
{
dynamic j2 = JsonConvert.DeserializeObject(result);
error = j2["error"];
} catch (Exception)
{ }

car.CreateExeptionlessLog("UpdateTeslaTokenFromRefreshTokenFromFleetAPI", error, LogLevel.Error)
.AddObject(result, "Result Content")
.Submit();
car.Log(result);
Thread.Sleep(30000);
return "";
}

Expand All @@ -765,7 +780,12 @@ private string UpdateTeslaTokenFromRefreshTokenFromFleetAPI(string refresh_token
}
else
{
car.CreateExeptionlessLog("UpdateTeslaTokenFromRefreshTokenFromFleetAPI", response.StatusCode.ToString(), LogLevel.Error)
.AddObject(result, "Result Content")
.Submit();

Log("Error getting Access Token from Refreh Token: " + (int)response.StatusCode + " / " + response.StatusCode.ToString());
Thread.Sleep(30000);
return "";
}
}
Expand All @@ -779,6 +799,7 @@ private string UpdateTeslaTokenFromRefreshTokenFromFleetAPI(string refresh_token
car.Log(ex.ToString());
car.CreateExceptionlessClient(ex).MarkAsCritical().Submit();
ExceptionlessClient.Default.ProcessQueueAsync();
Thread.Sleep(30000);
}

return "";
Expand Down Expand Up @@ -2402,7 +2423,7 @@ public async Task<string> IsOnline(bool returnOnUnauthorized = false)
if (temp_Tesla_Streamingtoken != Tesla_Streamingtoken)
{
Tesla_Streamingtoken = temp_Tesla_Streamingtoken;
Log("Streamingtoken changed: " + Tools.ObfuscateString(Tesla_Streamingtoken));
Log("Streamingtoken changed (IsOnline): " + Tools.ObfuscateString(Tesla_Streamingtoken));

// can be ignored, is not used at the moment car.Log("Tesla_Streamingtoken changed!");
}
Expand Down Expand Up @@ -2516,6 +2537,48 @@ public async Task<string> IsOnline(bool returnOnUnauthorized = false)
return "NULL";
}

void TryGetNewStreamingToken()
{
car.Log("TryGetNewStreamingToken");

string resultContent = "";
try
{
resultContent = GetCommand("vehicle_data").Result;

if (resultContent == INSERVICE)
{
System.Threading.Thread.Sleep(10000);
return;
}


Tools.SetThreadEnUS();
dynamic jsonResult = JsonConvert.DeserializeObject(resultContent);
string temp_Tesla_Streamingtoken = jsonResult["response"]["tokens"][0].ToString();

if (temp_Tesla_Streamingtoken != Tesla_Streamingtoken)
{
Tesla_Streamingtoken = temp_Tesla_Streamingtoken;
Log("Streamingtoken changed (TryGetNewStreamingToken): " + Tools.ObfuscateString(Tesla_Streamingtoken));
}

}
catch (Exception ex)
{
if (resultContent == null || resultContent == "NULL")
{
Log("TryGetNewStreamingToken = NULL!");
Thread.Sleep(10000);
}
else
{
SubmitExceptionlessClientWithResultContent(ex, resultContent);
ExceptionWriter(ex, resultContent);
}
}
}

void CheckVehicleConfig()
{
string resultContent2 = "";
Expand Down Expand Up @@ -3188,6 +3251,23 @@ public bool IsDriving(bool justinsertdb = false)
decimal dLongitude = 0;
int heading = 0;

try
{
string temp_Tesla_Streamingtoken = jsonResult["response"]["tokens"][0].ToString();

if (temp_Tesla_Streamingtoken != Tesla_Streamingtoken)
{
Tesla_Streamingtoken = temp_Tesla_Streamingtoken;
Log("Streamingtoken changed (IsDriving): " + Tools.ObfuscateString(Tesla_Streamingtoken));

// can be ignored, is not used at the moment car.Log("Tesla_Streamingtoken changed!");
}
} catch (Exception ex)
{
SubmitExceptionlessClientWithResultContent(ex, resultContent);
ExceptionWriter(ex, resultContent);
}

if (r2.ContainsKey("latitude"))
{
dLatitude = (decimal)r2["latitude"];
Expand Down Expand Up @@ -3222,6 +3302,7 @@ public bool IsDriving(bool justinsertdb = false)

double latitude = (double)dLatitude;
double longitude = (double)dLongitude;
car.CurrentJSON.heading = heading;

car.CurrentJSON.SetPosition(latitude, longitude, ts);

Expand Down Expand Up @@ -3541,7 +3622,6 @@ private void StartStream()
{
car.Log("Restart Streaming because get token lock suspended");
UpdateTeslaTokenFromRefreshToken();
RestartStreamThreadWithTask();
break;
}

Expand Down Expand Up @@ -3743,6 +3823,7 @@ private void StreamDataUpdate(string data)
last_latitude_streaming = latitude;
last_longitude_streaming = longitude;
last_power_streaming = dpower;

//Tools.DebugLog($"Stream: InsertPos({v[0]}, {latitude}, {longitude}, {ispeed}, {dpower}, {dodometer_km}, {ideal_battery_range_km}, {battery_range_km}, {isoc}, {outside_temp}, String.Empty)");
car.DbHelper.InsertPos(v[0], latitude, longitude, ispeed, dpower, dodometer_km, ideal_battery_range_km, battery_range_km, isoc, outside_temp, String.Empty);
}
Expand Down

0 comments on commit 6d9d1f5

Please sign in to comment.