200 lines
6.3 KiB
C#
200 lines
6.3 KiB
C#
using CLMGRLib;
|
|
using System.Diagnostics;
|
|
using TeamsLocalLibary.EventArgs;
|
|
|
|
namespace TeamsNetphoneLink.Netphone
|
|
{
|
|
public delegate void LineStateChangedEventHandler(LineState newLineState);
|
|
public delegate void LoggedInStateEventHandler(bool loggedInState);
|
|
|
|
public class NetPhoneEvents : IDisposable
|
|
{
|
|
private ClientLine SelectedLine;
|
|
private ClientLineMgrClass pCLMgr;
|
|
private ClientSdkEventSink MyEventSink;
|
|
private LineState LastLineState;
|
|
private bool lastLoggedInState;
|
|
|
|
private CancellationTokenSource aliveCheckTokenSource;
|
|
private CancellationToken aliveCheckToken;
|
|
|
|
private Dictionary<LineState, List<LineStateChangedEventHandler>> lineStateEvents = new Dictionary<LineState, List<LineStateChangedEventHandler>>();
|
|
private List<LoggedInStateEventHandler> loggedInStateEvents = new List<LoggedInStateEventHandler>();
|
|
|
|
|
|
private Timer loggedInStateTimer;
|
|
private Timer aliveTimer;
|
|
|
|
public event EventHandler<TokenReceivedEventArgs>? TokenReceived;
|
|
|
|
public NetPhoneEvents()
|
|
{
|
|
loggedInStateTimer = new Timer(CheckLoggedInState, null, Timeout.Infinite, 1000);
|
|
aliveTimer = new Timer(AliveCheck, null, Timeout.Infinite, 1000);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
loggedInStateTimer?.Change(Timeout.Infinite, 0);
|
|
loggedInStateTimer?.Dispose();
|
|
RemoveAllEventHandlers();
|
|
}
|
|
|
|
public async Task<bool> Initialize(bool waitForNetphone = false, CancellationToken cancellationToken = default)
|
|
{
|
|
if (Process.GetProcessesByName("CLMgr").Length == 0 && !waitForNetphone)
|
|
return false;
|
|
|
|
while (Process.GetProcessesByName("CLMgr").Length == 0)
|
|
await Task.Delay(1000, cancellationToken);
|
|
|
|
|
|
Console.WriteLine("new interface");
|
|
pCLMgr = new ClientLineMgrClass();
|
|
MyEventSink = new ClientSdkEventSink();
|
|
MyEventSink.Connect(pCLMgr, new LineManagerMessageHandler(OnLineManagerMessage));
|
|
|
|
aliveTimer.Change(0, 1000);
|
|
loggedInStateTimer.Change(0, 1500);
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
public void AliveCheck(object state)
|
|
{
|
|
if (Process.GetProcessesByName("CLMgr").Length == 0)
|
|
{
|
|
_ = Initialize(true).GetAwaiter().GetResult();
|
|
}
|
|
}
|
|
|
|
private void CheckLoggedInState(object state)
|
|
{
|
|
bool currentLoggedInState = false;
|
|
|
|
try
|
|
{
|
|
currentLoggedInState = pCLMgr.DispIsLoggedIn != 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
currentLoggedInState = false;
|
|
}
|
|
|
|
if (currentLoggedInState != lastLoggedInState)
|
|
{
|
|
lastLoggedInState = currentLoggedInState;
|
|
OnLoggedInStateChanged(currentLoggedInState);
|
|
}
|
|
}
|
|
|
|
|
|
private void OnLoggedInStateChanged(bool isLoggedIn)
|
|
{
|
|
foreach (var handler in loggedInStateEvents)
|
|
{
|
|
handler?.Invoke(isLoggedIn);
|
|
}
|
|
}
|
|
|
|
public void AddLineStateEventHandler(LineState lineState, LineStateChangedEventHandler handler)
|
|
{
|
|
lock (lineStateEvents)
|
|
{
|
|
if (!lineStateEvents.ContainsKey(lineState))
|
|
{
|
|
lineStateEvents[lineState] = new List<LineStateChangedEventHandler>();
|
|
}
|
|
lineStateEvents[lineState].Add(handler);
|
|
}
|
|
}
|
|
|
|
public void AddLoggedInStateEventHandler(LoggedInStateEventHandler handler)
|
|
{
|
|
lock (loggedInStateEvents)
|
|
{
|
|
loggedInStateEvents.Add(handler);
|
|
}
|
|
}
|
|
|
|
public void RemoveLineStateEventHandler(LineState lineState, LineStateChangedEventHandler handler)
|
|
{
|
|
lock (lineStateEvents)
|
|
{
|
|
if (lineStateEvents.ContainsKey(lineState))
|
|
{
|
|
lineStateEvents[lineState].Remove(handler);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RemoveLoggedInStateEventHandler(LoggedInStateEventHandler handler)
|
|
{
|
|
lock (loggedInStateEvents)
|
|
{
|
|
loggedInStateEvents.Remove(handler);
|
|
}
|
|
}
|
|
|
|
public void RemoveAllEventHandlers()
|
|
{
|
|
lock (lineStateEvents)
|
|
{
|
|
lineStateEvents.Clear();
|
|
}
|
|
lock (loggedInStateEvents)
|
|
{
|
|
loggedInStateEvents.Clear();
|
|
}
|
|
}
|
|
|
|
private void OnLineManagerMessage(ClientSdkEventArgs e)
|
|
{
|
|
SelectedLine = (ClientLine)pCLMgr.DispSelectedLine;
|
|
|
|
if (e.Msg == CLMgrMessage.CLMgrClientShutDownRequest)
|
|
{
|
|
//aliveCheckTokenSource.Cancel();
|
|
//MyEventSink.Disconnect();
|
|
//OnConnectionStateChanged(false,true);
|
|
}
|
|
|
|
if (e.Msg == CLMgrMessage.CLMgrLineStateChangedMessageEx)
|
|
{
|
|
int line = e.Param & 0xff;
|
|
int high = e.Param >> 8;
|
|
|
|
LineState NewLineState = (LineState)high;
|
|
|
|
if (LastLineState != NewLineState)
|
|
{
|
|
LastLineState = NewLineState;
|
|
|
|
lock (lineStateEvents)
|
|
{
|
|
if (lineStateEvents.ContainsKey(NewLineState))
|
|
{
|
|
foreach (var handler in lineStateEvents[NewLineState])
|
|
{
|
|
handler?.Invoke(NewLineState);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetRichPresenceStatus(int away, int dnd, DateTime expires)
|
|
{
|
|
var clientConfig = (ClientConfig)this.pCLMgr.ClientConfig;
|
|
clientConfig.SetRichPresenceStatus(away, dnd, expires);
|
|
}
|
|
|
|
public void SetAppointmentText(string appointmentText, DateTime expires)
|
|
{
|
|
var clientConfig = (ClientConfig)this.pCLMgr.ClientConfig;
|
|
clientConfig.SetAppointmentText(appointmentText, expires);
|
|
}
|
|
}
|
|
} |