diff --git a/.vs/hass-workstation-service/DesignTimeBuild/.dtbcache.v2 b/.vs/hass-workstation-service/DesignTimeBuild/.dtbcache.v2 index 4e4a2cb..27b853f 100644 Binary files a/.vs/hass-workstation-service/DesignTimeBuild/.dtbcache.v2 and b/.vs/hass-workstation-service/DesignTimeBuild/.dtbcache.v2 differ diff --git a/.vs/hass-workstation-service/v16/.suo b/.vs/hass-workstation-service/v16/.suo index fd6daf5..9700673 100644 Binary files a/.vs/hass-workstation-service/v16/.suo and b/.vs/hass-workstation-service/v16/.suo differ diff --git a/UserInterface/UserInterface.csproj b/UserInterface/UserInterface.csproj index 94e5121..100580f 100644 --- a/UserInterface/UserInterface.csproj +++ b/UserInterface/UserInterface.csproj @@ -21,7 +21,13 @@ - + + BackgroundServiceSettings.axaml + + + SensorSettings.axaml + + %(Filename) diff --git a/UserInterface/ViewModels/BackgroundServiceSettingsViewModel.cs b/UserInterface/ViewModels/BackgroundServiceSettingsViewModel.cs new file mode 100644 index 0000000..267723a --- /dev/null +++ b/UserInterface/ViewModels/BackgroundServiceSettingsViewModel.cs @@ -0,0 +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 BackgroundServiceSettingsViewModel : ViewModelBase + { + private string host; + private string username; + private string password; + private string message; + private bool isRunning; + + public bool IsRunning { get => isRunning; set => this.RaiseAndSetIfChanged(ref isRunning, value); } + public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); } + + public void UpdateStatus(bool isRunning, string message) + { + this.IsRunning = isRunning; + this.Message = message; + } + } +} diff --git a/UserInterface/ViewModels/BrokerSettingsViewModel.cs b/UserInterface/ViewModels/BrokerSettingsViewModel.cs index f57b81a..42e8ea6 100644 --- a/UserInterface/ViewModels/BrokerSettingsViewModel.cs +++ b/UserInterface/ViewModels/BrokerSettingsViewModel.cs @@ -1,4 +1,6 @@ -using System; +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using ReactiveUI; +using System; using System.Collections.Generic; using System.Text; @@ -6,8 +8,28 @@ namespace UserInterface.ViewModels { public class BrokerSettingsViewModel : ViewModelBase { - public string Host { get; set; } - public string Username { get; set; } - public string Password { get; set; } + private string host; + private string username; + private string password; + private string message; + private bool isConnected; + + public bool IsConnected { get => isConnected; set => this.RaiseAndSetIfChanged(ref isConnected, value); } + public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); } + public string Host { get => host; set => this.RaiseAndSetIfChanged(ref host, value); } + public string Username { get => username; set => this.RaiseAndSetIfChanged(ref username, value); } + public string Password { get => password; set => this.RaiseAndSetIfChanged(ref password, value); } + public void Update(MqttSettings settings) + { + this.Host = settings.Host; + this.Username = settings.Username; + this.Password = settings.Password; + } + + public void UpdateStatus(MqqtClientStatus status) + { + this.IsConnected = status.IsConnected; + this.Message = status.Message; + } } } diff --git a/UserInterface/Views/BackgroundServiceSettings.axaml b/UserInterface/Views/BackgroundServiceSettings.axaml new file mode 100644 index 0000000..d69e803 --- /dev/null +++ b/UserInterface/Views/BackgroundServiceSettings.axaml @@ -0,0 +1,14 @@ + + + Background service + + + + + + diff --git a/UserInterface/Views/BackgroundServiceSettings.axaml.cs b/UserInterface/Views/BackgroundServiceSettings.axaml.cs new file mode 100644 index 0000000..cf0721f --- /dev/null +++ b/UserInterface/Views/BackgroundServiceSettings.axaml.cs @@ -0,0 +1,76 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using Microsoft.Extensions.DependencyInjection; +using hass_workstation_service.Communication.NamedPipe; +using JKang.IpcServiceFramework.Client; +using System.Threading.Tasks; +using Avalonia.Interactivity; +using System.Reactive.Linq; +using UserInterface.ViewModels; +using System.Security; +using hass_workstation_service.Communication.InterProcesCommunication.Models; + +namespace UserInterface.Views +{ + public class BackgroundServiceSettings : UserControl + { + private readonly IIpcClient client; + + public BackgroundServiceSettings() + { + this.InitializeComponent(); + // register IPC clients + ServiceProvider serviceProvider = new ServiceCollection() + .AddNamedPipeIpcClient("broker", pipeName: "pipeinternal") + .BuildServiceProvider(); + + // resolve IPC client factory + IIpcClientFactory clientFactory = serviceProvider + .GetRequiredService>(); + + // create client + this.client = clientFactory.CreateClient("broker"); + + + DataContext = new BackgroundServiceSettingsViewModel(); + Ping(); + } + public async void Ping() { + while (true) + { + try + { + var result = await this.client.InvokeAsync(x => x.Ping("ping")); + if (result == "pong") + { + ((BackgroundServiceSettingsViewModel)this.DataContext).UpdateStatus(true, "All good"); + } + else + { + ((BackgroundServiceSettingsViewModel)this.DataContext).UpdateStatus(false, "Not running"); + } + } + catch (System.Exception) + { + ((BackgroundServiceSettingsViewModel)this.DataContext).UpdateStatus(false, "Not running"); + } + + + + await Task.Delay(1000); + } + } + + public void Start(object sender, RoutedEventArgs args) + { + //TODO: fix the path. This will depend on the deployment structure. + System.Diagnostics.Process.Start("hass-worstation-service.exe"); + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + } +} diff --git a/UserInterface/Views/BrokerSettings.axaml b/UserInterface/Views/BrokerSettings.axaml new file mode 100644 index 0000000..dfea2d9 --- /dev/null +++ b/UserInterface/Views/BrokerSettings.axaml @@ -0,0 +1,19 @@ + + + Mqtt broker + + + IP or hostname + + Username + + Password + + + + diff --git a/UserInterface/Views/BrokerSettings.axaml.cs b/UserInterface/Views/BrokerSettings.axaml.cs new file mode 100644 index 0000000..1d80121 --- /dev/null +++ b/UserInterface/Views/BrokerSettings.axaml.cs @@ -0,0 +1,82 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using Microsoft.Extensions.DependencyInjection; +using hass_workstation_service.Communication.NamedPipe; +using JKang.IpcServiceFramework.Client; +using System.Threading.Tasks; +using Avalonia.Interactivity; +using System.Reactive.Linq; +using UserInterface.ViewModels; +using System.Security; +using hass_workstation_service.Communication.InterProcesCommunication.Models; + +namespace UserInterface.Views +{ + public class BrokerSettings : UserControl + { + private readonly IIpcClient client; + + public BrokerSettings() + { + this.InitializeComponent(); + // register IPC clients + ServiceProvider serviceProvider = new ServiceCollection() + .AddNamedPipeIpcClient("broker", pipeName: "pipeinternal") + .BuildServiceProvider(); + + // resolve IPC client factory + IIpcClientFactory clientFactory = serviceProvider + .GetRequiredService>(); + + // create client + this.client = clientFactory.CreateClient("broker"); + + + DataContext = new BrokerSettingsViewModel(); + GetSettings(); + GetStatus(); + + } + public void Ping(object sender, RoutedEventArgs args) { + var result = this.client.InvokeAsync(x => x.Ping("ping")).Result; + } + + public void Configure(object sender, RoutedEventArgs args) + { + var model = (BrokerSettingsViewModel)this.DataContext; + var result = this.client.InvokeAsync(x => x.WriteMqttBrokerSettingsAsync(new MqttSettings() { Host = model.Host, Username = model.Username, Password = model.Password })); + } + + public async void GetSettings() + { + MqttSettings settings = await this.client.InvokeAsync(x => x.GetMqttBrokerSettings()); + ((BrokerSettingsViewModel)this.DataContext).Update(settings); + } + + public async void GetStatus() + { + while (true) + { + try + { + MqqtClientStatus status = await this.client.InvokeAsync(x => x.GetMqqtClientStatus()); + + ((BrokerSettingsViewModel)this.DataContext).UpdateStatus(status); + await Task.Delay(1000); + } + catch (System.Exception) + { + ((BrokerSettingsViewModel)this.DataContext).UpdateStatus(new MqqtClientStatus() {IsConnected = false, Message = "Unable to get connectionstatus" }); + } + + } + } + + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + } +} diff --git a/UserInterface/Views/MainWindow.axaml b/UserInterface/Views/MainWindow.axaml index f154da4..c2123af 100644 --- a/UserInterface/Views/MainWindow.axaml +++ b/UserInterface/Views/MainWindow.axaml @@ -4,16 +4,30 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:views="clr-namespace:UserInterface.Views" - mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" + mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="450" x:Class="UserInterface.Views.MainWindow" Icon="/Assets/hass-workstation-logo.ico" - Title="UserInterface"> + SizeToContent="WidthAndHeight" + Title="Settings" + Background="LightGray"> + - + + + + + + + diff --git a/UserInterface/Views/BrokerSettings/BrokerSettings.axaml b/UserInterface/Views/SensorSettings.axaml similarity index 69% rename from UserInterface/Views/BrokerSettings/BrokerSettings.axaml rename to UserInterface/Views/SensorSettings.axaml index 110b651..328e5f9 100644 --- a/UserInterface/Views/BrokerSettings/BrokerSettings.axaml +++ b/UserInterface/Views/SensorSettings.axaml @@ -2,10 +2,13 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" - x:Class="UserInterface.Views.BrokerSettings"> - + mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450" + MaxWidth="500" + x:Class="UserInterface.Views.SensorSettings"> + Mqtt broker + + IP or hostname Username diff --git a/UserInterface/Views/BrokerSettings/BrokerSettings.axaml.cs b/UserInterface/Views/SensorSettings.axaml.cs similarity index 77% rename from UserInterface/Views/BrokerSettings/BrokerSettings.axaml.cs rename to UserInterface/Views/SensorSettings.axaml.cs index 75ff0f3..2a2e62c 100644 --- a/UserInterface/Views/BrokerSettings/BrokerSettings.axaml.cs +++ b/UserInterface/Views/SensorSettings.axaml.cs @@ -9,22 +9,20 @@ using Avalonia.Interactivity; using System.Reactive.Linq; using UserInterface.ViewModels; using System.Security; +using hass_workstation_service.Communication.InterProcesCommunication.Models; namespace UserInterface.Views { - public class BrokerSettings : UserControl + public class SensorSettings : UserControl { private readonly IIpcClient client; - private string _host { get; set; } - private string _username { get; set; } - private string _password { get; set; } - public BrokerSettings() + + public SensorSettings() { - DataContext = new BrokerSettingsViewModel(); this.InitializeComponent(); - // register IPC clients + // register IPC clients ServiceProvider serviceProvider = new ServiceCollection() - .AddNamedPipeIpcClient("client1", pipeName: "pipeinternal") + .AddNamedPipeIpcClient("sensors", pipeName: "pipeinternal") .BuildServiceProvider(); // resolve IPC client factory @@ -32,7 +30,10 @@ namespace UserInterface.Views .GetRequiredService>(); // create client - this.client = clientFactory.CreateClient("client1"); + this.client = clientFactory.CreateClient("sensors"); + + + DataContext = new BrokerSettingsViewModel(); } public void Ping(object sender, RoutedEventArgs args) { @@ -42,12 +43,10 @@ namespace UserInterface.Views public void Configure(object sender, RoutedEventArgs args) { var model = (BrokerSettingsViewModel)this.DataContext; - var result = this.client.InvokeAsync(x => x.WriteMqttBrokerSettings(model.Host, model.Username, model.Password)); + var result = this.client.InvokeAsync(x => x.WriteMqttBrokerSettingsAsync(new MqttSettings() { Host = model.Host, Username = model.Username, Password = model.Password })); } - - private void InitializeComponent() { AvaloniaXamlLoader.Load(this); diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs new file mode 100644 index 0000000..343a0d3 --- /dev/null +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -0,0 +1,51 @@ +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using hass_workstation_service.Communication.NamedPipe; +using hass_workstation_service.Data; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace hass_workstation_service.Communication.InterProcesCommunication +{ + public class InterProcessApi : ServiceContractInterfaces + { + private readonly MqttPublisher _publisher; + private readonly IConfigurationService _configurationService; + + public InterProcessApi(MqttPublisher publisher, IConfigurationService configurationService) + { + _publisher = publisher; + _configurationService = configurationService; + } + + public MqqtClientStatus GetMqqtClientStatus() + { + return this._publisher.GetStatus(); + } + + public Task GetMqttBrokerSettings() + { + return this._configurationService.GetMqttBrokerSettings(); + } + + /// + /// You can use this to check if the application responds. + /// + /// + /// + public string Ping(string str) + { + if (str == "ping") + { + return "pong"; + } + return "what?"; + } + + public void WriteMqttBrokerSettingsAsync(MqttSettings settings) + { + this._configurationService.WriteMqttBrokerSettingsAsync(settings); + } + } +} diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractInterfaces.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractInterfaces.cs index 8750f31..de5500e 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractInterfaces.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractInterfaces.cs @@ -1,12 +1,16 @@ -using System; +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace hass_workstation_service.Communication.NamedPipe { public interface ServiceContractInterfaces { + Task GetMqttBrokerSettings(); public string Ping(string str); - void WriteMqttBrokerSettings(string host, string username, string password); + void WriteMqttBrokerSettingsAsync(MqttSettings settings); + MqqtClientStatus GetMqqtClientStatus(); } } diff --git a/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs new file mode 100644 index 0000000..a39663d --- /dev/null +++ b/hass-workstation-service/Communication/InterProcesCommunication/ServiceContractModels.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hass_workstation_service.Communication.InterProcesCommunication.Models +{ + public class MqttSettings + { + public string Host { get; set; } + public string Username { get; set; } + public string Password { get; set; } + } + + public class MqqtClientStatus + { + public bool IsConnected { get; set; } + public string Message { get; set; } + } +} diff --git a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs index 2b1dd92..4ae21fb 100644 --- a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs +++ b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs @@ -2,6 +2,7 @@ using System; using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using hass_workstation_service.Communication.InterProcesCommunication.Models; using hass_workstation_service.Communication.Util; using hass_workstation_service.Data; using Microsoft.Extensions.Logging; @@ -18,6 +19,7 @@ namespace hass_workstation_service.Communication private readonly IMqttClient _mqttClient; private readonly ILogger _logger; private readonly IConfigurationService _configurationService; + private string _mqttClientMessage { get; set; } public DateTime LastConfigAnnounce { get; private set; } public DeviceConfigModel DeviceConfigModel { get; private set; } public bool IsConnected @@ -45,16 +47,22 @@ namespace hass_workstation_service.Communication this.DeviceConfigModel = deviceConfigModel; this._configurationService = configurationService; - var options = _configurationService.ReadMqttSettings().Result; + var options = _configurationService.GetMqttClientOptionsAsync().Result; _configurationService.MqqtConfigChangedHandler = this.ReplaceMqttClient; var factory = new MqttFactory(); this._mqttClient = factory.CreateMqttClient(); this._mqttClient.ConnectAsync(options); + + this._mqttClient.UseConnectedHandler(e => { + this._mqttClientMessage = "All good"; + }); + // configure what happens on disconnect this._mqttClient.UseDisconnectedHandler(async e => { + this._mqttClientMessage = e.ReasonCode.ToString(); if (e.ReasonCode != MQTTnet.Client.Disconnecting.MqttClientDisconnectReason.NormalDisconnection) { _logger.LogWarning("Disconnected from server"); @@ -115,13 +123,15 @@ namespace hass_workstation_service.Communication } catch (Exception ex) { + this._mqttClientMessage = ex.Message; Log.Logger.Error("Could not connect to broker: " + ex.Message); } - finally - { - Log.Logger.Information("Connected to new broker"); - } } + + public MqqtClientStatus GetStatus() + { + return new MqqtClientStatus() { IsConnected = _mqttClient.IsConnected, Message = _mqttClientMessage }; + } } } diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index dcb4f0b..6dbf2bf 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -6,6 +6,7 @@ using System.Security; using System.Text.Json; using System.Threading.Tasks; using hass_workstation_service.Communication; +using hass_workstation_service.Communication.InterProcesCommunication.Models; using hass_workstation_service.Communication.NamedPipe; using hass_workstation_service.Domain.Sensors; using Microsoft.Extensions.Configuration; @@ -16,7 +17,7 @@ using Serilog; namespace hass_workstation_service.Data { - public class ConfigurationService : ServiceContractInterfaces, IConfigurationService + public class ConfigurationService : IConfigurationService { public ICollection ConfiguredSensors { get; private set; } public Action MqqtConfigChangedHandler { get; set; } @@ -32,14 +33,16 @@ namespace hass_workstation_service.Data public async void ReadSensorSettings(MqttPublisher publisher) { - IsolatedStorageFileStream stream = this._fileStorage.OpenFile("configured-sensors.json", FileMode.OpenOrCreate); - Log.Logger.Information($"reading configured sensors from: {stream.Name}"); List sensors = new List(); - if (stream.Length > 0) + using (var stream = this._fileStorage.OpenFile("configured-sensors.json", FileMode.OpenOrCreate)) { - sensors = await JsonSerializer.DeserializeAsync>(stream); + Log.Logger.Information($"reading configured sensors from: {stream.Name}"); + if (stream.Length > 0) + { + sensors = await JsonSerializer.DeserializeAsync>(stream); + } } - stream.Close(); + foreach (ConfiguredSensor configuredSensor in sensors) { AbstractSensor sensor; @@ -58,16 +61,9 @@ namespace hass_workstation_service.Data } } - public async Task ReadMqttSettings() + public async Task GetMqttClientOptionsAsync() { - IsolatedStorageFileStream stream = this._fileStorage.OpenFile("mqttbroker.json", FileMode.OpenOrCreate); - Log.Logger.Information($"reading configured mqttbroker from: {stream.Name}"); - ConfiguredMqttBroker configuredBroker = null; - if (stream.Length > 0) - { - configuredBroker = await JsonSerializer.DeserializeAsync(stream); - } - stream.Close(); + ConfiguredMqttBroker configuredBroker = await ReadMqttSettingsAsync(); if (configuredBroker != null && configuredBroker.Host != null) { @@ -89,64 +85,84 @@ namespace hass_workstation_service.Data } } - public async void WriteSettings() + /// + /// Gets the saved broker settings from configfile. Null if not found. + /// + /// + public async Task ReadMqttSettingsAsync() { - IsolatedStorageFileStream stream = this._fileStorage.OpenFile("configured-sensors.json", FileMode.OpenOrCreate); - Log.Logger.Information($"writing configured sensors to: {stream.Name}"); - List configuredSensorsToSave = new List(); - - foreach (AbstractSensor sensor in this.ConfiguredSensors) + ConfiguredMqttBroker configuredBroker = null; + using (IsolatedStorageFileStream stream = this._fileStorage.OpenFile("mqttbroker.json", FileMode.OpenOrCreate)) { - configuredSensorsToSave.Add(new ConfiguredSensor() { Id = sensor.Id, Name = sensor.Name, Type = sensor.GetType().Name }); + Log.Logger.Information($"reading configured mqttbroker from: {stream.Name}"); + if (stream.Length > 0) + { + configuredBroker = await JsonSerializer.DeserializeAsync(stream); + } } - await JsonSerializer.SerializeAsync(stream, configuredSensorsToSave); - stream.Close(); + return configuredBroker; + } + + public async void WriteSettingsAsync() + { + List configuredSensorsToSave = new List(); + using (IsolatedStorageFileStream stream = this._fileStorage.OpenFile("configured-sensors.json", FileMode.OpenOrCreate)) + { + Log.Logger.Information($"writing configured sensors to: {stream.Name}"); + foreach (AbstractSensor sensor in this.ConfiguredSensors) + { + configuredSensorsToSave.Add(new ConfiguredSensor() { Id = sensor.Id, Name = sensor.Name, Type = sensor.GetType().Name }); + } + + await JsonSerializer.SerializeAsync(stream, configuredSensorsToSave); + } } public void AddConfiguredSensor(AbstractSensor sensor) { this.ConfiguredSensors.Add(sensor); - WriteSettings(); + WriteSettingsAsync(); } public void AddConfiguredSensors(List sensors) { sensors.ForEach((sensor) => this.ConfiguredSensors.Add(sensor)); - WriteSettings(); + WriteSettingsAsync(); } - public async void WriteMqttBrokerSettings(string host, string username, string password) + /// + /// Writes provided settings to the config file and invokes a reconfigure to the current mqqtClient + /// + /// + public async void WriteMqttBrokerSettingsAsync(MqttSettings settings) { - IsolatedStorageFileStream stream = this._fileStorage.OpenFile("mqttbroker.json", FileMode.OpenOrCreate); - Log.Logger.Information($"writing configured mqttbroker to: {stream.Name}"); - ConfiguredMqttBroker configuredBroker = new ConfiguredMqttBroker() + using (IsolatedStorageFileStream stream = this._fileStorage.OpenFile("mqttbroker.json", FileMode.OpenOrCreate)) { - Host = host, - Username = username, - Password = password - }; + Log.Logger.Information($"writing configured mqttbroker to: {stream.Name}"); - await JsonSerializer.SerializeAsync(stream, configuredBroker); - stream.Close(); + ConfiguredMqttBroker configuredBroker = new ConfiguredMqttBroker() + { + Host = settings.Host, + Username = settings.Username, + Password = settings.Password + }; - this.MqqtConfigChangedHandler.Invoke(await this.ReadMqttSettings()); - } + await JsonSerializer.SerializeAsync(stream, configuredBroker); + } - + this.MqqtConfigChangedHandler.Invoke(await this.GetMqttClientOptionsAsync()); + } - /// - /// You can use this to check if the application responds. - /// - /// - /// - public string Ping(string str) + public async Task GetMqttBrokerSettings() { - if (str == "ping") + ConfiguredMqttBroker broker = await ReadMqttSettingsAsync(); + return new MqttSettings { - return "pong"; - } - return "what?"; + Host = broker?.Host, + Username = broker?.Username, + Password = broker?.Password + }; } } } \ No newline at end of file diff --git a/hass-workstation-service/Data/IConfigurationService.cs b/hass-workstation-service/Data/IConfigurationService.cs index 7dac53c..7bc98a1 100644 --- a/hass-workstation-service/Data/IConfigurationService.cs +++ b/hass-workstation-service/Data/IConfigurationService.cs @@ -1,4 +1,5 @@ using hass_workstation_service.Communication; +using hass_workstation_service.Communication.InterProcesCommunication.Models; using hass_workstation_service.Domain.Sensors; using MQTTnet.Client.Options; using System; @@ -15,10 +16,10 @@ namespace hass_workstation_service.Data void AddConfiguredSensor(AbstractSensor sensor); void AddConfiguredSensors(List sensors); - string Ping(string str); - Task ReadMqttSettings(); + Task GetMqttClientOptionsAsync(); void ReadSensorSettings(MqttPublisher publisher); - void WriteMqttBrokerSettings(string host, string username, string password); - void WriteSettings(); + void WriteMqttBrokerSettingsAsync(MqttSettings settings); + void WriteSettingsAsync(); + Task GetMqttBrokerSettings(); } } \ No newline at end of file diff --git a/hass-workstation-service/Program.cs b/hass-workstation-service/Program.cs index b5223bc..0d11ccd 100644 --- a/hass-workstation-service/Program.cs +++ b/hass-workstation-service/Program.cs @@ -18,6 +18,7 @@ using System.IO; using Microsoft.Win32; using JKang.IpcServiceFramework.Hosting; using hass_workstation_service.Communication.NamedPipe; +using hass_workstation_service.Communication.InterProcesCommunication; namespace hass_workstation_service { @@ -88,9 +89,8 @@ namespace hass_workstation_service Sw_version = GetVersion() }; services.AddSingleton(deviceConfig); - ConfigurationService configurationService = new ConfigurationService(); - services.AddSingleton(configurationService); - services.AddSingleton(configurationService); + services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddHostedService(); }).ConfigureIpcHost(builder =>