From f6d184fa2fabacfc681ebb01efa50a1d26807c5d Mon Sep 17 00:00:00 2001 From: sleevezipper Date: Sun, 7 Mar 2021 02:03:12 +0100 Subject: [PATCH] add media commands and keycommand --- README.md | 15 +++++++ .../ViewModels/AddCommandViewModel.cs | 3 ++ UserInterface/Views/AddCommandDialog.axaml | 2 + UserInterface/Views/AddCommandDialog.axaml.cs | 39 ++++++++++++++++--- .../InterProcessApi.cs | 11 +++++- .../ServiceContractModels.cs | 5 ++- .../Data/ConfigurationService.cs | 9 +++++ .../Domain/Commands/KeyCommand.cs | 3 ++ .../Domain/Commands/MediaMuteCommand.cs | 14 +++++++ .../Domain/Commands/MediaVolumeDownCommand.cs | 14 +++++++ .../Domain/Commands/MediaVolumeUpCommand.cs | 14 +++++++ 11 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 hass-workstation-service/Domain/Commands/MediaMuteCommand.cs create mode 100644 hass-workstation-service/Domain/Commands/MediaVolumeDownCommand.cs create mode 100644 hass-workstation-service/Domain/Commands/MediaVolumeUpCommand.cs diff --git a/README.md b/README.md index 055df7f..d2a95dc 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,21 @@ This command allows you to run any Windows Commands. The command will be run in |shutdown /s /t 300|Shuts the PC down after 5 minutes (300 seconds).| |C:\path\to\your\batchfile.bat|Run the specified batch file.| +### KeyCommand + +Sends a keystroke with the specified key. You can pick [any of these](https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes) key codes. + +### Media Commands + +There's several media commands available which are very self exlanatory. + +- Play/Pause +- Next +- Previous +- Volume up +- Volume down +- Mute (toggle) + ## Credits This project depends on work done by others and they should at least get a mention. Please note that this list is not complete yet. diff --git a/UserInterface/ViewModels/AddCommandViewModel.cs b/UserInterface/ViewModels/AddCommandViewModel.cs index 7fc458a..133bfca 100644 --- a/UserInterface/ViewModels/AddCommandViewModel.cs +++ b/UserInterface/ViewModels/AddCommandViewModel.cs @@ -13,9 +13,11 @@ namespace UserInterface.ViewModels public string Description { get => description; set => this.RaiseAndSetIfChanged(ref description, value); } public bool ShowCommandInput { get => showCommandInput; set => this.RaiseAndSetIfChanged(ref showCommandInput, value); } + public bool ShowKeyInput { get => showKeyInput; set => this.RaiseAndSetIfChanged(ref showKeyInput, value); } private string moreInfoLink; private bool showCommandInput; + private bool showKeyInput; public string MoreInfoLink { @@ -27,5 +29,6 @@ namespace UserInterface.ViewModels public string Name { get; set; } public string Command { get; set; } + public string Key { get; set; } } } diff --git a/UserInterface/Views/AddCommandDialog.axaml b/UserInterface/Views/AddCommandDialog.axaml index c4f6639..dc2e967 100644 --- a/UserInterface/Views/AddCommandDialog.axaml +++ b/UserInterface/Views/AddCommandDialog.axaml @@ -20,6 +20,8 @@ Command + Key + diff --git a/UserInterface/Views/AddCommandDialog.axaml.cs b/UserInterface/Views/AddCommandDialog.axaml.cs index 000d3e3..a0bd1f4 100644 --- a/UserInterface/Views/AddCommandDialog.axaml.cs +++ b/UserInterface/Views/AddCommandDialog.axaml.cs @@ -47,7 +47,7 @@ namespace UserInterface.Views public async void Save(object sender, RoutedEventArgs args) { var item = ((AddCommandViewModel)this.DataContext); - dynamic model = new { item.Name, item.Command}; + dynamic model = new { item.Name, item.Command, item.Key}; string json = JsonSerializer.Serialize(model); await this.client.InvokeAsync(x => x.AddCommand(item.SelectedType, json)); Close(); @@ -62,50 +62,77 @@ namespace UserInterface.Views item.Description = "This command lets you execute any command you want. It will run in a Windows Command Prompt silently. "; item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#customcommand"; 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#shutdowncommand"; item.ShowCommandInput = false; + item.ShowKeyInput = false; break; case AvailableCommands.RestartCommand: item.Description = "This command restarts the PC immediately. "; item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#restartcommand"; item.ShowCommandInput = false; + item.ShowKeyInput = false; break; case AvailableCommands.LogOffCommand: item.Description = "This command logs the current user off immediately. "; item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#logoffcommand"; item.ShowCommandInput = false; + item.ShowKeyInput = false; break; case AvailableCommands.KeyCommand: - item.Description = "This commands can be used to send emulate a keystroke."; + item.Description = "This command can be used to emulate a keystroke. It requires a key code which you can find by clicking the info button below."; item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#keycommand"; item.ShowCommandInput = false; + item.ShowKeyInput = true; break; case AvailableCommands.PlayPauseCommand: item.Description = "This command plays or pauses currently playing media."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#playpausecommand"; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#media-commands"; item.ShowCommandInput = false; + item.ShowKeyInput = false; break; case AvailableCommands.NextCommand: item.Description = "This command skips to the next media."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#nextcommand"; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#media-commands"; item.ShowCommandInput = false; + item.ShowKeyInput = false; break; case AvailableCommands.PreviousCommand: item.Description = "This command plays previous media."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#previouscommand"; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#media-commands"; item.ShowCommandInput = false; + item.ShowKeyInput = false; + break; + case AvailableCommands.VolumeDownCommand: + item.Description = "Lowers the system volume."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#media-commands"; + item.ShowCommandInput = false; + item.ShowKeyInput = false; + break; + case AvailableCommands.VolumeUpCommand: + item.Description = "Raises the system volume."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#media-commands"; + item.ShowCommandInput = false; + item.ShowKeyInput = false; + break; + case AvailableCommands.MuteCommand: + item.Description = "Toggles muting the system volume."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#media-commands"; + item.ShowCommandInput = false; + item.ShowKeyInput = false; break; default: item.Description = null; item.MoreInfoLink = null; item.ShowCommandInput = false; + item.ShowKeyInput = false; break; } } - public void OpenInfo(object sender, RoutedEventArgs args) + public void OpenInfo(object sender, RoutedEventArgs args) { var item = ((AddCommandViewModel)this.DataContext); BrowserUtil.OpenBrowser(item.MoreInfoLink); diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 9d18fb6..25eff46 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -197,8 +197,17 @@ namespace hass_workstation_service.Communication.InterProcesCommunication case AvailableCommands.PreviousCommand: commandToCreate = new MediaPreviousCommand(this._publisher, model.Name); break; + case AvailableCommands.VolumeUpCommand: + commandToCreate = new MediaVolumeUpCommand(this._publisher, model.Name); + break; + case AvailableCommands.VolumeDownCommand: + commandToCreate = new MediaVolumeDownCommand(this._publisher, model.Name); + break; + case AvailableCommands.MuteCommand: + commandToCreate = new MediaMuteCommand(this._publisher, model.Name); + break; case AvailableCommands.KeyCommand: - commandToCreate = new KeyCommand(this._publisher, (byte)model.KeyCode, model.Name); + commandToCreate = new KeyCommand(this._publisher, Convert.ToByte(model.Key, 16), model.Name); break; default: Log.Logger.Error("Unknown sensortype"); diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs index ccfaad5..768f5e8 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -63,6 +63,9 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models KeyCommand, PlayPauseCommand, NextCommand, - PreviousCommand + PreviousCommand, + VolumeUpCommand, + VolumeDownCommand, + MuteCommand } } diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index f9f2987..e58d95e 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -183,6 +183,15 @@ namespace hass_workstation_service.Data case "MediaPreviousCommand": command = new MediaPreviousCommand(publisher, configuredCommand.Name, configuredCommand.Id); break; + case "MediaVolumeUpCommand": + command = new MediaVolumeUpCommand(publisher, configuredCommand.Name, configuredCommand.Id); + break; + case "MediaVolumeDownCommand": + command = new MediaVolumeDownCommand(publisher, configuredCommand.Name, configuredCommand.Id); + break; + case "MediaMuteCommand": + command = new MediaMuteCommand(publisher, configuredCommand.Name, configuredCommand.Id); + break; case "KeyCommand": command = new KeyCommand(publisher, configuredCommand.KeyCode, configuredCommand.Name, configuredCommand.Id); break; diff --git a/hass-workstation-service/Domain/Commands/KeyCommand.cs b/hass-workstation-service/Domain/Commands/KeyCommand.cs index 58a3142..93c636d 100644 --- a/hass-workstation-service/Domain/Commands/KeyCommand.cs +++ b/hass-workstation-service/Domain/Commands/KeyCommand.cs @@ -15,6 +15,9 @@ namespace hass_workstation_service.Domain.Commands public const int VK_MEDIA_NEXT_TRACK = 0xB0; public const int VK_MEDIA_PLAY_PAUSE = 0xB3; public const int VK_MEDIA_PREV_TRACK = 0xB1; + public const int VK_VOLUME_MUTE = 0xAD; + public const int VK_VOLUME_UP = 0xAF; + public const int VK_VOLUME_DOWN = 0xAE; public byte KeyCode { get; protected set; } diff --git a/hass-workstation-service/Domain/Commands/MediaMuteCommand.cs b/hass-workstation-service/Domain/Commands/MediaMuteCommand.cs new file mode 100644 index 0000000..3dd4dbc --- /dev/null +++ b/hass-workstation-service/Domain/Commands/MediaMuteCommand.cs @@ -0,0 +1,14 @@ +using hass_workstation_service.Communication; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace hass_workstation_service.Domain.Commands +{ + public class MediaMuteCommand : KeyCommand + { + public MediaMuteCommand(MqttPublisher publisher, string name = "Mute", Guid id = default(Guid)) : base(publisher, KeyCommand.VK_VOLUME_MUTE, name ?? "Mute", id) { } + } +} diff --git a/hass-workstation-service/Domain/Commands/MediaVolumeDownCommand.cs b/hass-workstation-service/Domain/Commands/MediaVolumeDownCommand.cs new file mode 100644 index 0000000..4096e3c --- /dev/null +++ b/hass-workstation-service/Domain/Commands/MediaVolumeDownCommand.cs @@ -0,0 +1,14 @@ +using hass_workstation_service.Communication; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace hass_workstation_service.Domain.Commands +{ + public class MediaVolumeDownCommand : KeyCommand + { + public MediaVolumeDownCommand(MqttPublisher publisher, string name = "VolumeDown", Guid id = default(Guid)) : base(publisher, KeyCommand.VK_VOLUME_DOWN, name ?? "VolumeDown", id) { } + } +} diff --git a/hass-workstation-service/Domain/Commands/MediaVolumeUpCommand.cs b/hass-workstation-service/Domain/Commands/MediaVolumeUpCommand.cs new file mode 100644 index 0000000..0631160 --- /dev/null +++ b/hass-workstation-service/Domain/Commands/MediaVolumeUpCommand.cs @@ -0,0 +1,14 @@ +using hass_workstation_service.Communication; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace hass_workstation_service.Domain.Commands +{ + public class MediaVolumeUpCommand : KeyCommand + { + public MediaVolumeUpCommand(MqttPublisher publisher, string name = "VolumeUp", Guid id = default(Guid)) : base(publisher, KeyCommand.VK_VOLUME_UP, name ?? "VolumeUp", id) { } + } +}