sleep fix
parent
d97e1fa8b5
commit
206f2b89d2
|
@ -1,6 +1,8 @@
|
||||||
using MediatR;
|
using AutoMapper;
|
||||||
|
using MediatR;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NetworkResurrector.Agent.Application.Services.Abstractions;
|
using NetworkResurrector.Agent.Application.Services.Abstractions;
|
||||||
|
using NetworkResurrector.Agent.Domain.Models;
|
||||||
using NetworkResurrector.Agent.PublishedLanguage.Commands;
|
using NetworkResurrector.Agent.PublishedLanguage.Commands;
|
||||||
using NetworkResurrector.Agent.PublishedLanguage.Events;
|
using NetworkResurrector.Agent.PublishedLanguage.Events;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
@ -14,12 +16,14 @@ namespace NetworkResurrector.Agent.Application.CommandHandlers
|
||||||
private readonly ILogger<SleepHandler> _logger;
|
private readonly ILogger<SleepHandler> _logger;
|
||||||
private readonly IPowerService _powerService;
|
private readonly IPowerService _powerService;
|
||||||
private readonly IValidationService _validationService;
|
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;
|
_logger=logger;
|
||||||
_powerService=powerService;
|
_powerService=powerService;
|
||||||
_validationService=validationService;
|
_validationService=validationService;
|
||||||
|
_mapper=mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<SleepResult> Handle(Sleep command, CancellationToken cancellationToken)
|
public async Task<SleepResult> Handle(Sleep command, CancellationToken cancellationToken)
|
||||||
|
@ -36,7 +40,8 @@ namespace NetworkResurrector.Agent.Application.CommandHandlers
|
||||||
var stopWatch = new Stopwatch();
|
var stopWatch = new Stopwatch();
|
||||||
stopWatch.Start();
|
stopWatch.Start();
|
||||||
|
|
||||||
_powerService.Sleep();
|
var options = _mapper.Map<PowerOptions>(command.Options);
|
||||||
|
_powerService.Sleep(options);
|
||||||
|
|
||||||
stopWatch.Stop();
|
stopWatch.Stop();
|
||||||
_logger.LogDebug($"System sleeping finished - {stopWatch.ElapsedMilliseconds:N0} ms");
|
_logger.LogDebug($"System sleeping finished - {stopWatch.ElapsedMilliseconds:N0} ms");
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace NetworkResurrector.Agent.Application.Services.Abstractions
|
||||||
{
|
{
|
||||||
void Shutdown(PowerOptions options);
|
void Shutdown(PowerOptions options);
|
||||||
void Restart(PowerOptions options);
|
void Restart(PowerOptions options);
|
||||||
void Sleep();
|
void Sleep(PowerOptions options);
|
||||||
void Logout();
|
void Logout();
|
||||||
void Lock();
|
void Lock();
|
||||||
void Cancel();
|
void Cancel();
|
||||||
|
|
|
@ -16,7 +16,7 @@ namespace NetworkResurrector.Agent.Application.Services.Linux
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Sleep()
|
public void Sleep(PowerOptions options)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,16 +20,13 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
|
||||||
private string SetOptions(string arguments, PowerOptions options)
|
private string SetOptions(string arguments, PowerOptions options)
|
||||||
{
|
{
|
||||||
if (options == null)
|
if (options == null)
|
||||||
options = new PowerOptions(null, false);
|
return arguments;
|
||||||
|
|
||||||
if (options.Force)
|
if (options.Force)
|
||||||
arguments = $"{arguments} -f";
|
arguments = $"{arguments} -f";
|
||||||
|
|
||||||
var countdown = 0;
|
|
||||||
if (options.Delay.HasValue)
|
if (options.Delay.HasValue)
|
||||||
countdown = options.Delay.Value;
|
arguments = $"{arguments} -t {options.Delay.Value}";
|
||||||
|
|
||||||
arguments = $"{arguments} -t {countdown}";
|
|
||||||
|
|
||||||
return arguments;
|
return arguments;
|
||||||
}
|
}
|
||||||
|
@ -81,9 +78,16 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
|
||||||
ManageExecutionResponse(result);
|
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";
|
var arguments = "-h";
|
||||||
|
arguments = SetOptions(arguments, options);
|
||||||
_logger.LogInformation($"Sleep arguments: {arguments}");
|
_logger.LogInformation($"Sleep arguments: {arguments}");
|
||||||
var result =_cliService.Shutdown(arguments);
|
var result =_cliService.Shutdown(arguments);
|
||||||
ManageExecutionResponse(result);
|
ManageExecutionResponse(result);
|
||||||
|
|
|
@ -7,5 +7,6 @@ namespace NetworkResurrector.Agent.PublishedLanguage.Commands
|
||||||
public class Sleep : Command<SleepResult>
|
public class Sleep : Command<SleepResult>
|
||||||
{
|
{
|
||||||
public ActionOwner Owner { get; set; }
|
public ActionOwner Owner { get; set; }
|
||||||
|
public ActionOptions Options { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue