windows power methods
parent
e321769dd4
commit
fb5ec3fb8d
|
@ -25,7 +25,7 @@ namespace NetworkResurrector.Agent.Application
|
||||||
services.AddSingleton<ICliService, BashService>();
|
services.AddSingleton<ICliService, BashService>();
|
||||||
services.AddSingleton<IPowerService, LinuxPowerService>();
|
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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,10 @@ namespace NetworkResurrector.Agent.Application.Services.Abstractions
|
||||||
public interface IPowerService
|
public interface IPowerService
|
||||||
{
|
{
|
||||||
void Shutdown(PowerOptions options);
|
void Shutdown(PowerOptions options);
|
||||||
|
void Restart(PowerOptions options);
|
||||||
|
void Sleep(PowerOptions options);
|
||||||
|
void Logout();
|
||||||
void Lock();
|
void Lock();
|
||||||
|
void Cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,29 @@ namespace NetworkResurrector.Agent.Application.Services.Linux
|
||||||
throw new NotImplementedException();
|
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()
|
public void Lock()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Cancel()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using Microsoft.Extensions.Logging;
|
using NetworkResurrector.Agent.Application.Services.Abstractions;
|
||||||
using NetworkResurrector.Agent.Application.Services.Abstractions;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
@ -8,11 +7,8 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
|
||||||
{
|
{
|
||||||
internal class CmdService : ICliService
|
internal class CmdService : ICliService
|
||||||
{
|
{
|
||||||
private readonly ILogger<CmdService> _logger;
|
public CmdService()
|
||||||
|
|
||||||
public CmdService(ILogger<CmdService> logger)
|
|
||||||
{
|
{
|
||||||
_logger=logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public (bool success, string message) Execute(string command)
|
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($"Output:\n {commandOutput.ToString()}\n");
|
||||||
msg.Append($"Errors:\n {commandError.ToString()}\n");
|
msg.Append($"Errors:\n {commandError.ToString()}\n");
|
||||||
msg.Append($"Process ended at: {DateTime.Now}");
|
msg.Append($"Process ended at: {DateTime.Now}");
|
||||||
_logger.LogDebug($"Command: {command} | ExecutionLog: {msg}");
|
|
||||||
|
|
||||||
return (commandError.Length > 0, msg.ToString());
|
return (commandError.Length > 0, msg.ToString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
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.Domain.Models;
|
||||||
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace NetworkResurrector.Agent.Application.Services.Windows
|
namespace NetworkResurrector.Agent.Application.Services.Windows
|
||||||
|
@ -10,15 +11,15 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
|
||||||
private readonly ILogger<WindowsPowerService> _logger;
|
private readonly ILogger<WindowsPowerService> _logger;
|
||||||
private readonly ICliService _cliService;
|
private readonly ICliService _cliService;
|
||||||
|
|
||||||
public WindowsPowerService(ILogger<WindowsPowerService> logger)
|
public WindowsPowerService(ILogger<WindowsPowerService> logger, ICliService cliService)
|
||||||
{
|
{
|
||||||
_logger=logger;
|
_logger=logger;
|
||||||
|
_cliService=cliService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Shutdown(PowerOptions options)
|
private void StartShutdownProcess(string arguments)
|
||||||
{
|
{
|
||||||
|
var psi = new ProcessStartInfo("shutdown.exe", arguments)
|
||||||
var psi = new ProcessStartInfo("shutdown", "/s /t 0")
|
|
||||||
{
|
{
|
||||||
CreateNoWindow = true,
|
CreateNoWindow = true,
|
||||||
UseShellExecute = false
|
UseShellExecute = false
|
||||||
|
@ -27,9 +28,64 @@ namespace NetworkResurrector.Agent.Application.Services.Windows
|
||||||
Process.Start(psi);
|
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()
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace NetworkResurrector.Agent.Domain.Models
|
namespace NetworkResurrector.Agent.Domain.Models
|
||||||
{
|
{
|
||||||
public record PowerOptions(int Delay, bool Force);
|
public record PowerOptions(int? Delay, bool Force);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
namespace NetworkResurrector.Agent.PublishedLanguage.Dto
|
namespace NetworkResurrector.Agent.PublishedLanguage.Dto
|
||||||
{
|
{
|
||||||
public record ActionOwner(string OwnerCode);
|
public record ActionOwner(string OwnerCode);
|
||||||
public record ActionOptions(int Delay);
|
public record ActionOptions(int? Delay, bool Force);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue