diff --git a/Server/Components/Devices/DevicesFrame.razor b/Server/Components/Devices/DevicesFrame.razor index bc4c3cee6..67b7326dd 100644 --- a/Server/Components/Devices/DevicesFrame.razor +++ b/Server/Components/Devices/DevicesFrame.razor @@ -86,7 +86,7 @@
- @foreach (var device in _devicesForPage) + @foreach (var device in DisplayedDevices) { diff --git a/Server/Components/Devices/DevicesFrame.razor.cs b/Server/Components/Devices/DevicesFrame.razor.cs index 9b73f7fa5..cd0e0c51e 100644 --- a/Server/Components/Devices/DevicesFrame.razor.cs +++ b/Server/Components/Devices/DevicesFrame.razor.cs @@ -1,7 +1,9 @@ using Immense.SimpleMessenger; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components; +using Microsoft.Build.Framework; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using Remotely.Server.Enums; using Remotely.Server.Hubs; using Remotely.Server.Models.Messages; @@ -29,7 +31,6 @@ public partial class DevicesFrame : AuthComponentBase private readonly string _deviceGroupAll = Guid.NewGuid().ToString(); private readonly string _deviceGroupNone = Guid.NewGuid().ToString(); private readonly List _deviceGroups = new(); - private readonly List _devicesForPage = new(); private readonly SemaphoreSlim _devicesLock = new(1,1); private readonly List _filteredDevices = new(); private readonly List _sortableProperties = new(); @@ -44,27 +45,25 @@ public partial class DevicesFrame : AuthComponentBase [Inject] private ISelectedCardsStore CardStore { get; init; } = null!; - [Inject] - private ITerminalStore TerminalStore { get; init; } = null!; - [Inject] private ICircuitConnection CircuitConnection { get; init; } = null!; [Inject] private IDataService DataService { get; init; } = null!; + private Device[] DisplayedDevices => GetDisplayedDevices(); + + [Inject] + private ILogger Logger { get; init; } = null!; + + [Inject] + private ITerminalStore TerminalStore { get; init; } = null!; + [Inject] private IToastService ToastService { get; init; } = null!; private int TotalPages => (int)Math.Max(1, Math.Ceiling((decimal)_filteredDevices.Count / _devicesPerPage)); - private async Task HandleDisplayNotificationMessage(DisplayNotificationMessage message) - { - TerminalStore.AddTerminalLine(message.ConsoleText); - ToastService.ShowToast(message.ToastText, classString: message.ClassName); - await InvokeAsync(StateHasChanged); - } - public async Task Refresh() { await LoadDevices(); @@ -104,42 +103,6 @@ await Register( await LoadDevices(); } - private async Task HandleScriptResultMessage(ScriptResultMessage message) - { - await AddScriptResult(message.ScriptResult); - } - - private async Task HandleDeviceStateChangedMessage(DeviceStateChangedMessage message) - { - await _devicesLock.WaitAsync(); - - try - { - var device = message.Device; - - foreach (var collection in new[] { _allDevices, _devicesForPage }) - { - var index = collection.FindIndex(x => x.ID == device.ID); - if (index > -1) - { - collection[index] = device; - } - } - - Debouncer.Debounce(TimeSpan.FromSeconds(2), Refresh); - } - finally - { - _devicesLock.Release(); - } - } - - protected override async Task OnAfterRenderAsync(bool firstRender) - { - await base.OnAfterRenderAsync(firstRender); - await FilterDevices(); - } - private async Task AddScriptResult(ScriptResult result) { var deviceResult = await DataService.GetDevice(result.DeviceID); @@ -167,13 +130,13 @@ private async Task AddScriptResult(ScriptResult result) private async Task ClearSelectedCard() { await Messenger.Send( - new DeviceCardStateChangedMessage(string.Empty, DeviceCardState.Normal), + new DeviceCardStateChangedMessage(string.Empty, DeviceCardState.Normal), CircuitConnection.ConnectionId); } - private async Task FilterDevices() + private Device[] GetDisplayedDevices() { - await _devicesLock.WaitAsync(); + _devicesLock.Wait(); try { _filteredDevices.Clear(); @@ -205,7 +168,7 @@ private async Task FilterDevices() if (_selectedGroupId == _deviceGroupAll || _selectedGroupId == device.DeviceGroupID || ( - _selectedGroupId == _deviceGroupNone && + _selectedGroupId == _deviceGroupNone && string.IsNullOrWhiteSpace(device.DeviceGroupID ))) { @@ -238,17 +201,19 @@ private async Task FilterDevices() .Skip(skipCount) .Take(_devicesPerPage); - _devicesForPage.Clear(); - _devicesForPage.AddRange(appendDevices.Concat(devicesForPage)); - + return appendDevices.Concat(devicesForPage).ToArray(); + } + catch (Exception ex) + { + Logger.LogError(ex, "Error while filtering devices."); + ToastService.ShowToast2("Filter devices failed", ToastType.Error); + return Array.Empty(); } finally { _devicesLock.Release(); } } - - private string GetDisplayName(PropertyInfo propInfo) { return propInfo.GetCustomAttribute()?.Name ?? propInfo.Name; @@ -259,12 +224,44 @@ private string GetSortIcon() return $"oi-sort-{_sortDirection.ToString().ToLower()}"; } + private async Task HandleDeviceStateChangedMessage(DeviceStateChangedMessage message) + { + await _devicesLock.WaitAsync(); + + try + { + var device = message.Device; + + var index = _allDevices.FindIndex(x => x.ID == device.ID); + if (index > -1) + { + _allDevices[index] = device; + } + + Debouncer.Debounce(TimeSpan.FromSeconds(2), Refresh); + } + finally + { + _devicesLock.Release(); + } + } + + private async Task HandleDisplayNotificationMessage(DisplayNotificationMessage message) + { + TerminalStore.AddTerminalLine(message.ConsoleText); + ToastService.ShowToast(message.ToastText, classString: message.ClassName); + await InvokeAsync(StateHasChanged); + } private async Task HandleRefreshClicked() { await Refresh(); ToastService.ShowToast("Devices refreshed."); } + private async Task HandleScriptResultMessage(ScriptResultMessage message) + { + await AddScriptResult(message.ScriptResult); + } private async Task LoadDevices() { EnsureUserSet(); @@ -284,8 +281,6 @@ private async Task LoadDevices() { _devicesLock.Release(); } - - await FilterDevices(); } private void PageDown() { diff --git a/Shared/Entities/Device.cs b/Shared/Entities/Device.cs index e37e74c3d..b77533d42 100644 --- a/Shared/Entities/Device.cs +++ b/Shared/Entities/Device.cs @@ -50,7 +50,6 @@ public class Device [Display(Name = "Last Online")] public DateTimeOffset LastOnline { get; set; } - [Sortable] [Display(Name = "MAC Addresses")] public string[] MacAddresses { get; set; } = Array.Empty();