Felicity-Inverter-Monitor/src/Server/InverterService/StatusRetriever.cs
2025-03-12 21:24:51 +05:30

49 lines
1.4 KiB
C#

using InverterMon.Server.Persistence;
using InverterMon.Server.Persistence.Settings;
namespace InverterMon.Server.InverterService;
class StatusRetriever(
Database db,
FelicitySolarInverter inverter,
UserSettings userSettings,
IConfiguration config,
ILogger<StatusRetriever> log,
IHostApplicationLifetime appLife)
: BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken c)
{
var port = config["LaunchSettings:DeviceAddress"] ?? throw new ArgumentException("Device address not specified in appsettings.json file!");
while (!inverter.Connect(port))
{
log.LogCritical("Unable to connect to the inverter at [{port}]. Retrying in 5 seconds...", port);
await Task.Delay(5000);
}
appLife.ApplicationStopping.Register(inverter.Close);
while (!c.IsCancellationRequested)
{
inverter.Status.BatteryCapacity = userSettings.BatteryCapacity;
inverter.Status.PV_MaxCapacity = userSettings.PV_MaxCapacity;
try
{
inverter.UpdateStatus();
}
catch (Exception e)
{
log.LogError("Error while reading inverter status data! Details: [{msg}]", e.Message);
await Task.Delay(2000);
continue;
}
db.UpdateTodaysPvGeneration(c);
await Task.Delay(2000);
}
}
}