sleep fix

master
Tudor Stanciu 2022-01-12 19:38:03 +02:00
parent d97e1fa8b5
commit 206f2b89d2
5 changed files with 21 additions and 11 deletions

View File

@ -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<SleepHandler> _logger;
private readonly IPowerService _powerService;
private readonly IValidationService _validationService;
private readonly IMapper _mapper;
public SleepHandler(ILogger<SleepHandler> logger, IPowerService powerService, IValidationService validationService)
public SleepHandler(ILogger<SleepHandler> logger, IPowerService powerService, IValidationService validationService, IMapper mapper)
{
_logger=logger;
_powerService=powerService;
_validationService=validationService;
_mapper=mapper;
}
public async Task<SleepResult> 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<PowerOptions>(command.Options);
_powerService.Sleep(options);
stopWatch.Stop();
_logger.LogDebug($"System sleeping finished - {stopWatch.ElapsedMilliseconds:N0} ms");

View File

@ -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();

View File

@ -16,7 +16,7 @@ namespace NetworkResurrector.Agent.Application.Services.Linux
throw new NotImplementedException();
}
public void Sleep()
public void Sleep(PowerOptions options)
{
throw new NotImplementedException();
}

View File

@ -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);

View File

@ -7,5 +7,6 @@ namespace NetworkResurrector.Agent.PublishedLanguage.Commands
public class Sleep : Command<SleepResult>
{
public ActionOwner Owner { get; set; }
public ActionOptions Options { get; set; }
}
}