Skip to content

Commit

Permalink
Don't update DevicesFrame on DeviceUpdated and DeviceWentOffline mess…
Browse files Browse the repository at this point in the history
…ages. Only update card. Add explicit Refresh button.
  • Loading branch information
bitbound committed May 17, 2021
1 parent 7d36b57 commit c4fab7f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 45 deletions.
21 changes: 21 additions & 0 deletions Server/Components/Devices/DeviceCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public partial class DeviceCard : AuthComponentBase, IDisposable
public void Dispose()
{
AppState.PropertyChanged -= AppState_PropertyChanged;
CircuitConnection.MessageReceived -= CircuitConnection_MessageReceived;
GC.SuppressFinalize(this);
}

Expand All @@ -72,6 +73,7 @@ protected override async Task OnInitializedAsync()
await base.OnInitializedAsync();
_theme = await AppState.GetEffectiveTheme();
AppState.PropertyChanged += AppState_PropertyChanged;
CircuitConnection.MessageReceived += CircuitConnection_MessageReceived;
}

private void AppState_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
Expand All @@ -84,6 +86,25 @@ private void AppState_PropertyChanged(object sender, System.ComponentModel.Prope
}
}

private void CircuitConnection_MessageReceived(object sender, CircuitEvent e)
{
switch (e.EventName)
{
case CircuitEventName.DeviceUpdate:
case CircuitEventName.DeviceWentOffline:
{
if (e.Params?.FirstOrDefault() is Device device &&
device.ID == Device?.ID)
{
Device = device;
InvokeAsync(StateHasChanged);
}
break;
}
default:
break;
}
}
private void ContextMenuOpening(MouseEventArgs args)
{
if (GetCardState() == DeviceCardState.Normal)
Expand Down
3 changes: 2 additions & 1 deletion Server/Components/Devices/DevicesFrame.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
<label class="custom-control-label" for="hideOfflineDevicesCheckbox">Hide offline devices</label>
</div>
<div class="mt-2">
<button class="btn btn-sm btn-secondary" @onclick="SelectAllCards">Select All</button>
<button class="btn btn-sm btn-secondary mr-2" @onclick="SelectAllCards">Select All</button>
<button class="btn btn-sm btn-primary" @onclick="HandleRefreshClicked">Refresh</button>
</div>
</div>

Expand Down
73 changes: 36 additions & 37 deletions Server/Components/Devices/DevicesFrame.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ namespace Remotely.Server.Components.Devices
[Authorize]
public partial class DevicesFrame : AuthComponentBase, IDisposable
{
private readonly object _devicesLock = new();
private readonly List<Device> _allDevices = new();
private readonly string _deviceGroupAll = Guid.NewGuid().ToString();
private readonly string _deviceGroupNone = Guid.NewGuid().ToString();
private readonly List<Device> _devicesForPage = new();
private readonly List<DeviceGroup> _deviceGroups = new();
private readonly List<Device> _devicesForPage = new();
private readonly object _devicesLock = new();
private readonly List<Device> _filteredDevices = new();
private readonly ConcurrentDictionary<string, RemoteControlTarget> _remoteControlTargetLookup = new();
private readonly List<PropertyInfo> _sortableProperties = new();
Expand Down Expand Up @@ -141,12 +141,6 @@ private void CircuitConnection_MessageReceived(object sender, CircuitEvent args)
{
switch (args.EventName)
{
case CircuitEventName.DeviceUpdate:
case CircuitEventName.DeviceWentOffline:
{
Refresh();
}
break;
case CircuitEventName.DisplayMessage:
{
var terminalMessage = (string)args.Params[0];
Expand Down Expand Up @@ -190,34 +184,6 @@ private void ClearSelectedCard()
AppState.DevicesFrameFocusedCardState = DeviceCardState.Normal;
}

private string GetDisplayName(PropertyInfo propInfo)
{
return propInfo.GetCustomAttribute<DisplayAttribute>()?.Name ?? propInfo.Name;
}

private string GetSortIcon()
{
return $"oi-sort-{_sortDirection.ToString().ToLower()}";
}

private void LoadDevices()
{
lock (_devicesLock)
{
_allDevices.Clear();

var devices = DataService.GetDevicesForUser(Username)
.OrderByDescending(x => x.IsOnline)
.ToList();

_allDevices.AddRange(devices);

HighestVersion = _allDevices.Max(x => Version.TryParse(x.AgentVersion, out var result) ? result : default);
}

FilterDevices();
}

private void FilterDevices()
{
lock (_devicesLock)
Expand Down Expand Up @@ -278,11 +244,44 @@ private void FilterDevices()
.Take(_devicesPerPage);

_devicesForPage.Clear();
_devicesForPage.AddRange(devicesForPage);
_devicesForPage.AddRange(appendDevices.Concat(devicesForPage));
}

}

private string GetDisplayName(PropertyInfo propInfo)
{
return propInfo.GetCustomAttribute<DisplayAttribute>()?.Name ?? propInfo.Name;
}

private string GetSortIcon()
{
return $"oi-sort-{_sortDirection.ToString().ToLower()}";
}

private void HandleRefreshClicked()
{
Refresh();
ToastService.ShowToast("Devices refreshed.");
}

private void LoadDevices()
{
lock (_devicesLock)
{
_allDevices.Clear();

var devices = DataService.GetDevicesForUser(Username)
.OrderByDescending(x => x.IsOnline)
.ToList();

_allDevices.AddRange(devices);

HighestVersion = _allDevices.Max(x => Version.TryParse(x.AgentVersion, out var result) ? result : default);
}

FilterDevices();
}
private void PageDown()
{
if (_currentPage > 1)
Expand Down
20 changes: 13 additions & 7 deletions Tests.LoadTester/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Remotely.Tests.LoadTester
internal class Program
{
private static readonly SemaphoreSlim _lock = new(10, 10);
private static readonly double _heartbeatMs = TimeSpan.FromMinutes(1).TotalMilliseconds;
private static int _agentCount;
private static string _organizationId;
private static string _serverurl;
Expand Down Expand Up @@ -87,14 +88,19 @@ private static async Task StartAgent(int i)
return;
}

var heartbeatTimer = new System.Timers.Timer(TimeSpan.FromMinutes(5).TotalMilliseconds);
heartbeatTimer.Elapsed += async (sender, args) =>

_ = Task.Run(async () =>
{
var currentInfo = await _deviceInfo.CreateDevice(device.ID, _organizationId);
currentInfo.DeviceName = device.DeviceName;
await hubConnection.SendAsync("DeviceHeartbeat", currentInfo);
};
heartbeatTimer.Start();
await Task.Delay(new Random().Next(1, (int)_heartbeatMs));
var heartbeatTimer = new System.Timers.Timer(_heartbeatMs);
heartbeatTimer.Elapsed += async (sender, args) =>
{
var currentInfo = await _deviceInfo.CreateDevice(device.ID, _organizationId);
currentInfo.DeviceName = device.DeviceName;
await hubConnection.SendAsync("DeviceHeartbeat", currentInfo);
};
heartbeatTimer.Start();
});
}
catch (Exception ex)
{
Expand Down

0 comments on commit c4fab7f

Please sign in to comment.