2024-06-12 12:05:29 +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;
|
2024-06-05 14:16:29 +00:00
|
|
|
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
|
|
|
{
|
2024-06-12 12:05:29 +00:00
|
|
|
|
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);
|
|
|
|
|
2024-06-05 14:16:29 +00:00
|
|
|
var schneider = new Iem3KGridMeterDevice(hostName, port, Config.ModbusNodeId);
|
|
|
|
|
|
|
|
|
|
|
|
var schneiderStatus = Observable
|
2024-06-04 10:23:19 +00:00
|
|
|
.Interval(Config.UpdatePeriod)
|
2024-06-05 14:16:29 +00:00
|
|
|
.Select(_ => schneider.Read())
|
2024-06-04 10:23:19 +00:00
|
|
|
.Publish();
|
|
|
|
|
2024-06-05 14:16:29 +00:00
|
|
|
var x = schneider.Read();
|
2024-06-26 13:53:19 +00:00
|
|
|
|
2024-06-05 14:16:29 +00:00
|
|
|
var poller = schneiderStatus.Connect();
|
2024-06-04 10:23:19 +00:00
|
|
|
|
|
|
|
var properties = Config.DefaultProperties;
|
2024-06-12 12:05:29 +00:00
|
|
|
|
2024-06-05 14:16:29 +00:00
|
|
|
|
|
|
|
// Step 1: Access Config.Signals
|
|
|
|
var signalsCollection = Config.Signals;
|
|
|
|
|
2024-06-12 12:05:29 +00:00
|
|
|
/*var signals = Config
|
2024-06-04 10:23:19 +00:00
|
|
|
.Signals
|
2024-06-05 14:16:29 +00:00
|
|
|
.Select(signal => schneiderStatus.Select(signal.ToVeProperty))
|
2024-06-04 10:23:19 +00:00
|
|
|
.Merge()
|
2024-06-12 12:05:29 +00:00
|
|
|
.Do(p => properties.Set(p));*/
|
|
|
|
|
|
|
|
var signals = Config
|
|
|
|
.Signals
|
|
|
|
.Select(signal => schneiderStatus
|
|
|
|
.Select(reading =>
|
|
|
|
{
|
|
|
|
var property = signal.ToVeProperty(reading);
|
|
|
|
if (property == null)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Warning: Signal {signal} produced a null property.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Transformed Signal to Property: {property}");
|
|
|
|
}
|
|
|
|
return property;
|
|
|
|
})
|
|
|
|
.Where(property => property != null))
|
|
|
|
.Merge()
|
|
|
|
.Do(p =>
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Setting property: {p}");
|
|
|
|
properties.Set(p);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Log initial signals
|
|
|
|
/*Console.WriteLine("Initial Signals:");
|
|
|
|
foreach (var signal in signalsCollection)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"Signal: {signal}");
|
|
|
|
}*/
|
2024-06-04 10:23:19 +00:00
|
|
|
|
|
|
|
// TODO: remove when possible
|
|
|
|
// Apparently some VE services need to be periodically reminded that
|
|
|
|
// this service is /Connected
|
2024-06-12 12:05:29 +00:00
|
|
|
Console.WriteLine("Goes to subscribe");
|
2024-06-05 14:16:29 +00:00
|
|
|
schneiderStatus.Subscribe(_ => properties.Set("/Connected", 1));
|
2024-06-12 12:05:29 +00:00
|
|
|
Console.WriteLine("Subscribed successfully");
|
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.
|
2024-06-05 14:16:29 +00:00
|
|
|
var dbus = schneiderStatus
|
2024-06-04 10:23:19 +00:00
|
|
|
.Skip(1)
|
|
|
|
.Take(1)
|
|
|
|
.SelectMany(_ => PublishPropertiesOnDBus(properties, dbusAddress));
|
|
|
|
|
|
|
|
return await signals
|
2024-06-12 12:05:29 +00:00
|
|
|
.MergeErrors(dbus)
|
|
|
|
.Finally(poller.Dispose)
|
|
|
|
.SelectErrors();
|
2024-06-04 10:23:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|