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

62 lines
2.1 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using System.Text.Json.Nodes;
using InnovEnergy.Lib.Time.Unix;
2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Utils;
// ReSharper disable StringLiteralTypo
namespace InnovEnergy.Lib.Victron.VictronVRM;
public readonly partial record struct Installation(VrmAccount VrmAccount, JsonNode Json)
{
2023-02-16 12:57:06 +00:00
public UInt64 IdSite => Json.GetUInt64("idSite");
public UnixTime Created => Json.GetUInt32("syscreated").Apply(UnixTime.FromTicks);
// Settings
2023-02-16 12:57:06 +00:00
public String Name => Json.GetString("name");
public String Notes => Json.GetString("notes");
public String PhoneNumber => Json.GetString("phonenumber");
2023-05-11 08:20:47 +00:00
public String Identifier => Json.TryGetString("identifier");
2023-02-16 12:57:06 +00:00
// 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;
}
}