mirror of
https://github.com/Stefan-5422/-T5-Elektrifikatsiya.git
synced 2026-09-04 00:45:58 +02:00
Add device registration basics
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
|
||||||
namespace Elektrifikatsiya.Models;
|
namespace Elektrifikatsiya.Models;
|
||||||
@@ -6,24 +7,30 @@ namespace Elektrifikatsiya.Models;
|
|||||||
public class Device
|
public class Device
|
||||||
{
|
{
|
||||||
[Key]
|
[Key]
|
||||||
public string Key { get; private set; }
|
public string MacAddress { get; private set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public string Name { get; private set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public IPAddress Address { get; private set; }
|
public IPAddress IpAddress { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public User User { get; set; }
|
||||||
|
|
||||||
|
[NotMapped]
|
||||||
public int PowerUsage { get; set; }
|
public int PowerUsage { get; set; }
|
||||||
public string User { get; set; }
|
|
||||||
public string Room { get; set; }
|
public string Room { get; set; }
|
||||||
|
|
||||||
public Device(string key, string name, IPAddress address, int powerUsage, string user, string room)
|
[NotMapped]
|
||||||
|
public bool Available { get; set; }
|
||||||
|
|
||||||
|
public Device(string macAddress, string name, IPAddress address, User user, string room)
|
||||||
{
|
{
|
||||||
Key = key;
|
MacAddress = macAddress;
|
||||||
Name = name;
|
Name = name;
|
||||||
Address = address;
|
IpAddress = address;
|
||||||
PowerUsage = powerUsage;
|
|
||||||
User = user;
|
User = user;
|
||||||
Room = room;
|
Room = room;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace Elektrifikatsiya.Models;
|
||||||
|
|
||||||
|
public class DeviceStatusChagedEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
public string MacAddress { get; set; }
|
||||||
|
|
||||||
|
public DeviceStatusChagedEventArgs(string macAddress)
|
||||||
|
{
|
||||||
|
MacAddress = macAddress;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,10 +2,10 @@ using Blazorise;
|
|||||||
using Blazorise.Bootstrap;
|
using Blazorise.Bootstrap;
|
||||||
using Blazorise.Icons.Material;
|
using Blazorise.Icons.Material;
|
||||||
using Blazorise.Material;
|
using Blazorise.Material;
|
||||||
using Elektrifikatsiya.Database;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
using Elektrifikatsiya.Database;
|
using Elektrifikatsiya.Database;
|
||||||
|
using Elektrifikatsiya.Services;
|
||||||
|
using Elektrifikatsiya.Services.Implementations;
|
||||||
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
@@ -14,8 +14,16 @@ WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
|
|||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
builder.Services.AddServerSideBlazor();
|
builder.Services.AddServerSideBlazor();
|
||||||
|
builder.Services.AddHostedService<UpdateService>();
|
||||||
|
builder.Services.AddSingleton<IDeviceStatusService, DeviceStatusService>();
|
||||||
|
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.AddBlazorise(options =>
|
||||||
|
{
|
||||||
|
options.Immediate = true;
|
||||||
|
});
|
||||||
|
|
||||||
AddBlazorise(builder.Services);
|
AddBlazorise(builder.Services);
|
||||||
|
|
||||||
@@ -49,15 +57,7 @@ app.Run();
|
|||||||
|
|
||||||
void AddBlazorise(IServiceCollection services)
|
void AddBlazorise(IServiceCollection services)
|
||||||
{
|
{
|
||||||
_ = services
|
_ = services.AddBlazorise();
|
||||||
.AddBlazorise();
|
_ = services.AddMaterialProviders();
|
||||||
_ = services
|
_ = services.AddMaterialIcons();
|
||||||
.AddMaterialProviders()
|
|
||||||
.AddMaterialIcons();
|
|
||||||
}
|
}
|
||||||
builder.Services
|
|
||||||
.AddBlazorise(options =>
|
|
||||||
{
|
|
||||||
options.Immediate = true;
|
|
||||||
})
|
|
||||||
.AddBootstrapProviders();
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using Elektrifikatsiya.Models;
|
||||||
|
|
||||||
|
using FluentResults;
|
||||||
|
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace Elektrifikatsiya.Services;
|
||||||
|
|
||||||
|
public interface IDeviceManagmentService
|
||||||
|
{
|
||||||
|
public Task<Result<Device>> Register(IPAddress ip, User user, string? name = null, string room = "default");
|
||||||
|
|
||||||
|
public Task<Result> UnRegister(string macAdress);
|
||||||
|
|
||||||
|
public Result<Device> GetDevice(string macAdress);
|
||||||
|
|
||||||
|
public Result<List<Device>> GetDevices();
|
||||||
|
|
||||||
|
public Result<List<Device>> GetDevicesInRoom(string room);
|
||||||
|
|
||||||
|
public Result<List<Device>> GetDevicesOfUser(int userId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using Elektrifikatsiya.Models;
|
||||||
|
|
||||||
|
using FluentResults;
|
||||||
|
|
||||||
|
namespace Elektrifikatsiya.Services;
|
||||||
|
|
||||||
|
public interface IDeviceStatusService
|
||||||
|
{
|
||||||
|
public event EventHandler<DeviceStatusChagedEventArgs> OnDeviceStatusChanged;
|
||||||
|
|
||||||
|
public Result<List<Device>> GetDevices();
|
||||||
|
|
||||||
|
public Result UpdateDeviceStatus(Device device);
|
||||||
|
|
||||||
|
public Result TrackDevice(Device device);
|
||||||
|
}
|
||||||
+85
@@ -0,0 +1,85 @@
|
|||||||
|
using Elektrifikatsiya.Database;
|
||||||
|
using Elektrifikatsiya.Models;
|
||||||
|
|
||||||
|
using FluentResults;
|
||||||
|
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace Elektrifikatsiya.Services.Implementations;
|
||||||
|
|
||||||
|
public class DeviceManagmentService : IDeviceManagmentService
|
||||||
|
{
|
||||||
|
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
|
||||||
|
private readonly IDeviceStatusService deviceStatusService;
|
||||||
|
|
||||||
|
public DeviceManagmentService(DeviceManagmentDatabaseContext deviceManagmentDatabaseContext, IDeviceStatusService deviceStatusService)
|
||||||
|
{
|
||||||
|
this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext;
|
||||||
|
this.deviceStatusService = deviceStatusService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result<Device> GetDevice(string macAdress)
|
||||||
|
{
|
||||||
|
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||||
|
|
||||||
|
if (getDevicesResult.IsFailed)
|
||||||
|
{
|
||||||
|
return getDevicesResult.ToResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
Device? device = getDevicesResult.Value.FirstOrDefault(d => d.MacAddress == macAdress);
|
||||||
|
|
||||||
|
if (device is null)
|
||||||
|
{
|
||||||
|
return Result.Fail("Device does not exist!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result<List<Device>> GetDevices()
|
||||||
|
{
|
||||||
|
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||||
|
|
||||||
|
if (getDevicesResult.IsFailed)
|
||||||
|
{
|
||||||
|
return getDevicesResult.ToResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
return getDevicesResult.Value.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result<List<Device>> GetDevicesInRoom(string room)
|
||||||
|
{
|
||||||
|
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||||
|
|
||||||
|
if (getDevicesResult.IsFailed)
|
||||||
|
{
|
||||||
|
return getDevicesResult.ToResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
return getDevicesResult.Value.Where(d => d.Room == room).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result<List<Device>> GetDevicesOfUser(int userId)
|
||||||
|
{
|
||||||
|
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
|
||||||
|
|
||||||
|
if (getDevicesResult.IsFailed)
|
||||||
|
{
|
||||||
|
return getDevicesResult.ToResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
return getDevicesResult.Value.Where(d => d.User.Id == userId).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<Result<Device>> Register(IPAddress ip, User user, string? name = null, string room = "default")
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<Result> UnRegister(string macAdress)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
using Elektrifikatsiya.Models;
|
||||||
|
|
||||||
|
using FluentResults;
|
||||||
|
|
||||||
|
namespace Elektrifikatsiya.Services.Implementations;
|
||||||
|
|
||||||
|
public class DeviceStatusService : IDeviceStatusService
|
||||||
|
{
|
||||||
|
public event EventHandler<DeviceStatusChagedEventArgs>? OnDeviceStatusChanged;
|
||||||
|
|
||||||
|
private readonly Dictionary<string, Device> devices = new();
|
||||||
|
|
||||||
|
public Result<List<Device>> GetDevices()
|
||||||
|
{
|
||||||
|
return devices.Values.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result UpdateDeviceStatus(Device device)
|
||||||
|
{
|
||||||
|
Device? modDevice = devices.GetValueOrDefault(device.MacAddress);
|
||||||
|
|
||||||
|
if (modDevice is null)
|
||||||
|
{
|
||||||
|
return Result.Fail("Device not tracked!");
|
||||||
|
}
|
||||||
|
|
||||||
|
modDevice.PowerUsage = device.PowerUsage;
|
||||||
|
modDevice.Available = device.Available;
|
||||||
|
modDevice.IpAddress = device.IpAddress;
|
||||||
|
modDevice.Name = device.Name;
|
||||||
|
|
||||||
|
OnDeviceStatusChanged?.Invoke(this, new DeviceStatusChagedEventArgs(device.MacAddress));
|
||||||
|
|
||||||
|
return Result.Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result TrackDevice(Device device)
|
||||||
|
{
|
||||||
|
if (devices.ContainsKey(device.MacAddress))
|
||||||
|
{
|
||||||
|
return Result.Fail("Device already tracked!");
|
||||||
|
}
|
||||||
|
|
||||||
|
devices[device.MacAddress] = device;
|
||||||
|
|
||||||
|
return Result.Ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
using Elektrifikatsiya.Database;
|
||||||
|
using Elektrifikatsiya.Models;
|
||||||
|
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Elektrifikatsiya.Services.Implementations;
|
||||||
|
|
||||||
|
public class UpdateService : IHostedService, IDisposable
|
||||||
|
{
|
||||||
|
private readonly ILogger<UpdateService> logger;
|
||||||
|
private readonly IDeviceStatusService deviceStatusService;
|
||||||
|
private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext;
|
||||||
|
private Timer? timer = null;
|
||||||
|
|
||||||
|
public UpdateService(ILogger<UpdateService> logger, IDeviceStatusService deviceStatusService, DeviceManagmentDatabaseContext deviceManagmentDatabaseContext)
|
||||||
|
{
|
||||||
|
this.logger = logger;
|
||||||
|
this.deviceStatusService = deviceStatusService;
|
||||||
|
this.deviceManagmentDatabaseContext = deviceManagmentDatabaseContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
logger.LogInformation("Starting update service...");
|
||||||
|
|
||||||
|
foreach (Device device in await deviceManagmentDatabaseContext.Devices.AsNoTracking().ToListAsync(cancellationToken))
|
||||||
|
{
|
||||||
|
_ = deviceStatusService.TrackDevice(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
timer = new Timer(Update, null, TimeSpan.Zero, TimeSpan.FromSeconds(15));
|
||||||
|
|
||||||
|
logger.LogInformation("Update service started.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Update(object? state)
|
||||||
|
{
|
||||||
|
//TODO: Update all devices.
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
logger.LogInformation("Stopping update service.");
|
||||||
|
|
||||||
|
_ = timer?.Change(Timeout.Infinite, 0);
|
||||||
|
|
||||||
|
logger.LogInformation("Stopped update service.");
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Dispose(true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
timer?.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user