Move EnergyManager code to seperate class

This commit is contained in:
2023-02-02 14:15:39 +01:00
parent a7693e34ed
commit 31bf148e0f
2 changed files with 266 additions and 60 deletions

260
TestApp/EnergyManager.cs Normal file
View File

@@ -0,0 +1,260 @@
using MQTTnet;
using MQTTnet.Client;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
namespace TestApp
{
internal class EnergyManager
{
private MqttFactory _MqttFactory;
private IMqttClient _MqttClient;
private ImmutableDictionary<ulong, ulong> _CurrentValues = ImmutableDictionary<ulong, ulong>.Empty;
private EnergyManager()
{
_MqttFactory= new MqttFactory();
_MqttClient = _MqttFactory.CreateMqttClient();
}
public static async Task<EnergyManager> Create()
{
var ret = new EnergyManager();
await ret.Connect();
return ret;
}
private async Task Connect() {
var clientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("localhost")
.Build();
await _MqttClient.ConnectAsync(clientOptions);
_MqttClient.ApplicationMessageReceivedAsync += Client_ApplicationMessageReceivedAsync;
var subscribeOptions = _MqttFactory.CreateSubscribeOptionsBuilder()
.WithTopicFilter(
f => {
f.WithTopic("gdr/local/values/smart-meter");
}
)
.Build();
var response = await _MqttClient.SubscribeAsync(subscribeOptions);
}
private Task Client_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
{
switch (arg.ApplicationMessage.Topic)
{
case "gdr/local/values/smart-meter":
ParseGDRs(arg.ApplicationMessage.Payload);
break;
}
return Task.CompletedTask;
}
private void ParseGDRs(byte[] payload)
{
var gdrs = GDRs.Parser.ParseFrom(payload);
if (!gdrs.GDRs_.TryGetValue("smart-meter", out GDR smartMeterGDR))
{
return;
}
_CurrentValues = smartMeterGDR.Values.ToImmutableDictionary();
}
private double GetValue(ulong obisCode, double factor)
{
if (!_CurrentValues.TryGetValue(obisCode, out ulong value))
{
return double.NaN;
}
return factor * value;
}
/// <summary>in W</summary>
public double ActivePowerPositive => GetValue(1099528667391UL, 0.001);
/// <summary>in Wh</summary>
public double ActiveEnergyPositive => GetValue(1099528929535UL, 0.001);
/// <summary>in W</summary>
public double ActivePowerNegative => GetValue(1099545444607UL, 0.001);
/// <summary>in Wh</summary>
public double ActiveEnergyNegative => GetValue(1099545706751UL, 0.001);
/// <summary>in var</summary>
public double ReactivePowerPositive => GetValue(1099562221823UL, 0.001);
/// <summary>in varh</summary>
public double ReactiveEnergyPositive => GetValue(1099562483967UL, 0.001);
/// <summary>in var</summary>
public double ReactivePowerNegative => GetValue(1099578999039UL, 0.001);
/// <summary>in varh</summary>
public double ReactiveEnergyNegative => GetValue(1099579261183UL, 0.001);
/// <summary>in VA</summary>
public double ApparentPowerPositive => GetValue(1099662885119UL, 0.001);
/// <summary>in VAh</summary>
public double ApparentEnergyPositive => GetValue(1099663147263UL, 0.001);
/// <summary>in VA</summary>
public double ApparentPowerNegative => GetValue(1099679662335UL, 0.001);
/// <summary>in VAh</summary>
public double ApparentEnergyNegative => GetValue(1099679924479UL, 0.001);
/// <summary>in -</summary>
public double PowerFactor => GetValue(1099729993983UL, 0.001);
/// <summary>in Hz</summary>
public double SupplyFrequency => GetValue(1099746771199UL, 0.001);
/// <summary>in W</summary>
public double ActivePowerPositiveL1 => GetValue(1099864211711UL, 0.001);
/// <summary>in Wh</summary>
public double ActiveEnergyPositiveL1 => GetValue(1099864473855UL, 0.001);
/// <summary>in W</summary>
public double ActivePowerNegativeL1 => GetValue(1099880988927UL, 0.001);
/// <summary>in Wh</summary>
public double ActiveEnergyNegativeL1 => GetValue(1099881251071UL, 0.001);
/// <summary>in var</summary>
public double ReactivePowerPositiveL1 => GetValue(1099897766143UL, 0.001);
/// <summary>in varh</summary>
public double ReactiveEnergyPositiveL1 => GetValue(1099898028287UL, 0.001);
/// <summary>in var</summary>
public double ReactivePowerNegativeL1 => GetValue(1099914543359UL, 0.001);
/// <summary>in varh</summary>
public double ReactiveEnergyNegativeL1 => GetValue(1099914805503UL, 0.001);
/// <summary>in VA</summary>
public double ApparentPowerPositiveL1 => GetValue(1099998429439UL, 0.001);
/// <summary>in VAh</summary>
public double ApparentEnergyPositiveL1 => GetValue(1099998691583UL, 0.001);
/// <summary>in VA</summary>
public double ApparentPowerNegativeL1 => GetValue(1100015206655UL, 0.001);
/// <summary>in VAh</summary>
public double ApparentEnergyNegativeL1 => GetValue(1100015468799UL, 0.001);
/// <summary>in A</summary>
public double CurrentL1 => GetValue(1100031983871UL, 0.001);
/// <summary>in V</summary>
public double VoltageL1 => GetValue(1100048761087UL, 0.001);
/// <summary>in -</summary>
public double PowerFactorL1 => GetValue(1100065538303UL, 0.001);
/// <summary>in W</summary>
public double ActivePowerPositiveL2 => GetValue(1100199756031UL, 0.001);
/// <summary>in Wh</summary>
public double ActiveEnergyPositiveL2 => GetValue(1100200018175UL, 0.001);
/// <summary>in W</summary>
public double ActivePowerNegativeL2 => GetValue(1100216533247UL, 0.001);
/// <summary>in Wh</summary>
public double ActiveEnergyNegativeL2 => GetValue(1100216795391UL, 0.001);
/// <summary>in var</summary>
public double ReactivePowerPositiveL2 => GetValue(1100233310463UL, 0.001);
/// <summary>in varh</summary>
public double ReactiveEnergyPositiveL2 => GetValue(1100233572607UL, 0.001);
/// <summary>in var</summary>
public double ReactivePowerNegativeL2 => GetValue(1100250087679UL, 0.001);
/// <summary>in varh</summary>
public double ReactiveEnergyNegativeL2 => GetValue(1100250349823UL, 0.001);
/// <summary>in VA</summary>
public double ApparentPowerPositiveL2 => GetValue(1100333973759UL, 0.001);
/// <summary>in VAh</summary>
public double ApparentEnergyPositiveL2 => GetValue(1100334235903UL, 0.001);
/// <summary>in VA</summary>
public double ApparentPowerNegativeL2 => GetValue(1100350750975UL, 0.001);
/// <summary>in VAh</summary>
public double ApparentEnergyNegativeL2 => GetValue(1100351013119UL, 0.001);
/// <summary>in A</summary>
public double CurrentL2 => GetValue(1100367528191UL, 0.001);
/// <summary>in V</summary>
public double VoltageL2 => GetValue(1100384305407UL, 0.001);
/// <summary>in -</summary>
public double PowerFactorL2 => GetValue(1100401082623UL, 0.001);
/// <summary>in W</summary>
public double ActivePowerPositiveL3 => GetValue(1100535300351UL, 0.001);
/// <summary>in Wh</summary>
public double ActiveEnergyPositiveL3 => GetValue(1100535562495UL, 0.001);
/// <summary>in W</summary>
public double ActivePowerNegativeL3 => GetValue(1100552077567UL, 0.001);
/// <summary>in Wh</summary>
public double ActiveEnergyNegativeL3 => GetValue(1100552339711UL, 0.001);
/// <summary>in var</summary>
public double ReactivePowerPositiveL3 => GetValue(1100568854783UL, 0.001);
/// <summary>in varh</summary>
public double ReactiveEnergyPositiveL3 => GetValue(1100569116927UL, 0.001);
/// <summary>in var</summary>
public double ReactivePowerNegativeL3 => GetValue(1100585631999UL, 0.001);
/// <summary>in varh</summary>
public double ReactiveEnergyNegativeL3 => GetValue(1100585894143UL, 0.001);
/// <summary>in VA</summary>
public double ApparentPowerPositiveL3 => GetValue(1100669518079UL, 0.001);
/// <summary>in VAh</summary>
public double ApparentEnergyPositiveL3 => GetValue(1100669780223UL, 0.001);
/// <summary>in VA</summary>
public double ApparentPowerNegativeL3 => GetValue(1100686295295UL, 0.001);
/// <summary>in VAh</summary>
public double ApparentEnergyNegativeL3 => GetValue(1100686557439UL, 0.001);
/// <summary>in A</summary>
public double CurrentL3 => GetValue(1100703072511UL, 0.001);
/// <summary>in V</summary>
public double VoltageL3 => GetValue(1100719849727UL, 0.001);
/// <summary>in -</summary>
public double PowerFactorL3 => GetValue(1100736626943UL, 0.001);
}
}

