Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions src/Client.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -61,26 +62,36 @@ public class Client : INotifyPropertyChanged
public event EventHandler? Disconnected;
public event EventHandler? Connected;

public event PropertyChangedEventHandler? PropertyChanged;

public event EventHandler<TokenReceivedEventArgs>? TokenReceived;
public event EventHandler<SuccessReceivedEventArgs>? SuccessReceived;
public event PropertyChangedEventHandler? PropertyChanged;
public event EventHandler<TokenReceivedEventArgs>? TokenReceived;
public event EventHandler<SuccessReceivedEventArgs>? SuccessReceived;
public event EventHandler<ErrorReceivedEventArgs>? 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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions src/ClientMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down