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; public static class Inverter { private static Maybe NoDevice => Maybe.Nothing; public static Maybe GetInverter(this IEnumerable services) { return services .Where(VeService.IsInverterService) .Select(MakeDevice) .SingleOrDefault() .Maybe(); } public static (Maybe AcInBus, Maybe AcOutBus, Maybe DcBus) GetInverterBusses(this IEnumerable 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 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 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 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("/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("/Dc/0/Power")) // .FirstOrDefault(); private static Phase GetAcInPhase(this Props props, Int32 phase) { return new Phase { Voltage = props.GetProperty($"/Ac/ActiveIn/L{phase}/V"), Current = -props.GetProperty($"/Ac/ActiveIn/L{phase}/I"), // - !! Power = -props.GetProperty($"/Ac/ActiveIn/L{phase}/P"), // - !! }; } private static Phase GetAcOutPhase(this Props props, Int32 phase) { return new Phase { Voltage = props.GetProperty($"/Ac/Out/L{phase}/V"), Current = props.GetProperty($"/Ac/Out/L{phase}/I"), Power = props.GetProperty($"/Ac/Out/L{phase}/P"), }; } }