diff --git a/API/WebSocketManager.cs b/API/WebSocketManager.cs index 20a0c64..c7ece47 100644 --- a/API/WebSocketManager.cs +++ b/API/WebSocketManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Net.WebSockets; using System.Text; using System.Threading; @@ -24,6 +25,7 @@ public class WebSocketManager private readonly Action _updateTokenAction; public event EventHandler TeamsUpdateReceived; public event EventHandler MessageReceived; + public event Action ConnectionStatusChanged; private TaskCompletionSource _pairingResponseTaskSource; private Dictionary meetingState = new Dictionary() { @@ -35,6 +37,7 @@ public class WebSocketManager { "isBackgroundBlurred", false }, }; public static WebSocketManager Instance => _instance.Value; + private WebSocketManager() @@ -71,19 +74,29 @@ public async Task ConnectAsync(Uri uri) _currentUri = uri; await _clientWebSocket.ConnectAsync(uri, CancellationToken.None); - _isConnected = true; + SetIsConnected(true); StartReceiving(); } catch (Exception ex) { Log.Error("Failed to connect: " + ex.Message); - _isConnected = false; + SetIsConnected(false); } finally { _isConnecting = false; } } + private void SetIsConnected(bool value) + { + if (_isConnected != value) + { + _isConnected = value; + State.Instance.teamsRunning = value; //update the MQTT state + + ConnectionStatusChanged?.Invoke(IsConnected); //update the UI + } + } public async Task PairWithTeamsAsync(Action updateTokenCallback) { @@ -131,7 +144,8 @@ private async void StartReceiving() if (result.MessageType == WebSocketMessageType.Close) { await _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); - _isConnected = false; + SetIsConnected(false); + Log.Information("WebSocket closed."); } else @@ -148,7 +162,9 @@ private async void StartReceiving() catch (Exception ex) { Log.Error("Error in receiving loop: " + ex.Message); - _isConnected = false; + SetIsConnected(false); + + } } public async Task SendMessageAsync(string message, CancellationToken cancellationToken = default) @@ -403,7 +419,7 @@ public async Task DisconnectAsync() if (_clientWebSocket.State == WebSocketState.Open) { await _clientWebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Client disconnect", CancellationToken.None); - _isConnected = false; + SetIsConnected(false); Log.Information("Disconnected from server."); } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 2f3a191..66c2741 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -347,6 +347,31 @@ public async Task InitializeConnections() Dispatcher.Invoke(() => UpdateStatusMenuItems()); } + public bool IsTeamsConnected + { + get { return isTeamsConnected; } + set + { + if (isTeamsConnected != value) + { + isTeamsConnected = value; + + + if (isTeamsConnected) + { + TeamsConnectionStatusChanged(isTeamsConnected); + _ = _mqttService.PublishConfigurations(null!, _settings); + Console.WriteLine("Teams is now connected"); + } + else + { + TeamsConnectionStatusChanged(isTeamsConnected); + _= _mqttService.PublishConfigurations(null!, _settings); + Console.WriteLine("Teams is now disconnected"); + } + } + } + } private async void InitializeWebSocket() { var uri = new Uri($"ws://localhost:8124?token={_settings.PlainTeamsToken}&protocol-version=2.0.0&manufacturer=JimmyWhite&device=PC&app=THFHA&app-version=2.0.26"); @@ -357,6 +382,17 @@ await WebSocketManager.Instance.PairWithTeamsAsync(newToken => }); await WebSocketManager.Instance.ConnectAsync(uri); WebSocketManager.Instance.TeamsUpdateReceived += TeamsClient_TeamsUpdateReceived; + + + WebSocketManager.Instance.ConnectionStatusChanged += (isConnected) => + { + // Because this event handler might be called from a non-UI thread, + // use Dispatcher.Invoke to ensure that the UI update runs on the UI thread: + Dispatcher.Invoke(() => TeamsConnectionStatusChanged(isConnected)); + }; + + + Dispatcher.Invoke(() => UpdateStatusMenuItems()); } private void MainWindow_StateChanged(object sender, EventArgs e) @@ -815,8 +851,14 @@ private void TeamsConnectionStatusChanged(bool isConnected) { TeamsConnectionStatus.Text = isConnected ? "Teams: Connected" : "Teams: Disconnected"; _teamsStatusMenuItem.Header = "Teams Status: " + (isConnected ? "Connected" : "Disconnected"); + if (_latestMeetingUpdate != null && _latestMeetingUpdate.MeetingState != null) + { + _latestMeetingUpdate.MeetingState.teamsRunning = isConnected; + } + _ = _mqttService.PublishConfigurations(_latestMeetingUpdate, _settings); }); } + private async void TestTeamsConnection_Click(object sender, RoutedEventArgs e) { diff --git a/TEAMS2HA.csproj b/TEAMS2HA.csproj index c3cc892..fd21f57 100644 --- a/TEAMS2HA.csproj +++ b/TEAMS2HA.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.1.0.738 - 1.1.0.738 + 1.1.0.751 + 1.1.0.751 Assets\Square150x150Logo.scale-200.ico Teams2HA Square150x150Logo.scale-200.png