67 lines
2.5 KiB
C#
67 lines
2.5 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Reactive.Concurrency;
|
|
using System.Reactive.Linq;
|
|
using System.Reactive.Threading.Tasks;
|
|
using InnovEnergy.Lib.Utils;
|
|
using InnovEnergy.Lib.Victron.VictronVRM;
|
|
using SQLite;
|
|
using static System.Text.Json.JsonSerializer;
|
|
using Installation = InnovEnergy.App.VrmGrabber.DataTypes.Installation;
|
|
|
|
|
|
namespace InnovEnergy.App.VrmGrabber.Database;
|
|
|
|
|
|
public static partial class Db
|
|
{
|
|
public static Dictionary<Lib.Victron.VictronVRM.Installation, IReadOnlyList<Detail>> InstallationsAndDetails;
|
|
internal const String DbPath = "./db.sqlite";
|
|
|
|
|
|
private static SQLiteConnection Connection { get; } = new SQLiteConnection(DbPath);
|
|
public static TableQuery<Installation> Installations => Connection.Table<Installation>();
|
|
public static void Init()
|
|
{
|
|
// used to force static constructor
|
|
}
|
|
|
|
static Db()
|
|
{
|
|
// on startup create/migrate tables
|
|
|
|
Connection.RunInTransaction(() =>
|
|
{
|
|
// Connection.CreateTable<Installation>();
|
|
});
|
|
|
|
InstallationsAndDetails = new Dictionary<Lib.Victron.VictronVRM.Installation, IReadOnlyList<Detail>>();
|
|
|
|
Observable.Interval(TimeSpan.FromMinutes(5))
|
|
.ObserveOn(TaskPoolScheduler.Default)
|
|
.SubscribeOn(TaskPoolScheduler.Default)
|
|
.StartWith(0) // Do it right away (on startup)
|
|
.Select(UpdateInstallationsAndDetailsFromVrm)
|
|
.Select(t => t.ToObservable())
|
|
.Concat()
|
|
.Subscribe(d => InstallationsAndDetails = d, exception => exception.WriteLine());
|
|
}
|
|
|
|
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
|
|
private static async Task<Dictionary<Lib.Victron.VictronVRM.Installation, IReadOnlyList<Detail>>> UpdateInstallationsAndDetailsFromVrm(Int64 _)
|
|
{
|
|
var fileContent = await File.ReadAllTextAsync("./token.json");
|
|
|
|
var acc = Deserialize<AccToken>(fileContent);
|
|
var user = VrmAccount.Token(acc.idUser, acc.token);
|
|
var installations = await user.GetInstallations();
|
|
return installations
|
|
.Do(i=>i.Name.WriteLine())
|
|
.ToDictionary(i => i, i => i.GetDetails().Result);
|
|
}
|
|
}
|
|
|
|
public class AccToken
|
|
{
|
|
public UInt64 idUser { get; init;}
|
|
public String token { get; init;}
|
|
} |