Skip to content

Commit

Permalink
Merge pull request #3 from awaescher/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
awaescher authored Jun 5, 2024
2 parents c3ee4ce + 81c7e10 commit c9d04bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
25 changes: 14 additions & 11 deletions MaxPower/HostedServices/ExporterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
2 changes: 1 addition & 1 deletion MaxPower/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void Main(string[] args)

internal class FakeMaxTalkClient : IMaxTalkClient
{
public Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000)
public Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000, CancellationToken cancellationToken = default)
{
return Task.FromResult(new MaxValues
{
Expand Down
2 changes: 1 addition & 1 deletion MaxTalkSharp/IMaxTalkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IMaxTalkClient
{
Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000);
Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000, CancellationToken cancellationToken = default);
}
}
6 changes: 4 additions & 2 deletions MaxTalkSharp/MaxTalkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ public class MaxTalkClient : IMaxTalkClient
public static string ENERGY_YEAR = "KYR";
public static string ENERGY_TOTAL = "KT0";

public async Task<MaxValues> RequestAsync(string ip, int inverterId, int port, int timeout = 8000)
public async Task<MaxValues> 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);
Expand Down

0 comments on commit c9d04bd

Please sign in to comment.