using System.Text.Json.Nodes; using InnovEnergy.Lib.StatusApi; namespace InnovEnergy.App.SaliMax.Log; public static class JsonUtil { public static JsonObject CreateDevice(this DeviceType deviceType, String name) { return new JsonObject { { "Name", name }, { "Type", deviceType.ToString() } }; } public static JsonObject AddAcConnection(this JsonObject json, Decimal frequency, IEnumerable acPhases) { return json.AddAcConnection(frequency, acPhases.ToArray()); } public static JsonObject AddAcConnection(this JsonObject json, Decimal frequency, params JsonNode[] acPhases) { return json .AddProp("Ac", new JsonArray(acPhases)) .AddProp("Frequency", frequency); } public static JsonObject AddAlarms(this JsonObject json, IEnumerable alarms) { return json.AddProp("Alarms", alarms.ToJsonArray()); } public static JsonObject AddWarnings(this JsonObject json, IEnumerable warnings) { return json.AddProp("Warnings", warnings.ToJsonArray()); } public static JsonObject AddProp(this JsonObject json, String key, JsonNode? value) { json.Add(key, value); return json; } public static JsonObject AddDcConnection(this JsonObject json, Decimal current, Decimal voltage) { return json.AddProp("Dc", CreateDcPhase(current, voltage)); } public static JsonObject AddDc48Connection(this JsonObject json, Decimal current, Decimal voltage) { return json.AddProp("Dc48", CreateDcPhase(current, voltage)); } public static JsonObject CreateAcPhase(Decimal current, Decimal voltage, Decimal phi) { return new JsonObject { ["Current"] = current, ["Voltage"] = voltage, ["Phi" ] = phi, }; } public static JsonObject CreateDcPhase(Decimal current, Decimal voltage) { return new JsonObject { ["Current"] = current , ["Voltage"] = voltage , ["Power"] = current * voltage, }; } public static Decimal Round3(this Decimal val) { return Decimal.Round(val, 3); } public static Decimal Round0(this Decimal val) { return Decimal.Round(val, 0); } public static JsonObject CreateBus(String left, String top, String bottom, String right, String name) { return new JsonObject { ["Name"] = name, ["Left"] = left, ["Top"] = top, ["Bottom"] = bottom, ["Right"] = right }; } public static String Port(DeviceType dt, BusPort bp, Boolean display = true) { return $"{Enum.GetName(dt)}:{Enum.GetName(bp)}:{(display ? "show" : "hide")}"; } public static JsonArray ToJsonArray(this IEnumerable things) { var jsonValues = things .Select(t => t!.ToString()) .Select(t => JsonValue.Create(t)) .OfType() .ToArray(); return new JsonArray(jsonValues); } }