using MediatR; using Microsoft.Extensions.Logging; using NetworkResurrector.Server.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 { private readonly ILogger _logger; private readonly IWakeOnLanService _wakeOnLanService; public WakeMachineHandler(ILogger logger, IWakeOnLanService wakeOnLanService) { _logger = logger; _wakeOnLanService = wakeOnLanService; } public async Task 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}"); } } } }