-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamsTpPlugin.cs
190 lines (165 loc) · 6.46 KB
/
TeamsTpPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using Microsoft.Extensions.Logging;
using System.Reflection;
using TeamsIntegration.Configurations;
using TeamsIntegration.Enums;
using TeamsIntegration.Util;
using TouchPortalSDK.Interfaces;
using TouchPortalSDK.Messages.Events;
using TouchPortalSDK.Messages.Models;
namespace TeamsIntegration;
/// <summary>
/// This is the TouchPortal plugin
/// </summary>
public class TeamsTpPlugin : PluginBase
{
private const String CPluginId = "info.sowa.teams";
private readonly CancellationTokenSource _CancellationTokenSource;
private readonly TeamsConfiguration _Configuration;
private readonly TeamsWsIntegration _TeamsWsIntegration;
public TeamsTpPlugin(ITouchPortalClientFactory clientFactory, TeamsWsIntegration teamsWsIntegration, ILogger<TeamsTpPlugin> logger)
{
_TeamsWsIntegration = teamsWsIntegration;
Logger = logger;
Client = clientFactory.Create(this);
_CancellationTokenSource = new CancellationTokenSource();
_Configuration = new TeamsConfiguration(logger)
{
Manufacturer = (GetType().Assembly.GetCustomAttribute(typeof(AssemblyCompanyAttribute)) as AssemblyCompanyAttribute)?.Company,
App = (GetType().Assembly.GetCustomAttribute(typeof(AssemblyProductAttribute)) as AssemblyProductAttribute)?.Product,
AppVersion = GetType().Assembly.GetName().Version?.ToString()
};
_Configuration.Load();
}
protected override ILogger Logger { get; }
protected override ITouchPortalClient Client { get; }
public Boolean IsConnected => Client.IsConnected;
protected override String GetPluginId()
{
return CPluginId;
}
public void Run()
{
try
{
if (!Client.Connect())
{
Logger.LogError("Client could not connect to TouchPortal");
return;
}
_TeamsWsIntegration.Run(this, _Configuration, _CancellationTokenSource.Token);
}
catch (Exception ex)
{
Logger.LogError("Run: {ExMessage}", ex.Message);
}
}
public override void OnInfoEvent(InfoEvent message)
{
Logger.LogObjectAsJson(message.Settings);
ConfigurationUpdate(message.Settings);
}
public override void OnSettingsEvent(SettingsEvent message)
{
Logger.LogObjectAsJson(message.Values);
ConfigurationUpdate(message.Values);
}
public override void OnClosedEvent(String message)
{
try
{
_CancellationTokenSource.Cancel();
}
catch (Exception ex)
{
Logger.LogError("OnClosedEvent: {ExMessage}", ex.Message);
}
finally
{
Environment.Exit(0);
}
}
public override void OnActionEvent(ActionEvent message)
{
try
{
Logger.LogTrace("Event {MessageType}", message.Type);
switch (message.Type)
{
case "action":
Logger.LogTrace("Action {ActionId}", message.ActionId);
switch (message.ActionId)
{
case CPluginId + ".action.toggle.microphone":
_TeamsWsIntegration.Command(TeamsAction.Toggle_Mute, GetTpActionState(message));
break;
case CPluginId + ".action.toggle.camera":
_TeamsWsIntegration.Command(TeamsAction.Toggle_Video, GetTpActionState(message));
break;
case CPluginId + ".action.toggle.backgroundblur":
_TeamsWsIntegration.Command(TeamsAction.Toggle_Background_Blur, GetTpActionState(message));
break;
case CPluginId + ".action.toggle.hand":
_TeamsWsIntegration.Command(TeamsAction.Toggle_Hand, GetTpActionState(message));
break;
case CPluginId + ".action.call.leave":
_TeamsWsIntegration.Command(TeamsAction.Leave_Call);
break;
case CPluginId + ".action.react.applause":
_TeamsWsIntegration.Command(TeamsAction.React_Applause);
break;
case CPluginId + ".action.react.laugh":
_TeamsWsIntegration.Command(TeamsAction.React_Laugh);
break;
case CPluginId + ".action.react.like":
_TeamsWsIntegration.Command(TeamsAction.React_Like);
break;
case CPluginId + ".action.react.love":
_TeamsWsIntegration.Command(TeamsAction.React_Love);
break;
case CPluginId + ".action.react.surprised":
_TeamsWsIntegration.Command(TeamsAction.React_Surprised);
break;
}
break;
case "up":
break;
case "down":
break;
}
}
catch (Exception ex)
{
Logger.LogError("OnActionEvent: {ExMessage}", ex.Message);
}
}
public Boolean SendStateUpdate(String state, Object update)
{
return Client.IsConnected && Client.StateUpdate($"{CPluginId}.state.{state.ToLowerInvariant()}", update.ToString());
}
private void ConfigurationUpdate(IEnumerable<Setting> settings)
{
foreach (Setting setting in settings)
{
switch (setting.Name)
{
case "Teams address":
String configurationTeamsIpDns = setting.Value.Trim();
if (!String.IsNullOrEmpty(configurationTeamsIpDns))
{
_Configuration.TeamsIpDns = configurationTeamsIpDns;
_Configuration.ConfigurationChanged = true;
}
break;
}
}
}
private TpActionState GetTpActionState(ActionEvent message)
{
String set = message.Data.FirstOrDefault(d => d.Id == $"{message.ActionId}.set")?.Value;
if (!Enum.TryParse(set ?? "Toggle", true, out TpActionState tpAction))
{
Logger.LogError("Error parsing state {Set}", set);
}
return tpAction;
}
}