Skip to content

Commit c9d04bd

Browse files
authored
Merge pull request #3 from awaescher/improvements
Improvements
2 parents c3ee4ce + 81c7e10 commit c9d04bd

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

MaxPower/HostedServices/ExporterService.cs

+14-11
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,27 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
2424
if (cancellationToken.IsCancellationRequested)
2525
return;
2626

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

29-
try
30-
{
31-
var data = await MaxTalkClient.RequestAsync(inverter.Ip, inverter.Id, inverter.Port);
29+
string[] labels = [inverter.Ip, inverter.Id.ToString()];
3230

33-
string[] labels = [inverter.Ip, inverter.Id.ToString()];
34-
_energyDay.WithLabels(labels).Set(data.EnergyDay);
35-
_energyMonth.WithLabels(labels).Set(data.EnergyMonth);
36-
_energyYear.WithLabels(labels).Set(data.EnergyYear);
37-
_energyTotal.WithLabels(labels).Set(data.EnergyTotal);
31+
MaxValues? data = null;
3832

39-
Logger.LogInformation("Inverter \"{inverterId}\" made \"{energyDay} kWh today.", inverter.Id, data.EnergyDay);
33+
try
34+
{
35+
data = await MaxTalkClient.RequestAsync(inverter.Ip, inverter.Id, inverter.Port, timeout: 8000, cancellationToken);
36+
Logger.LogInformation("Inverter {inverterId} made {energyDay} kWh today.", inverter.Id, data.EnergyDay);
4037
}
4138
catch (Exception ex)
4239
{
43-
Logger.LogError(ex, "An error occured while executing inverter \"{inverterId}\" at \"{inverterIp}:{inverterPort}\".", inverter.Id, inverter.Ip, inverter.Port);
40+
var message = "An error occured while executing inverter {inverterId} at {inverterIp}:{inverterPort}.\r\nMessage: {message}";
41+
Logger.LogError(ex, message, inverter.Id, inverter.Ip, inverter.Port, ex.Message + Environment.NewLine + ex.InnerException?.Message ?? "");
4442
}
43+
44+
_energyDay.WithLabels(labels).Set(data?.EnergyDay ?? 0);
45+
_energyMonth.WithLabels(labels).Set(data?.EnergyMonth ?? 0);
46+
_energyYear.WithLabels(labels).Set(data?.EnergyYear ?? 0);
47+
_energyTotal.WithLabels(labels).Set(data?.EnergyTotal ?? 0);
4548
}
4649

4750
Logger.LogInformation($"Entering sleep state for {MaxSettings.PollIntervalSeconds} seconds.");

MaxPower/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void Main(string[] args)
5252

5353
internal class FakeMaxTalkClient : IMaxTalkClient
5454
{
55-
public Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000)
55+
public Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000, CancellationToken cancellationToken = default)
5656
{
5757
return Task.FromResult(new MaxValues
5858
{

MaxTalkSharp/IMaxTalkClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public interface IMaxTalkClient
44
{
5-
Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000);
5+
Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000, CancellationToken cancellationToken = default);
66
}
77
}

MaxTalkSharp/MaxTalkClient.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ public class MaxTalkClient : IMaxTalkClient
1010
public static string ENERGY_YEAR = "KYR";
1111
public static string ENERGY_TOTAL = "KT0";
1212

13-
public async Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000)
13+
public async Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000, CancellationToken cancellationToken = default)
1414
{
1515
using var client = new TcpClient();
1616

1717
try
1818
{
19-
await client.ConnectAsync(ip, port);
19+
await client.ConnectAsync(ip, port, cancellationToken);
2020
client.SendTimeout = timeout;
2121
client.ReceiveTimeout = timeout;
2222

2323
var inverterStringId = inverterId.ToString().PadLeft(2, '0');
2424
var query = QueryBuilder.Build("FB", inverterStringId, $"{ENERGY_DAY};{ENERGY_MONTH};{ENERGY_YEAR};{ENERGY_TOTAL}");
2525

26+
System.Diagnostics.Debug.WriteLine("Query: " + query);
27+
2628
using var writer = new StreamWriter(client.GetStream(), Encoding.UTF8) { AutoFlush = true };
2729
using var reader = new StreamReader(client.GetStream(), Encoding.UTF8);
2830
await writer.WriteAsync(query);

0 commit comments

Comments
 (0)