Innovenergy_trunk/csharp/App/SchneiderMeterDriver/SchneiderMeterDriver.cs

156 lines
5.2 KiB
C#
Raw Normal View History

2024-06-07 08:36:15 +00:00
/*using System.Reactive.Linq;
2024-06-04 10:23:19 +00:00
using InnovEnergy.Lib.Devices.IEM3kGridMeter;
using InnovEnergy.Lib.Protocols.DBus;
using InnovEnergy.Lib.Protocols.Modbus.Clients;
using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Utils.Reflection;
2024-06-04 10:23:19 +00:00
using InnovEnergy.Lib.Victron.VeDBus;
2024-06-04 13:28:17 +00:00
namespace InnovEnergy.App.SchneiderDriver;
2024-06-04 10:23:19 +00:00
2024-06-04 13:28:17 +00:00
public static class SchneiderMeterDriver
2024-06-04 10:23:19 +00:00
{
public static Task<Exception> Run(String hostName, Bus dbusAddress)
{
return Run(hostName, ModbusTcpClient.DefaultPort, dbusAddress);
}
public static async Task<Exception> Run(String hostName, UInt16 port, Bus dbusAddress)
{
// var ep = new UnixDomainSocketEndPoint("/home/eef/graber_dbus.sock");
// var auth = AuthenticationMethod.ExternalAsRoot();
// dbusAddress = new Bus(ep, auth);
var schneider = new Iem3KGridMeterDevice(hostName, port, Config.ModbusNodeId);
var schneiderStatus = Observable
2024-06-04 10:23:19 +00:00
.Interval(Config.UpdatePeriod)
.Select(_ => schneider.Read())
2024-06-04 10:23:19 +00:00
.Publish();
var x = schneider.Read();
x?.ActivePowerL1.WriteLine();
x?.ActivePowerL2.WriteLine();
x?.ActivePowerL3.WriteLine();
var poller = schneiderStatus.Connect();
2024-06-04 10:23:19 +00:00
var properties = Config.DefaultProperties;
foreach (var p in properties)
{
p.WriteLine(" Signal");
}
// Step 1: Access Config.Signals
var signalsCollection = Config.Signals;
2024-06-04 10:23:19 +00:00
foreach (var s in signalsCollection)
{
s.WriteLine(" Signal");
}
2024-06-04 10:23:19 +00:00
var signals = Config
.Signals
.Select(signal => schneiderStatus.Select(signal.ToVeProperty))
2024-06-04 10:23:19 +00:00
.Merge()
.Do(p => properties.Set(p));
// TODO: remove when possible
// Apparently some VE services need to be periodically reminded that
// this service is /Connected
schneiderStatus.Subscribe(_ => properties.Set("/Connected", 1));
2024-06-04 10:23:19 +00:00
// Wait until status is read once to make sure all
// properties are set when we go onto the bus.
var dbus = schneiderStatus
2024-06-04 10:23:19 +00:00
.Skip(1)
.Take(1)
.SelectMany(_ => PublishPropertiesOnDBus(properties, dbusAddress));
return await signals
.MergeErrors(dbus)
.Finally(poller.Dispose)
.SelectErrors();
}
private static Task<Exception> PublishPropertiesOnDBus(VeProperties properties, Bus bus)
{
Console.WriteLine($"Connecting to DBus {bus}");
return properties.PublishOnDBus(bus, Config.BusName);
}
2024-06-07 08:36:15 +00:00
}*/
using System;
using System.Reactive.Linq;
using InnovEnergy.Lib.Devices.IEM3kGridMeter;
using InnovEnergy.Lib.Protocols.DBus;
using InnovEnergy.Lib.Protocols.Modbus.Clients;
using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Victron.VeDBus;
namespace InnovEnergy.App.SchneiderDriver
{
public static class SchneiderMeterDriver
{
public static Task<Exception> Run(string hostName, Bus dbusAddress)
{
return Run(hostName, ModbusTcpClient.DefaultPort, dbusAddress);
}
public static async Task<Exception> Run(string hostName, ushort port, Bus dbusAddress)
{
var schneider = new Iem3KGridMeterDevice(hostName, port, Config.ModbusNodeId);
var schneiderStatus = Observable
.Interval(Config.UpdatePeriod)
.Select(_ =>
{
var status = schneider.Read();
if (status == null)
{
Console.WriteLine("Failed to read data from Iem3KGridMeterDevice");
}
return status;
})
.Where(status => status != null) // Ignore null readings
.Publish();
var poller = schneiderStatus.Connect();
var properties = Config.DefaultProperties;
var signals = Config
.Signals
.Select(signal => schneiderStatus.Select(signal.ToVeProperty))
.Merge()
.Do(p => properties.Set(p));
schneiderStatus.Subscribe(_ => properties.Set("/Connected", 1));
var dbus = schneiderStatus
.Skip(1)
.Take(1)
.SelectMany(_ => PublishPropertiesOnDBus(properties, dbusAddress));
return await signals
.MergeErrors(dbus)
.Finally(poller.Dispose)
.SelectErrors();
}
private static Task<Exception> PublishPropertiesOnDBus(VeProperties properties, Bus bus)
{
Console.WriteLine($"Connecting to DBus {bus}");
return properties.PublishOnDBus(bus, Config.BusName);
}
}
}