network-resurrector/NetworkResurrector.Application/CommandHandlers/WakeMachineHandler.cs

39 lines
1.4 KiB
C#
Raw Normal View History

2020-11-25 17:01:14 +02:00
using MediatR;
using Microsoft.Extensions.Logging;
using NetworkResurrector.Abstractions;
using NetworkResurrector.Application.Commands;
using NetworkResurrector.Application.Events;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace NetworkResurrector.Application.CommandHandlers
{
public class WakeMachineHandler : IRequestHandler<WakeMachine, MachineWaked>
{
private readonly ILogger<WakeMachineHandler> _logger;
private readonly IWakeOnLanService _wakeOnLanService;
public WakeMachineHandler(ILogger<WakeMachineHandler> logger, IWakeOnLanService wakeOnLanService)
{
_logger = logger;
_wakeOnLanService = wakeOnLanService;
}
public async Task<MachineWaked> Handle(WakeMachine command, CancellationToken cancellationToken)
{
try
{
var (success, status) = await _wakeOnLanService.Wake(command.MacAddress);
return new MachineWaked(success, status);
}
catch (Exception ex)
{
var correlationIdMsg = $"CorrelationId: {command.Metadata.CorrelationId}";
_logger.LogError(ex, $"An unexpected error has occurred. {correlationIdMsg}");
return new MachineWaked(false, $"{ex.Message} {correlationIdMsg}");
}
}
}
}