diff --git a/.vs/hass-workstation-service/v16/.suo b/.vs/hass-workstation-service/v16/.suo index 9700673..8d790af 100644 Binary files a/.vs/hass-workstation-service/v16/.suo and b/.vs/hass-workstation-service/v16/.suo differ diff --git a/UserInterface/ViewModels/BackgroundServiceSettingsViewModel.cs b/UserInterface/ViewModels/BackgroundServiceSettingsViewModel.cs index 267723a..e77fa1b 100644 --- a/UserInterface/ViewModels/BackgroundServiceSettingsViewModel.cs +++ b/UserInterface/ViewModels/BackgroundServiceSettingsViewModel.cs @@ -8,19 +8,26 @@ namespace UserInterface.ViewModels { public class BackgroundServiceSettingsViewModel : ViewModelBase { - private string host; - private string username; - private string password; private string message; private bool isRunning; + private bool isAutostartEnabled; - public bool IsRunning { get => isRunning; set => this.RaiseAndSetIfChanged(ref isRunning, value); } - public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); } + public bool IsAutoStartEnabled { + get => isAutostartEnabled; + private set => this.RaiseAndSetIfChanged(ref isAutostartEnabled, value); } + public bool IsRunning { get => isRunning; private set => this.RaiseAndSetIfChanged(ref isRunning, value); } + public string Message { get => message; private set => this.RaiseAndSetIfChanged(ref message, value); } public void UpdateStatus(bool isRunning, string message) { this.IsRunning = isRunning; this.Message = message; } + + public void UpdateAutostartStatus(bool isEnabled) + { + this.IsAutoStartEnabled = isEnabled; + } + } } diff --git a/UserInterface/Views/BackgroundServiceSettings.axaml b/UserInterface/Views/BackgroundServiceSettings.axaml index d69e803..4b7baed 100644 --- a/UserInterface/Views/BackgroundServiceSettings.axaml +++ b/UserInterface/Views/BackgroundServiceSettings.axaml @@ -4,11 +4,23 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450" x:Class="UserInterface.Views.BackgroundServiceSettings"> - - Background service - - + + + Background service + + - + + + + + + Autostart + + + + + + diff --git a/UserInterface/Views/BackgroundServiceSettings.axaml.cs b/UserInterface/Views/BackgroundServiceSettings.axaml.cs index cf0721f..046f362 100644 --- a/UserInterface/Views/BackgroundServiceSettings.axaml.cs +++ b/UserInterface/Views/BackgroundServiceSettings.axaml.cs @@ -55,9 +55,10 @@ namespace UserInterface.Views { ((BackgroundServiceSettingsViewModel)this.DataContext).UpdateStatus(false, "Not running"); } - - - + + var autostartresult = await this.client.InvokeAsync(x => x.IsAutoStartEnabled()); + ((BackgroundServiceSettingsViewModel)this.DataContext).UpdateAutostartStatus(autostartresult); + await Task.Delay(1000); } } @@ -68,6 +69,15 @@ namespace UserInterface.Views System.Diagnostics.Process.Start("hass-worstation-service.exe"); } + public void EnableAutostart(object sender, RoutedEventArgs args) + { + this.client.InvokeAsync(x => x.EnableAutostart(true)); + } + public void DisableAutostart(object sender, RoutedEventArgs args) + { + this.client.InvokeAsync(x => x.EnableAutostart(false)); + } + private void InitializeComponent() { AvaloniaXamlLoader.Load(this); diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 343a0d3..2c3414e 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -47,5 +47,15 @@ namespace hass_workstation_service.Communication.InterProcesCommunication { this._configurationService.WriteMqttBrokerSettingsAsync(settings); } + + public void EnableAutostart(bool enable) + { + this._configurationService.EnableAutoStart(enable); + } + + public bool IsAutoStartEnabled() + { + return this._configurationService.IsAutoStartEnabled(); + } } } diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractInterfaces.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractInterfaces.cs index de5500e..b686baa 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractInterfaces.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractInterfaces.cs @@ -12,5 +12,7 @@ namespace hass_workstation_service.Communication.NamedPipe public string Ping(string str); void WriteMqttBrokerSettingsAsync(MqttSettings settings); MqqtClientStatus GetMqqtClientStatus(); + void EnableAutostart(bool enable); + bool IsAutoStartEnabled(); } } diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index 6dbf2bf..df04c03 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.IO.IsolatedStorage; +using System.Linq; using System.Security; using System.Text.Json; using System.Threading.Tasks; @@ -10,6 +11,7 @@ using hass_workstation_service.Communication.InterProcesCommunication.Models; using hass_workstation_service.Communication.NamedPipe; using hass_workstation_service.Domain.Sensors; using Microsoft.Extensions.Configuration; +using Microsoft.Win32; using MQTTnet; using MQTTnet.Client; using MQTTnet.Client.Options; @@ -164,5 +166,38 @@ namespace hass_workstation_service.Data Password = broker?.Password }; } + + /// + /// Enable or disable autostarting the background service. It does this by adding the application shortcut (appref-ms) to the registry run key for the current user + /// + /// + public void EnableAutoStart(bool enable) + { + if (enable) + { + Log.Logger.Information("configuring autostart"); + // The path to the key where Windows looks for startup applications + RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); + + //Path to launch shortcut + string startPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + @"\hass-workstation-service\hass-workstation-service.appref-ms"; + + rkApp.SetValue("hass-workstation-service", startPath); + rkApp.Close(); + } + else + { + Log.Logger.Information("removing autostart"); + RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); + rkApp.DeleteValue("hass-workstation-service"); + rkApp.Close(); + } + } + + public bool IsAutoStartEnabled() + { + RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); + return rkApp.GetValue("hass-workstation-service") != null; + } } } \ No newline at end of file diff --git a/hass-workstation-service/Data/IConfigurationService.cs b/hass-workstation-service/Data/IConfigurationService.cs index 7bc98a1..9a8a69d 100644 --- a/hass-workstation-service/Data/IConfigurationService.cs +++ b/hass-workstation-service/Data/IConfigurationService.cs @@ -21,5 +21,7 @@ namespace hass_workstation_service.Data void WriteMqttBrokerSettingsAsync(MqttSettings settings); void WriteSettingsAsync(); Task GetMqttBrokerSettings(); + void EnableAutoStart(bool enable); + bool IsAutoStartEnabled(); } } \ No newline at end of file diff --git a/hass-workstation-service/Program.cs b/hass-workstation-service/Program.cs index 0d11ccd..6178520 100644 --- a/hass-workstation-service/Program.cs +++ b/hass-workstation-service/Program.cs @@ -37,26 +37,6 @@ namespace hass_workstation_service { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - if (args.Contains("--autostart=true")) - { - Log.Logger.Information("configuring autostart"); - // The path to the key where Windows looks for startup applications - RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); - - //Path to launch shortcut - string startPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + @"\hass-workstation-service\hass-workstation-service.appref-ms"; - - rkApp.SetValue("hass-workstation-service", startPath); - rkApp.Close(); - } - else if (args.Contains("--autostart=false")) - { - Log.Logger.Information("removing autostart"); - RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); - rkApp.DeleteSubKey("hass-workstation-service"); - rkApp.Close(); - } - await CreateHostBuilder(args).RunConsoleAsync(); }