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