diff --git a/UserInterface/UserInterface.csproj b/UserInterface/UserInterface.csproj index 342d878..633f585 100644 --- a/UserInterface/UserInterface.csproj +++ b/UserInterface/UserInterface.csproj @@ -5,7 +5,6 @@ UserInterface.Program - diff --git a/UserInterface/ViewModels/AddCommandViewModel.cs b/UserInterface/ViewModels/AddCommandViewModel.cs index 133bfca..8aa0633 100644 --- a/UserInterface/ViewModels/AddCommandViewModel.cs +++ b/UserInterface/ViewModels/AddCommandViewModel.cs @@ -1,34 +1,26 @@ using hass_workstation_service.Communication.InterProcesCommunication.Models; using ReactiveUI; -using System; -using System.Collections.Generic; -using System.Text; namespace UserInterface.ViewModels { public class AddCommandViewModel : ViewModelBase { - private AvailableCommands selectedType; - private string description; + private AvailableCommands _selectedType; + private string _name; + private string _description; + private bool _showCommandInput; + private bool _showKeyInput; + private string _moreInfoLink; + private string _command; + private string _key; - 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 - { - get { return moreInfoLink; } - set { this.RaiseAndSetIfChanged(ref moreInfoLink, value); } - } - - public AvailableCommands SelectedType { get => selectedType; set => this.RaiseAndSetIfChanged(ref selectedType, value); } - - public string Name { get; set; } - public string Command { get; set; } - public string Key { get; set; } + public AvailableCommands SelectedType { get => _selectedType; set => this.RaiseAndSetIfChanged(ref _selectedType, value); } + public string Name { get => _name; set => this.RaiseAndSetIfChanged(ref _name, value); } + 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); } + public string MoreInfoLink { get => _moreInfoLink; set => this.RaiseAndSetIfChanged(ref _moreInfoLink, value); } + public string Command { get => _command; set => this.RaiseAndSetIfChanged(ref _command, value); } + public string Key { get => _key; set => this.RaiseAndSetIfChanged(ref _key, value); } } -} +} \ No newline at end of file diff --git a/UserInterface/ViewModels/AddSensorViewModel.cs b/UserInterface/ViewModels/AddSensorViewModel.cs index e3303f6..4e22e0f 100644 --- a/UserInterface/ViewModels/AddSensorViewModel.cs +++ b/UserInterface/ViewModels/AddSensorViewModel.cs @@ -1,40 +1,28 @@ using hass_workstation_service.Communication.InterProcesCommunication.Models; using ReactiveUI; -using System; -using System.Collections.Generic; -using System.Text; namespace UserInterface.ViewModels { public class AddSensorViewModel : ViewModelBase { - private AvailableSensors selectedType; - private string description; - private bool showQueryInput; - - public string Description { get => description; set => this.RaiseAndSetIfChanged(ref description, value); } - public bool ShowQueryInput { get => showQueryInput; set => this.RaiseAndSetIfChanged(ref showQueryInput, value); } - public bool ShowWindowNameInput { get => showWindowNameInput; set => this.RaiseAndSetIfChanged(ref showWindowNameInput, value); } - - public bool ShowDetectionModeOptions { get => showDetectionModeOptions; set => this.RaiseAndSetIfChanged(ref showDetectionModeOptions, value); } - - private string moreInfoLink; - private int updateInterval; - private bool showWindowNameInput; - private bool showDetectionModeOptions; - - public string MoreInfoLink - { - get { return moreInfoLink; } - set { this.RaiseAndSetIfChanged(ref moreInfoLink, value); } - } - - - public AvailableSensors SelectedType { get => selectedType; set => this.RaiseAndSetIfChanged(ref selectedType, value); } - - public string Name { get; set; } - public string Query { get; set; } - public string WindowName { get; set; } - public int UpdateInterval { get => updateInterval; set => this.RaiseAndSetIfChanged(ref updateInterval, value); } + private AvailableSensors _selectedType; + private string _name; + private int _updateInterval; + private string _description; + private bool _showQueryInput; + private bool _showWindowNameInput; + private string _moreInfoLink; + private string _query; + private string _windowName; + + public AvailableSensors SelectedType { get => _selectedType; set => this.RaiseAndSetIfChanged(ref _selectedType, value); } + public string Name { get => _name; set => this.RaiseAndSetIfChanged(ref _name, value); } + public int UpdateInterval { get => _updateInterval; set => this.RaiseAndSetIfChanged(ref _updateInterval, value); } + public string Description { get => _description; set => this.RaiseAndSetIfChanged(ref _description, value); } + public bool ShowQueryInput { get => _showQueryInput; set => this.RaiseAndSetIfChanged(ref _showQueryInput, value); } + public bool ShowWindowNameInput { get => _showWindowNameInput; set => this.RaiseAndSetIfChanged(ref _showWindowNameInput, value); } + public string MoreInfoLink { get => _moreInfoLink; set => this.RaiseAndSetIfChanged(ref _moreInfoLink, value); } + public string Query { get => _query; set => this.RaiseAndSetIfChanged(ref _query, value); } + public string WindowName { get => _windowName; set => this.RaiseAndSetIfChanged(ref _windowName, value); } } -} +} \ No newline at end of file diff --git a/UserInterface/ViewModels/CommandSettingsViewModel.cs b/UserInterface/ViewModels/CommandSettingsViewModel.cs index 4477508..ac7eb8a 100644 --- a/UserInterface/ViewModels/CommandSettingsViewModel.cs +++ b/UserInterface/ViewModels/CommandSettingsViewModel.cs @@ -1,15 +1,20 @@ -using ReactiveUI; +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using ReactiveUI; using System; using System.Collections.Generic; -using System.Text; namespace UserInterface.ViewModels { public class CommandSettingsViewModel : ViewModelBase { - private ICollection configuredCommands; + private ICollection _configuredCommands; + + public ICollection ConfiguredCommands + { + get => _configuredCommands; + set => this.RaiseAndSetIfChanged(ref _configuredCommands, value); + } - public ICollection ConfiguredCommands { get => configuredCommands; set => this.RaiseAndSetIfChanged(ref configuredCommands, value); } public void TriggerUpdate() { this.RaisePropertyChanged(); @@ -19,7 +24,7 @@ namespace UserInterface.ViewModels public class CommandViewModel : ViewModelBase { public Guid Id { get; set; } - public string Type { get; set; } + public AvailableCommands Type { get; set; } public string Name { get; set; } } -} +} \ No newline at end of file diff --git a/UserInterface/ViewModels/SensorSettingsViewModel.cs b/UserInterface/ViewModels/SensorSettingsViewModel.cs index 1939bf6..2a52fd0 100644 --- a/UserInterface/ViewModels/SensorSettingsViewModel.cs +++ b/UserInterface/ViewModels/SensorSettingsViewModel.cs @@ -1,15 +1,20 @@ -using ReactiveUI; +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using ReactiveUI; using System; using System.Collections.Generic; -using System.Text; namespace UserInterface.ViewModels { public class SensorSettingsViewModel : ViewModelBase { - private ICollection configuredSensors; + private ICollection _configuredSensors; + + public ICollection ConfiguredSensors + { + get => _configuredSensors; + set => this.RaiseAndSetIfChanged(ref _configuredSensors, value); + } - public ICollection ConfiguredSensors { get => configuredSensors; set => this.RaiseAndSetIfChanged(ref configuredSensors, value); } public void TriggerUpdate() { this.RaisePropertyChanged(); @@ -21,30 +26,21 @@ namespace UserInterface.ViewModels private string _value; public Guid Id { get; set; } - public string Type { get; set; } + public AvailableSensors Type { get; set; } public string Name { get; set; } public int UpdateInterval { get; set; } public string Value { - get => _value; set + get => _value; + set { this.RaiseAndSetIfChanged(ref _value, value); - this.RaisePropertyChanged("ValueString"); + this.RaisePropertyChanged(nameof(ValueString)); } } + public string UnitOfMeasurement { get; set; } - public string ValueString - { - get - { - if (!string.IsNullOrWhiteSpace(_value)) - { - return _value + " " + UnitOfMeasurement; - } - else return ""; - - } - } + public string ValueString => string.IsNullOrWhiteSpace(_value) ? string.Empty : $"{_value} {UnitOfMeasurement}"; } -} +} \ No newline at end of file diff --git a/UserInterface/Views/AddCommandDialog.axaml b/UserInterface/Views/AddCommandDialog.axaml index dc2e967..30bdcf7 100644 --- a/UserInterface/Views/AddCommandDialog.axaml +++ b/UserInterface/Views/AddCommandDialog.axaml @@ -5,24 +5,26 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="UserInterface.Views.AddCommandDialog" SizeToContent="WidthAndHeight" - Title="Add command"> - - Command type + Title="Add / edit command"> + + Command type - - - - Name - + + + + Name + - - Command - - Key - - - - + + Command + + Key + + + + + + diff --git a/UserInterface/Views/AddCommandDialog.axaml.cs b/UserInterface/Views/AddCommandDialog.axaml.cs index a1ed877..dbda8a9 100644 --- a/UserInterface/Views/AddCommandDialog.axaml.cs +++ b/UserInterface/Views/AddCommandDialog.axaml.cs @@ -1,156 +1,193 @@ -using Avalonia; -using Avalonia.Controls; -using Avalonia.Interactivity; -using Avalonia.Markup.Xaml; -using hass_workstation_service.Communication.InterProcesCommunication.Models; -using hass_workstation_service.Communication.NamedPipe; -using JKang.IpcServiceFramework.Client; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Dynamic; -using System.Linq; -using System.Text.Json; -using UserInterface.Util; -using UserInterface.ViewModels; - -namespace UserInterface.Views -{ - public class AddCommandDialog : Window - { - private readonly IIpcClient client; - public ComboBox comboBox { get; set; } - public ComboBox detectionModecomboBox { get; set; } - public AddCommandDialog() - { - this.InitializeComponent(); - DataContext = new AddCommandViewModel(); - this.comboBox = this.FindControl("ComboBox"); - this.comboBox.Items = Enum.GetValues(typeof(AvailableCommands)).Cast().OrderBy(v => v.ToString()); - this.comboBox.SelectedIndex = 0; - - // register IPC clients - ServiceProvider serviceProvider = new ServiceCollection() - .AddNamedPipeIpcClient("addCommand", pipeName: "pipeinternal") - .BuildServiceProvider(); - - // resolve IPC client factory - IIpcClientFactory clientFactory = serviceProvider - .GetRequiredService>(); - - // create client - this.client = clientFactory.CreateClient("addCommand"); - } - - public async void Save(object sender, RoutedEventArgs args) - { - var item = ((AddCommandViewModel)this.DataContext); - 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(); - } - - public void ComboBoxClosed(object sender, SelectionChangedEventArgs args) - { - var item = ((AddCommandViewModel)this.DataContext); - switch (this.comboBox.SelectedItem) - { - case AvailableCommands.CustomCommand: - 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 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#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#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#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) - { - var item = ((AddCommandViewModel)this.DataContext); - BrowserUtil.OpenBrowser(item.MoreInfoLink); - } - - public void Test(object sender, RoutedEventArgs args) - { - var item = ((AddCommandViewModel)this.DataContext); - - System.Diagnostics.Process process = new System.Diagnostics.Process(); - System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); - startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; - startInfo.FileName = "cmd.exe"; - startInfo.Arguments = $"/k {"echo You won't see this window normally. &&" + item.Command}"; - process.StartInfo = startInfo; - process.Start(); - } - - private void InitializeComponent() - { - AvaloniaXamlLoader.Load(this); - } - } -} +using Avalonia; +using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Markup.Xaml; +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using hass_workstation_service.Communication.NamedPipe; +using JKang.IpcServiceFramework.Client; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Linq; +using System.Text.Json; +using UserInterface.Util; +using UserInterface.ViewModels; + +namespace UserInterface.Views +{ + public class AddCommandDialog : Window + { + private readonly IIpcClient _client; + public ComboBox ComboBox { get; set; } + public ComboBox DetectionModecomboBox { get; set; } + public Guid CommandId { get; } + + public AddCommandDialog(Guid commandId) : this() + { + CommandId = commandId; + GetCommandInfo(CommandId); + Title = "Edit command"; + } + + public AddCommandDialog() + { + InitializeComponent(); + DataContext = new AddCommandViewModel(); + ComboBox = this.FindControl("ComboBox"); + ComboBox.Items = Enum.GetValues(typeof(AvailableCommands)).Cast().OrderBy(v => v.ToString()); + ComboBox.SelectedIndex = 0; + + // register IPC clients + ServiceProvider serviceProvider = new ServiceCollection() + .AddNamedPipeIpcClient("addCommand", pipeName: "pipeinternal") + .BuildServiceProvider(); + + // resolve IPC client factory + IIpcClientFactory clientFactory = serviceProvider + .GetRequiredService>(); + + // create client + _client = clientFactory.CreateClient("addCommand"); + Title = "Add command"; + + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + + private async void GetCommandInfo(Guid commandId) + { + var command = await _client.InvokeAsync(x => x.GetConfiguredCommand(commandId)); + + ComboBox.SelectedItem = command.Type; + FillDefaultValues(); + ComboBox.IsEnabled = false; + var item = (AddCommandViewModel)DataContext; + item.SelectedType = command.Type; + item.Name = command.Name; + item.Command = command.Command; + item.Key = command.Key; + + } + + public async void Save(object sender, RoutedEventArgs args) + { + var item = (AddCommandViewModel)DataContext; + dynamic model = new { item.Name, item.Command, item.Key }; + string json = JsonSerializer.Serialize(model); + if (CommandId == Guid.Empty) + await _client.InvokeAsync(x => x.AddCommand(item.SelectedType, json)); + else + await _client.InvokeAsync(x => x.UpdateCommandById(CommandId, json)); + + Close(); + } + + public void ComboBoxClosed(object sender, SelectionChangedEventArgs args) + { + FillDefaultValues(); + } + + private void FillDefaultValues() + { + var item = (AddCommandViewModel)DataContext; + switch (ComboBox.SelectedItem) + { + case AvailableCommands.CustomCommand: + 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 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#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#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#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) + { + var item = (AddCommandViewModel)DataContext; + BrowserUtil.OpenBrowser(item.MoreInfoLink); + } + + public void Test(object sender, RoutedEventArgs args) + { + var item = (AddCommandViewModel)DataContext; + + var process = new System.Diagnostics.Process(); + var startInfo = new System.Diagnostics.ProcessStartInfo + { + WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal, + FileName = "cmd.exe", + Arguments = $"/k {"echo You won't see this window normally. &&" + item.Command}" + }; + process.StartInfo = startInfo; + process.Start(); + } + } +} \ No newline at end of file diff --git a/UserInterface/Views/AddSensorDialog.axaml b/UserInterface/Views/AddSensorDialog.axaml index 10d62ec..8b3b295 100644 --- a/UserInterface/Views/AddSensorDialog.axaml +++ b/UserInterface/Views/AddSensorDialog.axaml @@ -5,27 +5,27 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="UserInterface.Views.AddSensorDialog" SizeToContent="WidthAndHeight" - Title="Add sensor"> - - Sensor type + Title="Add / edit sensor"> + + Sensor type - - - - Name - + + + + Name + - Update interval - - - - - - Query - - Window name - This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium". - + Update interval + + + + + + Query + + Window name + This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium". + - + diff --git a/UserInterface/Views/AddSensorDialog.axaml.cs b/UserInterface/Views/AddSensorDialog.axaml.cs index 8c12a81..de6da7e 100644 --- a/UserInterface/Views/AddSensorDialog.axaml.cs +++ b/UserInterface/Views/AddSensorDialog.axaml.cs @@ -1,195 +1,222 @@ -using Avalonia; -using Avalonia.Controls; -using Avalonia.Interactivity; -using Avalonia.Markup.Xaml; -using hass_workstation_service.Communication.InterProcesCommunication.Models; -using hass_workstation_service.Communication.NamedPipe; -using JKang.IpcServiceFramework.Client; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Dynamic; -using System.Linq; -using System.Text.Json; -using UserInterface.Util; -using UserInterface.ViewModels; - -namespace UserInterface.Views -{ - public class AddSensorDialog : Window - { - private readonly IIpcClient client; - public ComboBox comboBox { get; set; } - public ComboBox detectionModecomboBox { get; set; } - public AddSensorDialog() - { - this.InitializeComponent(); - DataContext = new AddSensorViewModel(); - this.comboBox = this.FindControl("ComboBox"); - this.comboBox.Items = Enum.GetValues(typeof(AvailableSensors)).Cast().OrderBy(v => v.ToString()); - this.comboBox.SelectedIndex = 0; - - // register IPC clients - ServiceProvider serviceProvider = new ServiceCollection() - .AddNamedPipeIpcClient("addsensor", pipeName: "pipeinternal") - .BuildServiceProvider(); - - // resolve IPC client factory - IIpcClientFactory clientFactory = serviceProvider - .GetRequiredService>(); - - // create client - this.client = clientFactory.CreateClient("addsensor"); - } - - public async void Save(object sender, RoutedEventArgs args) - { - var item = ((AddSensorViewModel)this.DataContext); - dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName}; - string json = JsonSerializer.Serialize(model); - await this.client.InvokeAsync(x => x.AddSensor(item.SelectedType, json)); - Close(); - } - - public void ComboBoxClosed(object sender, SelectionChangedEventArgs args) - { - var item = ((AddSensorViewModel)this.DataContext); - switch (this.comboBox.SelectedItem) - { - case AvailableSensors.UserNotificationStateSensor: - item.Description = "This sensor watches the UserNotificationState. This is normally used in applications to determine if it is appropriate to send a notification but we can use it to expose this state. \n "; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#usernotificationstate"; - item.ShowDetectionModeOptions = false; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - case AvailableSensors.DummySensor: - item.Description = "This sensor spits out a random number every second. Useful for testing, maybe you'll find some other use for it."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#dummy"; - item.ShowDetectionModeOptions = false; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 1; - break; - case AvailableSensors.CPULoadSensor: - item.Description = "This sensor checks the current CPU load. It averages the load on all logical cores every second and rounds the output to two decimals."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#cpuload"; - item.ShowDetectionModeOptions = false; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - case AvailableSensors.CurrentClockSpeedSensor: - item.Description = "This sensor returns the BIOS configured baseclock for the processor."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#currentclockspeed"; - item.ShowDetectionModeOptions = false; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 3600; - break; - case AvailableSensors.WMIQuerySensor: - item.Description = "This advanced sensor executes a user defined WMI query and exposes the result. The query should return a single value."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#wmiquerysensor"; - item.ShowDetectionModeOptions = false; - item.ShowQueryInput = true; - item.ShowWindowNameInput = false; - item.UpdateInterval = 10; - break; - case AvailableSensors.MemoryUsageSensor: - item.Description = "This sensor calculates the percentage of used memory."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#usedmemory"; - item.ShowDetectionModeOptions = false; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 10; - break; - case AvailableSensors.ActiveWindowSensor: - item.Description = "This sensor exposes the name of the currently active window."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#activewindow"; - item.ShowDetectionModeOptions = false; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - case AvailableSensors.WebcamActiveSensor: - item.Description = "This sensor shows if the webcam is currently being used."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#webcamactive"; - item.ShowDetectionModeOptions = true; - item.ShowQueryInput = false; - item.UpdateInterval = 10; - break; - case AvailableSensors.MicrophoneActiveSensor: - item.Description = "This sensor shows if the microphone is currently in use."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#microphoneactive"; - item.ShowDetectionModeOptions = false; - item.ShowQueryInput = false; - item.UpdateInterval = 10; - break; - case AvailableSensors.NamedWindowSensor: - item.Description = "This sensor returns true if a window was found with the name you search for. "; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#namedwindow"; - item.ShowQueryInput = false; - item.ShowWindowNameInput = true; - item.UpdateInterval = 5; - break; - case AvailableSensors.LastActiveSensor: - item.Description = "This sensor returns the date/time that the workstation was last active."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#lastactive"; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - case AvailableSensors.LastBootSensor: - item.Description = "This sensor returns the date/time that Windows was last booted"; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#lastboot"; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - case AvailableSensors.SessionStateSensor: - item.Description = "This sensor returns the state of the Windows session."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#sessionstate"; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - case AvailableSensors.CurrentVolumeSensor: - item.Description = "This sensor returns the volume of currently playing audio."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#currentvolume"; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - case AvailableSensors.GPUTemperatureSensor: - item.Description = "This sensor returns the current temperature of the GPU in °C."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#gputemperature"; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - case AvailableSensors.GPULoadSensor: - item.Description = "This sensor returns the current GPU load."; - item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#gpuload"; - item.ShowQueryInput = false; - item.ShowWindowNameInput = false; - item.UpdateInterval = 5; - break; - default: - item.Description = null; - item.MoreInfoLink = null; - item.ShowQueryInput = false; - break; - } - } - public void OpenInfo(object sender, RoutedEventArgs args) - { - var item = ((AddSensorViewModel)this.DataContext); - BrowserUtil.OpenBrowser(item.MoreInfoLink); - } - - private void InitializeComponent() - { - AvaloniaXamlLoader.Load(this); - } - } -} +using Avalonia; +using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Markup.Xaml; +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using hass_workstation_service.Communication.NamedPipe; +using JKang.IpcServiceFramework.Client; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Linq; +using System.Text.Json; +using UserInterface.Util; +using UserInterface.ViewModels; + +namespace UserInterface.Views +{ + public class AddSensorDialog : Window + { + private readonly IIpcClient _client; + public ComboBox ComboBox { get; set; } + public ComboBox DetectionModecomboBox { get; set; } + public Guid SensorId { get; } + + public AddSensorDialog(Guid sensorId) : this() + { + SensorId = sensorId; + GetSensorInfo(SensorId); + Title = "Edit sensor"; + } + + public AddSensorDialog() + { + InitializeComponent(); + DataContext = new AddSensorViewModel(); + ComboBox = this.FindControl("ComboBox"); + ComboBox.Items = Enum.GetValues(typeof(AvailableSensors)).Cast().OrderBy(v => v.ToString()); + ComboBox.SelectedIndex = 0; + + // register IPC clients + ServiceProvider serviceProvider = new ServiceCollection() + .AddNamedPipeIpcClient("addsensor", pipeName: "pipeinternal") + .BuildServiceProvider(); + + // resolve IPC client factory + IIpcClientFactory clientFactory = serviceProvider + .GetRequiredService>(); + + // create client + _client = clientFactory.CreateClient("addsensor"); + Title = "Add sensor"; + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + + private async void GetSensorInfo(Guid sensorId) + { + ConfiguredSensorModel sensor = await _client.InvokeAsync(x => x.GetConfiguredSensor(sensorId)); + + ComboBox.SelectedItem = sensor.Type; + FillDefaultValues(); + ComboBox.IsEnabled = false; + var item = (AddSensorViewModel)DataContext; + item.SelectedType = sensor.Type; + item.Name = sensor.Name; + item.UpdateInterval = sensor.UpdateInterval; + item.Query = sensor.Query; + item.WindowName = sensor.WindowName; + + Title = $"Edit {sensor.Name}"; + } + + public async void Save(object sender, RoutedEventArgs args) + { + var item = (AddSensorViewModel)DataContext; + dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName }; + string json = JsonSerializer.Serialize(model); + if (SensorId == Guid.Empty) + await _client.InvokeAsync(x => x.AddSensor(item.SelectedType, json)); + else + await _client.InvokeAsync(x => x.UpdateSensorById(SensorId, json)); + + Close(); + } + + public void ComboBoxClosed(object sender, SelectionChangedEventArgs args) + { + FillDefaultValues(); + } + + private void FillDefaultValues() + { + var item = (AddSensorViewModel)DataContext; + switch (ComboBox.SelectedItem) + { + case AvailableSensors.UserNotificationStateSensor: + item.Description = "This sensor watches the UserNotificationState. This is normally used in applications to determine if it is appropriate to send a notification but we can use it to expose this state. \n "; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#usernotificationstate"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + case AvailableSensors.DummySensor: + item.Description = "This sensor spits out a random number every second. Useful for testing, maybe you'll find some other use for it."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#dummy"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 1; + break; + case AvailableSensors.CPULoadSensor: + item.Description = "This sensor checks the current CPU load. It averages the load on all logical cores every second and rounds the output to two decimals."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#cpuload"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + case AvailableSensors.CurrentClockSpeedSensor: + item.Description = "This sensor returns the BIOS configured baseclock for the processor."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#currentclockspeed"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 3600; + break; + case AvailableSensors.WMIQuerySensor: + item.Description = "This advanced sensor executes a user defined WMI query and exposes the result. The query should return a single value."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#wmiquerysensor"; + item.ShowQueryInput = true; + item.ShowWindowNameInput = false; + item.UpdateInterval = 10; + break; + case AvailableSensors.MemoryUsageSensor: + item.Description = "This sensor calculates the percentage of used memory."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#usedmemory"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 10; + break; + case AvailableSensors.ActiveWindowSensor: + item.Description = "This sensor exposes the name of the currently active window."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#activewindow"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + case AvailableSensors.WebcamActiveSensor: + item.Description = "This sensor shows if the webcam is currently being used."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#webcamactive"; + item.ShowQueryInput = false; + item.UpdateInterval = 10; + break; + case AvailableSensors.MicrophoneActiveSensor: + item.Description = "This sensor shows if the microphone is currently in use."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#microphoneactive"; + item.ShowQueryInput = false; + item.UpdateInterval = 10; + break; + case AvailableSensors.NamedWindowSensor: + item.Description = "This sensor returns true if a window was found with the name you search for. "; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#namedwindow"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = true; + item.UpdateInterval = 5; + break; + case AvailableSensors.LastActiveSensor: + item.Description = "This sensor returns the date/time that the workstation was last active."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#lastactive"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + case AvailableSensors.LastBootSensor: + item.Description = "This sensor returns the date/time that Windows was last booted"; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#lastboot"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + case AvailableSensors.SessionStateSensor: + item.Description = "This sensor returns the state of the Windows session."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#sessionstate"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + case AvailableSensors.CurrentVolumeSensor: + item.Description = "This sensor returns the volume of currently playing audio."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#currentvolume"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + case AvailableSensors.GPUTemperatureSensor: + item.Description = "This sensor returns the current temperature of the GPU in °C."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#gputemperature"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + case AvailableSensors.GPULoadSensor: + item.Description = "This sensor returns the current GPU load."; + item.MoreInfoLink = "https://github.com/sleevezipper/hass-workstation-service#gpuload"; + item.ShowQueryInput = false; + item.ShowWindowNameInput = false; + item.UpdateInterval = 5; + break; + default: + item.Description = null; + item.MoreInfoLink = null; + item.ShowQueryInput = false; + break; + } + } + + public void OpenInfo(object sender, RoutedEventArgs args) + { + var item = (AddSensorViewModel)DataContext; + BrowserUtil.OpenBrowser(item.MoreInfoLink); + } + } +} \ No newline at end of file diff --git a/UserInterface/Views/AppInfo.axaml.cs b/UserInterface/Views/AppInfo.axaml.cs index 388d13c..ed0b184 100644 --- a/UserInterface/Views/AppInfo.axaml.cs +++ b/UserInterface/Views/AppInfo.axaml.cs @@ -16,19 +16,19 @@ namespace UserInterface.Views { public class AppInfo : UserControl { - private readonly IIpcClient client; + private readonly IIpcClient client; public AppInfo() { this.InitializeComponent(); // register IPC clients ServiceProvider serviceProvider = new ServiceCollection() - .AddNamedPipeIpcClient("info", pipeName: "pipeinternal") + .AddNamedPipeIpcClient("info", pipeName: "pipeinternal") .BuildServiceProvider(); // resolve IPC client factory - IIpcClientFactory clientFactory = serviceProvider - .GetRequiredService>(); + IIpcClientFactory clientFactory = serviceProvider + .GetRequiredService>(); // create client this.client = clientFactory.CreateClient("info"); diff --git a/UserInterface/Views/BackgroundServiceSettings.axaml.cs b/UserInterface/Views/BackgroundServiceSettings.axaml.cs index 23588c9..f2b518f 100644 --- a/UserInterface/Views/BackgroundServiceSettings.axaml.cs +++ b/UserInterface/Views/BackgroundServiceSettings.axaml.cs @@ -15,19 +15,19 @@ namespace UserInterface.Views { public class BackgroundServiceSettings : UserControl { - private readonly IIpcClient _client; + private readonly IIpcClient _client; public BackgroundServiceSettings() { this.InitializeComponent(); // register IPC clients ServiceProvider serviceProvider = new ServiceCollection() - .AddNamedPipeIpcClient("broker", pipeName: "pipeinternal") + .AddNamedPipeIpcClient("broker", pipeName: "pipeinternal") .BuildServiceProvider(); // resolve IPC client factory - IIpcClientFactory clientFactory = serviceProvider - .GetRequiredService>(); + IIpcClientFactory clientFactory = serviceProvider + .GetRequiredService>(); // create client this._client = clientFactory.CreateClient("broker"); diff --git a/UserInterface/Views/BrokerSettings.axaml.cs b/UserInterface/Views/BrokerSettings.axaml.cs index 3e59837..e870a80 100644 --- a/UserInterface/Views/BrokerSettings.axaml.cs +++ b/UserInterface/Views/BrokerSettings.axaml.cs @@ -17,19 +17,19 @@ namespace UserInterface.Views { public class BrokerSettings : UserControl { - private readonly IIpcClient client; + private readonly IIpcClient client; public BrokerSettings() { this.InitializeComponent(); // register IPC clients ServiceProvider serviceProvider = new ServiceCollection() - .AddNamedPipeIpcClient("broker", pipeName: "pipeinternal") + .AddNamedPipeIpcClient("broker", pipeName: "pipeinternal") .BuildServiceProvider(); // resolve IPC client factory - IIpcClientFactory clientFactory = serviceProvider - .GetRequiredService>(); + IIpcClientFactory clientFactory = serviceProvider + .GetRequiredService>(); // create client this.client = clientFactory.CreateClient("broker"); diff --git a/UserInterface/Views/CommandSettings.axaml b/UserInterface/Views/CommandSettings.axaml index 6998808..0bc4aac 100644 --- a/UserInterface/Views/CommandSettings.axaml +++ b/UserInterface/Views/CommandSettings.axaml @@ -22,7 +22,7 @@