diff --git a/MaxPower/HostedServices/ExporterService.cs b/MaxPower/HostedServices/ExporterService.cs index 0a1168c..9ab22ac 100644 --- a/MaxPower/HostedServices/ExporterService.cs +++ b/MaxPower/HostedServices/ExporterService.cs @@ -24,24 +24,27 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken) if (cancellationToken.IsCancellationRequested) return; - Logger.LogInformation("Reading data from inverter \"{inverterId}\" at \"{inverterIp}:{inverterPort}\" ...", inverter.Id, inverter.Ip, inverter.Port); + 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()]; - 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); + MaxValues? data = null; - Logger.LogInformation("Inverter \"{inverterId}\" made \"{energyDay} kWh today.", inverter.Id, data.EnergyDay); + try + { + data = await MaxTalkClient.RequestAsync(inverter.Ip, inverter.Id, inverter.Port, timeout: 8000, cancellationToken); + 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); + var message = "An error occured while executing inverter {inverterId} at {inverterIp}:{inverterPort}.\r\nMessage: {message}"; + Logger.LogError(ex, message, inverter.Id, inverter.Ip, inverter.Port, ex.Message + Environment.NewLine + ex.InnerException?.Message ?? ""); } + + _energyDay.WithLabels(labels).Set(data?.EnergyDay ?? 0); + _energyMonth.WithLabels(labels).Set(data?.EnergyMonth ?? 0); + _energyYear.WithLabels(labels).Set(data?.EnergyYear ?? 0); + _energyTotal.WithLabels(labels).Set(data?.EnergyTotal ?? 0); } Logger.LogInformation($"Entering sleep state for {MaxSettings.PollIntervalSeconds} seconds."); diff --git a/MaxPower/Program.cs b/MaxPower/Program.cs index ede0e5b..4fe8142 100644 --- a/MaxPower/Program.cs +++ b/MaxPower/Program.cs @@ -52,7 +52,7 @@ public static void Main(string[] args) internal class FakeMaxTalkClient : IMaxTalkClient { - public Task RequestAsync(string ip, int inverterId, int port, int timeout = 8000) + public Task RequestAsync(string ip, int inverterId, int port, int timeout = 8000, CancellationToken cancellationToken = default) { return Task.FromResult(new MaxValues { diff --git a/MaxTalkSharp/IMaxTalkClient.cs b/MaxTalkSharp/IMaxTalkClient.cs index 2a81ab8..9be0170 100644 --- a/MaxTalkSharp/IMaxTalkClient.cs +++ b/MaxTalkSharp/IMaxTalkClient.cs @@ -2,6 +2,6 @@ { public interface IMaxTalkClient { - Task RequestAsync(string ip, int inverterId, int port, int timeout = 8000); + Task RequestAsync(string ip, int inverterId, int port, int timeout = 8000, CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/MaxTalkSharp/MaxTalkClient.cs b/MaxTalkSharp/MaxTalkClient.cs index 9d38a70..237c579 100644 --- a/MaxTalkSharp/MaxTalkClient.cs +++ b/MaxTalkSharp/MaxTalkClient.cs @@ -10,19 +10,21 @@ public class MaxTalkClient : IMaxTalkClient public static string ENERGY_YEAR = "KYR"; public static string ENERGY_TOTAL = "KT0"; - public async Task RequestAsync(string ip, int inverterId, int port, int timeout = 8000) + public async Task RequestAsync(string ip, int inverterId, int port, int timeout = 8000, CancellationToken cancellationToken = default) { using var client = new TcpClient(); try { - await client.ConnectAsync(ip, port); + await client.ConnectAsync(ip, port, cancellationToken); client.SendTimeout = timeout; client.ReceiveTimeout = timeout; var inverterStringId = inverterId.ToString().PadLeft(2, '0'); var query = QueryBuilder.Build("FB", inverterStringId, $"{ENERGY_DAY};{ENERGY_MONTH};{ENERGY_YEAR};{ENERGY_TOTAL}"); + System.Diagnostics.Debug.WriteLine("Query: " + query); + using var writer = new StreamWriter(client.GetStream(), Encoding.UTF8) { AutoFlush = true }; using var reader = new StreamReader(client.GetStream(), Encoding.UTF8); await writer.WriteAsync(query);