Skip to content

Commit

Permalink
Merge pull request #1365 from superfloh247/bugfix-GetChargingHistoryV…
Browse files Browse the repository at this point in the history
…2Service.LoadLatest

Bugfix get charging history v2 service.load latest
  • Loading branch information
bassmaster187 authored Oct 2, 2024
2 parents 1ccaa02 + 7008fb1 commit 2f69c0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
26 changes: 15 additions & 11 deletions TeslaLogger/DBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
using Newtonsoft.Json;
using System.Diagnostics;
using System.Data.Common;
using System.Runtime.InteropServices.ComTypes;
using Microsoft.VisualBasic.Logging;

namespace TeslaLogger
{
Expand All @@ -29,6 +27,8 @@ public class DBHelper
private Car car;
bool CleanPasswortDone; // defaults to false

private static Random random = new Random();

internal static string Database = "teslalogger";
internal static string User = "root";
internal static string Password = "teslalogger";
Expand Down Expand Up @@ -1830,15 +1830,19 @@ internal void CloseChargingStates()
{
_ = Task.Factory.StartNew(() =>
{
Thread.Sleep(600000); // sleep 10 minutes so that the invoice is ready
GetChargingHistoryV2Service.LoadLatest(car);
if (GetChargingHistoryV2Service.SyncAll(car) == 0)
{
// invoice not ready yet
Thread.Sleep(3600000); // sleep 60 minutes so that the invoice is ready
GetChargingHistoryV2Service.LoadLatest(car);
_ = GetChargingHistoryV2Service.SyncAll(car);
}
Thread.Sleep(600000 + random.Next(1000, 5000)); // sleep 10+rand minutes so that the invoice is ready
if (GetChargingHistoryV2Service.LoadLatest(car))
{
if (GetChargingHistoryV2Service.SyncAll(car) == 0)
{
// invoice not ready yet
Thread.Sleep(3600000 + random.Next(1000, 5000)); // sleep 60+rand minutes so that the invoice is ready
if (GetChargingHistoryV2Service.LoadLatest(car))
{
_ = GetChargingHistoryV2Service.SyncAll(car);
}
}
}
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
}
Expand Down
11 changes: 6 additions & 5 deletions TeslaLogger/GetChargingHistoryV2Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,31 +190,32 @@ internal static void LoadAll(Car car)
}
}

internal static void LoadLatest(Car car)
internal static bool LoadLatest(Car car)
{
Tools.DebugLog($"GetChargingHistoryV2Service.LoadLatest(#{car.CarInDB})");
string result = car.webhelper.GetChargingHistoryV2(1).Result;
if (result == null || result == "{}" || string.IsNullOrEmpty(result))
{
Tools.DebugLog($"GetChargingHistoryV2Service.LoadLatest(#{car.CarInDB}): result == null");
return;
return false;
}
if (result.Contains("Retry later"))
{
Tools.DebugLog($"GetChargingHistoryV2Service.LoadLatest(#{car.CarInDB}): Retry later");
return;
return false;
}
else if (result.Contains("vehicle unavailable"))
{
Tools.DebugLog($"GetChargingHistoryV2Service.LoadLatest(#{car.CarInDB}): vehicle unavailable");
return;
return false;
}
else if (result.Contains("502 Bad Gateway"))
{
Tools.DebugLog($"GetChargingHistoryV2Service.LoadLatest(#{car.CarInDB}): 502 Bad Gateway");
return;
return false;
}
_ = ParseJSON(result, car);
return true;
}

internal static void CheckSchema()
Expand Down

0 comments on commit 2f69c0d

Please sign in to comment.