Fix: Broken stuff

This commit is contained in:
Stefan
2023-03-01 12:46:19 +01:00
parent d0aa87d3c9
commit 1f745951d4
6 changed files with 106 additions and 91 deletions
@@ -34,4 +34,6 @@ public class Device
User = user; User = user;
Room = room; Room = room;
} }
private Device(){}
} }
@@ -1,6 +1,7 @@
@page "/addplug" @page "/addplug"
@using Blazorise.Components; @using Blazorise.Components;
@using Elektrifikatsiya.Models; @using Elektrifikatsiya.Models;
@using System.Net
@inject IVersionProvider VersionProvider @inject IVersionProvider VersionProvider
<Row Height=Height.Is50 Padding="Padding.Is3.OnDesktop.Is0"> <Row Height=Height.Is50 Padding="Padding.Is3.OnDesktop.Is0">
@@ -22,7 +23,7 @@
</Row> </Row>
<Row Width="Width.Is100"> <Row Width="Width.Is100">
<Column> <Column>
<Small>IP: @context.Item.Address</Small> <Small>IP: @context.Item.IpAddress</Small>
</Column> </Column>
</Row> </Row>
</Column> </Column>
@@ -39,5 +40,5 @@
</Row> </Row>
@code { @code {
List<Device> plugs = new List<Device>() { new Device("Placeholder", "Plug", System.Net.IPAddress.Broadcast, 60, "Oldin", "4AHINF"), new Device("Placeholder", "Plug", System.Net.IPAddress.Broadcast, 60, "Oldin", "4AHINF"), new Device("Placeholder", "Plug", System.Net.IPAddress.Broadcast, 60, "Oldin", "4AHINF") }; List<Device> plugs = new List<Device>() { new Device("ffff:ffff:ffff:ffff", "Huso", IPAddress.Broadcast, new User("Joe", "qiowruioewjfopa", Role.Admin), "Raum"), new Device("ffff:ffff:ffff:ffff", "Huso", IPAddress.Broadcast, new User("Joe", "qiowruioewjfopa", Role.Admin), "Raum"), new Device("ffff:ffff:ffff:ffff", "Huso", IPAddress.Broadcast, new User("Joe", "qiowruioewjfopa", Role.Admin), "Raum"), };
} }
@@ -1,6 +1,7 @@
@page "/" @page "/"
@using Blazorise.Components; @using Blazorise.Components;
@using Elektrifikatsiya.Models; @using Elektrifikatsiya.Models;
@using System.Net
@inject IVersionProvider VersionProvider @inject IVersionProvider VersionProvider
<Row Padding="Padding.Is3.OnDesktop.Is0"> <Row Padding="Padding.Is3.OnDesktop.Is0">
@@ -69,8 +70,7 @@
//TODO: insert new event here if plug produces one //TODO: insert new event here if plug produces one
List<Event> events = new List<Event>() { new Event("Placeholder", "Placeholder", DateTime.Now), new Event("Placeholder", "Placeholder", DateTime.Now), new Event("Placeholder", "Placeholder", DateTime.Now) }; List<Event> events = new List<Event>() { new Event("Placeholder", "Placeholder", DateTime.Now), new Event("Placeholder", "Placeholder", DateTime.Now), new Event("Placeholder", "Placeholder", DateTime.Now) };
//TODO: insert new Device here if user adds one //TODO: insert new Device here if user adds one
List<Device> plugs = new List<Device>() { new Device("Placeholder", "Plug", System.Net.IPAddress.Broadcast, 60, "Oldin", "4AHINF"), new Device("Placeholder", "Plug", System.Net.IPAddress.Broadcast, 60, "Oldin", "4AHINF"), new Device("Placeholder", "Plug", System.Net.IPAddress.Broadcast, 60, "Oldin", "4AHINF") }; List<Device> plugs = new List<Device>() { new Device("ffff:ffff:ffff:ffff", "Huso", IPAddress.Broadcast, new User("Joe", "qiowruioewjfopa", Role.Admin), "Raum"), new Device("ffff:ffff:ffff:ffff", "Huso", IPAddress.Broadcast, new User("Joe", "qiowruioewjfopa", Role.Admin), "Raum"), new Device("ffff:ffff:ffff:ffff", "Huso", IPAddress.Broadcast, new User("Joe", "qiowruioewjfopa", Role.Admin), "Raum"), };
//code for graph //code for graph
LineChart<double> lineChart; LineChart<double> lineChart;
@@ -4,6 +4,7 @@ using Blazorise.Icons.Material;
using Blazorise.Material; using Blazorise.Material;
using Elektrifikatsiya.Database; using Elektrifikatsiya.Database;
using Elektrifikatsiya.Models;
using Elektrifikatsiya.Services; using Elektrifikatsiya.Services;
using Elektrifikatsiya.Services.Implementations; using Elektrifikatsiya.Services.Implementations;
@@ -20,6 +21,7 @@ builder.Services.AddTransient<IDeviceManagmentService, DeviceManagmentService>()
builder.Services.AddDbContext<UserDatabaseContext>(options => options.UseSqlite("Data Source=./UserDatabase.sqlite")); builder.Services.AddDbContext<UserDatabaseContext>(options => options.UseSqlite("Data Source=./UserDatabase.sqlite"));
builder.Services.AddDbContext<DeviceManagmentDatabaseContext>((options) => options.UseSqlite("Data Source=./DeviceManagement.sqlite")); builder.Services.AddDbContext<DeviceManagmentDatabaseContext>((options) => options.UseSqlite("Data Source=./DeviceManagement.sqlite"));
builder.Services.AddBootstrapProviders(); builder.Services.AddBootstrapProviders();
builder.Services.AddHttpClient<IDeviceManagmentService, DeviceManagmentService>();
builder.Services.AddBlazorise(options => builder.Services.AddBlazorise(options =>
{ {
options.Immediate = true; options.Immediate = true;
@@ -12,9 +12,10 @@ public class DeviceManagmentService : IDeviceManagmentService
{ {
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext; private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
private readonly IDeviceStatusService deviceStatusService; private readonly IDeviceStatusService deviceStatusService;
private readonly IServiceScopeFactory serviceScopeFactory;
private readonly HttpClient httpClient; private readonly HttpClient httpClient;
public DeviceManagmentService(DeviceManagmentDatabaseContext deviceManagmentDatabaseContext, IDeviceStatusService deviceStatusService, HttpClient httpClient) public DeviceManagmentService(IServiceScopeFactory serviceScopeFactory, IDeviceStatusService deviceStatusService, HttpClient httpClient)
{ {
this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext; this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext;
this.deviceStatusService = deviceStatusService; this.deviceStatusService = deviceStatusService;
@@ -78,6 +79,9 @@ public class DeviceManagmentService : IDeviceManagmentService
public async Task<Result<Device>> Register(IPAddress ip, User user, string? name = null, string room = "default") public async Task<Result<Device>> Register(IPAddress ip, User user, string? name = null, string room = "default")
{ {
using IServiceScope scope = serviceScopeFactory.CreateScope();
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = scope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
ShellyResponse? shellyResponse = await httpClient.GetFromJsonAsync<ShellyResponse>($"{ip}/shelly"); ShellyResponse? shellyResponse = await httpClient.GetFromJsonAsync<ShellyResponse>($"{ip}/shelly");
if (shellyResponse is null || shellyResponse.Type != "SHPLG-S") if (shellyResponse is null || shellyResponse.Type != "SHPLG-S")
@@ -104,6 +108,9 @@ public class DeviceManagmentService : IDeviceManagmentService
public async Task<Result> Unregister(string macAdress) public async Task<Result> Unregister(string macAdress)
{ {
using IServiceScope scope = serviceScopeFactory.CreateScope();
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = scope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
Result<Device> getDeviceResult = GetDevice(macAdress); Result<Device> getDeviceResult = GetDevice(macAdress);
if (getDeviceResult.IsFailed) if (getDeviceResult.IsFailed)
@@ -10,18 +10,21 @@ public class UpdateService : IHostedService, IDisposable
{ {
private readonly ILogger<UpdateService> logger; private readonly ILogger<UpdateService> logger;
private readonly IDeviceStatusService deviceStatusService; private readonly IDeviceStatusService deviceStatusService;
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext; private readonly IServiceScopeFactory serviceScopeFactory;
private Timer? timer = null; private Timer? timer = null;
public UpdateService(ILogger<UpdateService> logger, IDeviceStatusService deviceStatusService, DeviceManagmentDatabaseContext deviceManagmentDatabaseContext) public UpdateService(ILogger<UpdateService> logger, IDeviceStatusService deviceStatusService, IServiceScopeFactory serviceScopeFactory)
{ {
this.logger = logger; this.logger = logger;
this.deviceStatusService = deviceStatusService; this.deviceStatusService = deviceStatusService;
this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext; this.serviceScopeFactory = serviceScopeFactory;
} }
public async Task StartAsync(CancellationToken cancellationToken) public async Task StartAsync(CancellationToken cancellationToken)
{ {
IServiceScope serviceScope = serviceScopeFactory.CreateScope();
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = serviceScope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
logger.LogInformation("Starting update service..."); logger.LogInformation("Starting update service...");
foreach (Device device in await deviceManagmentDatabaseContext.Devices.AsNoTracking().ToListAsync(cancellationToken)) foreach (Device device in await deviceManagmentDatabaseContext.Devices.AsNoTracking().ToListAsync(cancellationToken))