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

60 lines
2.0 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using System.Text.Json.Nodes;
using InnovEnergy.Lib.Utils;
using InnovEnergy.Time.Unix;
// 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 UnixTime Created => Json.GetUInt32("syscreated").Apply(UnixTime.FromTicks);
// Settings
public String Name => Json.GetString("name");
public String Notes => Json.GetString("notes");
public String PhoneNumber => Json.GetString("phonenumber");
// 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;
}
}