Skip to content

Commit

Permalink
Add logging on the server for wake commands sent.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Jun 22, 2023
1 parent 7d7fa9f commit f5f11ca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Server/Components/Devices/DeviceCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private async Task WakeDevice()
if (result.IsSuccess)
{
ToastService.ShowToast2(
$"Wake command sent to {result.Value} peer devices.",
$"Wake command sent to peer devices.",
ToastType.Success);
}
else
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Devices/DevicesFrame.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ private async Task WakeDevices()
if (result.IsSuccess)
{
ToastService.ShowToast2(
$"Wake commands sent to {result.Value} peer devices.",
$"Wake commands sent to peer devices.",
ToastType.Success);
}
else
Expand Down
36 changes: 18 additions & 18 deletions Server/Hubs/CircuitConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,14 @@ public interface ICircuitConnection
/// Peer devices are those in the same group or the same public IP.
/// </summary>
/// <param name="device"></param>
/// <returns>The number of peer devices that broadcasted the WOL packet.</returns>
Task<Result<int>> WakeDevice(Device device);
Task<Result> WakeDevice(Device device);

/// <summary>
/// Sends a Wake-On-LAN request for the specified device to its peer devices.
/// Peer devices are those in the same group or the same public IP.
/// </summary>
/// <param name="devices"></param>
/// <returns>The number of peer devices that broadcasted the WOL packet.</returns>
Task<Result<int>> WakeDevices(Device[] devices);
Task<Result> WakeDevices(Device[] devices);
}

public class CircuitConnection : CircuitHandler, ICircuitConnection
Expand Down Expand Up @@ -447,13 +445,13 @@ public Task UploadFiles(List<string> fileIDs, string transferID, string[] device
return Task.CompletedTask;
}

public async Task<Result<int>> WakeDevice(Device device)
public async Task<Result> WakeDevice(Device device)
{
try
{
if (!_dataService.DoesUserHaveAccessToDevice(device.ID, User.Id))
{
return Result.Fail<int>("Unauthorized.") ;
return Result.Fail("Unauthorized.") ;
}

var availableDevices = _serviceSessionCache
Expand All @@ -465,16 +463,16 @@ public async Task<Result<int>> WakeDevice(Device device)

await SendWakeCommand(device, availableDevices);

return Result.Ok(availableDevices.Length);
return Result.Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error waking device {deviceId}.", device.ID);
return Result.Fail<int>(ex);
return Result.Fail(ex);
}
}

public async Task<Result<int>> WakeDevices(Device[] devices)
public async Task<Result> WakeDevices(Device[] devices)
{
try
{
Expand All @@ -498,8 +496,6 @@ public async Task<Result<int>> WakeDevices(Device[] devices)
{
var group = devicesByGroupId.GetOrAdd(device.DeviceGroupID, key => new());
group.Add(device);
// We only need it added to one group.
continue;
}

if (!string.IsNullOrWhiteSpace(device.PublicIP))
Expand All @@ -509,32 +505,27 @@ public async Task<Result<int>> WakeDevices(Device[] devices)
}
}


var peerCount = 0;

foreach (var deviceToWake in filteredDevices)
{
if (!string.IsNullOrWhiteSpace(deviceToWake.DeviceGroupID) &&
devicesByGroupId.TryGetValue(deviceToWake.DeviceGroupID, out var groupList))
{
await SendWakeCommand(deviceToWake, groupList);
peerCount += groupList.Count;
}

if (!string.IsNullOrWhiteSpace(deviceToWake.PublicIP) &&
devicesByPublicIp.TryGetValue(deviceToWake.PublicIP, out var ipList))
{
await SendWakeCommand(deviceToWake, ipList);
peerCount += ipList.Count;
}

}
return Result.Ok(peerCount);
return Result.Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while waking devices.");
return Result.Fail<int>(ex);
return Result.Fail(ex);
}
}

Expand Down Expand Up @@ -604,6 +595,15 @@ private async Task SendWakeCommand(Device deviceToWake, IEnumerable<Device> peer
{
if (_serviceSessionCache.TryGetConnectionId(peerDevice.ID, out var connectionId))
{
_logger.LogInformation(
"Sending wake command for device {deviceName} ({deviceId}) to " +
"peer device {peerDeviceName} ({peerDeviceId}). " +
"Sender: {username}.",
deviceToWake.DeviceName,
deviceToWake.ID,
peerDevice.DeviceName,
peerDevice.ID,
User.UserName);
await _agentHubContext.Clients.Client(connectionId).SendAsync("WakeDevice", mac);
}
}
Expand Down

0 comments on commit f5f11ca

Please sign in to comment.