Skip to content

Commit

Permalink
Add Port for MQTT Server
Browse files Browse the repository at this point in the history
Signed-off-by: Johnnie Blows <[email protected]>
  • Loading branch information
johnnieblows committed Dec 18, 2023
1 parent a1acaf5 commit ed0b911
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,8 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

# Visual Studio Code
.vscode/
nuget.config
6 changes: 4 additions & 2 deletions API/MqttClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ public bool IsAttemptingConnection
get { return _isAttemptingConnection; }
private set { _isAttemptingConnection = value; }
}
public MqttClientWrapper(string clientId, string mqttBroker, string username, string password)
public MqttClientWrapper(string clientId, string mqttBroker, string mqttPort, string username, string password)
{
var factory = new MqttFactory();
_mqttClient = factory.CreateMqttClient() as MqttClient;

int mqttportInt = System.Convert.ToInt32(mqttPort);

_mqttOptions = new MqttClientOptionsBuilder()
.WithClientId(clientId)
.WithTcpServer(mqttBroker)
.WithTcpServer(mqttBroker, mqttportInt)
.WithCredentials(username, password)
.WithCleanSession()
.Build();
Expand Down
2 changes: 2 additions & 0 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
<StackPanel Grid.Row="2" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Text="MQTT host address:" Width="200" VerticalAlignment="Center"/>
<TextBox x:Name="MqttAddress" Width="300" Margin="10,0,0,0" Style="{DynamicResource MaterialDesignTextBox}"/>
<TextBlock Text="MQTT port:" Width="100" VerticalAlignment="Center" Margin="10,0,0,0"/>
<TextBox x:Name="MqttPort" Width="80" Margin="10,0,0,0" Style="{DynamicResource MaterialDesignTextBox}"/>
</StackPanel>

<StackPanel Grid.Row="3" Margin="0,10,0,0" Orientation="Horizontal">
Expand Down
9 changes: 9 additions & 0 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System.Windows;
using TEAMS2HA.API;
using TEAMS2HA.Properties;

namespace TEAMS2HA
{
Expand Down Expand Up @@ -89,6 +90,8 @@ public static AppSettings Instance

public string MqttAddress { get; set; }

public string MqttPort { get; set; }

public string MqttUsername { get; set; }

public bool RunAtWindowsBoot { get; set; }
Expand Down Expand Up @@ -256,6 +259,7 @@ public MainWindow()
mqttClientWrapper = new MqttClientWrapper(
"TEAMS2HA",
_settings.MqttAddress,
_settings.MqttPort,
_settings.MqttUsername,
_settings.MqttPassword
);
Expand Down Expand Up @@ -776,6 +780,7 @@ private async void MainPage_Loaded(object sender, RoutedEventArgs e)
MqttUserNameBox.Text = _settings.MqttUsername;
MQTTPasswordBox.Password = _settings.MqttPassword;
MqttAddress.Text = _settings.MqttAddress;
MqttPort.Text = _settings.MqttPort;
if (_settings.PlainTeamsToken == null)
{
TeamsApiKeyBox.Text = "Not Paired";
Expand Down Expand Up @@ -924,11 +929,13 @@ private bool SaveSettings()
bool mqttSettingsChanged =
settings.MqttAddress != MqttAddress.Text ||
settings.MqttUsername != MqttUserNameBox.Text ||
settings.MqttPort != MqttPort.Text ||
settings.MqttPassword != MQTTPasswordBox.Password;

settings.RunAtWindowsBoot = RunAtWindowsBootCheckBox.IsChecked ?? false;
settings.RunMinimized = RunMinimisedCheckBox.IsChecked ?? false;
settings.MqttAddress = MqttAddress.Text;
settings.MqttPort = MqttPort.Text;
settings.MqttUsername = MqttUserNameBox.Text;
settings.MqttPassword = MQTTPasswordBox.Password;
settings.Theme = isDarkTheme ? "Dark" : "Light";
Expand All @@ -949,6 +956,7 @@ private void SaveSettings_Click(object sender, RoutedEventArgs e)
mqttClientWrapper = new MqttClientWrapper(
"TEAMS2HA",
_settings.MqttAddress,
_settings.MqttPort,
_settings.MqttUsername,
_settings.MqttPassword
);
Expand Down Expand Up @@ -1094,5 +1102,6 @@ private void ToggleThemeButton_Click(object sender, RoutedEventArgs e)
}

#endregion Private Methods

}
}

0 comments on commit ed0b911

Please sign in to comment.