Innovenergy_trunk/csharp/app/VenusLogger/Parsers/Inverter.cs

131 lines
3.8 KiB
C#

using System.Collections.Immutable;
using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Victron.VeDBus;
using InnovEnergy.WireFormat.VictronV1;
using static InnovEnergy.VenusLogger.Parsers.Utils;
namespace InnovEnergy.VenusLogger.Parsers;
using Props = ImmutableDictionary<String, VeProperty>;
public static class Inverter
{
private static Maybe<Device> NoDevice => Maybe<Device>.Nothing;
public static Maybe<Device> GetInverter(this IEnumerable<ServiceProperties> services)
{
return services
.Where(VeService.IsInverterService)
.Select(MakeDevice)
.SingleOrDefault()
.Maybe();
}
public static (Maybe<Device> AcInBus, Maybe<Device> AcOutBus, Maybe<Device> DcBus) GetInverterBusses(this IEnumerable<ServiceProperties> services)
{
return services
.Where(VeService.IsInverterService)
.Select(s => (s.GetAcIn(), s.GetAcOut(), s.GetDc()))
.SingleOrDefault((NoDevice, NoDevice, NoDevice));
}
private static Device MakeDevice(ServiceProperties s)
{
var acIn = s.GetAcIn();
var acOut = s.GetAcOut();
var dc = s.GetDc();
return new Device
{
Type = DeviceType.Inverter,
Devices = { acIn.Concat(acOut).Concat(dc) }
};
}
private static Maybe<Device> GetAcIn(this ServiceProperties serviceProperties)
{
var props = serviceProperties.Properties;
var phases = AcPhases
.TrySelect(props.GetAcInPhase)
.ToList();
if (!phases.Any())
return null;
return new Device
{
Type = DeviceType.AcInBus,
Phases = { phases },
};
}
private static Maybe<Device> GetAcOut(this ServiceProperties serviceProperties)
{
var props = serviceProperties.Properties;
var phases = AcPhases
.TrySelect(props.GetAcOutPhase)
.ToList();
if (!phases.Any())
return null;
return new Device
{
Type = DeviceType.AcOutBus,
Phases = { phases },
};
}
private static Maybe<Device> GetDc(this ServiceProperties serviceProperties)
{
return new Device
{
Type = DeviceType.DcBus,
Phases = { serviceProperties.Properties.GetDcPhase() },
};
}
private static void GetLosses(this ServiceProperties serviceProperties)
{
var dcPhase = serviceProperties.Properties.GetDcPhase();
var dcPower = serviceProperties.Properties.GetProperty<Single>("/Dc/0/Power");
var losses = dcPower - dcPhase.Power;
Console.WriteLine("Dc Losses " + losses);
// return new Device
// {
// Type = DeviceType.DcBus,
// Phases = { dcPhase },
// };
}
// var dcPower = services
// .Where(VeService.IsInverterService)
// .Select(p => p.Properties.GetProperty<Single>("/Dc/0/Power"))
// .FirstOrDefault();
private static Phase GetAcInPhase(this Props props, Int32 phase)
{
return new Phase
{
Voltage = props.GetProperty<Single>($"/Ac/ActiveIn/L{phase}/V"),
Current = -props.GetProperty<Single>($"/Ac/ActiveIn/L{phase}/I"), // - !!
Power = -props.GetProperty<Single>($"/Ac/ActiveIn/L{phase}/P"), // - !!
};
}
private static Phase GetAcOutPhase(this Props props, Int32 phase)
{
return new Phase
{
Voltage = props.GetProperty<Single>($"/Ac/Out/L{phase}/V"),
Current = props.GetProperty<Single>($"/Ac/Out/L{phase}/I"),
Power = props.GetProperty<Single>($"/Ac/Out/L{phase}/P"),
};
}
}