implement ToJson() for DeviceStatus

This commit is contained in:
ig 2023-03-02 18:03:10 +01:00
parent 8ae9119858
commit 94e5496872
3 changed files with 56 additions and 3 deletions

View File

@ -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", "");
}
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();
// }
// }

View File

@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../InnovEnergy.Lib.props" />
<!-- <Import Project="../../App/InnovEnergy.App.props" />-->
<ItemGroup>
<ProjectReference Include="../Protocols/Modbus/Modbus.csproj" />

View File

@ -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)