Skip to content

Commit

Permalink
Fix device filtering. Remove MacAddresses from sortable properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 31, 2023
1 parent a379a67 commit 469c91a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Server/Components/Devices/DevicesFrame.razor
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</div>
</div>
<div id="deviceListDiv" class="p-2 mb-5">
@foreach (var device in _devicesForPage)
@foreach (var device in DisplayedDevices)
{
<CascadingValue Value="this">
<DeviceCard @key="device.ID" Device="device" />
Expand Down
111 changes: 53 additions & 58 deletions Server/Components/Devices/DevicesFrame.razor.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<DeviceGroup> _deviceGroups = new();
private readonly List<Device> _devicesForPage = new();
private readonly SemaphoreSlim _devicesLock = new(1,1);
private readonly List<Device> _filteredDevices = new();
private readonly List<PropertyInfo> _sortableProperties = new();
Expand All @@ -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<DevicesFrame> 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();
Expand Down Expand Up @@ -104,42 +103,6 @@ await Register<ScriptResultMessage, string>(
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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -205,7 +168,7 @@ private async Task FilterDevices()
if (_selectedGroupId == _deviceGroupAll ||
_selectedGroupId == device.DeviceGroupID ||
(
_selectedGroupId == _deviceGroupNone &&
_selectedGroupId == _deviceGroupNone &&
string.IsNullOrWhiteSpace(device.DeviceGroupID
)))
{
Expand Down Expand Up @@ -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<Device>();
}
finally
{
_devicesLock.Release();
}
}


private string GetDisplayName(PropertyInfo propInfo)
{
return propInfo.GetCustomAttribute<DisplayAttribute>()?.Name ?? propInfo.Name;
Expand All @@ -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();
Expand All @@ -284,8 +281,6 @@ private async Task LoadDevices()
{
_devicesLock.Release();
}

await FilterDevices();
}
private void PageDown()
{
Expand Down
1 change: 0 additions & 1 deletion Shared/Entities/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();

Expand Down

0 comments on commit 469c91a

Please sign in to comment.