From a6252895bbd814241254a7e6345e90759492c175 Mon Sep 17 00:00:00 2001 From: ig Date: Tue, 4 Apr 2023 10:23:29 +0200 Subject: [PATCH] Update StatusApi & Units --- csharp/Lib/StatusApi/BatteryStatus.cs | 7 +- .../Lib/StatusApi/BatteryStatus.generated.cs | 16 --- csharp/Lib/StatusApi/DcDcConverterStatus.cs | 6 +- .../DcDcConverterStatus.generated.cs | 16 --- csharp/Lib/StatusApi/DeviceStatus.cs | 52 --------- .../Generator/OpParallelAttribute.cs | 5 - csharp/Lib/StatusApi/Generator/Template.txt | 16 --- csharp/Lib/StatusApi/Generator/generate.sh | 13 --- csharp/Lib/StatusApi/MpptStatus.cs | 6 +- csharp/Lib/StatusApi/MpptStatus.generated.cs | 16 --- csharp/Lib/StatusApi/PowerMeterStatus.cs | 6 +- .../StatusApi/PowerMeterStatus.generated.cs | 16 --- .../StatusApi/SinglePhaseInverterStatus.cs | 10 +- .../SinglePhaseInverterStatus.generated.cs | 16 --- .../StatusApi/SinglePhasePvInverterStatus.cs | 9 +- .../SinglePhasePvInverterStatus.generated.cs | 16 --- csharp/Lib/StatusApi/StatusApi.csproj | 16 +++ .../Lib/StatusApi/ThreePhaseInverterStatus.cs | 9 +- .../ThreePhaseInverterStatus.generated.cs | 16 --- .../StatusApi/ThreePhasePvInverterStatus.cs | 9 +- .../ThreePhasePvInverterStatus.generated.cs | 16 --- csharp/Lib/Units/Composite/Ac3Bus.cs | 15 ++- csharp/Lib/Units/Composite/AcPhase.cs | 2 +- csharp/Lib/Units/Composite/DcBus.cs | 5 - csharp/Lib/Units/Percent.generated.cs | 71 ++++++++++++ csharp/Lib/Units/Units.cs | 101 +++++++++++++++--- 26 files changed, 205 insertions(+), 281 deletions(-) delete mode 100644 csharp/Lib/StatusApi/BatteryStatus.generated.cs delete mode 100644 csharp/Lib/StatusApi/DcDcConverterStatus.generated.cs delete mode 100644 csharp/Lib/StatusApi/DeviceStatus.cs delete mode 100644 csharp/Lib/StatusApi/Generator/OpParallelAttribute.cs delete mode 100644 csharp/Lib/StatusApi/Generator/Template.txt delete mode 100755 csharp/Lib/StatusApi/Generator/generate.sh delete mode 100644 csharp/Lib/StatusApi/MpptStatus.generated.cs delete mode 100644 csharp/Lib/StatusApi/PowerMeterStatus.generated.cs delete mode 100644 csharp/Lib/StatusApi/SinglePhaseInverterStatus.generated.cs delete mode 100644 csharp/Lib/StatusApi/SinglePhasePvInverterStatus.generated.cs delete mode 100644 csharp/Lib/StatusApi/ThreePhaseInverterStatus.generated.cs delete mode 100644 csharp/Lib/StatusApi/ThreePhasePvInverterStatus.generated.cs create mode 100644 csharp/Lib/Units/Percent.generated.cs diff --git a/csharp/Lib/StatusApi/BatteryStatus.cs b/csharp/Lib/StatusApi/BatteryStatus.cs index f8a0124d5..d512b931f 100644 --- a/csharp/Lib/StatusApi/BatteryStatus.cs +++ b/csharp/Lib/StatusApi/BatteryStatus.cs @@ -1,14 +1,13 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.StatusApi.Generator; using InnovEnergy.Lib.Units; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi; -using T = BatteryStatus; -[OpParallel] -public partial record BatteryStatus : DeviceStatus, IDcConnection +#pragma warning disable CS8618 + +public record BatteryStatus : IDcConnection { public DcBus Dc { get; init; } public Percent Soc { get; init; } diff --git a/csharp/Lib/StatusApi/BatteryStatus.generated.cs b/csharp/Lib/StatusApi/BatteryStatus.generated.cs deleted file mode 100644 index d5f549056..000000000 --- a/csharp/Lib/StatusApi/BatteryStatus.generated.cs +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = BatteryStatus; - -[GeneratedCode("generate.sh", "1")] -public partial record BatteryStatus -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/StatusApi/DcDcConverterStatus.cs b/csharp/Lib/StatusApi/DcDcConverterStatus.cs index 21941d5e6..9a7e1c2d1 100644 --- a/csharp/Lib/StatusApi/DcDcConverterStatus.cs +++ b/csharp/Lib/StatusApi/DcDcConverterStatus.cs @@ -1,10 +1,10 @@ -using InnovEnergy.Lib.StatusApi.Generator; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi; -[OpParallel] -public partial record DcDcConverterStatus : DeviceStatus +#pragma warning disable CS8618 + +public record DcDcConverterStatus { public DcBus Left { get; init; } public DcBus Right { get; init; } diff --git a/csharp/Lib/StatusApi/DcDcConverterStatus.generated.cs b/csharp/Lib/StatusApi/DcDcConverterStatus.generated.cs deleted file mode 100644 index 32a133a8a..000000000 --- a/csharp/Lib/StatusApi/DcDcConverterStatus.generated.cs +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = DcDcConverterStatus; - -[GeneratedCode("generate.sh", "1")] -public partial record DcDcConverterStatus -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/StatusApi/DeviceStatus.cs b/csharp/Lib/StatusApi/DeviceStatus.cs deleted file mode 100644 index 380007d85..000000000 --- a/csharp/Lib/StatusApi/DeviceStatus.cs +++ /dev/null @@ -1,52 +0,0 @@ -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -public abstract record DeviceStatus -{ - public String DeviceType => GetType() - .Unfold(t => t.BaseType) - .First(t => t.IsAbstract) - .Name - .Replace("Status", ""); -} - - -// public static class Program -// { -// public static void Main(string[] args) -// { -// var x = new ThreePhasePvInverterStatus -// { -// Ac = new() -// { -// Frequency = 50, -// L1 = new() -// { -// Current = 10, -// Voltage = 10, -// Phi = 0, -// }, -// L2 = new() -// { -// Current = 52, -// Voltage = 220, -// Phi = Angle.Pi / 2, -// }, -// L3 = new() -// { -// Current = 158, -// Voltage = 454, -// Phi = Angle.Pi / 3, -// }, -// }, -// Strings = new DcBus[] -// { -// new() { Current = 10, Voltage = 22 }, -// new() { Current = 12, Voltage = 33 }, -// } -// }; -// -// var s = x.ToJson(); -// } -// } \ No newline at end of file diff --git a/csharp/Lib/StatusApi/Generator/OpParallelAttribute.cs b/csharp/Lib/StatusApi/Generator/OpParallelAttribute.cs deleted file mode 100644 index ec94f9d34..000000000 --- a/csharp/Lib/StatusApi/Generator/OpParallelAttribute.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace InnovEnergy.Lib.StatusApi.Generator; - -[AttributeUsage(AttributeTargets.Class)] -internal class OpParallelAttribute : Attribute -{} \ No newline at end of file diff --git a/csharp/Lib/StatusApi/Generator/Template.txt b/csharp/Lib/StatusApi/Generator/Template.txt deleted file mode 100644 index 43f22557f..000000000 --- a/csharp/Lib/StatusApi/Generator/Template.txt +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = Template; - -[GeneratedCode("generate.sh", "1")] -public partial record Template -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/StatusApi/Generator/generate.sh b/csharp/Lib/StatusApi/Generator/generate.sh deleted file mode 100755 index 1233bd1b7..000000000 --- a/csharp/Lib/StatusApi/Generator/generate.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - - -scriptDir=$( dirname -- "$0"; ) -cd "$scriptDir/.." || exit - -for path in $(grep -e '\[OpParallel\]' -l *.cs) -do - file=$(basename -- "$path") - class="${file%.*}" - echo "generating $file" - sed "s/Template/$class/g" "./Generator/Template.txt" > "./$class.generated.cs" -done \ No newline at end of file diff --git a/csharp/Lib/StatusApi/MpptStatus.cs b/csharp/Lib/StatusApi/MpptStatus.cs index 53acdf439..c8b52cb98 100644 --- a/csharp/Lib/StatusApi/MpptStatus.cs +++ b/csharp/Lib/StatusApi/MpptStatus.cs @@ -1,11 +1,11 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.StatusApi.Generator; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi; -[OpParallel] -public partial record MpptStatus : IDcConnection, IPvConnection +#pragma warning disable CS8618 + +public record MpptStatus : IDcConnection, IPvConnection { public DcBus Dc { get; init; } public IReadOnlyList Strings { get; init; } diff --git a/csharp/Lib/StatusApi/MpptStatus.generated.cs b/csharp/Lib/StatusApi/MpptStatus.generated.cs deleted file mode 100644 index 5e2afe796..000000000 --- a/csharp/Lib/StatusApi/MpptStatus.generated.cs +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = MpptStatus; - -[GeneratedCode("generate.sh", "1")] -public partial record MpptStatus -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/StatusApi/PowerMeterStatus.cs b/csharp/Lib/StatusApi/PowerMeterStatus.cs index 8140afef3..5c10f1ded 100644 --- a/csharp/Lib/StatusApi/PowerMeterStatus.cs +++ b/csharp/Lib/StatusApi/PowerMeterStatus.cs @@ -1,11 +1,11 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.StatusApi.Generator; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi; -[OpParallel] -public partial record PowerMeterStatus : DeviceStatus, IAc3Connection +#pragma warning disable CS8618 + +public record PowerMeterStatus : IAc3Connection { public Ac3Bus Ac { get; init; } } \ No newline at end of file diff --git a/csharp/Lib/StatusApi/PowerMeterStatus.generated.cs b/csharp/Lib/StatusApi/PowerMeterStatus.generated.cs deleted file mode 100644 index 9e6dcaf9c..000000000 --- a/csharp/Lib/StatusApi/PowerMeterStatus.generated.cs +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = PowerMeterStatus; - -[GeneratedCode("generate.sh", "1")] -public partial record PowerMeterStatus -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/StatusApi/SinglePhaseInverterStatus.cs b/csharp/Lib/StatusApi/SinglePhaseInverterStatus.cs index 6688d8886..247831ffc 100644 --- a/csharp/Lib/StatusApi/SinglePhaseInverterStatus.cs +++ b/csharp/Lib/StatusApi/SinglePhaseInverterStatus.cs @@ -1,14 +1,12 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.StatusApi.Generator; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi; -[OpParallel] -public partial record SinglePhaseInverterStatus : - DeviceStatus, - IAc1Connection, - IDcConnection + +#pragma warning disable CS8618 + +public record SinglePhaseInverterStatus : IAc1Connection, IDcConnection { public Ac1Bus Ac { get; init; } public DcBus Dc { get; init; } diff --git a/csharp/Lib/StatusApi/SinglePhaseInverterStatus.generated.cs b/csharp/Lib/StatusApi/SinglePhaseInverterStatus.generated.cs deleted file mode 100644 index 047f609fc..000000000 --- a/csharp/Lib/StatusApi/SinglePhaseInverterStatus.generated.cs +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = SinglePhaseInverterStatus; - -[GeneratedCode("generate.sh", "1")] -public partial record SinglePhaseInverterStatus -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/StatusApi/SinglePhasePvInverterStatus.cs b/csharp/Lib/StatusApi/SinglePhasePvInverterStatus.cs index 269254b6e..6a47bb521 100644 --- a/csharp/Lib/StatusApi/SinglePhasePvInverterStatus.cs +++ b/csharp/Lib/StatusApi/SinglePhasePvInverterStatus.cs @@ -1,14 +1,11 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.StatusApi.Generator; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi; -[OpParallel] -public partial record SinglePhasePvInverterStatus : - DeviceStatus, - IAc1Connection, - IPvConnection +#pragma warning disable CS8618 + +public record SinglePhasePvInverterStatus : IAc1Connection, IPvConnection { public Ac1Bus Ac { get; init; } public IReadOnlyList Strings { get; init; } diff --git a/csharp/Lib/StatusApi/SinglePhasePvInverterStatus.generated.cs b/csharp/Lib/StatusApi/SinglePhasePvInverterStatus.generated.cs deleted file mode 100644 index 37340f111..000000000 --- a/csharp/Lib/StatusApi/SinglePhasePvInverterStatus.generated.cs +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = SinglePhasePvInverterStatus; - -[GeneratedCode("generate.sh", "1")] -public partial record SinglePhasePvInverterStatus -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/StatusApi/StatusApi.csproj b/csharp/Lib/StatusApi/StatusApi.csproj index b68a8b119..e5229ac29 100644 --- a/csharp/Lib/StatusApi/StatusApi.csproj +++ b/csharp/Lib/StatusApi/StatusApi.csproj @@ -7,6 +7,22 @@ + + + + + + + + + + + + + + + + diff --git a/csharp/Lib/StatusApi/ThreePhaseInverterStatus.cs b/csharp/Lib/StatusApi/ThreePhaseInverterStatus.cs index c525a6a67..b33618c97 100644 --- a/csharp/Lib/StatusApi/ThreePhaseInverterStatus.cs +++ b/csharp/Lib/StatusApi/ThreePhaseInverterStatus.cs @@ -1,14 +1,11 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.StatusApi.Generator; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi; -[OpParallel] -public partial record ThreePhaseInverterStatus : - DeviceStatus, - IAc3Connection, - IDcConnection +#pragma warning disable CS8618 + +public record ThreePhaseInverterStatus : IAc3Connection, IDcConnection { public Ac3Bus Ac { get; init; } public DcBus Dc { get; init; } diff --git a/csharp/Lib/StatusApi/ThreePhaseInverterStatus.generated.cs b/csharp/Lib/StatusApi/ThreePhaseInverterStatus.generated.cs deleted file mode 100644 index 989941505..000000000 --- a/csharp/Lib/StatusApi/ThreePhaseInverterStatus.generated.cs +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = ThreePhaseInverterStatus; - -[GeneratedCode("generate.sh", "1")] -public partial record ThreePhaseInverterStatus -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/StatusApi/ThreePhasePvInverterStatus.cs b/csharp/Lib/StatusApi/ThreePhasePvInverterStatus.cs index 8c122ffb3..94c31980d 100644 --- a/csharp/Lib/StatusApi/ThreePhasePvInverterStatus.cs +++ b/csharp/Lib/StatusApi/ThreePhasePvInverterStatus.cs @@ -1,14 +1,11 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.StatusApi.Generator; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi; -[OpParallel] -public partial record ThreePhasePvInverterStatus : - DeviceStatus, - IAc3Connection, - IPvConnection +#pragma warning disable CS8618 + +public record ThreePhasePvInverterStatus : IAc3Connection, IPvConnection { public Ac3Bus Ac { get; init; } public IReadOnlyList Strings { get; init; } diff --git a/csharp/Lib/StatusApi/ThreePhasePvInverterStatus.generated.cs b/csharp/Lib/StatusApi/ThreePhasePvInverterStatus.generated.cs deleted file mode 100644 index 4e7170a21..000000000 --- a/csharp/Lib/StatusApi/ThreePhasePvInverterStatus.generated.cs +++ /dev/null @@ -1,16 +0,0 @@ -#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. -using System.CodeDom.Compiler; -using InnovEnergy.Lib.Units.Composite; -using InnovEnergy.Lib.Utils; - -namespace InnovEnergy.Lib.StatusApi; - -using T = ThreePhasePvInverterStatus; - -[GeneratedCode("generate.sh", "1")] -public partial record ThreePhasePvInverterStatus -{ - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); - public static T operator |(T left, T right) => OpParallel(left, right); -} - \ No newline at end of file diff --git a/csharp/Lib/Units/Composite/Ac3Bus.cs b/csharp/Lib/Units/Composite/Ac3Bus.cs index e35c73cfc..bdd505a07 100644 --- a/csharp/Lib/Units/Composite/Ac3Bus.cs +++ b/csharp/Lib/Units/Composite/Ac3Bus.cs @@ -1,20 +1,19 @@ -using InnovEnergy.Lib.Utils; using static DecimalMath.DecimalEx; namespace InnovEnergy.Lib.Units.Composite; + +#pragma warning disable CS8618 + public record Ac3Bus { - public AcPhase L1 { get; init; } - public AcPhase L2 { get; init; } - public AcPhase L3 { get; init; } - public Frequency Frequency { get; init; } + public AcPhase L1 { get; init; } + public AcPhase L2 { get; init; } + public AcPhase L3 { get; init; } + public Frequency Frequency { get; init; } public ApparentPower ApparentPower => L1.ApparentPower + L2.ApparentPower + L3.ApparentPower; public ReactivePower ReactivePower => L1.ReactivePower + L2.ReactivePower + L3.ReactivePower; public Power ActivePower => L1.ActivePower + L2.ActivePower + L3.ActivePower; public Angle Phi => ATan2(ReactivePower, ActivePower); - - public static Ac3Bus operator |(Ac3Bus left, Ac3Bus right) => OpParallel(left, right); - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); } \ No newline at end of file diff --git a/csharp/Lib/Units/Composite/AcPhase.cs b/csharp/Lib/Units/Composite/AcPhase.cs index 8186e01f9..d57b68aa8 100644 --- a/csharp/Lib/Units/Composite/AcPhase.cs +++ b/csharp/Lib/Units/Composite/AcPhase.cs @@ -19,7 +19,7 @@ public record AcPhase : IBus init => _Current = value >= 0m ? value : throw new ArgumentException("RMS value cannot be negative"); } - public Angle Phi { get; init; } + public Angle Phi { get; init; } public ApparentPower ApparentPower => Voltage.Value * Current.Value ; public Power ActivePower => ApparentPower.Value * PowerFactor; diff --git a/csharp/Lib/Units/Composite/DcBus.cs b/csharp/Lib/Units/Composite/DcBus.cs index 9bab0143a..8fbdd9802 100644 --- a/csharp/Lib/Units/Composite/DcBus.cs +++ b/csharp/Lib/Units/Composite/DcBus.cs @@ -1,5 +1,3 @@ -using InnovEnergy.Lib.Utils; - namespace InnovEnergy.Lib.Units.Composite; public record DcBus : IBus @@ -8,7 +6,4 @@ public record DcBus : IBus public Current Current { get; init; } public Power Power => Current * Voltage; - - public static DcBus operator |(DcBus left, DcBus right) => OpParallel(left, right); - private static readonly Func OpParallel = "|".CreateBinaryOpForProps(); } \ No newline at end of file diff --git a/csharp/Lib/Units/Percent.generated.cs b/csharp/Lib/Units/Percent.generated.cs new file mode 100644 index 000000000..e67b97384 --- /dev/null +++ b/csharp/Lib/Units/Percent.generated.cs @@ -0,0 +1,71 @@ +#nullable enable // Auto-generated code requires an explicit '#nullable' directive in source. +#define Generate + +using static System.Math; +using System.Text.Json; +using System.Text.Json.Serialization; +using InnovEnergy.Lib.Utils; +using System.CodeDom.Compiler; + +namespace InnovEnergy.Lib.Units; + +using T = Percent; + +[GeneratedCode("generate.sh", "1")] +[JsonConverter(typeof(PercentConverter))] +public readonly partial struct Percent +{ + public Decimal Value { get; } + public override String ToString() => Value.RoundToSignificantDigits(Units.DisplaySignificantDigits) + Unit; + + // scalar multiplication + + public static T operator *(Decimal scalar, T t) => new T(scalar * t.Value); + public static T operator *(T t, Decimal scalar) => new T(scalar * t.Value); + public static T operator /(T t, Decimal scalar) => new T(t.Value / scalar); + + // addition + + public static T operator +(T left, T right) => new T(left.Value + right.Value); + public static T operator -(T left, T right) => new T(left.Value - right.Value); + public static T operator -(T t) => new T(-t.Value); + + // compare + + public static Boolean operator ==(T left, T right) => left.Value == right.Value; + public static Boolean operator !=(T left, T right) => left.Value != right.Value; + public static Boolean operator > (T left, T right) => left.Value > right.Value; + public static Boolean operator < (T left, T right) => left.Value < right.Value; + public static Boolean operator >=(T left, T right) => left.Value >= right.Value; + public static Boolean operator <=(T left, T right) => left.Value <= right.Value; + + // conversion + + public static implicit operator T(Decimal d) => new T(d); + public static implicit operator T(Double d) => new T((Decimal)d); + public static implicit operator T(Int32 i) => new T(i); + public static implicit operator Decimal(T t) => t.Value; + + // equality + + public Boolean Equals(T other) => Value == other.Value; + public override Boolean Equals(Object? obj) => obj is T other && Equals(other); + public override Int32 GetHashCode() => Value.GetHashCode(); + +} + + +internal class PercentConverter : JsonConverter +{ + public override Percent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return new Percent(reader.GetDecimal()); + } + + public override void Write(Utf8JsonWriter writer, Percent value, JsonSerializerOptions options) + { + var rounded = value.Value.RoundToSignificantDigits(Units.JsonSignificantDigits); + + writer.WriteNumberValue(rounded); + } +} \ No newline at end of file diff --git a/csharp/Lib/Units/Units.cs b/csharp/Lib/Units/Units.cs index 3694c0783..d9ee297c5 100644 --- a/csharp/Lib/Units/Units.cs +++ b/csharp/Lib/Units/Units.cs @@ -1,23 +1,7 @@ -using System.Text.Json.Serialization; - namespace InnovEnergy.Lib.Units; public static class Units { - static Units() - { - JsonConverters = typeof(Units) - .Assembly - .GetTypes() - .Where(t => t.IsAssignableTo(typeof(JsonConverter))) - .Select(Activator.CreateInstance) - .Cast() - .ToArray(); - } - - - public static IReadOnlyList JsonConverters { get; } - public static Byte DisplaySignificantDigits { get; set; } = 3; public static Byte JsonSignificantDigits { get; set; } = 3; @@ -31,4 +15,89 @@ public static class Units public static Angle Rad (this Decimal value) => value; public static Temperature Celsius(this Decimal value) => value; public static Energy KWh (this Decimal value) => value; +} + +public static class Prefixes +{ + private static readonly IReadOnlyList Big = new[] + { + "", + "k", + "M", + "G", + "T", + "P", + "E", + "Y", + }; + + private static readonly IReadOnlyList Small = new[] + { + "", + "m", + "ยต", + "n", + "p", + "f", + "a", + "z", + "y", + }; + + public static String TestGetPrefix(Double v, String unit) + { + if (v == 0) + return ""; + + var log10 = Math.Log10(v / 10); + var l = (Int32)Math.Floor(log10 / 3); + var lookUp = l > 0 ? Big : Small; + var i = Math.Abs(l); + + return $"{v / Math.Pow(10.0, l * 3.0)} {lookUp[i]}{unit}"; + } + + + public static String TestGetPrefix(Decimal v, String unit) + { + if (v == 0m) + return ""; + + var d = (Double)v; + var log10 = Math.Log10(d / 10); + var l = (Int32)Math.Floor(log10 / 3); + var lookUp = l > 0 ? Big : Small; + var i = Math.Abs(l); + + return $"{d / Math.Pow(10.0, l * 3.0)} {lookUp[i]}{unit}"; + } + + public static String TestGetPrefix2(Decimal v, String unit) + { + if (v == 0m) + return ""; + + var a = Math.Abs(v); + var s = Math.Sign(v); + + var i = 0; + + while (a >= 10000m) + { + a /= 1000; + i++; + } + while (a < 10m) + { + a *= 1000; + i--; + } + + var lookUp = i >= 0 ? Big : Small; + + var r = Decimal.Floor(a * 10m) / 10m; + + return $"{r*s} {lookUp[Math.Abs(i)]}{unit}"; + } + } \ No newline at end of file