implement autostart configuring

pull/9/head
sleevezipper 4 years ago
parent 9489e977b3
commit c4ac3a5932

@ -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;
}
}
}

@ -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">
<StackPanel Margin="30" HorizontalAlignment="Left">
<StackPanel Margin="30">
<StackPanel Margin="0 0 0 20" HorizontalAlignment="Left">
<ContentControl FontSize="18" FontWeight="Bold">Background service</ContentControl>
<TextBlock IsVisible="{Binding IsRunning}" Foreground="Green" Text="{Binding Message}"></TextBlock >
<TextBlock IsVisible="{Binding !IsRunning}" Foreground="Red" Text="{Binding Message}"></TextBlock >
<Button Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Start">Start</Button>
<Button IsVisible="{Binding !IsRunning}" Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Start">Start</Button>
</StackPanel>
<StackPanel HorizontalAlignment="Stretch">
<ContentControl FontSize="14" FontWeight="Bold">Autostart</ContentControl>
<TextBlock IsVisible="{Binding IsAutoStartEnabled}" Foreground="Green" Text="Autostart is enabled"></TextBlock >
<TextBlock IsVisible="{Binding !IsAutoStartEnabled}" Foreground="Red" Text="Autostart is NOT enabled"></TextBlock >
<Button IsVisible="{Binding !IsAutoStartEnabled}" Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="EnableAutostart">Enable</Button>
<Button IsVisible="{Binding IsAutoStartEnabled}" Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="DisableAutostart">Disable</Button>
</StackPanel>
</StackPanel>
</UserControl>

@ -56,7 +56,8 @@ 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);

@ -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();
}
}
}

@ -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();
}
}

@ -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
};
}
/// <summary>
/// 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
/// </summary>
/// <param name="enable"></param>
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;
}
}
}

@ -21,5 +21,7 @@ namespace hass_workstation_service.Data
void WriteMqttBrokerSettingsAsync(MqttSettings settings);
void WriteSettingsAsync();
Task<MqttSettings> GetMqttBrokerSettings();
void EnableAutoStart(bool enable);
bool IsAutoStartEnabled();
}
}

@ -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();
}

Loading…
Cancel
Save