Innovenergy_trunk/csharp/Lib/Victron/VictronVRM/Installation.cs

61 lines
2.0 KiB
C#

using System.Text.Json.Nodes;
using InnovEnergy.Lib.Utils;
// ReSharper disable StringLiteralTypo
namespace InnovEnergy.Lib.Victron.VictronVRM;
public readonly partial record struct Installation(VrmAccount VrmAccount, JsonNode Json)
{
public UInt64 IdSite => Json.GetUInt64("idSite");
public UInt32 Created => Json.GetUInt32("syscreated");
// Settings
public String Name => Json.GetString("name");
public String Notes => Json.GetString("notes");
public String PhoneNumber => Json.GetString("phonenumber");
public String Identifier => Json.TryGetString("identifier");
// TODO: alternative way to handle settings? make them writeable here and have an UpdateInstallation function?
// public String Name
// {
// get => Json.GetString("name");
// set => Json["name"] = value;
// }
// UniqueId is encoded as json number when its hex value contains no letters !!!
public String UniqueId => Json.TryGetString("identifier") ??
Json.TryGetUInt64("identifier")?.ToString() ??
Json.TryGetUInt32("identifier")?.ToString() ??
"unknown";
public async Task<IReadOnlyList<Detail>> GetDetails(Int32 nDetails = 1000)
{
var reply = await VrmAccount.DetailsRequest(IdSite, nDetails).TryGetJson();
var vrmReply = new Reply(reply);
if (!vrmReply.Success)
throw new Exception(nameof(GetDetails) + " failed");
var records = vrmReply.Records;
return records
.Select(d => new Detail(d!))
.ToArray(records.Count);
}
public async Task<IReadOnlyList<Device>> GetDevices()
{
var reply = await VrmAccount.DevicesRequest(IdSite).TryGetJson();
var vrmReply = new Reply(reply);
if (!vrmReply.Success)
throw new Exception(nameof(GetDevices) + " failed");
return vrmReply.Devices;
}
}