View File

@@ -1,18 +1,16 @@
using MQTTnet; using NModbus;
using MQTTnet.Client;
using NModbus;
using System.Net.Sockets; using System.Net.Sockets;
namespace TestApp namespace TestApp
{ {
internal class Program internal class Program
{ {
private static IMqttClient _MqttClient; private static EnergyManager _EnergyManager;
private static Dictionary<UInt64, UInt64> _CurrentValues = new Dictionary<ulong, ulong>();
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
await ConnectMQTT(); _EnergyManager = await EnergyManager.Create();
while (true) while (true)
{ {
@@ -39,7 +37,7 @@ namespace TestApp
{ {
try try
{ {
var currentWirkleistung = _CurrentValues.GetValueOrDefault(1099528667391UL) / 1000; var currentWirkleistung = _EnergyManager.ActivePowerPositive;
ushort input = master.ReadHoldingRegisters(1, 1000, 1)[0]; ushort input = master.ReadHoldingRegisters(1, 1000, 1)[0];
ushort output = (ushort)(input + currentWirkleistung); ushort output = (ushort)(input + currentWirkleistung);
@@ -53,57 +51,5 @@ namespace TestApp
} }
} }
} }
private static async Task ConnectMQTT() {
var factory = new MqttFactory();
_MqttClient = factory.CreateMqttClient();
var clientOptions = new MqttClientOptionsBuilder()
.WithTcpServer("localhost")
.Build();
Console.WriteLine("Connecting to local MQTT ...");
await _MqttClient.ConnectAsync(clientOptions);
Console.WriteLine("Connected to local MQTT!");
_MqttClient.ApplicationMessageReceivedAsync += Client_ApplicationMessageReceivedAsync;
var subscribeOptions = factory.CreateSubscribeOptionsBuilder()
.WithTopicFilter(
f => {
f.WithTopic("gdr/local/values/smart-meter");
}
)
.Build();
Console.WriteLine("Subscribing to smart-meter values ...");
var response = await _MqttClient.SubscribeAsync(subscribeOptions);
Console.WriteLine("Subscribed to smart-meter values!");
}
private static Task Client_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
{
switch (arg.ApplicationMessage.Topic) {
case "gdr/local/values/smart-meter":
ParseGDRs(arg.ApplicationMessage.Payload);
break;
}
return Task.CompletedTask;
}
private static void ParseGDRs(byte[] payload)
{
var gdrs = GDRs.Parser.ParseFrom(payload);
if (!gdrs.GDRs_.TryGetValue("smart-meter", out GDR smartMeterGDR)) {
return;
}
foreach (var value in smartMeterGDR.Values) {
_CurrentValues[value.Key] = value.Value;
}
}
} }
} }