From 94e54968725ac42debfbc00ffb619c8fdeb7b7d3 Mon Sep 17 00:00:00 2001 From: ig Date: Thu, 2 Mar 2023 18:03:10 +0100 Subject: [PATCH] implement ToJson() for DeviceStatus --- csharp/Lib/StatusApi/DeviceStatus.cs | 54 ++++++++++++++++++++++++++- csharp/Lib/StatusApi/StatusApi.csproj | 1 + csharp/Lib/Units/Composite/AcPhase.cs | 4 +- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/csharp/Lib/StatusApi/DeviceStatus.cs b/csharp/Lib/StatusApi/DeviceStatus.cs index 6cdc1ef3f..ef92280ab 100644 --- a/csharp/Lib/StatusApi/DeviceStatus.cs +++ b/csharp/Lib/StatusApi/DeviceStatus.cs @@ -1,12 +1,64 @@ +using System.Text.Json; using InnovEnergy.Lib.Utils; +using static InnovEnergy.Lib.Units.Units; namespace InnovEnergy.Lib.StatusApi; public abstract record DeviceStatus { + private static readonly JsonSerializerOptions JsonSerializerOptions; + + static DeviceStatus() + { + JsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true }; + JsonConverters.ForEach(JsonSerializerOptions.Converters.Add); // how stupid is that?!! + } + public String DeviceType => GetType() .Generate(t => t.BaseType!) .First(t => t.IsAbstract) .Name .Replace("Status", ""); -} \ No newline at end of file + + public String ToJson() => JsonSerializer.Serialize(this, GetType(), JsonSerializerOptions); +} + + +// 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/StatusApi.csproj b/csharp/Lib/StatusApi/StatusApi.csproj index d14ca02c9..b68a8b119 100644 --- a/csharp/Lib/StatusApi/StatusApi.csproj +++ b/csharp/Lib/StatusApi/StatusApi.csproj @@ -1,5 +1,6 @@ + diff --git a/csharp/Lib/Units/Composite/AcPhase.cs b/csharp/Lib/Units/Composite/AcPhase.cs index faa20f23d..d590fbdee 100644 --- a/csharp/Lib/Units/Composite/AcPhase.cs +++ b/csharp/Lib/Units/Composite/AcPhase.cs @@ -22,9 +22,9 @@ public record AcPhase : IBus public Angle Phi { get; init; } public ApparentPower ApparentPower => Voltage.Value * Current.Value ; - public Power ActivePower => ApparentPower.Value * PowerFactor; + public Power ActivePower => ApparentPower.Value * PowerFactor.Value; public ReactivePower ReactivePower => ApparentPower.Value * Sin(Phi); - public Decimal PowerFactor => Cos(Phi); + public Number PowerFactor => Cos(Phi); public static AcPhase operator |(AcPhase left, AcPhase right)