Skip to content

Commit

Permalink
feat(ConfigJsonService): use charging details for grid voltage calcul…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
pkuehnel committed Dec 7, 2024
1 parent b4c6dc1 commit 5efbdbe
Showing 1 changed file with 12 additions and 34 deletions.
46 changes: 12 additions & 34 deletions TeslaSolarCharger/Server/Services/ConfigJsonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,47 +407,25 @@ private async Task AddCachedCarStatesToCars(List<DtoCar> cars)
public async Task UpdateAverageGridVoltage()
{
logger.LogTrace("{method}()", nameof(UpdateAverageGridVoltage));
var homeGeofence = configurationWrapper.GeoFence();
const int lowestWorldWideGridVoltage = 100;
const int voltageBuffer = 15;
const int lowestGridVoltageToSearchFor = lowestWorldWideGridVoltage - voltageBuffer;
try
{
var teslaMateContext = teslaMateDbContextWrapper.GetTeslaMateContextIfAvailable();
if (teslaMateContext != default && configurationWrapper.UseTeslaMateIntegration())
var chargerVoltages = await teslaSolarChargerContext.ChargingDetails
.Where(c => (c.ChargerVoltage != null)
&& (c.ChargerVoltage > lowestGridVoltageToSearchFor))
.OrderByDescending(c => c.Id)
.Select(c => c.ChargerVoltage)
.Take(1000)
.ToListAsync().ConfigureAwait(false);
if (chargerVoltages.Count > 10)
{
var chargerVoltages = await teslaMateContext
.Charges
.Where(c => c.ChargingProcess.Geofence != null
&& c.ChargingProcess.Geofence.Name == homeGeofence
&& c.ChargerVoltage > lowestGridVoltageToSearchFor)
.OrderByDescending(c => c.Id)
.Select(c => c.ChargerVoltage)
.Take(1000)
.ToListAsync().ConfigureAwait(false);
if (chargerVoltages.Count > 10)
{
var averageValue = Convert.ToInt32(chargerVoltages.Average(c => c!.Value));
logger.LogDebug("Use {averageVoltage}V for charge speed calculation", averageValue);
settings.AverageHomeGridVoltage = averageValue;
}
var averageValue = Convert.ToInt32(chargerVoltages.Average(c => c!.Value));
logger.LogDebug("Use {averageVoltage}V for charge speed calculation", averageValue);
settings.AverageHomeGridVoltage = averageValue;
}
else
{
var chargerVoltages = await teslaSolarChargerContext.ChargingDetails
.Where(c => c.ChargerVoltage != null && c.ChargerVoltage > 40)
.OrderByDescending(c => c.Id)
.Select(c => c.ChargerVoltage)
.Take(1000)
.ToListAsync().ConfigureAwait(false);
if (chargerVoltages.Count > 10)
{
var averageValue = Convert.ToInt32(chargerVoltages.Average(c => c!.Value));
logger.LogDebug("Use {averageVoltage}V for charge speed calculation", averageValue);
settings.AverageHomeGridVoltage = averageValue;
}
}


}
catch (Exception ex)
{
Expand Down

0 comments on commit 5efbdbe

Please sign in to comment.