Merge branch 'Development' of https://github.com/Stefan-5422/-T5-Elektrifikatsiya into Development

This commit is contained in:
Friend2868
2023-06-13 23:41:34 +02:00
10 changed files with 271 additions and 266 deletions
@@ -1 +0,0 @@

@@ -1,4 +1,5 @@
<Bar Breakpoint="Breakpoint.Desktop" NavigationBreakpoint="Breakpoint.Tablet" ThemeContrast="ThemeContrast.Dark"
@using Elektrifikatsiya.Models;
<Bar Breakpoint="Breakpoint.Desktop" NavigationBreakpoint="Breakpoint.Tablet" ThemeContrast="ThemeContrast.Dark"
Mode="BarMode.VerticalInline" CollapseMode="BarCollapseMode.Small">
<BarToggler TextColor="TextColor.Primary" />
<BarBrand>
@@ -17,12 +18,16 @@
Dashboard
</BarLink>
</BarItem>
<Protected RequiredRole="Role.Admin">
<Authorized>
<BarItem>
<BarLink To="/admin" TextColor="TextColor.White">
<BarIcon Style="color:white" IconName="IconName.ShieldAlt" />
Admin
</BarLink>
</BarItem>
</Authorized>
</Protected>
<BarItem>
<BarLink To="/settings" TextColor="TextColor.White">
<BarIcon Style="color:white" IconName="IconName.Wrench" />
@@ -1,5 +1,6 @@
@page "/Admin"
@using Blazorise.Components;
@using Elektrifikatsiya.Components
@using Elektrifikatsiya.Models;
@using Elektrifikatsiya.Database;
@using System.Net
@@ -12,6 +12,7 @@
@inject IDeviceStatusService DeviceStatusService;
@inject IDeviceManagmentService DeviceManagmentService;
@inject IEventService EventService;
@inject IAuthenticationService AuthenticationService;
@inject IEnergyPriceService EnergyPriceService;
@inject MainDatabaseContext MainDatabaseContext;
@@ -27,9 +27,9 @@ public partial class Dashboard
}
}
protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
plugs = DeviceStatusService.GetDevices().ValueOrDefault ?? new List<Device>();
plugs = DeviceManagmentService.GetDevices((await AuthenticationService.GetUserAsync()).ValueOrDefault).ValueOrDefault ?? new List<Device>();
events = EventService.Events;
EventService.OnEventCalled += (__, e) =>
@@ -10,6 +10,7 @@
@inject IDeviceManagmentService DeviceManagmentService
@inject IMessageService MessageService
@inject IAuthenticationService AuthenticationService
@inject MainDatabaseContext MainDatabaseContext
<Div>
@@ -157,9 +158,9 @@
Device? SelectedDevice = null;
Device? DeviceCopy = null;
protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
Result<List<Device>> getDevicesResult = DeviceManagmentService.GetDevices();
Result<List<Device>> getDevicesResult = DeviceManagmentService.GetDevices((await AuthenticationService.GetUserAsync()).ValueOrDefault);
if (getDevicesResult.IsSuccess)
{
@@ -14,7 +14,7 @@ public interface IDeviceManagmentService
public Result<Device> GetDevice(string macAdress);
public Result<List<Device>> GetDevices();
public Result<List<Device>> GetDevices(User user);
public Result<List<Device>> GetDevicesInRoom(string room);
@@ -49,7 +49,7 @@ public class DeviceManagmentService : IDeviceManagmentService
return device;
}
public Result<List<Device>> GetDevices()
public Result<List<Device>> GetDevices(User user)
{
Result<List<Device>> getDevicesResult = deviceStatusService.GetDevices();
@@ -58,7 +58,7 @@ public class DeviceManagmentService : IDeviceManagmentService
return getDevicesResult.ToResult();
}
return getDevicesResult.Value.ToList();
return getDevicesResult.Value.Where(d => d.User.Id == user.Id || user.Role == Role.Admin).ToList();
}
public Result<List<Device>> GetDevicesInRoom(string room)
@@ -1,19 +1,18 @@
using Elektrifikatsiya.Models;
using FluentResults;
using System.Reflection;
namespace Elektrifikatsiya.Services.Implementations
{
namespace Elektrifikatsiya.Services.Implementations;
public class EventService : IEventService
{
private readonly ILogger<EventService> logger;
public event EventHandler<EventServiceEventArgs> OnEventCalled;
public event EventHandler<EventServiceEventArgs>? OnEventCalled;
public List<Event> Events { get; } = new();
public EventService(ILogger<EventService> logger, IDeviceStatusService deviceStatusService, IDeviceManagmentService deviceManagmentService)
public EventService(IDeviceStatusService deviceStatusService, IDeviceManagmentService deviceManagmentService)
{
this.logger = logger;
List<Device> deviceList = deviceManagmentService.GetDevices().ValueOrDefault.Select(x=>x.CopyDevice()).ToList();
List<Device> deviceList = deviceStatusService.GetDevices().ValueOrDefault.Select(x => x.CopyDevice()).ToList();
deviceStatusService.OnDeviceStatusChanged += (_, e) =>
{
@@ -26,10 +25,11 @@ namespace Elektrifikatsiya.Services.Implementations
{
PropertyInfo[] properties = typeof(Device).GetProperties();
foreach(PropertyInfo property in properties) {
var currentvalue = property.GetValue(deviceList[i], null);
var newvalue = property.GetValue(currentDevice, null);
if(newvalue is not null && !newvalue.ToString().Equals(currentvalue.ToString()))
foreach (PropertyInfo property in properties)
{
object? currentvalue = property.GetValue(deviceList[i], null);
object? newvalue = property.GetValue(currentDevice, null);
if (newvalue is not null && !newvalue?.ToString()?.Equals(currentvalue?.ToString()) == true)
{
string eventName = $"Plug {deviceList[i].Name} changed: {property.Name}";
string description = $"Changed {property.Name} of plug [{currentvalue}] to [{newvalue}]";
@@ -39,7 +39,6 @@ namespace Elektrifikatsiya.Services.Implementations
Events.Insert(0, newEvent);
OnEventCalled?.Invoke(this, new EventServiceEventArgs(newEvent));
}
}
deviceList[i].OverwiteDevice(currentDevice);
newDevice = false; break;
@@ -52,4 +51,3 @@ namespace Elektrifikatsiya.Services.Implementations
};
}
}
}
@@ -11,7 +11,7 @@ public class EmailSettings
public string SmtpServer { get; set; }
public int SmtpPort { get; set; }
public Dictionary<string, HandlebarsTemplate<object, object>> CompiledTemplates = new();
public Dictionary<string, HandlebarsTemplate<object, object>> CompiledTemplates { get; } = new();
public void CompileTemplates()
{