diff --git a/UserInterface/Assets/InstallBurntToast.ps1 b/UserInterface/Assets/InstallBurntToast.ps1 new file mode 100644 index 0000000..89add73 --- /dev/null +++ b/UserInterface/Assets/InstallBurntToast.ps1 @@ -0,0 +1 @@ +Install-Module -Name BurntToast \ No newline at end of file diff --git a/UserInterface/UserInterface.csproj b/UserInterface/UserInterface.csproj index 73c7ed9..0036f73 100644 --- a/UserInterface/UserInterface.csproj +++ b/UserInterface/UserInterface.csproj @@ -7,8 +7,17 @@ + + + + + + + + PreserveNewest + diff --git a/UserInterface/Views/AddCommandDialog.axaml.cs b/UserInterface/Views/AddCommandDialog.axaml.cs index e0e0b96..e5e6693 100644 --- a/UserInterface/Views/AddCommandDialog.axaml.cs +++ b/UserInterface/Views/AddCommandDialog.axaml.cs @@ -100,6 +100,12 @@ namespace UserInterface.Views item.ShowCommandInput = true; item.ShowKeyInput = false; break; + case AvailableCommands.CustomPowerShellCommand: + item.Description = "This command lets you execute any command you want. It will run in a Powershell window silently. "; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service/blob/master/documentation/Commands.md#custompowershellcommand"; + item.ShowCommandInput = true; + item.ShowKeyInput = false; + break; case AvailableCommands.ShutdownCommand: item.Description = "This command shuts down the PC immediately. "; item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service/blob/master/documentation/Commands.md#shutdowncommand"; diff --git a/UserInterface/Views/GeneralSettings.axaml b/UserInterface/Views/GeneralSettings.axaml index 4ca197d..3077b8a 100644 --- a/UserInterface/Views/GeneralSettings.axaml +++ b/UserInterface/Views/GeneralSettings.axaml @@ -23,5 +23,8 @@ If a sensor is called "ActiveWindow" and the name prefix is set to "laptop", the + + + diff --git a/UserInterface/Views/GeneralSettings.axaml.cs b/UserInterface/Views/GeneralSettings.axaml.cs index 39b2618..281987b 100644 --- a/UserInterface/Views/GeneralSettings.axaml.cs +++ b/UserInterface/Views/GeneralSettings.axaml.cs @@ -57,6 +57,19 @@ namespace UserInterface.Views ((GeneralSettingsViewModel)this.DataContext).Update(settings); } + public void InstallBurntToast(object sender, RoutedEventArgs args) + { + var process = new System.Diagnostics.Process(); + var startInfo = new System.Diagnostics.ProcessStartInfo + { + WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal, + FileName = "powershell.exe", + Arguments = $"-NoExit Start-Process 'powershell InstallBurntToast.ps1' -Verb runAs" + }; + process.StartInfo = startInfo; + process.Start(); + } + private void InitializeComponent() { diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index e02e73e..ccb16d9 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -183,8 +183,8 @@ namespace hass_workstation_service.Communication.InterProcesCommunication AvailableSensors.LastBootSensor => new LastBootSensor(_publisher, (int)model.UpdateInterval, model.Name), AvailableSensors.SessionStateSensor => new SessionStateSensor(_publisher, (int)model.UpdateInterval, model.Name), AvailableSensors.CurrentVolumeSensor => new CurrentVolumeSensor(_publisher, (int)model.UpdateInterval, model.Name), - AvailableSensors.GPUTemperatureSensor => new GpuTemperatureSensor(_publisher, (int)model.UpdateInterval, model.Name), - AvailableSensors.GPULoadSensor => new GpuLoadSensor(_publisher, (int)model.UpdateInterval, model.Name), + AvailableSensors.GPUTemperatureSensor => new GPUTemperatureSensor(_publisher, (int)model.UpdateInterval, model.Name), + AvailableSensors.GPULoadSensor => new GPULoadSensor(_publisher, (int)model.UpdateInterval, model.Name), _ => null }; } @@ -203,6 +203,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication AvailableCommands.RestartCommand => new RestartCommand(_publisher, model.Name), AvailableCommands.LogOffCommand => new LogOffCommand(_publisher, model.Name), AvailableCommands.CustomCommand => new CustomCommand(_publisher, model.Command, model.Name), + AvailableCommands.CustomPowerShellCommand => new CustomPowerShellCommand(_publisher, model.Command, model.Name), AvailableCommands.PlayPauseCommand => new PlayPauseCommand(_publisher, model.Name), AvailableCommands.NextCommand => new NextCommand(_publisher, model.Name), AvailableCommands.PreviousCommand => new PreviousCommand(_publisher, model.Name), diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs index 9a692ba..eb0139b 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -116,6 +116,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models PreviousCommand, VolumeUpCommand, VolumeDownCommand, - MuteCommand + MuteCommand, + CustomPowerShellCommand } } \ No newline at end of file diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index dfdafc8..cdca7c5 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -138,11 +138,11 @@ namespace hass_workstation_service.Data case "CurrentVolumeSensor": sensor = new CurrentVolumeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); break; - case "GpuTemperatureSensor": - sensor = new GpuTemperatureSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); + case "GPUTemperatureSensor": + sensor = new GPUTemperatureSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); break; - case "GpuLoadSensor": - sensor = new GpuLoadSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); + case "GPULoadSensor": + sensor = new GPULoadSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); break; // keep this one last! case "WMIQuerySensor": @@ -196,6 +196,9 @@ namespace hass_workstation_service.Data case "CustomCommand": command = new CustomCommand(publisher, configuredCommand.Command, configuredCommand.Name, configuredCommand.Id); break; + case "CustomPowerShellCommand": + command = new CustomPowerShellCommand(publisher, configuredCommand.Command, configuredCommand.Name, configuredCommand.Id); + break; case "MediaPlayPauseCommand": command = new PlayPauseCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; diff --git a/hass-workstation-service/Domain/Commands/CustomCommand.cs b/hass-workstation-service/Domain/Commands/CustomCommand.cs index f4dced8..6d9c617 100644 --- a/hass-workstation-service/Domain/Commands/CustomCommand.cs +++ b/hass-workstation-service/Domain/Commands/CustomCommand.cs @@ -46,9 +46,6 @@ namespace hass_workstation_service.Domain.Commands } this.State = "OFF"; } - - - public override CommandDiscoveryConfigModel GetAutoDiscoveryConfig() { return new CommandDiscoveryConfigModel() diff --git a/hass-workstation-service/Domain/Commands/CustomPowerShellCommand.cs b/hass-workstation-service/Domain/Commands/CustomPowerShellCommand.cs new file mode 100644 index 0000000..e6091d2 --- /dev/null +++ b/hass-workstation-service/Domain/Commands/CustomPowerShellCommand.cs @@ -0,0 +1,73 @@ +using hass_workstation_service.Communication; +using Serilog; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace hass_workstation_service.Domain.Commands +{ + public class CustomPowerShellCommand : AbstractCommand + { + public string Command { get; protected set; } + public string State { get; protected set; } + public Process Process { get; private set; } + public CustomPowerShellCommand(MqttPublisher publisher, string command, string name = "CustomPowerShell", Guid id = default(Guid)) : base(publisher, name ?? "CustomPowerShell", id) + { + this.Command = command; + this.State = "OFF"; + } + + public override async void TurnOn() + { + this.State = "ON"; + this.Process = new Process(); + ProcessStartInfo startInfo = new ProcessStartInfo(); + startInfo.WindowStyle = ProcessWindowStyle.Hidden; + startInfo.CreateNoWindow = true; + startInfo.FileName = @"powershell.exe"; + startInfo.Arguments = $"\"&' { this.Command } '\""; + this.Process.StartInfo = startInfo; + try + { + this.Process.Start(); + } + catch (Exception e) + { + Log.Logger.Error($"Sensor {this.Name} failed", e); + this.State = "FAILED"; + } + + while (!this.Process.HasExited) + { + await Task.Delay(1000); + } + this.State = "OFF"; + } + public override CommandDiscoveryConfigModel GetAutoDiscoveryConfig() + { + return new CommandDiscoveryConfigModel() + { + Name = this.Name, + NamePrefix = Publisher.NamePrefix, + Unique_id = this.Id.ToString(), + Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability", + Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/set", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", + Device = this.Publisher.DeviceConfigModel, + }; + } + + public override string GetState() + { + return this.State; + } + + public override void TurnOff() + { + this.Process.Kill(); + } + } +} diff --git a/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs b/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs index 91e5a66..a6bc639 100644 --- a/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs +++ b/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs @@ -9,11 +9,11 @@ using LibreHardwareMonitor.Hardware; namespace hass_workstation_service.Domain.Sensors { - public class GpuLoadSensor : AbstractSensor + public class GPULoadSensor : AbstractSensor { private Computer _computer; private IHardware _gpu; - public GpuLoadSensor(MqttPublisher publisher, int? updateInterval = null, string name = "GPULoad", Guid id = default(Guid)) : base(publisher, name ?? "GPULoad", updateInterval ?? 10, id) + public GPULoadSensor(MqttPublisher publisher, int? updateInterval = null, string name = "GPULoad", Guid id = default(Guid)) : base(publisher, name ?? "GPULoad", updateInterval ?? 10, id) { _computer = new Computer { diff --git a/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs b/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs index f16edda..62586af 100644 --- a/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs +++ b/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs @@ -9,11 +9,11 @@ using LibreHardwareMonitor.Hardware; namespace hass_workstation_service.Domain.Sensors { - public class GpuTemperatureSensor : AbstractSensor + public class GPUTemperatureSensor : AbstractSensor { private Computer _computer; private IHardware _gpu; - public GpuTemperatureSensor(MqttPublisher publisher, int? updateInterval = null, string name = "GPUTemperature", Guid id = default(Guid)) : base(publisher, name ?? "GPUTemperature", updateInterval ?? 10, id) + public GPUTemperatureSensor(MqttPublisher publisher, int? updateInterval = null, string name = "GPUTemperature", Guid id = default(Guid)) : base(publisher, name ?? "GPUTemperature", updateInterval ?? 10, id) { _computer = new Computer { diff --git a/hass-workstation-service/InstallBurntToast.ps1 b/hass-workstation-service/InstallBurntToast.ps1 new file mode 100644 index 0000000..89add73 --- /dev/null +++ b/hass-workstation-service/InstallBurntToast.ps1 @@ -0,0 +1 @@ +Install-Module -Name BurntToast \ No newline at end of file diff --git a/hass-workstation-service/hass-workstation-service.csproj b/hass-workstation-service/hass-workstation-service.csproj index fc2ee17..2bdd719 100644 --- a/hass-workstation-service/hass-workstation-service.csproj +++ b/hass-workstation-service/hass-workstation-service.csproj @@ -26,6 +26,7 @@ + @@ -33,6 +34,9 @@ + + PreserveNewest + Always