From 88df0d266ba6f59547a608bf55337a61d34b4e3c Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Wed, 27 Mar 2024 16:00:18 +0000 Subject: [PATCH 1/4] fixes --- API/MqttService.cs | 3 ++- MainWindow.xaml.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/API/MqttService.cs b/API/MqttService.cs index 670b67b..bd4bffe 100644 --- a/API/MqttService.cs +++ b/API/MqttService.cs @@ -19,7 +19,7 @@ public class MqttService private const int MaxConnectionRetries = 2; private const int RetryDelayMilliseconds = 1000; - private readonly string _deviceId; + private string _deviceId; private bool _isAttemptingConnection = false; private MqttClient _mqttClient; private bool _mqttClientsubscribed = false; @@ -426,6 +426,7 @@ public void UpdateConnectionStatus(string status) //could be obsolete public async Task UpdateSettingsAsync(AppSettings newSettings) { _settings = newSettings; + _deviceId = _settings.SensorPrefix; InitializeClientOptions(); // Reinitialize MQTT client options if (IsConnected) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 9879e38..2876e69 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -813,7 +813,7 @@ private async Task SaveSettingsAsync() // Save the updated settings to file settings.SaveSettingsToFile(); - if (mqttSettingsChanged) + if (mqttSettingsChanged || sensorPrefixChanged) { // Perform actions if MQTT settings have changed Log.Debug("SaveSettingsAsync: MQTT settings have changed. Reconnecting MQTT client..."); From 72a1e36b9984dd15596fe47adc68e6978a81dbb1 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Wed, 27 Mar 2024 16:10:31 +0000 Subject: [PATCH 2/4] fixes --- MainWindow.xaml.cs | 11 ++++++++--- TEAMS2HA.csproj | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 2876e69..22031ac 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -723,18 +723,23 @@ private void MyNotifyIcon_Click(object sender, EventArgs e) } } - private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) + private async void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) { if (e.Mode == PowerModes.Resume) { Log.Information("System is waking up from sleep. Re-establishing connections..."); // Implement logic to re-establish connections - ReestablishConnections(); + await ReestablishConnections(); + // publish current meeting state + await _mqttService.PublishConfigurations(_latestMeetingUpdate, _settings); } } - private async void ReestablishConnections() + private async + + Task +ReestablishConnections() { try { diff --git a/TEAMS2HA.csproj b/TEAMS2HA.csproj index 602f196..448f60b 100644 --- a/TEAMS2HA.csproj +++ b/TEAMS2HA.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.1.0.501 - 1.1.0.501 + 1.1.0.504 + 1.1.0.504 Assets\Square150x150Logo.scale-200.ico Teams2HA Square150x150Logo.scale-200.png From ea84d1a6ea310562ded567c91ceac41137219f52 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Wed, 27 Mar 2024 16:24:02 +0000 Subject: [PATCH 3/4] improvements and logging --- API/MqttService.cs | 6 ++++-- MainWindow.xaml.cs | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/API/MqttService.cs b/API/MqttService.cs index bd4bffe..daa953d 100644 --- a/API/MqttService.cs +++ b/API/MqttService.cs @@ -286,6 +286,8 @@ public async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSettings if (forcePublish || !_previousSensorStates.TryGetValue(sensorKey, out var previousState) || previousState != stateValue) { + Log.Information($"Force Publishing configuration for {sensorName} with state {stateValue}."); + _previousSensorStates[sensorKey] = stateValue; // Update the stored state if(forcePublish) { @@ -311,7 +313,7 @@ public async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSettings .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce) .WithRetainFlag(true) .Build(); - + Log.Information($"Publishing configuration for {sensorName} with state {stateValue}."); await PublishAsync(switchConfigMessage); var stateMessage = new MqttApplicationMessageBuilder() @@ -342,7 +344,7 @@ public async Task PublishConfigurations(MeetingUpdate meetingUpdate, AppSettings .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce) .WithRetainFlag(true) .Build(); - + Log.Information($"Publishing configuration for {sensorName} with state {stateValue}."); await PublishAsync(binarySensorConfigMessage); var binarySensorStateMessage = new MqttApplicationMessageBuilder() diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 22031ac..542305d 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -728,6 +728,9 @@ private async void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e if (e.Mode == PowerModes.Resume) { Log.Information("System is waking up from sleep. Re-establishing connections..."); + // Add a delay to allow the network to come up + await Task.Delay(TimeSpan.FromSeconds(10)); // Adjust based on your needs + // Implement logic to re-establish connections await ReestablishConnections(); // publish current meeting state @@ -755,6 +758,8 @@ private async await initializeteamsconnection(); Dispatcher.Invoke(() => UpdateStatusMenuItems()); } + // Force publish all sensor states after reconnection + await _mqttService.PublishConfigurations(_latestMeetingUpdate, _settings, forcePublish: true); } catch (Exception ex) { From cfa45d888231ceb0547fe660a9fbcd835537e496 Mon Sep 17 00:00:00 2001 From: Jimmy White Date: Wed, 27 Mar 2024 16:44:49 +0000 Subject: [PATCH 4/4] fixes for coming out of standby --- MainWindow.xaml.cs | 26 ++++++++++++++------------ TEAMS2HA.csproj | 4 ++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 542305d..f58c41e 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -412,6 +412,7 @@ private void AboutMenuItem_Click(object sender, RoutedEventArgs e) aboutWindow.ShowDialog(); } + private void ApplyTheme(string theme) { isDarkTheme = theme == "Dark"; @@ -467,17 +468,6 @@ private bool CheckIfSensorPrefixChanged(AppSettings newSettings) return newSettings.SensorPrefix != currentSettings.SensorPrefix; } - //private async void CheckMqttConnection() //could be obsolete - //{ - // if (_mqttService != null && !_mqttService.IsConnected && !_mqttService.IsAttemptingConnection) - // { - // Log.Debug("CheckMqttConnection: MQTT Client Not Connected. Attempting reconnection."); - // await _mqttService.ConnectAsync(); - // await _mqttService.SubscribeAsync("homeassistant/switch/+/set", MqttQualityOfServiceLevel.AtLeastOnce); - // _mqttService.UpdateConnectionStatus("Connected"); - // UpdateStatusMenuItems(); - // } - //} private void CreateNotifyIconContextMenu() { @@ -531,7 +521,16 @@ private async Task HandleCommandToTeams(string jsonMessage) await _teamsClient.SendMessageAsync(jsonMessage); } } - + private async void CheckTeamsConnectionStatus() + { + if (!_teamsClient.IsConnected) + { + string teamsToken = _settings.PlainTeamsToken; + var uri = new Uri($"ws://localhost:8124?token={teamsToken}&protocol-version=2.0.0&manufacturer=JimmyWhite&device=PC&app=THFHA&app-version=2.0.26"); + Log.Debug("Teams appears to be disconnected. Attempting to reconnect..."); + await _teamsClient.StartConnectionAsync(uri); + } + } private async Task initializeteamsconnection() { string teamsToken = _settings.PlainTeamsToken; @@ -735,6 +734,9 @@ private async void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e await ReestablishConnections(); // publish current meeting state await _mqttService.PublishConfigurations(_latestMeetingUpdate, _settings); + CheckTeamsConnectionStatus(); + _teamsClient.ConnectionStatusChanged -= TeamsConnectionStatusChanged; + _teamsClient.ConnectionStatusChanged += TeamsConnectionStatusChanged; } } diff --git a/TEAMS2HA.csproj b/TEAMS2HA.csproj index 448f60b..989548b 100644 --- a/TEAMS2HA.csproj +++ b/TEAMS2HA.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.1.0.504 - 1.1.0.504 + 1.1.0.507 + 1.1.0.507 Assets\Square150x150Logo.scale-200.ico Teams2HA Square150x150Logo.scale-200.png