added VrmGrabber to replicate VRM installations in memory (to run run the server.py file with flask run) FLASK_APP=server.py

This commit is contained in:
Kim 2023-05-04 13:59:47 +02:00
parent 9f0d37ce03
commit d1fb0a8ac2
7 changed files with 81 additions and 16 deletions

Binary file not shown.

View File

@ -0,0 +1,10 @@
defaultaccount = "Kim"
[[accounts]]
account = "innovenergy-ag"
defaultZone = "ch-dk-2"
endpoint = "https://api.exoscale.com/v1"
environment = ""
key = "EXOf67c5b528282988503ddab12"
name = "Kim"
secret = "KBFh5HvoSQcTtGYcWSm4Qn4m-WFutKe89UqsOdOL-ts"

View File

@ -1,6 +1,7 @@
using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Nodes;
using Flurl.Util;
using InnovEnergy.App.Backend.Database;
using InnovEnergy.Lib.Victron.VictronVRM;
@ -14,22 +15,61 @@ using Token = String;
[Route("api/")]
public class Controller : ControllerBase
{
[HttpPost(nameof(GetInstallation))]
[HttpGet(nameof(GetInstallation))]
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public Object GetInstallation(String serialNumber)
public Object GetInstallation(UInt64 serialNumber)
{
foreach (var detailList in Db.InstallationsAndDetails.Values.ToList().Select((Value, Index) => new {Value, Index}))
var instList = Db.InstallationsAndDetails.Values.ToList();
foreach (var detailList in instList.Select((value, index) => new { Value = value, Index = index}))
{
if (detailList.Value.All(detail => detail.FormattedValue != serialNumber)) continue;
var retour = JsonSerializer.Serialize(Db.InstallationsAndDetails.Keys.ToList()[detailList.Index]);
retour += JsonSerializer.Serialize(detailList.Value);
if (detailList.Value.All(detail => detail.Json["idSite"]?.GetValue<UInt64>() != serialNumber)) continue;
var retour = Db.InstallationsAndDetails.Keys.ToList()[detailList.Index].Json;
retour["details"] = JsonSerializer.Deserialize<JsonArray>(JsonSerializer.Serialize(detailList.Value.Select(d => d.Json).ToArray()));
return retour;
}
return new NotFoundResult();
}
[HttpGet(nameof(GetInstallationList))]
public Object GetInstallationList()
{
var instList = Db.InstallationsAndDetails.Keys.ToList();
if (instList.Count == 0) return 0;
var returnJson = new Dictionary<String, Dictionary<String, String>>();
foreach (var installation in instList)
{
returnJson[installation.Name] = new Dictionary<String, String>();
returnJson[installation.Name].Add("ip", $"<a target='_blank' href=https://{Ip(installation)}> On-Device-Gui </a>");
try
{
returnJson[installation.Name].Add("idSite",
$"<a target='_blank' href=https://vrm.victronenergy.com/installation/{installation.IdSite}/dashboard> Dashboard </a>");
returnJson[installation.Name]
.Add("identifier", $"{installation.Json["identifier"]?.GetValue<String>()}");
returnJson[installation.Name].Add("machine serial number", $"{Serial(installation)}");
}
catch (Exception)
{
continue;
}
}
return returnJson;
}
private static String? Ip(Installation installation)
{
return Db.InstallationsAndDetails[installation].RemoteSupportIp();
}
private static String? Serial(Installation installation)
{
return Db.InstallationsAndDetails[installation].MachineSerial();
}
}
// installation Name, ip (link uf gui), idSite (vrm link), identifier , machineserial (HQ...)

View File

@ -3,8 +3,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Text.Json;
using Flurl.Util;
using static System.Text.Json.JsonSerializer;
using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Victron.VictronVRM;
@ -52,10 +51,10 @@ public static partial class Db
[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 content = await File.ReadAllTextAsync("./token.json");
var fileContent = await File.ReadAllTextAsync("./token.json");
var acc = JsonSerializer.Deserialize<AccToken>(content)!;
using var user = VrmAccount.Token(acc.idUser, acc.token);
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())
@ -65,6 +64,6 @@ public static partial class Db
public class AccToken
{
public UInt64 idUser;
public String token;
public UInt64 idUser { get; init;}
public String token { get; init;}
}

View File

@ -0,0 +1,14 @@
import requests as requests
from flask import Flask
from json2html import json2html
app = Flask(__name__)
serverUrl = "https://127.0.0.1:7087/api" #todo change me
@app.route('/')
def hello():
json = requests.get(serverUrl + '/GetInstallationList', verify=False) #TODO VERIFY
if json.content == b'0':
return "Still loading Installations"
return json2html.convert(json = json.json(), escape=False)

View File

@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
namespace InnovEnergy.Lib.Utils;

View File

@ -8,6 +8,7 @@ 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);
@ -15,7 +16,7 @@ public readonly partial record struct Installation(VrmAccount VrmAccount, JsonNo
public String Name => Json.GetString("name");
public String Notes => Json.GetString("notes");
public String PhoneNumber => Json.GetString("phonenumber");
public String Identifier => Json.GetString("identifier");
// TODO: alternative way to handle settings? make them writeable here and have an UpdateInstallation function?
// public String Name
// {