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;
Room = room;
}
private Device(){}
}
@@ -1,6 +1,7 @@
@page "/addplug"
@using Blazorise.Components;
@using Elektrifikatsiya.Models;
@using System.Net
@inject IVersionProvider VersionProvider
<Row Height=Height.Is50 Padding="Padding.Is3.OnDesktop.Is0">
@@ -22,7 +23,7 @@
</Row>
<Row Width="Width.Is100">
<Column>
<Small>IP: @context.Item.Address</Small>
<Small>IP: @context.Item.IpAddress</Small>
</Column>
</Row>
</Column>
@@ -39,5 +40,5 @@
</Row>
@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 "/"
@using Blazorise.Components;
@using Elektrifikatsiya.Models;
@using System.Net
@inject IVersionProvider VersionProvider
<Row Padding="Padding.Is3.OnDesktop.Is0">
@@ -69,8 +70,7 @@
//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) };
//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
LineChart<double> lineChart;
@@ -4,6 +4,7 @@ using Blazorise.Icons.Material;
using Blazorise.Material;
using Elektrifikatsiya.Database;
using Elektrifikatsiya.Models;
using Elektrifikatsiya.Services;
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<DeviceManagmentDatabaseContext>((options) => options.UseSqlite("Data Source=./DeviceManagement.sqlite"));
builder.Services.AddBootstrapProviders();
builder.Services.AddHttpClient<IDeviceManagmentService, DeviceManagmentService>();
builder.Services.AddBlazorise(options =>
{
options.Immediate = true;
@@ -12,9 +12,10 @@ public class DeviceManagmentService : IDeviceManagmentService
{
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
private readonly IDeviceStatusService deviceStatusService;
private readonly IServiceScopeFactory serviceScopeFactory;
private readonly HttpClient httpClient;
public DeviceManagmentService(DeviceManagmentDatabaseContext deviceManagmentDatabaseContext, IDeviceStatusService deviceStatusService, HttpClient httpClient)
public DeviceManagmentService(IServiceScopeFactory serviceScopeFactory, IDeviceStatusService deviceStatusService, HttpClient httpClient)
{
this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext;
this.deviceStatusService = deviceStatusService;
@@ -78,16 +79,19 @@ public class DeviceManagmentService : IDeviceManagmentService
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");
if(shellyResponse is null || shellyResponse.Type != "SHPLG-S")
if (shellyResponse is null || shellyResponse.Type != "SHPLG-S")
{
return Result.Fail("Device is not a \"SHPLG-S\" or not reachable!");
}
string mac = shellyResponse.Mac;
if(PhysicalAddress.TryParse(mac, out _))
if (PhysicalAddress.TryParse(mac, out _))
{
return Result.Fail("Invalid mac address!");
}
@@ -104,6 +108,9 @@ public class DeviceManagmentService : IDeviceManagmentService
public async Task<Result> Unregister(string macAdress)
{
using IServiceScope scope = serviceScopeFactory.CreateScope();
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = scope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
Result<Device> getDeviceResult = GetDevice(macAdress);
if (getDeviceResult.IsFailed)
@@ -10,18 +10,21 @@ public class UpdateService : IHostedService, IDisposable
{
private readonly ILogger<UpdateService> logger;
private readonly IDeviceStatusService deviceStatusService;
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
private readonly IServiceScopeFactory serviceScopeFactory;
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.deviceStatusService = deviceStatusService;
this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext;
this.serviceScopeFactory = serviceScopeFactory;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
IServiceScope serviceScope = serviceScopeFactory.CreateScope();
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = serviceScope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
logger.LogInformation("Starting update service...");
foreach (Device device in await deviceManagmentDatabaseContext.Devices.AsNoTracking().ToListAsync(cancellationToken))