added VrmGrabber Still WIP

This commit is contained in:
Kim 2023-04-27 16:59:45 +02:00
parent 76e23d579f
commit 313dd4d406
14 changed files with 280 additions and 1 deletions

View File

@ -0,0 +1,35 @@
using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using Flurl.Util;
using InnovEnergy.App.Backend.Database;
using InnovEnergy.Lib.Victron.VictronVRM;
using Microsoft.AspNetCore.Mvc;
namespace InnovEnergy.App.Backend.Controllers;
using Token = String;
[ApiController]
[Route("api/")]
public class Controller : ControllerBase
{
[HttpPost(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)
{
foreach (var detailList in Db.InstallationsAndDetails.Values.ToList().Select((Value, Index) => new {Value, 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);
return retour;
}
return new NotFoundResult();
}
}

View File

@ -0,0 +1,18 @@
using InnovEnergy.Lib.Victron.VictronVRM;
using SQLite;
namespace InnovEnergy.App.Backend.DataTypes;
public class Installation : TreeNode
{
public Installation()
{
}
public String Name { get; set; }
// Settings
public IReadOnlyList<Detail> Notes { get; set; }
}

View File

@ -0,0 +1,7 @@
namespace InnovEnergy.App.Backend.DataTypes.Methods;
public static class InstallationMethods
{
}

View File

@ -0,0 +1,11 @@
using SQLite;
namespace InnovEnergy.App.Backend.DataTypes;
public abstract partial class TreeNode
{
[PrimaryKey, AutoIncrement]
public virtual Int64 Id { get; set; }
public String Information { get; set; } = ""; // unstructured random info
}

View File

@ -0,0 +1,14 @@
using InnovEnergy.App.Backend.DataTypes;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
public static Boolean Create(Installation installation)
{
// SQLite wrapper is smart and *modifies* t's Id to the one generated (autoincrement) by the insertion
return Connection.Insert(installation) > 0;
}
}

View File

@ -0,0 +1,70 @@
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 InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Victron.VictronVRM;
using SQLite;
using Installation = InnovEnergy.App.Backend.DataTypes.Installation;
namespace InnovEnergy.App.Backend.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 content = await File.ReadAllTextAsync("./token.json");
var acc = JsonSerializer.Deserialize<AccToken>(content)!;
using 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;
public String token;
}

View File

@ -0,0 +1,15 @@
using InnovEnergy.App.Backend.DataTypes;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
public static Boolean Delete(Installation installation)
{
return Installations.Delete(i => i.Id == installation.Id) > 0;
}
}

View File

@ -0,0 +1,16 @@
using InnovEnergy.App.Backend.DataTypes;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
public static Installation? GetInstallationById(Int64? id)
{
return Installations
.FirstOrDefault(i => i.Id == id);
}
}

View File

@ -0,0 +1,41 @@
using InnovEnergy.App.Backend.Database;
using Microsoft.OpenApi.Models;
namespace InnovEnergy.App.Backend;
public static class Program
{
public static void Main(String[] args)
{
//Db.CreateFakeRelations();
Db.Init();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", OpenApiInfo);
c.UseAllOfToExtendReferenceSchemas();
c.SupportNonNullableReferenceTypes();
});
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.MapControllers();
app.Run();
}
private static OpenApiInfo OpenApiInfo { get; } = new OpenApiInfo
{
Title = "InnovEnergy VRM Grabber",
Version = "v1"
};
}

View File

@ -0,0 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="../InnovEnergy.App.props" />
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="3.2.4" />
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
<PackageReference Include="MailKit" Version="3.6.0" />
<PackageReference Include="Microsoft.AspNet.Identity.Core" Version="2.2.3" />
<PackageReference Include="Microsoft.AspNet.Identity.Owin" Version="2.2.3" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.9" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="1.26.0" />
<PackageReference Include="Microsoft.Owin.Cors" Version="4.2.2" />
<PackageReference Include="Microsoft.Owin.Host.SystemWeb" Version="4.2.2" />
<PackageReference Include="Microsoft.Owin.Security" Version="4.2.2" />
<PackageReference Include="Microsoft.Owin.Security.Cookies" Version="4.2.2" />
<PackageReference Include="Microsoft.Owin.Security.OAuth" Version="4.2.2" />
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
<PackageReference Include="sqlite-net-sqlcipher" Version="1.9.141-beta" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters.Abstractions" Version="7.0.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../Lib/Utils/Utils.csproj" />
<ProjectReference Include="..\..\Lib\Victron\VictronVRM\VictronVRM.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Resources/s3cmd.py">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Content Update="Resources\urlAndKey.json">
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>

Binary file not shown.

View File

@ -0,0 +1 @@
{"idUser":55450,"token":"909379784fa601a52ab4756c1cc9690ecc769db4b661214f9b165eda20be9913"}

View File

@ -73,6 +73,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Meta", "Meta", "{AED84693-C
../.gitignore = ../.gitignore
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VrmGrabber", "App\VrmGrabber\VrmGrabber.csproj", "{4F9BB20B-8030-48AB-A37B-23796459D516}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -184,6 +186,10 @@ Global
{B816BB44-E97E-4E02-B80A-BEDB5B923A96}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B816BB44-E97E-4E02-B80A-BEDB5B923A96}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B816BB44-E97E-4E02-B80A-BEDB5B923A96}.Release|Any CPU.Build.0 = Release|Any CPU
{4F9BB20B-8030-48AB-A37B-23796459D516}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F9BB20B-8030-48AB-A37B-23796459D516}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F9BB20B-8030-48AB-A37B-23796459D516}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F9BB20B-8030-48AB-A37B-23796459D516}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CF4834CB-91B7-4172-AC13-ECDA8613CD17} = {145597B4-3E30-45E6-9F72-4DD43194539A}
@ -216,5 +222,6 @@ Global
{C04FB6DA-23C6-46BB-9B21-8F4FBA32FFF7} = {AD5B98A8-AB7F-4DA2-B66D-5B4E63E7D854}
{4A67D79F-F0C9-4BBC-9601-D5948E6C05D3} = {AD5B98A8-AB7F-4DA2-B66D-5B4E63E7D854}
{B816BB44-E97E-4E02-B80A-BEDB5B923A96} = {DDDBEFD0-5DEA-4C7C-A9F2-FDB4636CF092}
{4F9BB20B-8030-48AB-A37B-23796459D516} = {145597B4-3E30-45E6-9F72-4DD43194539A}
EndGlobalSection
EndGlobal

View File

@ -37,7 +37,7 @@ public static class Requests
.AppendPathSegment("users")
.AppendPathSegment(account.UserId)
.AppendPathSegment("installations")
.WithHeader("X-Authorization", account.Auth);
.WithHeader("x-authorization", account.Auth);
}
public static IFlurlRequest TagsRequest(this VrmAccount account, UInt64 installationId)