From 206f2b89d2171b885abb99808017cfb1d0b9522d Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 12 Jan 2022 19:38:03 +0200 Subject: [PATCH] sleep fix --- .../CommandHandlers/SleepHandler.cs | 11 ++++++++--- .../Services/Abstractions/IPowerService.cs | 2 +- .../Services/Linux/LinuxPowerService.cs | 2 +- .../Services/Windows/WindowsPowerService.cs | 16 ++++++++++------ .../Commands/Sleep.cs | 1 + 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/SleepHandler.cs b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/SleepHandler.cs index 94422f2..92eead7 100644 --- a/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/SleepHandler.cs +++ b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/SleepHandler.cs @@ -1,6 +1,8 @@ -using MediatR; +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; @@ -14,12 +16,14 @@ namespace NetworkResurrector.Agent.Application.CommandHandlers private readonly ILogger _logger; private readonly IPowerService _powerService; private readonly IValidationService _validationService; + private readonly IMapper _mapper; - public SleepHandler(ILogger logger, IPowerService powerService, IValidationService validationService) + 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) @@ -36,7 +40,8 @@ namespace NetworkResurrector.Agent.Application.CommandHandlers var stopWatch = new Stopwatch(); stopWatch.Start(); - _powerService.Sleep(); + var options = _mapper.Map(command.Options); + _powerService.Sleep(options); stopWatch.Stop(); _logger.LogDebug($"System sleeping finished - {stopWatch.ElapsedMilliseconds:N0} ms"); diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IPowerService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IPowerService.cs index 6beed27..92a8fc1 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IPowerService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IPowerService.cs @@ -6,7 +6,7 @@ namespace NetworkResurrector.Agent.Application.Services.Abstractions { void Shutdown(PowerOptions options); void Restart(PowerOptions options); - void Sleep(); + void Sleep(PowerOptions options); void Logout(); void Lock(); void Cancel(); diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Linux/LinuxPowerService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Linux/LinuxPowerService.cs index 6eaac5e..6614e18 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Linux/LinuxPowerService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Linux/LinuxPowerService.cs @@ -16,7 +16,7 @@ namespace NetworkResurrector.Agent.Application.Services.Linux throw new NotImplementedException(); } - public void Sleep() + public void Sleep(PowerOptions options) { throw new NotImplementedException(); } diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs index 60613c2..1d23181 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs @@ -20,16 +20,13 @@ namespace NetworkResurrector.Agent.Application.Services.Windows private string SetOptions(string arguments, PowerOptions options) { if (options == null) - options = new PowerOptions(null, false); + return arguments; if (options.Force) arguments = $"{arguments} -f"; - var countdown = 0; if (options.Delay.HasValue) - countdown = options.Delay.Value; - - arguments = $"{arguments} -t {countdown}"; + arguments = $"{arguments} -t {options.Delay.Value}"; return arguments; } @@ -81,9 +78,16 @@ namespace NetworkResurrector.Agent.Application.Services.Windows ManageExecutionResponse(result); } - public void Sleep() + public void Sleep(PowerOptions options) { + if (options.Delay.HasValue) + { + _logger.LogWarning($"Sleep action does not accept delay."); + options = new PowerOptions(null, options.Force); + } + var arguments = "-h"; + arguments = SetOptions(arguments, options); _logger.LogInformation($"Sleep arguments: {arguments}"); var result =_cliService.Shutdown(arguments); ManageExecutionResponse(result); diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Sleep.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Sleep.cs index abdf984..79090cf 100644 --- a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Sleep.cs +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Commands/Sleep.cs @@ -7,5 +7,6 @@ namespace NetworkResurrector.Agent.PublishedLanguage.Commands public class Sleep : Command { public ActionOwner Owner { get; set; } + public ActionOptions Options { get; set; } } }