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;
|
||||
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;
|
||||
|
||||
+90
-83
@@ -10,116 +10,123 @@ namespace Elektrifikatsiya.Services.Implementations;
|
||||
|
||||
public class DeviceManagmentService : IDeviceManagmentService
|
||||
{
|
||||
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
|
||||
private readonly IDeviceStatusService deviceStatusService;
|
||||
private readonly HttpClient httpClient;
|
||||
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
|
||||
private readonly IDeviceStatusService deviceStatusService;
|
||||
private readonly IServiceScopeFactory serviceScopeFactory;
|
||||
private readonly HttpClient httpClient;
|
||||
|
||||
public DeviceManagmentService(DeviceManagmentDatabaseContext deviceManagmentDatabaseContext, IDeviceStatusService deviceStatusService, HttpClient httpClient)
|
||||
{
|
||||
this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext;
|
||||
this.deviceStatusService = deviceStatusService;
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
public DeviceManagmentService(IServiceScopeFactory serviceScopeFactory, IDeviceStatusService deviceStatusService, HttpClient httpClient)
|
||||
{
|
||||
this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext;
|
||||
this.deviceStatusService = deviceStatusService;
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public Result<Device> GetDevice(string macAdress)
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
public Result<Device> GetDevice(string macAdress)
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
|
||||
if (getDevicesResult.IsFailed)
|
||||
{
|
||||
return getDevicesResult.ToResult();
|
||||
}
|
||||
if (getDevicesResult.IsFailed)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return Result.Fail("Device does not exist!");
|
||||
}
|
||||
if (device is null)
|
||||
{
|
||||
return Result.Fail("Device does not exist!");
|
||||
}
|
||||
|
||||
return device;
|
||||
}
|
||||
return device;
|
||||
}
|
||||
|
||||
public Result<List<Device>> GetDevices()
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
public Result<List<Device>> GetDevices()
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
|
||||
if (getDevicesResult.IsFailed)
|
||||
{
|
||||
return getDevicesResult.ToResult();
|
||||
}
|
||||
if (getDevicesResult.IsFailed)
|
||||
{
|
||||
return getDevicesResult.ToResult();
|
||||
}
|
||||
|
||||
return getDevicesResult.Value.ToList();
|
||||
}
|
||||
return getDevicesResult.Value.ToList();
|
||||
}
|
||||
|
||||
public Result<List<Device>> GetDevicesInRoom(string room)
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
public Result<List<Device>> GetDevicesInRoom(string room)
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
|
||||
if (getDevicesResult.IsFailed)
|
||||
{
|
||||
return getDevicesResult.ToResult();
|
||||
}
|
||||
if (getDevicesResult.IsFailed)
|
||||
{
|
||||
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)
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
public Result<List<Device>> GetDevicesOfUser(int userId)
|
||||
{
|
||||
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||
|
||||
if (getDevicesResult.IsFailed)
|
||||
{
|
||||
return getDevicesResult.ToResult();
|
||||
}
|
||||
if (getDevicesResult.IsFailed)
|
||||
{
|
||||
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")
|
||||
{
|
||||
ShellyResponse? shellyResponse = await httpClient.GetFromJsonAsync<ShellyResponse>($"{ip}/shelly");
|
||||
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>();
|
||||
|
||||
if(shellyResponse is null || shellyResponse.Type != "SHPLG-S")
|
||||
{
|
||||
return Result.Fail("Device is not a \"SHPLG-S\" or not reachable!");
|
||||
}
|
||||
ShellyResponse? shellyResponse = await httpClient.GetFromJsonAsync<ShellyResponse>($"{ip}/shelly");
|
||||
|
||||
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 _))
|
||||
{
|
||||
return Result.Fail("Invalid mac address!");
|
||||
}
|
||||
string mac = shellyResponse.Mac;
|
||||
|
||||
Device device = new Device(mac, name ?? mac, ip, user, room);
|
||||
if (PhysicalAddress.TryParse(mac, out _))
|
||||
{
|
||||
return Result.Fail("Invalid mac address!");
|
||||
}
|
||||
|
||||
deviceManagmentDatabaseContext.Add(device);
|
||||
Result saveDatabaseChangesResult = await Result.Try(async Task () => await deviceManagmentDatabaseContext.SaveChangesAsync());
|
||||
Device device = new Device(mac, name ?? mac, ip, user, room);
|
||||
|
||||
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)
|
||||
{
|
||||
Result<Device> getDeviceResult = GetDevice(macAdress);
|
||||
return Result.Merge(saveDatabaseChangesResult, trackDeviceResult).ToResult(device);
|
||||
}
|
||||
|
||||
if (getDeviceResult.IsFailed)
|
||||
{
|
||||
return getDeviceResult.ToResult();
|
||||
}
|
||||
public async Task<Result> Unregister(string macAdress)
|
||||
{
|
||||
using IServiceScope scope = serviceScopeFactory.CreateScope();
|
||||
DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = scope.ServiceProvider.GetRequiredService<DeviceManagmentDatabaseContext>();
|
||||
|
||||
Result untrackDeviceResult = deviceStatusService.UntrackDevice(macAdress);
|
||||
Result<Device> getDeviceResult = GetDevice(macAdress);
|
||||
|
||||
if (untrackDeviceResult.IsFailed)
|
||||
{
|
||||
return untrackDeviceResult;
|
||||
}
|
||||
if (getDeviceResult.IsFailed)
|
||||
{
|
||||
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 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)
|
||||
{
|
||||
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))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user