Skip to content

Commit

Permalink
Improve aborting
Browse files Browse the repository at this point in the history
  • Loading branch information
awaescher committed May 17, 2024
1 parent da29bdd commit 3b3dc5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
49 changes: 24 additions & 25 deletions MaxPower/HostedServices/ExporterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,35 @@ public class ExporterService(MaxSettings maxSettings, IEnumerable<InverterConfig

protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
try
while (!cancellationToken.IsCancellationRequested)
{
while (!cancellationToken.IsCancellationRequested)
foreach (var inverter in Inverters)
{
foreach (var inverter in Inverters)
if (cancellationToken.IsCancellationRequested)
return;

Logger.LogInformation("Reading data from inverter \"{inverterId}\" at \"{inverterIp}:{inverterPort}\" ...", inverter.Id, inverter.Ip, inverter.Port);

try
{
Logger.LogInformation("Reading data from inverter \"{inverterId}\" at \"{inverterIp}:{inverterPort}\" ...", inverter.Id, inverter.Ip, inverter.Port);

try
{
var data = await MaxTalkClient.RequestAsync(inverter.Ip, inverter.Id, inverter.Port);

string[] labels = [inverter.Ip, inverter.Id.ToString()];
_energyDay.WithLabels(labels).Set(data.EnergyDay);
_energyMonth.WithLabels(labels).Set(data.EnergyMonth);
_energyYear.WithLabels(labels).Set(data.EnergyYear);
_energyTotal.WithLabels(labels).Set(data.EnergyTotal);
}
catch (Exception ex)
{
Logger.LogError(ex, "An error occured while executing inverter \"{inverterId}\" at \"{inverterIp}:{inverterPort}\".", inverter.Id, inverter.Ip, inverter.Port);
}
}
var data = await MaxTalkClient.RequestAsync(inverter.Ip, inverter.Id, inverter.Port);

Logger.LogInformation($"Entering sleep state for {MaxSettings.PollIntervalSeconds} seconds.");
await Task.Delay(TimeSpan.FromSeconds(MaxSettings.PollIntervalSeconds), cancellationToken);
string[] labels = [inverter.Ip, inverter.Id.ToString()];
_energyDay.WithLabels(labels).Set(data.EnergyDay);
_energyMonth.WithLabels(labels).Set(data.EnergyMonth);
_energyYear.WithLabels(labels).Set(data.EnergyYear);
_energyTotal.WithLabels(labels).Set(data.EnergyTotal);

Logger.LogInformation("Inverter \"{inverterId}\" made \"{energyDay} kWh today.", inverter.Id, data.EnergyDay);
}
catch (Exception ex)
{
Logger.LogError(ex, "An error occured while executing inverter \"{inverterId}\" at \"{inverterIp}:{inverterPort}\".", inverter.Id, inverter.Ip, inverter.Port);
}
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{

Logger.LogInformation($"Entering sleep state for {MaxSettings.PollIntervalSeconds} seconds.");
await Task.Delay(TimeSpan.FromSeconds(MaxSettings.PollIntervalSeconds), cancellationToken);
}
}
}
1 change: 0 additions & 1 deletion MaxTalkSharp/MaxTalkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public async Task<MaxValues> RequestAsync(string ip, int inverterId, int port, i
finally
{
client.Close();
client.Dispose();
}

return new MaxValues();
Expand Down

0 comments on commit 3b3dc5e

Please sign in to comment.