Innovenergy_trunk/csharp/app/CsController/Program.cs

62 lines
1.9 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using System.Reactive.Linq;
using InnovEnergy.Lib.Protocols.DBus;
using InnovEnergy.Lib.Protocols.DBus.Protocol.DataTypes;
using InnovEnergy.Lib.Victron.VeDBus;
// dotnet publish EmuMeter.csproj -c Release -r linux-arm -p:PublishSingleFile=true --self-contained true && \
// rsync -av bin/Release/net6.0/linux-arm/publish/ root@10.2.1.6:/home/root/emu && clear && \
// ssh root@10.2.1.6 /home/root/emu/EmuMeter
Console.WriteLine("Starting CsController ");
//Enable out InnovEnergy GUI Page through DBusService.DBUS_SERVICE_UNSUPPORTED
// var service = new DBusService("com.victronenergy.unsupported");
var veProperties = new VeProperties();
veProperties.Set("/CustomName", "InnovEnergy");
veProperties.Set("/ProductName", "InnovEnergySW");
var dbus = new DBusConnection(Bus.System);
// var ep = new UnixDomainSocketEndPoint("/home/kim/graber_dbus.sock");
// var auth = AuthenticationMethod.ExternalAsRoot();
// var dbusAddress = new Bus(ep, auth);
// var dbus = new DBusConnection(dbusAddress);
await veProperties.PublishOnDBus(Bus.System, "com.victronenergy.unsupported");
var battery = "com.victronenergy.battery.ttyUSB0";
var soc = 21.0;
//TODO change ttyUSB0 for generic Battery on dbus
var names = await dbus.ListNames();
foreach (var name in names)
{
if(name.Contains("com.victronenergy.battery."))
{
battery = name;
break;
}
}
var mustCharge = dbus
.ObserveSignalMessages(sender: battery, objectPath: "/Soc")
.Select(m => m.Payload)!
.OfType<IDictionary<String, Variant>>()
.Where(d => d.ContainsKey("Value"))
.Select(d => d["Value"].Value)
.OfType<Double>()
.Do(s => Console.WriteLine($"soc = {s}"))
.Select(d => d < 67)
.DistinctUntilChanged();
mustCharge.Subscribe(b =>
{
dbus.SetValue("com.victronenergy.settings", "/Settings/CGwacs/BatteryLife/State", b ? 9 : 10); // 9 = charge battery, 10 = optimized (no batterylife)
});
// Console.ReadLine();