From 7be1fd6a69a2dfdfe91581436945e0838fbb95c5 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 17 Jan 2022 19:22:31 +0200 Subject: [PATCH] WakeMachineHandler fix --- .../CommandHandlers/PingMachineHandler.cs | 19 ++++++++++++++++--- .../CommandHandlers/WakeMachineHandler.cs | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs index 309d5e9..4607308 100644 --- a/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/PingMachineHandler.cs @@ -1,9 +1,11 @@ using MediatR; using Microsoft.Extensions.Logging; +using NetworkResurrector.Api.Domain.Constants; using NetworkResurrector.Api.Domain.Repositories; using NetworkResurrector.Api.PublishedLanguage.Commands; using NetworkResurrector.Api.PublishedLanguage.Events; using NetworkResurrector.Server.Wrapper.Services; +using System; using System.Threading; using System.Threading.Tasks; @@ -26,11 +28,22 @@ namespace NetworkResurrector.Api.Application.CommandHandlers { _logger.LogDebug($"Start pinging machine {command.MachineId}"); var machine = await _repository.GetMachine(command.MachineId); + var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.PING); //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}"); + + MachinePinged result; + switch (powerConfiguration.Performer.PerformerCode) + { + case PowerActionPerformers.NETWORK_RESURRECTOR_SERVER: + var pingResult = await _resurrectorService.Ping(machine.IPv4Address ?? machine.MachineName); + result = new MachinePinged(pingResult.Success, pingResult.Status); + _logger.LogDebug($"Machine {command.MachineId} pinging finished. Success: {result.Success}; Status: {result.Status}"); + break; + + default: + throw new Exception($"Power action performer {powerConfiguration.Performer.PerformerCode} is not implemented."); + } return result; } diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs index c5c4351..c783e2f 100644 --- a/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs @@ -1,9 +1,11 @@ using MediatR; using Microsoft.Extensions.Logging; +using NetworkResurrector.Api.Domain.Constants; using NetworkResurrector.Api.Domain.Repositories; using NetworkResurrector.Api.PublishedLanguage.Commands; using NetworkResurrector.Api.PublishedLanguage.Events; using NetworkResurrector.Server.Wrapper.Services; +using System; using System.Threading; using System.Threading.Tasks; @@ -26,11 +28,22 @@ namespace NetworkResurrector.Api.Application.CommandHandlers { _logger.LogDebug($"Start waking up machine {command.MachineId}"); var machine = await _repository.GetMachine(command.MachineId); + var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.WAKE); //log activity - var wakeResult = await _resurrectorService.Wake(machine.MACAddress); - var result = new MachineWaked(wakeResult.Success, wakeResult.Status); - _logger.LogDebug($"Machine {command.MachineId} wake up finished. Success: {result.Success}; Status: {result.Status}"); + + MachineWaked result; + switch (powerConfiguration.Performer.PerformerCode) + { + case PowerActionPerformers.NETWORK_RESURRECTOR_SERVER: + var wakeResult = await _resurrectorService.Wake(machine.MACAddress); + result = new MachineWaked(wakeResult.Success, wakeResult.Status); + _logger.LogDebug($"Machine {command.MachineId} wake up finished. Success: {result.Success}; Status: {result.Status}"); + break; + + default: + throw new Exception($"Power action performer {powerConfiguration.Performer.PerformerCode} is not implemented."); + } return result; }