From 8e665d2d73df2ae0edc271bef02b121998e4c00e Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 10 Jan 2022 11:41:30 +0200 Subject: [PATCH] new routes --- .../CommandHandlers/RestartHandler.cs | 52 +++++++++++++++++++ .../CommandHandlers/SleepHandler.cs | 52 +++++++++++++++++++ .../Commands/Restart.cs | 12 +++++ .../Commands/Sleep.cs | 12 +++++ .../Events/RestartResult.cs | 10 ++++ .../Events/SleepResult.cs | 10 ++++ .../Controllers/HostController.cs | 18 ++++++- 7 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 src/agent/NetworkResurrector.Agent.Application/CommandHandlers/RestartHandler.cs create mode 100644 src/agent/NetworkResurrector.Agent.Application/CommandHandlers/SleepHandler.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Restart.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Sleep.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/RestartResult.cs create mode 100644 src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/SleepResult.cs diff --git a/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/RestartHandler.cs b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/RestartHandler.cs new file mode 100644 index 0000000..f664fe5 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/RestartHandler.cs @@ -0,0 +1,52 @@ +using AutoMapper; +using MediatR; +using Microsoft.Extensions.Logging; +using NetworkResurrector.Agent.Application.Services.Abstractions; +using NetworkResurrector.Agent.Domain.Models; +using NetworkResurrector.Agent.PublishedLanguage.Commands; +using NetworkResurrector.Agent.PublishedLanguage.Events; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Agent.Application.CommandHandlers +{ + public class RestartHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly IPowerService _powerService; + private readonly IValidationService _validationService; + private readonly IMapper _mapper; + + public RestartHandler(ILogger logger, IPowerService powerService, IValidationService validationService, IMapper mapper) + { + _logger=logger; + _powerService=powerService; + _validationService=validationService; + _mapper=mapper; + } + + public async Task Handle(Restart command, CancellationToken cancellationToken) + { + return await Task.Run(() => Handle(command)); + } + + private RestartResult Handle(Restart command) + { + _validationService.ValidateRestrictions(command.Owner); + + _logger.LogDebug($"Start restarting the system."); + + var stopWatch = new Stopwatch(); + stopWatch.Start(); + + var options = _mapper.Map(command.Options); + _powerService.Restart(options); + + stopWatch.Stop(); + _logger.LogDebug($"System restart finished - {stopWatch.ElapsedMilliseconds:N0} ms"); + + return new RestartResult() { Success = true }; + } + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/SleepHandler.cs b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/SleepHandler.cs new file mode 100644 index 0000000..92eead7 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/SleepHandler.cs @@ -0,0 +1,52 @@ +using AutoMapper; +using MediatR; +using Microsoft.Extensions.Logging; +using NetworkResurrector.Agent.Application.Services.Abstractions; +using NetworkResurrector.Agent.Domain.Models; +using NetworkResurrector.Agent.PublishedLanguage.Commands; +using NetworkResurrector.Agent.PublishedLanguage.Events; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Agent.Application.CommandHandlers +{ + public class SleepHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly IPowerService _powerService; + private readonly IValidationService _validationService; + private readonly IMapper _mapper; + + public SleepHandler(ILogger logger, IPowerService powerService, IValidationService validationService, IMapper mapper) + { + _logger=logger; + _powerService=powerService; + _validationService=validationService; + _mapper=mapper; + } + + public async Task Handle(Sleep command, CancellationToken cancellationToken) + { + return await Task.Run(() => Handle(command)); + } + + private SleepResult Handle(Sleep command) + { + _validationService.ValidateRestrictions(command.Owner); + + _logger.LogDebug($"Start sleeping the system."); + + var stopWatch = new Stopwatch(); + stopWatch.Start(); + + var options = _mapper.Map(command.Options); + _powerService.Sleep(options); + + stopWatch.Stop(); + _logger.LogDebug($"System sleeping finished - {stopWatch.ElapsedMilliseconds:N0} ms"); + + return new SleepResult() { Success = true }; + } + } +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Restart.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Restart.cs new file mode 100644 index 0000000..3775264 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Restart.cs @@ -0,0 +1,12 @@ +using NDB.Application.DataContracts; +using NetworkResurrector.Agent.PublishedLanguage.Dto; +using NetworkResurrector.Agent.PublishedLanguage.Events; + +namespace NetworkResurrector.Agent.PublishedLanguage.Commands +{ + public class Restart : Command + { + public ActionOwner Owner { get; set; } + public ActionOptions Options { get; set; } + } +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Sleep.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Sleep.cs new file mode 100644 index 0000000..79090cf --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Sleep.cs @@ -0,0 +1,12 @@ +using NDB.Application.DataContracts; +using NetworkResurrector.Agent.PublishedLanguage.Dto; +using NetworkResurrector.Agent.PublishedLanguage.Events; + +namespace NetworkResurrector.Agent.PublishedLanguage.Commands +{ + public class Sleep : Command + { + public ActionOwner Owner { get; set; } + public ActionOptions Options { get; set; } + } +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/RestartResult.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/RestartResult.cs new file mode 100644 index 0000000..528583d --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/RestartResult.cs @@ -0,0 +1,10 @@ +using NetworkResurrector.Agent.PublishedLanguage.Dto; + +namespace NetworkResurrector.Agent.PublishedLanguage.Events +{ + public class RestartResult + { + public bool Success { get; set; } + public HostInfo Host { get; set; } + } +} diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/SleepResult.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/SleepResult.cs new file mode 100644 index 0000000..af869f8 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Events/SleepResult.cs @@ -0,0 +1,10 @@ +using NetworkResurrector.Agent.PublishedLanguage.Dto; + +namespace NetworkResurrector.Agent.PublishedLanguage.Events +{ + public class SleepResult + { + public bool Success { get; set; } + public HostInfo Host { get; set; } + } +} diff --git a/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs b/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs index b8ea0e0..7f1d60c 100644 --- a/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs +++ b/src/agent/NetworkResurrector.Agent/Controllers/HostController.cs @@ -19,9 +19,23 @@ namespace NetworkResurrector.Agent.Controllers } [HttpPost("shutdown")] - public async Task Shutdown([FromBody] Shutdown shutdownMachine) + public async Task Shutdown([FromBody] Shutdown shutdown) { - var result = await _mediator.Send(shutdownMachine); + var result = await _mediator.Send(shutdown); + return Ok(result); + } + + [HttpPost("restart")] + public async Task Restart([FromBody] Restart restart) + { + var result = await _mediator.Send(restart); + return Ok(result); + } + + [HttpPost("sleep")] + public async Task Sleep([FromBody] Sleep sleep) + { + var result = await _mediator.Send(sleep); return Ok(result); } }