From 45a80a5b6161b745e436723468095c3cef5e25a8 Mon Sep 17 00:00:00 2001 From: Eion Robb Date: Thu, 15 Aug 2024 22:13:15 +1200 Subject: [PATCH] Add support for Pairing command also, for MacOS --- src/Client.cs | 61 ++++++++++++++++++++++++++++---------------- src/ClientMessage.cs | 3 +++ 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/Client.cs b/src/Client.cs index 8d10957..1098b61 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -1,6 +1,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Net.WebSockets; +using System.Runtime.InteropServices; using System.Text; using System.Text.Json; using Ro.Teams.LocalApi.EventArgs; @@ -61,26 +62,36 @@ public class Client : INotifyPropertyChanged public event EventHandler? Disconnected; public event EventHandler? Connected; - public event PropertyChangedEventHandler? PropertyChanged; - - public event EventHandler? TokenReceived; - public event EventHandler? SuccessReceived; + public event PropertyChangedEventHandler? PropertyChanged; + + public event EventHandler? TokenReceived; + public event EventHandler? SuccessReceived; public event EventHandler? ErrorReceived; + private readonly string teamsProcessName; + private int requestId; + public Client(bool autoConnect = false, string manufacturer = "Ro.", string device = "TeamsApiClient", string app = "TeamsApiClient", string appVersion = "1.0.0", string? token = null, CancellationToken cancellationToken = default) - { - clientInfo = new() { - App = app, - AppVersion = appVersion, - Device = device, - Manufacturer = manufacturer, - Token = token - }; - + { + clientInfo = new() { + App = app, + AppVersion = appVersion, + Device = device, + Manufacturer = manufacturer, + Token = token + }; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { + teamsProcessName = "MSTeams"; + } else { + teamsProcessName = "ms-teams"; + } + requestId = 1; + if (autoConnect) _ = Connect(true, cancellationToken); } - + public async Task Disconnect(CancellationToken cancellationToken = default) { if (ws is null) return; @@ -310,10 +321,10 @@ public async Task Connect(bool waitForTeams = true, CancellationToken cancellati { await connectingSemaphore.WaitAsync(cancellationToken); - if (Process.GetProcessesByName("ms-teams").Length == 0 && !waitForTeams) + if (Process.GetProcessesByName(teamsProcessName).Length == 0 && !waitForTeams) return; - while (Process.GetProcessesByName("ms-teams").Length == 0) + while (Process.GetProcessesByName(teamsProcessName).Length == 0) await Task.Delay(1000, cancellationToken); if (ws is not null) @@ -351,6 +362,9 @@ public async Task Connect(bool waitForTeams = true, CancellationToken cancellati Connected?.Invoke(this, System.EventArgs.Empty); Receive(cancellationToken); + if (string.IsNullOrWhiteSpace(clientInfo.Token)) { + await SendCommand(MeetingAction.Pair, null, cancellationToken); + } await SendCommand(MeetingAction.QueryMeetingState, null, cancellationToken); } finally @@ -386,11 +400,12 @@ private async Task SendCommand(MeetingAction action, ClientMessageParameterType? var message = JsonSerializer.Serialize(new ClientMessage() { Action = action, - Parameters = type is null ? null : new ClientMessageParameter + Parameters = type is null ? null : new ClientMessageParameter { Type = type.Value - } - }, jsonSerializerOptions); + }, + RequestId = requestId++ + }, jsonSerializerOptions); try { @@ -424,7 +439,9 @@ public async Task ReactWow(CancellationToken cancellationToken = default) public async Task UpdateState(CancellationToken cancellationToken = default) => await SendCommand(MeetingAction.QueryMeetingState, null, cancellationToken); - - public async Task ToggleChat(CancellationToken cancellationToken = default) - => await SendCommand(MeetingAction.ToggleUI, ClientMessageParameterType.ToggleUiChat, cancellationToken); + + public async Task ToggleChat(CancellationToken cancellationToken = default) + => await SendCommand(MeetingAction.ToggleUI, ClientMessageParameterType.ToggleUiChat, cancellationToken); + public async Task Pair(CancellationToken cancellationToken = default) + => await SendCommand(MeetingAction.Pair, null, cancellationToken); } diff --git a/src/ClientMessage.cs b/src/ClientMessage.cs index e0358a8..4fa02e2 100644 --- a/src/ClientMessage.cs +++ b/src/ClientMessage.cs @@ -23,6 +23,9 @@ internal enum MeetingAction [JsonPropertyName("none")] None = 0, + [JsonPropertyName("pair")] + Pair = 0b0000_0001_0000_0001, + [JsonPropertyName("query-state")] QueryMeetingState = 0b0000_0001_0000_0000,