From fb5ec3fb8debe6deb91a5ce0b8d51752d4f1f7c7 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 10 Jan 2022 11:28:39 +0200 Subject: [PATCH] windows power methods --- .../DependencyInjectionExtensions.cs | 2 +- .../Services/Abstractions/IPowerService.cs | 4 ++ .../Services/Linux/LinuxPowerService.cs | 20 ++++++ .../Services/Windows/CmdService.cs | 9 +-- .../Services/Windows/WindowsPowerService.cs | 66 +++++++++++++++++-- .../Models/Records.cs | 2 +- .../Dto/CommandRecords.cs | 2 +- 7 files changed, 90 insertions(+), 15 deletions(-) diff --git a/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs b/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs index 47e26d9..13c7529 100644 --- a/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs +++ b/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs @@ -25,7 +25,7 @@ namespace NetworkResurrector.Agent.Application services.AddSingleton(); services.AddSingleton(); } - else throw new Exception("Cannot register IPowerService. Unknown operating system."); + else throw new Exception("Cannot register power services. Unknown operating system."); } } } diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IPowerService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IPowerService.cs index 96601c2..92a8fc1 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IPowerService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IPowerService.cs @@ -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(); } } diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Linux/LinuxPowerService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Linux/LinuxPowerService.cs index 745cc85..6614e18 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Linux/LinuxPowerService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Linux/LinuxPowerService.cs @@ -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(); + } } } diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs index a2ffe4b..a5b3e3e 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs @@ -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 _logger; - - public CmdService(ILogger 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()); } diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs index 828153b..abdac92 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs @@ -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 _logger; private readonly ICliService _cliService; - public WindowsPowerService(ILogger logger) + public WindowsPowerService(ILogger 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"); } } } diff --git a/src/agent/NetworkResurrector.Agent.Domain/Models/Records.cs b/src/agent/NetworkResurrector.Agent.Domain/Models/Records.cs index eeff596..409ab7a 100644 --- a/src/agent/NetworkResurrector.Agent.Domain/Models/Records.cs +++ b/src/agent/NetworkResurrector.Agent.Domain/Models/Records.cs @@ -1,4 +1,4 @@ namespace NetworkResurrector.Agent.Domain.Models { - public record PowerOptions(int Delay, bool Force); + public record PowerOptions(int? Delay, bool Force); } diff --git a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/CommandRecords.cs b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/CommandRecords.cs index 5a8ffb6..b0306d1 100644 --- a/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/CommandRecords.cs +++ b/src/agent/NetworkResurrector.Agent.PublishedLanguage/Dto/CommandRecords.cs @@ -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); }