From be452d190ccc4646ecc99411d3ac06a6bde9c093 Mon Sep 17 00:00:00 2001 From: ig Date: Thu, 31 Aug 2023 14:16:09 +0200 Subject: [PATCH] "normalize" DcBus --- csharp/Lib/Devices/AMPT/AmptDevices.cs | 18 +++++++-- .../Devices/Battery48TL/Battery48TlRecords.cs | 10 ++--- .../Trumpf/TruConvertAc/AcDcDevicesRecord.cs | 10 ++--- .../Trumpf/TruConvertDc/DcDcDevicesRecord.cs | 24 +++++++----- .../Trumpf/TruConvertDc/Status/DcDcStatus.cs | 22 +++++------ csharp/Lib/StatusApi/DeviceTypes/IAcDc1.cs | 1 - csharp/Lib/StatusApi/DeviceTypes/IBattery.cs | 1 - csharp/Lib/StatusApi/DeviceTypes/IDcDc.cs | 2 +- csharp/Lib/StatusApi/DeviceTypes/IDcMeter.cs | 7 ++++ csharp/Lib/Units/Composite/Ac1Bus.cs | 3 -- csharp/Lib/Units/Composite/AcPower.cs | 39 +++++++++---------- csharp/Lib/Units/Composite/DcBus.cs | 16 ++++---- 12 files changed, 85 insertions(+), 68 deletions(-) create mode 100644 csharp/Lib/StatusApi/DeviceTypes/IDcMeter.cs diff --git a/csharp/Lib/Devices/AMPT/AmptDevices.cs b/csharp/Lib/Devices/AMPT/AmptDevices.cs index b973f3109..7345c2c3c 100644 --- a/csharp/Lib/Devices/AMPT/AmptDevices.cs +++ b/csharp/Lib/Devices/AMPT/AmptDevices.cs @@ -62,7 +62,11 @@ public class AmptDevices var busVoltage = nStringOptimizers == 0 ? 0 : soStati.Average(r => r.Voltage); var busCurrent = nStringOptimizers == 0 ? 0 : soStati.Sum (r => r.Current); - var dc = DcBus.FromVoltageCurrent(busVoltage, busCurrent); + var dc = new DcBus + { + Voltage = busVoltage, + Current = busCurrent, + }; // flatten the 2 strings of each SO into one array var strings = soStati.SelectMany(GetStrings).ToArray(nStrings); @@ -80,8 +84,16 @@ public class AmptDevices { // hardcoded: every SO has 2 strings (produced like this by AMPT) - yield return DcBus.FromVoltageCurrent(r.String1Voltage, r.String1Current); - yield return DcBus.FromVoltageCurrent(r.String2Voltage, r.String2Current); + yield return new() + { + Voltage = r.String1Voltage, + Current = r.String1Current, + }; + yield return new() + { + Voltage = r.String2Voltage, + Current = r.String2Current, + }; } diff --git a/csharp/Lib/Devices/Battery48TL/Battery48TlRecords.cs b/csharp/Lib/Devices/Battery48TL/Battery48TlRecords.cs index 21bf097bb..256f982fe 100644 --- a/csharp/Lib/Devices/Battery48TL/Battery48TlRecords.cs +++ b/csharp/Lib/Devices/Battery48TL/Battery48TlRecords.cs @@ -35,11 +35,11 @@ public class Battery48TlRecords HeatingPower = records.Sum(b => b.HeatingPower), TimeToToc = records.Min(r => r.TimeToTOC), - Dc = DcBus.FromVoltageCurrent - ( - records.Average(r => r.Dc.Voltage), - records.Sum(r => r.Dc.Current) - ) + Dc = new() + { + Voltage = records.Average(r => r.Dc.Voltage), + Current = records.Sum(r => r.Dc.Current), + } }; } diff --git a/csharp/Lib/Devices/Trumpf/TruConvertAc/AcDcDevicesRecord.cs b/csharp/Lib/Devices/Trumpf/TruConvertAc/AcDcDevicesRecord.cs index 577b485d2..d6f1d6b45 100644 --- a/csharp/Lib/Devices/Trumpf/TruConvertAc/AcDcDevicesRecord.cs +++ b/csharp/Lib/Devices/Trumpf/TruConvertAc/AcDcDevicesRecord.cs @@ -72,11 +72,11 @@ public class AcDcDevicesRecord var p = Ac.Power.Active.Value; var i = u == 0 ? 0 : p / u; - return DcBus.FromVoltageCurrent - ( - voltage: u, - current: i - ); + return new() + { + Voltage = u, + Current = i, + }; } } } \ No newline at end of file diff --git a/csharp/Lib/Devices/Trumpf/TruConvertDc/DcDcDevicesRecord.cs b/csharp/Lib/Devices/Trumpf/TruConvertDc/DcDcDevicesRecord.cs index 605081415..9d38c9cd5 100644 --- a/csharp/Lib/Devices/Trumpf/TruConvertDc/DcDcDevicesRecord.cs +++ b/csharp/Lib/Devices/Trumpf/TruConvertDc/DcDcDevicesRecord.cs @@ -6,7 +6,11 @@ namespace InnovEnergy.Lib.Devices.Trumpf.TruConvertDc; public class DcDcDevicesRecord { - private static readonly DcBus NoDevice = DcBus.FromVoltageCurrent(0, 0); + private static readonly DcBus NoDevice = new() + { + Voltage = 0, + Current = 0, + }; public DcDcDevicesRecord(SystemControlRegisters? systemControl, IReadOnlyList devices) { @@ -14,16 +18,16 @@ public class DcDcDevicesRecord Devices = devices; } - public DcStatus Dc => new DcStatus + public DcStatus Dc => new() { - Battery = Devices.Count == 0 + Battery = Devices.Count == 0 ? NoDevice - : DcBus.FromVoltageCurrent - ( - Devices.Average(r => r.Status.Dc.Battery.Voltage.Value), - Devices.Sum(r => r.Status.Dc.Battery.Current.Value) - ), - + : new() + { + Voltage = Devices.Average(r => r.Status.Dc.Battery.Voltage.Value), + Current = Devices.Sum(r => r.Status.Dc.Battery.Current.Value), + }, + Link = Devices.Count == 0 ? NoDevice : DcBus.FromVoltageCurrent @@ -32,7 +36,7 @@ public class DcDcDevicesRecord Devices.Sum(r => r.Status.Dc.Link.Current.Value) ) }; - + public SystemControlRegisters? SystemControl { get; } public IReadOnlyList Devices { get; } diff --git a/csharp/Lib/Devices/Trumpf/TruConvertDc/Status/DcDcStatus.cs b/csharp/Lib/Devices/Trumpf/TruConvertDc/Status/DcDcStatus.cs index c6635a3ea..cc20812d7 100644 --- a/csharp/Lib/Devices/Trumpf/TruConvertDc/Status/DcDcStatus.cs +++ b/csharp/Lib/Devices/Trumpf/TruConvertDc/Status/DcDcStatus.cs @@ -21,19 +21,19 @@ public class DcDcStatus var linkCurrent = batteryCurrent == 0 ? 0 : _Self.BatteryVoltage * _Self.BatteryCurrent / _Self.DcLinkVoltage; - + return new() { - Link = DcBus.FromVoltageCurrent - ( - voltage: _Self.DcLinkVoltage, - current: -linkCurrent // TODO: review sign is reversed - ), - Battery = DcBus.FromVoltageCurrent - ( - voltage: _Self.BatteryVoltage, - current: -batteryCurrent // TODO: review sign is reversed - ) + Link = new() + { + Voltage = _Self.DcLinkVoltage, + Current = -linkCurrent, + }, + Battery = new() + { + Voltage = _Self.BatteryVoltage, + Current = -batteryCurrent, + } }; } } diff --git a/csharp/Lib/StatusApi/DeviceTypes/IAcDc1.cs b/csharp/Lib/StatusApi/DeviceTypes/IAcDc1.cs index 9ed14c27b..4bdeb9f1d 100644 --- a/csharp/Lib/StatusApi/DeviceTypes/IAcDc1.cs +++ b/csharp/Lib/StatusApi/DeviceTypes/IAcDc1.cs @@ -1,5 +1,4 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi.DeviceTypes; diff --git a/csharp/Lib/StatusApi/DeviceTypes/IBattery.cs b/csharp/Lib/StatusApi/DeviceTypes/IBattery.cs index b40f1a711..278d5dc67 100644 --- a/csharp/Lib/StatusApi/DeviceTypes/IBattery.cs +++ b/csharp/Lib/StatusApi/DeviceTypes/IBattery.cs @@ -1,6 +1,5 @@ using InnovEnergy.Lib.StatusApi.Connections; using InnovEnergy.Lib.Units; -using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi.DeviceTypes; diff --git a/csharp/Lib/StatusApi/DeviceTypes/IDcDc.cs b/csharp/Lib/StatusApi/DeviceTypes/IDcDc.cs index 9ff3ec702..d801bf93e 100644 --- a/csharp/Lib/StatusApi/DeviceTypes/IDcDc.cs +++ b/csharp/Lib/StatusApi/DeviceTypes/IDcDc.cs @@ -6,7 +6,7 @@ namespace InnovEnergy.Lib.StatusApi.DeviceTypes; public interface IDcDc { - DcBus DcLeft { get; } + DcBus DcLeft { get; } DcBus DcRight { get; } } diff --git a/csharp/Lib/StatusApi/DeviceTypes/IDcMeter.cs b/csharp/Lib/StatusApi/DeviceTypes/IDcMeter.cs new file mode 100644 index 000000000..9f3737c53 --- /dev/null +++ b/csharp/Lib/StatusApi/DeviceTypes/IDcMeter.cs @@ -0,0 +1,7 @@ +using InnovEnergy.Lib.StatusApi.Connections; + +namespace InnovEnergy.Lib.StatusApi.DeviceTypes; + +public interface IDcMeter : IDcConnection +{ +} \ No newline at end of file diff --git a/csharp/Lib/Units/Composite/Ac1Bus.cs b/csharp/Lib/Units/Composite/Ac1Bus.cs index deff1105b..e62569e96 100644 --- a/csharp/Lib/Units/Composite/Ac1Bus.cs +++ b/csharp/Lib/Units/Composite/Ac1Bus.cs @@ -2,9 +2,6 @@ namespace InnovEnergy.Lib.Units.Composite; public sealed class Ac1Bus { - private Ac1Bus() - {} - public required Voltage Voltage { get; init; } public required Current Current { get; init; } public required AcPower Power { get; init; } diff --git a/csharp/Lib/Units/Composite/AcPower.cs b/csharp/Lib/Units/Composite/AcPower.cs index c8fc3cc89..e95141f69 100644 --- a/csharp/Lib/Units/Composite/AcPower.cs +++ b/csharp/Lib/Units/Composite/AcPower.cs @@ -6,6 +6,8 @@ namespace InnovEnergy.Lib.Units.Composite; public sealed class AcPower { + private AcPower(){} + public required ApparentPower Apparent { get; init; } public required ActivePower Active { get; init; } public required ReactivePower Reactive { get; init; } @@ -81,29 +83,24 @@ public sealed class AcPower CosPhi = Cos(phi.Value) }; } - - public static AcPower operator +(AcPower left, AcPower right) - { - return FromActiveReactive - ( - left.Active + right.Active, - left.Reactive + right.Reactive - ); - } - public static AcPower operator -(AcPower left, AcPower right) - { - return left + -right; - } + public static AcPower operator +(AcPower left, AcPower right) => FromActiveReactive + ( + left.Active + right.Active, + left.Reactive + right.Reactive + ); - public static AcPower operator -(AcPower p) - { - return FromActiveReactive - ( - -p.Active, - -p.Reactive - ); - } + public static AcPower operator -(AcPower left, AcPower right) => FromActiveReactive + ( + left.Active - right.Active, + left.Reactive - right.Reactive + ); + + public static AcPower operator -(AcPower p) => FromActiveReactive + ( + -p.Active, + -p.Reactive + ); public static implicit operator AcPower(Double p) => FromActiveReactive(p, 0); diff --git a/csharp/Lib/Units/Composite/DcBus.cs b/csharp/Lib/Units/Composite/DcBus.cs index 74437f3c4..4b70c5706 100644 --- a/csharp/Lib/Units/Composite/DcBus.cs +++ b/csharp/Lib/Units/Composite/DcBus.cs @@ -4,18 +4,20 @@ namespace InnovEnergy.Lib.Units.Composite; public sealed class DcBus { - private DcBus() {} - - public required Voltage Voltage { get; init; } - public required Current Current { get; init; } - public required DcPower Power { get; init; } + public required Voltage Voltage { get; init; } + public required Current Current { get; init; } + + public DcPower Power => Voltage * Current; public static DcBus FromVoltageCurrent(Voltage voltage, Current current) => new() { Voltage = voltage, Current = current, - Power = current.Value * voltage.Value, }; - public static DcBus Null => FromVoltageCurrent(0, 0); + public static DcBus Null => new() + { + Voltage = 0, + Current = 0, + }; } \ No newline at end of file