windows power methods

master
Tudor Stanciu 2022-01-10 11:28:39 +02:00
parent e321769dd4
commit fb5ec3fb8d
7 changed files with 90 additions and 15 deletions

View File

@ -25,7 +25,7 @@ namespace NetworkResurrector.Agent.Application
services.AddSingleton<ICliService, BashService>();
services.AddSingleton<IPowerService, LinuxPowerService>();
}
else throw new Exception("Cannot register IPowerService. Unknown operating system.");
else throw new Exception("Cannot register power services. Unknown operating system.");
}
}
}

View File

@ -5,6 +5,10 @@ namespace NetworkResurrector.Agent.Application.Services.Abstractions
public interface IPowerService
{
void Shutdown(PowerOptions options);
void Restart(PowerOptions options);
void Sleep(PowerOptions options);
void Logout();
void Lock();
void Cancel();
}
}

View File

@ -11,9 +11,29 @@ namespace NetworkResurrector.Agent.Application.Services.Linux
throw new NotImplementedException();
}
public void Restart(PowerOptions options)
{
throw new NotImplementedException();
}
public void Sleep(PowerOptions options)
{
throw new NotImplementedException();
}
public void Logout()
{
throw new NotImplementedException();
}
public void Lock()
{
throw new NotImplementedException();
}
public void Cancel()
{
throw new NotImplementedException();
}
}
}

View File

@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.Application.Services.Abstractions;
using NetworkResurrector.Agent.Application.Services.Abstractions;
using System;
using System.Diagnostics;
using System.Text;
@ -8,11 +7,8 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
{
internal class CmdService : ICliService
{
private readonly ILogger<CmdService> _logger;
public CmdService(ILogger<CmdService> logger)
public CmdService()
{
_logger=logger;
}
public (bool success, string message) Execute(string command)
@ -58,7 +54,6 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
msg.Append($"Output:\n {commandOutput.ToString()}\n");
msg.Append($"Errors:\n {commandError.ToString()}\n");
msg.Append($"Process ended at: {DateTime.Now}");
_logger.LogDebug($"Command: {command} | ExecutionLog: {msg}");
return (commandError.Length > 0, msg.ToString());
}

View File

@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.Application.Services.Abstractions;
using NetworkResurrector.Agent.Domain.Models;
using System;
using System.Diagnostics;
namespace NetworkResurrector.Agent.Application.Services.Windows
@ -10,15 +11,15 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
private readonly ILogger<WindowsPowerService> _logger;
private readonly ICliService _cliService;
public WindowsPowerService(ILogger<WindowsPowerService> logger)
public WindowsPowerService(ILogger<WindowsPowerService> logger, ICliService cliService)
{
_logger=logger;
_cliService=cliService;
}
public void Shutdown(PowerOptions options)
private void StartShutdownProcess(string arguments)
{
var psi = new ProcessStartInfo("shutdown", "/s /t 0")
var psi = new ProcessStartInfo("shutdown.exe", arguments)
{
CreateNoWindow = true,
UseShellExecute = false
@ -27,9 +28,64 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
Process.Start(psi);
}
private string SetOptions(string arguments, PowerOptions options)
{
if (options.Force)
arguments = $"{arguments} -f";
var countdown = 0;
if (options.Delay.HasValue)
countdown = options.Delay.Value;
arguments = $"{arguments} -t {countdown}";
return arguments;
}
public void Shutdown(PowerOptions options)
{
var arguments = "-s";
arguments = SetOptions(arguments, options);
_logger.LogInformation($"Shutdown arguments: {arguments}");
StartShutdownProcess(arguments);
}
public void Restart(PowerOptions options)
{
var arguments = "-r";
arguments = SetOptions(arguments, options);
_logger.LogInformation($"Restart arguments: {arguments}");
StartShutdownProcess(arguments);
}
public void Sleep(PowerOptions options)
{
var arguments = "-h";
arguments = SetOptions(arguments, options);
_logger.LogInformation($"Sleep arguments: {arguments}");
StartShutdownProcess(arguments);
}
public void Logout()
{
StartShutdownProcess("-l");
}
public void Lock()
{
var (success, message) = _cliService.Execute("rundll32.exe user32.dll,LockWorkStation");
var command = "rundll32.exe user32.dll,LockWorkStation";
var (success, message) = _cliService.Execute(command);
var msg = $"Command: {command} | ExecutionLog: {message}";
if (success)
_logger.LogDebug(msg);
else
throw new Exception(msg);
}
public void Cancel()
{
StartShutdownProcess("-a");
}
}
}

View File

@ -1,4 +1,4 @@
namespace NetworkResurrector.Agent.Domain.Models
{
public record PowerOptions(int Delay, bool Force);
public record PowerOptions(int? Delay, bool Force);
}

View File

@ -1,5 +1,5 @@
namespace NetworkResurrector.Agent.PublishedLanguage.Dto
{
public record ActionOwner(string OwnerCode);
public record ActionOptions(int Delay);
public record ActionOptions(int? Delay, bool Force);
}