ping machine from API by machineId

master
Tudor Stanciu 2022-01-13 12:21:32 +02:00
parent 24ef29449e
commit f997efbed1
4 changed files with 43 additions and 10 deletions

View File

@ -0,0 +1,38 @@
using MediatR;
using Microsoft.Extensions.Logging;
using NetworkResurrector.Api.Domain.Repositories;
using NetworkResurrector.Api.PublishedLanguage.Commands;
using NetworkResurrector.Api.PublishedLanguage.Events;
using NetworkResurrector.Server.Wrapper.Services;
using System.Threading;
using System.Threading.Tasks;
namespace NetworkResurrector.Api.Application.CommandHandlers
{
public class PingMachineHandler : IRequestHandler<PingMachine, MachinePinged>
{
private readonly ILogger<PingMachineHandler> _logger;
private readonly IResurrectorService _resurrectorService;
private readonly INetworkRepository _repository;
public PingMachineHandler(ILogger<PingMachineHandler> logger, IResurrectorService resurrectorService, INetworkRepository repository)
{
_logger=logger;
_resurrectorService=resurrectorService;
_repository=repository;
}
public async Task<MachinePinged> Handle(PingMachine command, CancellationToken cancellationToken)
{
_logger.LogDebug($"Start pinging machine {command.MachineId}");
var machine = await _repository.GetMachine(command.MachineId);
//log activity
var pingResult = await _resurrectorService.Ping(machine.IPv4Address ?? machine.MachineName);
var result = new MachinePinged(pingResult.Success, pingResult.Status);
_logger.LogDebug($"Machine {command.MachineId} pinging finished. Success: {result.Success}; Status: {result.Status}");
return result;
}
}
}

View File

@ -24,11 +24,14 @@ namespace NetworkResurrector.Api.Application.CommandHandlers
public async Task<MachineWaked> Handle(WakeMachine command, CancellationToken cancellationToken) public async Task<MachineWaked> Handle(WakeMachine command, CancellationToken cancellationToken)
{ {
_logger.LogDebug($"Start waking up machine {command.MachineId}");
var machine = await _repository.GetMachine(command.MachineId); var machine = await _repository.GetMachine(command.MachineId);
//log activity //log activity
var wakeResult = await _resurrectorService.Wake(machine.MACAddress); var wakeResult = await _resurrectorService.Wake(machine.MACAddress);
var result = new MachineWaked(wakeResult.Success, wakeResult.Status); var result = new MachineWaked(wakeResult.Success, wakeResult.Status);
_logger.LogDebug($"Machine {command.MachineId} wake up finished. Success: {result.Success}; Status: {result.Status}");
return result; return result;
} }
} }

View File

@ -1,8 +1,4 @@
namespace NetworkResurrector.Api.PublishedLanguage.Events namespace NetworkResurrector.Api.PublishedLanguage.Events
{ {
public class MachinePinged public record MachinePinged(bool Success, string Status);
{
public bool Success { get; set; }
public string Status { get; set; }
}
} }

View File

@ -1,8 +1,4 @@
namespace NetworkResurrector.Api.PublishedLanguage.Events namespace NetworkResurrector.Api.PublishedLanguage.Events
{ {
public class MachineShutdown public record MachineShutdown(bool Success, string Status);
{
public bool Success { get; set; }
public string Status { get; set; }
}
} }