118 lines
3.2 KiB
C#
118 lines
3.2 KiB
C#
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<JsonNode> 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<T>(this JsonObject json, IEnumerable<T> alarms)
|
|
{
|
|
return json.AddProp("Alarms", alarms.ToJsonArray());
|
|
}
|
|
|
|
public static JsonObject AddWarnings<T>(this JsonObject json, IEnumerable<T> 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<T>(this IEnumerable<T> things)
|
|
{
|
|
var jsonValues = things
|
|
.Select(t => t!.ToString())
|
|
.Select(t => JsonValue.Create(t))
|
|
.OfType<JsonNode>()
|
|
.ToArray();
|
|
|
|
return new JsonArray(jsonValues);
|
|
}
|
|
|
|
} |