-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ChargingLogs): Log chargingDetails and charging processes
- Loading branch information
Showing
15 changed files
with
232 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
TeslaSolarCharger.Model/Entities/TeslaSolarCharger/ChargingDetail.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace TeslaSolarCharger.Model.Entities.TeslaSolarCharger; | ||
|
||
public class ChargingDetail | ||
{ | ||
public int Id { get; set; } | ||
public DateTime TimeStamp { get; set; } | ||
public int SolarPower { get; set; } | ||
public int GridPower { get; set; } | ||
|
||
public int ChargingProcessId { get; set; } | ||
|
||
public ChargingProcess ChargingProcess { get; set; } = null!; | ||
} |
17 changes: 17 additions & 0 deletions
17
TeslaSolarCharger.Model/Entities/TeslaSolarCharger/ChargingProcess.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace TeslaSolarCharger.Model.Entities.TeslaSolarCharger; | ||
|
||
public class ChargingProcess | ||
{ | ||
public int Id { get; set; } | ||
public DateTime StartDate { get; set; } | ||
public DateTime? EndDate { get; set; } | ||
public decimal? UsedGridEnergy { get; set; } | ||
public decimal? UsedSolarEnergy { get; set; } | ||
public decimal? Cost { get; set; } | ||
|
||
public int CarId { get; set; } | ||
|
||
public Car Car { get; set; } = null!; | ||
|
||
public List<ChargingDetail> ChargingDetails { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
TeslaSolarCharger/Server/Scheduling/Jobs/ChargingDetailsAddJob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Quartz; | ||
using TeslaSolarCharger.Server.Services.ApiServices.Contracts; | ||
|
||
namespace TeslaSolarCharger.Server.Scheduling.Jobs; | ||
|
||
[DisallowConcurrentExecution] | ||
public class ChargingDetailsAddJob(ILogger<ChargingDetailsAddJob> logger, | ||
ITscOnlyChargingCostService service) | ||
: IJob | ||
{ | ||
public async Task Execute(IJobExecutionContext context) | ||
{ | ||
logger.LogTrace("{method}({context})", nameof(Execute), context); | ||
await service.AddChargingDetailsForAllCars().ConfigureAwait(false); | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
TeslaSolarCharger/Server/Scheduling/Jobs/PowerDistributionAddJob.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
TeslaSolarCharger/Server/Services/ApiServices/Contracts/ITscOnlyChargingCostService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace TeslaSolarCharger.Server.Services.ApiServices.Contracts; | ||
|
||
public interface ITscOnlyChargingCostService | ||
{ | ||
Task AddChargingDetailsForAllCars(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
TeslaSolarCharger/Server/Services/TscOnlyChargingCostService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using TeslaSolarCharger.Model.Contracts; | ||
using TeslaSolarCharger.Model.Entities.TeslaSolarCharger; | ||
using TeslaSolarCharger.Server.Services.ApiServices.Contracts; | ||
using TeslaSolarCharger.Shared.Contracts; | ||
using TeslaSolarCharger.Shared.Dtos.Contracts; | ||
|
||
namespace TeslaSolarCharger.Server.Services; | ||
|
||
public class TscOnlyChargingCostService(ILogger<TscOnlyChargingCostService> logger, | ||
ITeslaSolarChargerContext context, | ||
ISettings settings, | ||
IDateTimeProvider dateTimeProvider, | ||
IConfigurationWrapper configurationWrapper) : ITscOnlyChargingCostService | ||
{ | ||
public async Task FinalizeFinishedChargingProcesses() | ||
{ | ||
logger.LogTrace("{method}()", nameof(FinalizeFinishedChargingProcesses)); | ||
var openChargingProcesses = await context.ChargingProcesses | ||
.Where(cp => cp.EndDate == null) | ||
.ToListAsync().ConfigureAwait(false); | ||
var timeSpanToHandleChargingProcessAsCompleted = TimeSpan.FromMinutes(10); | ||
foreach (var chargingProcess in openChargingProcesses) | ||
{ | ||
var latestChargingDetail = await context.ChargingDetails | ||
.Where(cd => cd.ChargingProcessId == chargingProcess.Id) | ||
.OrderByDescending(cd => cd.Id) | ||
.FirstOrDefaultAsync().ConfigureAwait(false); | ||
if (latestChargingDetail == default) | ||
{ | ||
logger.LogWarning("No charging detail found for charging process with ID {chargingProcessId}.", chargingProcess.Id); | ||
continue; | ||
} | ||
|
||
if (latestChargingDetail.TimeStamp.Add(timeSpanToHandleChargingProcessAsCompleted) < dateTimeProvider.UtcNow()) | ||
{ | ||
try | ||
{ | ||
await FinalizeChargingProcess(chargingProcess); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "Error while finalizing charging process with ID {chargingProcessId}.", chargingProcess.Id); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private async Task FinalizeChargingProcess(ChargingProcess chargingProcess) | ||
{ | ||
logger.LogTrace("{method}({chargingProcessId})", nameof(FinalizeChargingProcess), chargingProcess.Id); | ||
var chargingDetails = await context.ChargingDetails | ||
.Where(cd => cd.ChargingProcessId == chargingProcess.Id) | ||
.OrderBy(cd => cd.Id) | ||
.ToListAsync().ConfigureAwait(false); | ||
decimal usedSolarEnergy = 0; | ||
decimal usedGridEnergy = 0; | ||
decimal cost = 0; | ||
for (var index = 1; index < chargingDetails.Count; index++) | ||
{ | ||
var chargingDetail = chargingDetails[index]; | ||
var timeSpanSinceLastDetail = chargingDetail.TimeStamp - chargingDetails[index - 1].TimeStamp; | ||
usedSolarEnergy += (decimal)(chargingDetail.SolarPower * timeSpanSinceLastDetail.TotalHours); | ||
usedGridEnergy += (decimal)(chargingDetail.GridPower * timeSpanSinceLastDetail.TotalHours); | ||
} | ||
chargingProcess.EndDate = chargingDetails.Last().TimeStamp; | ||
chargingProcess.UsedSolarEnergy = usedSolarEnergy; | ||
chargingProcess.UsedGridEnergy = usedGridEnergy; | ||
chargingProcess.Cost = cost; | ||
await context.SaveChangesAsync().ConfigureAwait(false); | ||
} | ||
|
||
public async Task AddChargingDetailsForAllCars() | ||
{ | ||
logger.LogTrace("{method}()", nameof(AddChargingDetailsForAllCars)); | ||
var availableSolarPower = GetSolarPower(); | ||
foreach (var car in settings.CarsToManage.OrderBy(c => c.ChargingPriority)) | ||
{ | ||
var chargingPowerAtHome = car.ChargingPowerAtHome ?? 0; | ||
if (chargingPowerAtHome < 1) | ||
{ | ||
logger.LogTrace("Car with ID {carId} 0 Watt chargingPower at home", car.Id); | ||
continue; | ||
} | ||
var chargingDetail = await GetAttachedChargingDetail(car.Id); | ||
if (chargingPowerAtHome < availableSolarPower) | ||
{ | ||
chargingDetail.SolarPower = chargingPowerAtHome; | ||
chargingDetail.GridPower = 0; | ||
} | ||
else | ||
{ | ||
chargingDetail.SolarPower = availableSolarPower; | ||
chargingDetail.GridPower = chargingPowerAtHome - availableSolarPower; | ||
} | ||
availableSolarPower -= chargingDetail.SolarPower; | ||
if (availableSolarPower < 0) | ||
{ | ||
availableSolarPower = 0; | ||
} | ||
} | ||
await context.SaveChangesAsync().ConfigureAwait(false); | ||
} | ||
|
||
private int GetSolarPower() | ||
{ | ||
var solarPower = settings.Overage; | ||
if (solarPower == default && settings.InverterPower != default) | ||
{ | ||
//no grid meter available, so we have to calculate the power by using the inverter power and the power buffer | ||
var powerBuffer = configurationWrapper.PowerBuffer(true); | ||
solarPower = settings.InverterPower | ||
//if powerBuffer is negative, it will be subtracted as it should be expected home power usage when no grid meter is available | ||
- (powerBuffer > 0 ? powerBuffer : 0); | ||
} | ||
|
||
if (solarPower == default) | ||
{ | ||
logger.LogInformation("No solar power available, using 0 as default."); | ||
return 0; | ||
} | ||
return (int)solarPower; | ||
} | ||
|
||
private async Task<ChargingDetail> GetAttachedChargingDetail(int carId) | ||
{ | ||
var latestOpenChargingProcessId = await context.ChargingProcesses | ||
.Where(cp => cp.CarId == carId && cp.EndDate == null) | ||
.OrderByDescending(cp => cp.StartDate) | ||
.Select(cp => cp.Id) | ||
.FirstOrDefaultAsync().ConfigureAwait(false); | ||
var chargingDetail = new ChargingDetail | ||
{ | ||
TimeStamp = dateTimeProvider.UtcNow(), | ||
}; | ||
if (latestOpenChargingProcessId == default) | ||
{ | ||
var chargingProcess = new ChargingProcess | ||
{ | ||
StartDate = chargingDetail.TimeStamp, | ||
CarId = carId, | ||
}; | ||
context.ChargingProcesses.Add(chargingProcess); | ||
chargingProcess.ChargingDetails.Add(chargingDetail); | ||
} | ||
else | ||
{ | ||
chargingDetail.ChargingProcessId = latestOpenChargingProcessId; | ||
} | ||
return chargingDetail; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters