-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCompanionModeEmulator.js
335 lines (294 loc) · 10.2 KB
/
CompanionModeEmulator.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
Copyright (c) 2022 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
*/
import xapi from 'xapi';
// Please configure the following constant variables
const MAX_VOLUME = 0;
const PREFER_JOIN_OBTP = true;
const LEAVE_CAMERA_ON = false;
const MANUAL_TOGGLE_FUNC = true;
var storedDestination = '';
var storedPostDialKeys = '';
const BTN_MANUAL_JOIN = 'panel_manual_board_join'
const BTN_TOGGLE = 'panel_manual_toggle'
var inCompanionMode = true;
var originalVolume = 0;
var inCall = false;
function installOffButton() {
xapi.Command.UserInterface.Extensions.Panel.Save({ PanelId: 'panel_manual_toggle' },
`<Extensions><Version>1.8</Version>
<Panel>
<Order>2</Order>
<PanelId>panel_manual_toggle</PanelId>
<Type>Home</Type>
<Icon>Tv</Icon>
<Color>#D43B52</Color>
<Name>Turn Off Companion</Name>
<ActivityType>Custom</ActivityType>
</Panel>
</Extensions>`);
}
function installOnButton() {
xapi.Command.UserInterface.Extensions.Panel.Save({ PanelId: 'panel_manual_toggle' },
`<Extensions><Version>1.8</Version>
<Panel>
<Order>2</Order>
<PanelId>panel_manual_toggle</PanelId>
<Type>Home</Type>
<Icon>Tv</Icon>
<Color>#1D805F</Color>
<Name>Turn On Companion</Name>
<ActivityType>Custom</ActivityType>
</Panel>
</Extensions>`);
}
function setVolume(vol) {
xapi.Command.Audio.Volume.Set({ Level: vol });
}
function alert2(title, text = '', duration = 5) {
xapi.Command.UserInterface.Message.Alert.Display({
Title: title,
Text: text,
Duration: duration,
});
}
function listenToCalls() {
xapi.Event.CallSuccessful.on(async () => {
if (inCompanionMode) {
const call = await xapi.Status.Call.get();
if (call.length < 1) {
setMute(true);
}
console.log(call[0].CallbackNumber);
setMute(true);
if (!LEAVE_CAMERA_ON) {
xapi.Command.Video.Input.MainVideo.Mute();
}
xapi.Command.Video.Layout.LayoutFamily.Set({ CustomLayoutName: 'value', LayoutFamily: 'overlay', Target: 'Local' });
xapi.Command.Video.Selfview.Set({ Mode: 'off' });
xapi.Command.Video.PresentationPIP.Set({ Position: 'UpperRight' });
xapi.Command.Video.PresentationView.Set({ View: 'Maximized' });
inCall = true;
if (storedPostDialKeys != '') {
xapi.Command.Call.DTMFSend({ DTMFString: storedPostDialKeys });
storedPostDialKeys = '';
}
}
});
xapi.Event.CallDisconnect.on(() => {
if (inCompanionMode) {
setMute(true);
inCall = false;
}
});
}
function alert(title, text, duration = 5) {
xapi.Command.UserInterface.Message.Alert.Display({
Title: title,
Text: text,
Duration: duration,
});
}
function setMute(mute) {
if (mute) {
xapi.Command.Audio.Microphones.Mute();
alert2('A script automatically muted you');
}
else {
xapi.Command.Audio.Microphones.Unmute();
}
}
function limitVolume(volume) {
if (inCompanionMode) {
if (volume > MAX_VOLUME) {
setVolume(MAX_VOLUME);
alert(
'Volume restricted',
'Max volume on this video system is restricted by a script',
);
}
}
}
function companionDeviceJoin(destination) {
console.log(`in companionDeviceJoin with destination ${destination}`)
if (PREFER_JOIN_OBTP) {
xapi.Command.Bookings.List().then(result => {
console.log(result.Booking[0].DialInfo.Calls.Call[0].Number);
xapi.Command.Dial({ Number: result.Booking[0].DialInfo.Calls.Call[0].Number });
}
);
} else if (destination != '') {
var destSplit = destination.split(':')
console.log(destSplit)
if (destSplit.length == 1) {
xapi.Command.Dial({ Number: destination });
}
else if (destSplit[0] == 'wbxmn')
xapi.Command.Webex.Join({ Number: destSplit[1] });
else if (destSplit[0] == 'instant-meeting|https') {
const dialURL = 'https:' + destSplit[1];
console.log(`about to just dial into the URL: ${dialURL}`)
xapi.Command.Webex.Join({ Number: dialURL });
}
else
console.log('Not a valid destination: ', destination)
}
else {
console.log('No destination to dial received from main codec, set PREFER_JOIN_OBTP to true if you wish to just dial scheduled meeting.')
}
}
function companionDeviceShowJoin(destination) {
if (destination != '' && destination != 'spark:instant-meeting') {
storedDestination = destination;
// Activate panel custom join panel button
xapi.Command.UserInterface.Extensions.Panel.Update(
{ PanelId: BTN_MANUAL_JOIN, Visibility: 'Auto' });
}
else {
if (destination == '')
console.log('No destination to dial received from main codec, custom Join button not shown.')
else
console.log('Instant meeting destination received from main codec, just let the regular Join button show automatically.')
}
}
function processTriggeringKeypress(destination) {
if (inCall) {
xapi.Command.Call.DTMFSend({ DTMFString: destination });
}
else {
storedPostDialKeys += destination;
}
}
function handlePanelButtonClicked(event) {
if (event.PanelId == BTN_MANUAL_JOIN) {
if (storedDestination != '') {
var destSplit = storedDestination.split(':')
if (destSplit.length == 1)
xapi.Command.Dial({ Number: storedDestination });
else if (destSplit[0] == 'wbxmn')
xapi.Command.Webex.Join({ Number: destSplit[1] });
else
console.log('Not a valid destination: ', storedDestination)
}
}
if (event.PanelId == BTN_TOGGLE) {
console.log('BTN_TOGGLE pressed');
if (inCompanionMode) {
console.log("In companion mode, toggling...")
inCompanionMode = false;
installOnButton();
setVolume(originalVolume);
}
else {
console.log("In regular mode, turning on companion...")
storeOriginalVolume();
checkInitialVolume();
inCompanionMode = true;
if (MANUAL_TOGGLE_FUNC) {
installOffButton();
}
}
}
}
function companionDeviceDisconnect() {
xapi.Command.Call.Disconnect();
storedDestination = '';
// Hide custom join panel button
xapi.Command.UserInterface.Extensions.Panel.Update(
{ PanelId: BTN_MANUAL_JOIN, Visibility: 'Hidden' });
}
async function storeOriginalVolume() {
try {
originalVolume = await xapi.Status.Audio.Volume.get();
}
catch (error) {
console.error(error);
}
}
async function checkInitialVolume() {
try {
const volume = await xapi.Status.Audio.Volume.get();
limitVolume(volume);
}
catch (error) {
console.error(error);
}
}
function handleMessage(event) {
console.log(`handleMessage: ${event.Text}`);
var eventText = event.Text;
var eventSplit = eventText.split(':');
var parsedCommand = eventSplit[0];
var destination = '';
var theProtocol = '';
if (eventSplit.length > 1) {
// the destination URL likely comes with 'spark:' or 'h323'... we want to pass on to companionDeviceJoin()
// just the destination without the protocol, but we do want to extract the protocol in case we want to make
// other decisions on it.
if (eventSplit.length == 3) {
theProtocol = eventSplit[1];
if (theProtocol == 'wbxmn') //adding this condition here for adhoc join from Triggering device with Webex icon and just the number
destination = eventSplit[1] + ':' + eventSplit[2]
else
destination = eventSplit[2];
} else if (eventSplit.length == 4) {
theProtocol = eventSplit[1];
// this is to handle when we get instruction to join
// a webex meeting with the meeting ID as destination
destination = eventSplit[2] + ':' + eventSplit[3];
}
else {
destination = eventSplit[1];
}
}
switch (parsedCommand) {
case "JoinMeeting":
companionDeviceJoin(destination);
break;
case "ShowJoinMeeting":
companionDeviceShowJoin(destination);
break;
case "DisconnectMeeting":
companionDeviceDisconnect();
break;
case "Keypress":
processTriggeringKeypress(destination);
}
}
// ---------------------- ERROR HANDLING
function handleError(error) {
console.log(error);
}
function init() {
// configure HTTP settings
xapi.config.set('HttpClient Mode', 'On').catch(handleError);
xapi.config.set('HttpClient AllowInsecureHTTPS:', 'True').catch(handleError);
xapi.config.set('HttpClient AllowHTTP:', 'True').catch(handleError);
// register callback for processing messages from main codec
xapi.event.on('Message Send', handleMessage);
storeOriginalVolume();
listenToCalls();
xapi.Status.Audio.Volume.on((volume) => {
//console.log('Volume changed to: ${volume}');
limitVolume(volume);
});
checkInitialVolume();
if (MANUAL_TOGGLE_FUNC) {
installOffButton();
}
xapi.event.on('UserInterface Extensions Panel Clicked', (event) =>
handlePanelButtonClicked(event));
xapi.Command.UserInterface.Extensions.Panel.Update(
{ PanelId: BTN_MANUAL_JOIN, Visibility: 'Hidden' });
}
init();