add support for mqtt port and tls. This solves #11

notify^2
sleevezipper 4 years ago
parent b3e9c91689
commit 001e7c522c

@ -2,6 +2,7 @@
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace UserInterface.ViewModels
@ -13,17 +14,28 @@ namespace UserInterface.ViewModels
private string password;
private string message;
private bool isConnected;
private int? port;
private bool useTLS;
public bool IsConnected { get => isConnected; set => this.RaiseAndSetIfChanged(ref isConnected, value); }
public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
[Required(AllowEmptyStrings = false)]
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); }
[Required]
[Range(1, 65535)]
public int? Port { get => port; set => this.RaiseAndSetIfChanged(ref port, value); }
public bool UseTLS { get => useTLS; set => this.RaiseAndSetIfChanged(ref useTLS, value); }
public void Update(MqttSettings settings)
{
this.Host = settings.Host;
this.Username = settings.Username;
this.Password = settings.Password;
this.Port = settings.Port;
this.UseTLS = settings.UseTLS;
}
public void UpdateStatus(MqqtClientStatus status)

@ -1,11 +1,18 @@
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace UserInterface.ViewModels
{
public class ViewModelBase : ReactiveObject
{
public bool IsValid<T>(T obj, out ICollection<ValidationResult> results)
{
results = new List<ValidationResult>();
return Validator.TryValidateObject(obj, new ValidationContext(obj), results, true);
}
}
}

@ -10,6 +10,17 @@
<TextBlock IsVisible="{Binding !IsConnected}" Foreground="Red" Text="{Binding Message}"></TextBlock >
<ContentControl Margin="0 20 0 10">IP or hostname</ContentControl>
<TextBox Text="{Binding Host}" HorizontalAlignment="Left" Width="100" Watermark="192.168.1.200"/>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<ContentControl Margin="0 20 0 10">Port</ContentControl>
<TextBox Text="{Binding Port}" HorizontalAlignment="Left" Width="50" Watermark="1883"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="30 0 0 0">
<ContentControl Margin="0 20 0 10">Use TLS</ContentControl>
<CheckBox IsChecked="{Binding UseTLS}" HorizontalAlignment="Left" Margin="0 3 0 0"/>
</StackPanel>
</StackPanel>
<ContentControl Margin="0 20 0 10">Username</ContentControl>
<TextBox Text="{Binding Username}" MinWidth="150"/>
<ContentControl Margin="0 20 0 10">Password</ContentControl>

@ -10,6 +10,8 @@ using System.Reactive.Linq;
using UserInterface.ViewModels;
using System.Security;
using hass_workstation_service.Communication.InterProcesCommunication.Models;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
namespace UserInterface.Views
{
@ -45,7 +47,11 @@ namespace UserInterface.Views
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 ?? "" }));
ICollection<ValidationResult> results;
if (model.IsValid(model, out results))
{
var result = this.client.InvokeAsync(x => x.WriteMqttBrokerSettingsAsync(new MqttSettings() { Host = model.Host, Username = model.Username, Password = model.Password ?? "", Port = model.Port, UseTLS = model.UseTLS }));
}
}
public async void GetSettings()

@ -10,6 +10,8 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models
public string Host { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public int? Port { get; set; }
public bool UseTLS { get; set; }
}
public class MqqtClientStatus

@ -10,6 +10,7 @@ using MQTTnet;
using MQTTnet.Adapter;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using MQTTnet.Exceptions;
using Serilog;
namespace hass_workstation_service.Communication
@ -135,7 +136,11 @@ namespace hass_workstation_service.Communication
this._mqttClientMessage = ex.ResultCode.ToString();
Log.Logger.Error("Could not connect to broker: " + ex.ResultCode.ToString());
}
catch (MqttCommunicationException ex)
{
this._mqttClientMessage = ex.ToString();
Log.Logger.Error("Could not connect to broker: " + ex.Message);
}
}
public MqqtClientStatus GetStatus()

@ -34,12 +34,12 @@ namespace hass_workstation_service.Data
{
if (!File.Exists(Path.Combine(path, "mqttbroker.json")))
{
File.Create(Path.Combine(path, "mqttbroker.json"));
File.Create(Path.Combine(path, "mqttbroker.json")).Close();
}
if (!File.Exists(Path.Combine(path, "configured-sensors.json")))
{
File.Create(Path.Combine(path, "configured-sensors.json"));
File.Create(Path.Combine(path, "configured-sensors.json")).Close();
}
ConfiguredSensors = new List<AbstractSensor>();
@ -123,8 +123,12 @@ namespace hass_workstation_service.Data
{
var mqttClientOptions = new MqttClientOptionsBuilder()
.WithTcpServer(configuredBroker.Host)
// .WithTls()
.WithTcpServer(configuredBroker.Host, configuredBroker.Port)
.WithTls(new MqttClientOptionsBuilderTlsParameters()
{
UseTls = configuredBroker.UseTLS,
AllowUntrustedCertificates = true
})
.WithCredentials(configuredBroker.Username, configuredBroker.Password.ToString())
.Build();
return mqttClientOptions;
@ -248,7 +252,9 @@ namespace hass_workstation_service.Data
{
Host = settings.Host,
Username = settings.Username,
Password = settings.Password ?? ""
Password = settings.Password ?? "",
Port = settings.Port ?? 1883,
UseTLS = settings.UseTLS
};
await JsonSerializer.SerializeAsync(stream, configuredBroker);
@ -265,7 +271,9 @@ namespace hass_workstation_service.Data
{
Host = broker?.Host,
Username = broker?.Username,
Password = broker?.Password
Password = broker?.Password,
Port = broker?.Port,
UseTLS = broker?.UseTLS ?? false
};
}

@ -7,7 +7,12 @@ namespace hass_workstation_service.Data
{
private string username;
private string password;
private int? port;
public string Host { get; set; }
public int Port { get => port ?? 1883; set => port = value; }
public bool UseTLS { get; set; }
public string Username
{

Loading…
Cancel
Save