119 lines
4.0 KiB
C#
119 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Net.WebSockets;
|
|
using System.Reflection;
|
|
using TeamsLocalLibary;
|
|
using TeamsLocalLibary.EventArgs;
|
|
|
|
namespace TeamsNetphoneLink.Teams
|
|
{
|
|
public class TeamsLocalAPI
|
|
{
|
|
public event EventHandler<ConnectionStateChangedEventArgs>? ConnectionState;
|
|
public event EventHandler<TokenReceivedEventArgs>? TokenReceived;
|
|
|
|
private Client teamsClient;
|
|
private string token = Settings.Default.Token != string.Empty ? Settings.Default.Token : null;
|
|
private Dictionary<string, List<PropertyChangedEventHandler>> propertyChangedHandlers = new Dictionary<string, List<PropertyChangedEventHandler>>();
|
|
private CancellationTokenSource cts = new();
|
|
|
|
// Destructor
|
|
~TeamsLocalAPI()
|
|
{
|
|
RemoveAllEventHandlers();
|
|
}
|
|
|
|
public async Task<bool> Initialize()
|
|
{
|
|
// Initialize the TeamsClient with token and no auto-connect
|
|
teamsClient = new Client(autoConnect: false, token: token) ?? throw new Exception("Could not create client");
|
|
|
|
// Event-handler for token reception
|
|
teamsClient.TokenReceived += (_, args) =>
|
|
{
|
|
Settings.Default.Token = args.Token;
|
|
Settings.Default.Save();
|
|
|
|
TokenReceived?.Invoke(_, args);
|
|
};
|
|
|
|
WebSocketState lastConnectionState = WebSocketState.None;
|
|
teamsClient.ConnectionState += (sender, args) =>
|
|
{
|
|
if (args.WebSocketState != lastConnectionState)
|
|
{
|
|
lastConnectionState = args.WebSocketState;
|
|
// Raise the ConnectionState event in TeamsLocalAPI
|
|
ConnectionState?.Invoke(sender, args);
|
|
}
|
|
};
|
|
|
|
teamsClient.PropertyChanged += HandlePropertyChanged;
|
|
|
|
teamsClient.ErrorReceived += (_, args) => Console.WriteLine("Event: ErrorReceived: {0}", args.ErrorMessage);
|
|
|
|
return await teamsClient.Connect(true, cts.Token);
|
|
}
|
|
|
|
public void AddTokenRecievedHandler(EventHandler<TokenReceivedEventArgs> handler)
|
|
{
|
|
TokenReceived += handler;
|
|
}
|
|
|
|
public void RemoveTokenRecievedHandler(EventHandler<TokenReceivedEventArgs> handler)
|
|
{
|
|
TokenReceived -= handler;
|
|
}
|
|
|
|
public void AddConnectionStateHandler(EventHandler<ConnectionStateChangedEventArgs> handler)
|
|
{
|
|
ConnectionState += handler;
|
|
}
|
|
|
|
public void RemoveConnectionStateHandler(EventHandler<ConnectionStateChangedEventArgs> handler)
|
|
{
|
|
ConnectionState -= handler;
|
|
}
|
|
|
|
public async void SendDummyCommand()
|
|
{
|
|
var dummy = teamsClient.IsMuted = true;
|
|
}
|
|
|
|
public void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
{
|
|
if (e.PropertyName is not null && propertyChangedHandlers.ContainsKey(e.PropertyName))
|
|
{
|
|
foreach (var handler in propertyChangedHandlers[e.PropertyName])
|
|
{
|
|
handler?.Invoke(sender, e);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddEventHandler(string propertyName, PropertyChangedEventHandler handler)
|
|
{
|
|
if (!propertyChangedHandlers.ContainsKey(propertyName))
|
|
{
|
|
propertyChangedHandlers[propertyName] = new List<PropertyChangedEventHandler>();
|
|
}
|
|
|
|
propertyChangedHandlers[propertyName].Add(handler);
|
|
}
|
|
|
|
public void RemoveEventHandler(string propertyName, PropertyChangedEventHandler handler)
|
|
{
|
|
if (propertyChangedHandlers.ContainsKey(propertyName))
|
|
{
|
|
propertyChangedHandlers[propertyName].Remove(handler);
|
|
}
|
|
}
|
|
|
|
public void RemoveAllEventHandlers()
|
|
{
|
|
propertyChangedHandlers.Clear();
|
|
}
|
|
}
|
|
}
|