mirror of
https://github.com/Stefan-5422/-T5-Elektrifikatsiya.git
synced 2026-09-04 00:45:58 +02:00
Fix: Broken stuff
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
+90
-83
@@ -10,116 +10,123 @@ namespace Elektrifikatsiya.Services.Implementations;
|
|||||||
|
|
||||||
public class DeviceManagmentService : IDeviceManagmentService
|
public class DeviceManagmentService : IDeviceManagmentService
|
||||||
{
|
{
|
||||||
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
|
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
|
||||||
private readonly IDeviceStatusService deviceStatusService;
|
private readonly IDeviceStatusService deviceStatusService;
|
||||||
private readonly HttpClient httpClient;
|
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.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext;
|
||||||
this.deviceStatusService = deviceStatusService;
|
this.deviceStatusService = deviceStatusService;
|
||||||
this.httpClient = httpClient;
|
this.httpClient = httpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<Device> GetDevice(string macAdress)
|
public Result<Device> GetDevice(string macAdress)
|
||||||
{
|
{
|
||||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||||
|
|
||||||
if (getDevicesResult.IsFailed)
|
if (getDevicesResult.IsFailed)
|
||||||
{
|
{
|
||||||
return getDevicesResult.ToResult();
|
return getDevicesResult.ToResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
Device? device = getDevicesResult.Value.FirstOrDefault(d => d.MacAddress == macAdress);
|
Device? device = getDevicesResult.Value.FirstOrDefault(d => d.MacAddress == macAdress);
|
||||||
|
|
||||||
if (device is null)
|
if (device is null)
|
||||||
{
|
{
|
||||||
return Result.Fail("Device does not exist!");
|
return Result.Fail("Device does not exist!");
|
||||||
}
|
}
|
||||||
|
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<List<Device>> GetDevices()
|
public Result<List<Device>> GetDevices()
|
||||||
{
|
{
|
||||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||||
|
|
||||||
if (getDevicesResult.IsFailed)
|
if (getDevicesResult.IsFailed)
|
||||||
{
|
{
|
||||||
return getDevicesResult.ToResult();
|
return getDevicesResult.ToResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
return getDevicesResult.Value.ToList();
|
return getDevicesResult.Value.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<List<Device>> GetDevicesInRoom(string room)
|
public Result<List<Device>> GetDevicesInRoom(string room)
|
||||||
{
|
{
|
||||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||||
|
|
||||||
if (getDevicesResult.IsFailed)
|
if (getDevicesResult.IsFailed)
|
||||||
{
|
{
|
||||||
return getDevicesResult.ToResult();
|
return getDevicesResult.ToResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
return getDevicesResult.Value.Where(d => d.Room == room).ToList();
|
return getDevicesResult.Value.Where(d => d.Room == room).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<List<Device>> GetDevicesOfUser(int userId)
|
public Result<List<Device>> GetDevicesOfUser(int userId)
|
||||||
{
|
{
|
||||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||||
|
|
||||||
if (getDevicesResult.IsFailed)
|
if (getDevicesResult.IsFailed)
|
||||||
{
|
{
|
||||||
return getDevicesResult.ToResult();
|
return getDevicesResult.ToResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
return getDevicesResult.Value.Where(d => d.User.Id == userId).ToList();
|
return getDevicesResult.Value.Where(d => d.User.Id == userId).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
{
|
{
|
||||||
ShellyResponse? shellyResponse = await httpClient.GetFromJsonAsync<ShellyResponse>($"{ip}/shelly");
|
using IServiceScope scope = serviceScopeFactory.CreateScope();
|
||||||
|
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = scope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
|
||||||
|
|
||||||
if(shellyResponse is null || shellyResponse.Type != "SHPLG-S")
|
ShellyResponse? shellyResponse = await httpClient.GetFromJsonAsync<ShellyResponse>($"{ip}/shelly");
|
||||||
{
|
|
||||||
return Result.Fail("Device is not a \"SHPLG-S\" or not reachable!");
|
|
||||||
}
|
|
||||||
|
|
||||||
string mac = shellyResponse.Mac;
|
if (shellyResponse is null || shellyResponse.Type != "SHPLG-S")
|
||||||
|
{
|
||||||
|
return Result.Fail("Device is not a \"SHPLG-S\" or not reachable!");
|
||||||
|
}
|
||||||
|
|
||||||
if(PhysicalAddress.TryParse(mac, out _))
|
string mac = shellyResponse.Mac;
|
||||||
{
|
|
||||||
return Result.Fail("Invalid mac address!");
|
|
||||||
}
|
|
||||||
|
|
||||||
Device device = new Device(mac, name ?? mac, ip, user, room);
|
if (PhysicalAddress.TryParse(mac, out _))
|
||||||
|
{
|
||||||
|
return Result.Fail("Invalid mac address!");
|
||||||
|
}
|
||||||
|
|
||||||
deviceManagmentDatabaseContext.Add(device);
|
Device device = new Device(mac, name ?? mac, ip, user, room);
|
||||||
Result saveDatabaseChangesResult = await Result.Try(async Task () => await deviceManagmentDatabaseContext.SaveChangesAsync());
|
|
||||||
|
|
||||||
Result trackDeviceResult = deviceStatusService.TrackDevice(device);
|
deviceManagmentDatabaseContext.Add(device);
|
||||||
|
Result saveDatabaseChangesResult = await Result.Try(async Task () => await deviceManagmentDatabaseContext.SaveChangesAsync());
|
||||||
|
|
||||||
return Result.Merge(saveDatabaseChangesResult, trackDeviceResult).ToResult(device);
|
Result trackDeviceResult = deviceStatusService.TrackDevice(device);
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Result> Unregister(string macAdress)
|
return Result.Merge(saveDatabaseChangesResult, trackDeviceResult).ToResult(device);
|
||||||
{
|
}
|
||||||
Result<Device> getDeviceResult = GetDevice(macAdress);
|
|
||||||
|
|
||||||
if (getDeviceResult.IsFailed)
|
public async Task<Result> Unregister(string macAdress)
|
||||||
{
|
{
|
||||||
return getDeviceResult.ToResult();
|
using IServiceScope scope = serviceScopeFactory.CreateScope();
|
||||||
}
|
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = scope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
|
||||||
|
|
||||||
Result untrackDeviceResult = deviceStatusService.UntrackDevice(macAdress);
|
Result<Device> getDeviceResult = GetDevice(macAdress);
|
||||||
|
|
||||||
if (untrackDeviceResult.IsFailed)
|
if (getDeviceResult.IsFailed)
|
||||||
{
|
{
|
||||||
return untrackDeviceResult;
|
return getDeviceResult.ToResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceManagmentDatabaseContext.Remove(getDeviceResult.Value);
|
Result untrackDeviceResult = deviceStatusService.UntrackDevice(macAdress);
|
||||||
|
|
||||||
return await Result.Try(async Task () => await deviceManagmentDatabaseContext.SaveChangesAsync());
|
if (untrackDeviceResult.IsFailed)
|
||||||
}
|
{
|
||||||
|
return untrackDeviceResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
deviceManagmentDatabaseContext.Remove(getDeviceResult.Value);
|
||||||
|
|
||||||
|
return await Result.Try(async Task () => await deviceManagmentDatabaseContext.SaveChangesAsync());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,19 +10,22 @@ 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)
|
||||||
{
|
{
|
||||||
logger.LogInformation("Starting update service...");
|
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))
|
foreach (Device device in await deviceManagmentDatabaseContext.Devices.AsNoTracking().ToListAsync(cancellationToken))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user