From c4ac3a593299d465a953e720570216337c8f6f6e Mon Sep 17 00:00:00 2001 From: sleevezipper Date: Fri, 1 Jan 2021 12:12:59 +0100 Subject: [PATCH] implement autostart configuring --- .vs/hass-workstation-service/v16/.suo | Bin 231936 -> 239104 bytes .../BackgroundServiceSettingsViewModel.cs | 17 ++++++--- .../Views/BackgroundServiceSettings.axaml | 22 ++++++++--- .../Views/BackgroundServiceSettings.axaml.cs | 16 ++++++-- .../InterProcessApi.cs | 10 +++++ .../ServiceContractInterfaces.cs | 2 + .../Data/ConfigurationService.cs | 35 ++++++++++++++++++ .../Data/IConfigurationService.cs | 2 + hass-workstation-service/Program.cs | 20 ---------- 9 files changed, 91 insertions(+), 33 deletions(-) diff --git a/.vs/hass-workstation-service/v16/.suo b/.vs/hass-workstation-service/v16/.suo index 970067328a7adb1e766ad5361ea7b0298ab31206..8d790af541c4a292925a15cff9c275a12964a23e 100644 GIT binary patch delta 7583 zcmds64_K5{x}WoY0}e942&16js9-QEApiUu;D~5wh-CiDh(N%E5O5rbIrEVdMaHqYrF2=TTiL=WV_oRZ+G=xy(RZ|h5<3jyt_|b?>)os zJMTH?{m%QI_q^XZ?>W>p1=h6(ZcZJXtDuZ#h*FN?&sB1%a-zs)De)tG@5ha;6w`GOdDQ%2PoN9ALOIF^hD%Sfe2tUfM!oJ zf=&edfVdv@vhll4mX3{(Fx1;7>ykjH0YSho(3XsRJ}@0Xvu{U#5X1N`T~w_OR1OdV zWC9t0>{XBaPA@$I`Q2VR3wZ%h1WX3T0WpAlCmnepAT2cwm;v+!f`M$PA$g>Fktj-r&I(?h!ClLnHxG|mP)j)EB$?a?eR9odG*LzR?aVutfhy+Fseph z1~JkzWU?pO*f>{p(lg9OL#;m7^}(-tLsFo}HujeHJbL8lIn1rgeej?Kze^-tfLE zIo|{=zuM!-PXNCM+JX1bLukMsK)nzAGjN*s8}b{e`Kuny9pkAr+QxjwKx`?0EM}D@ z1(f`-WqJ}X$nIBrb16b9xDMs@fK*tvZ$SQxm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6NeoCG zGla=AC=iPmrS2-;vY6ZmXwu1-z3~z4H`+4L_fn+55unm`$1&npOiNls712q z=^u&owUVAC!SooY>POO}@W(MC!T%=UPZcbDL@?f#Kp$lKO)=804B9}h5sH4ZV(QMJ z;KtRs7tjdqNTg+Wk4xv8MyluU=FxO%U@|QsZVPtGqblAsp3dOqWi;P;6IDr_4YV=L zU5+m=r%JTI+vBNGGR~uK5$;_>%h-wjEU9@lU9RMX-E;!$*=*o1-iL=n!F}|IyA@xk zjHk<`i*iU*v>E1zAj69plU)fG|?1PqMXE$9fy|$B13g_8vSY6#sf2(3AV>8)M Ltl|5>-+})CsO 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(); }