-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm-alert-kiosk.js
346 lines (252 loc) · 9.03 KB
/
alarm-alert-kiosk.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
336
337
338
339
340
341
342
343
344
345
346
import xapi from 'xapi';
// This macro will monitor the T3 Alarm Detection status on your Webex Device.
// Once an alarm event has been detected, it will end any active calls and
// display the alert URL. You can create a custom alert web page and change
// the example alert URL to yours if you want, just ensure the Webex Device
// can reach the site from the network its connected to.
// If your device is in Kiosk Mode, the Kiosk URL will be changed to the alert
// URL and back to the Kiosk URL after the alert event has ended.
// Specify the URL alert you wish to display
const ALERT_URL = 'https://wxsd-sales.github.io/alarm-alert-macro/';
// Allow the user to dismiss the notication and return to normal
const ALLOW_DISMISS = true;
// The macro can automatically end any active calls
const END_CAll = true;
/////////////////////////////////////////////
////////////.Do not change below ////////////
/////////////////////////////////////////////
// Activate the alert notifications
async function activateAlert() {
// End any active calls
if (END_CAll) {
const calls = await xapi.Status.Call.get()
if(calls.length > 0) {
console.log('Call present, ending call first');
xapi.Command.Call.Disconnect();
return;
}
}
// Check to see if this device has kiosk mode enable
// and change the Kiosk URL to the Alert URL
try {
const kiosk = await xapi.Config.UserInterface.Kiosk.Mode.get();
if (kiosk === 'On') {
console.log('Kiosk mode enabled, setting Kiosk URL to Alert URL')
const kioskURL = await xapi.Config.UserInterface.Kiosk.Mode.URL.get();
console.log('Saving Kiosk URL for later recovery');
mem.write('KioskURL', kioskURL);
xapi.Config.UserInterface.Kiosk.Mode.URL.set(ALERT_URL);
return;
}
} catch {
console.log('Kiosk Mode not available on this device');
}
console.log('Displaying Web View: ' +ALERT_URL);
xapi.Command.UserInterface.WebView.Display(
{ Url: ALERT_URL });
if (ALLOW_DISMISS) {
displayPrompt();
}
}
// Monitor call disconnects and activate the alert
// if there is still an active alarm
async function callDisconnect(event) {
console.log('Call Disconnect occured');
console.log(event);
const active = await xapi.Status.RoomAnalytics.T3Alarm.Detected.get();
if (active === 'True') {
console.log('Active still active');
setTimeout(activateAlert, 1000);
} else {
console.log('No active alarms');
deactiveAlert();
}
}
// This function removes the http/s portions of urls
function removeHttp(url) {
if (url.startsWith('https://')) {
const https = 'https://';
return url.slice(https.length);
}
if (url.startsWith('http://')) {
const http = 'http://';
return url.slice(http.length);
}
return url;
}
// This function will compare a URL against the Alert URL
function activeURL(url) {
const urlNoHTTP = removeHttp(url);
const alertNoHTTP = removeHttp(ALERT_URL);
return urlNoHTTP.indexOf(alertNoHTTP) != -1;
}
// This function will deactivate any alert that may be present
async function deactiveAlert() {
// First check if kisk mode is enabled and change alert URL
// back the the saved kiosk URL
try {
const kiosk = await xapi.Config.UserInterface.Kiosk.Mode.get();
if (kiosk === 'On') {
const kioskURL = await xapi.Config.UserInterface.Kiosk.Mode.URL.get();
if (activeURL(kioskURL)) {
console.log('Kiosk URL is the Alert URL');
const storedURL = mem.read('KioskURL');
console.log('Restoring stored Kiosk URL: ' + storedURL);
xapi.Config.UserInterface.Kiosk.Mode.URL.set(storedURL);
return;
} else {
console.log('Alarm URL not active');
return;
}
}
} catch {
console.log('Kiosk mode not available on this device');
}
const webViews = await xapi.Status.UserInterface.WebView.get();
for (let i = 0; i < webViews.length; i++) {
if(activeURL(webViews[i].URL)) {
console.log('Alert URL is active, clearing')
xapi.Command.UserInterface.WebView.Clear();
xapi.Command.UserInterface.Message.Prompt.Clear(
{ FeedbackId: 'dismiss_alarm' });
}
}
xapi.Command.UserInterface.Message.Prompt.Clear(
{ FeedbackId: 'dismiss_alarm' });
}
// This function will process alarm events
function alarmEvent(active) {
if (active === 'True') {
console.log('Alarm active, activating alert')
activateAlert();
} else {
console.log('Alarm deactived, deactive alert')
deactiveAlert();
}
}
// Display a user prompt with the option to dismiss the alarm notification
function displayPrompt() {
console.log('Displaying dismiss prompt');
xapi.Command.UserInterface.Message.Prompt.Display(
{
Title: 'Alarm Detected',
Text: 'Alarm detected, tap below to dismiss notification',
FeedbackId: 'dismiss_alarm',
"Option.1": 'Dismiss notification'
});
}
// Process the user repsonse
function promptResponse(event) {
if (event.FeedbackId === 'dismiss_alarm' && event.OptionId == 1 ) {
deactiveAlert();
}
}
async function init() {
// Initialize persistent memory
await memoryInit()
// Enable Web Engine
xapi.Config.WebEngine.Mode.set('On');
// Enable the T3 Alarm Dection feature
xapi.Config.RoomAnalytics.T3AlarmDetection.Mode.set('On');
// Check if there is already an active alarm
const active = await xapi.Status.RoomAnalytics.T3Alarm.Detected.get();
if (active === 'True') {
console.log('Active alarm detected');
activateAlert();
} else {
console.log('No active alarms, deactivating any existing alerts');
deactiveAlert();
}
// Subscribe to all future alarm events
xapi.Status.RoomAnalytics.T3Alarm.Detected.on(alarmEvent);
// Listen for message prompt repsonses if this is enabled
if (ALLOW_DISMISS) {
xapi.Event.UserInterface.Message.Prompt.Response.on(promptResponse);
}
xapi.Event.CallDisconnect.on(callDisconnect);
}
init();
//--__--__--__--__--__--__
//Memory Related Macros
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
var mem = {
"localScript": module.name.replace('./', '')
};
function memoryInit() {
return new Promise((resolve) => {
xapi.command('macros macro get', {
Name: "Memory_Storage"
}).then(() => {
resolve();
}).catch(e => {
xapi.Command.UserInterface.Message.Alert.Display({
Title: '⚠ Setting up USB mode ⚠',
Text: 'Set-up detected, running initial USB mode check<p>Please Wait until this prompt clears. Approximate Wait 25-30 seconds'
})
console.debug('Uh-Oh, no storage Macro found, building "' + "Memory_Storage");
xapi.command('macros macro save', {
Name: "Memory_Storage"
},
`var memory = {\n\t"./_$Info": {\n\t\t"Warning": "Do NOT modify this document, as other Scripts/Macros may rely on this information", \n\t\t"AvailableFunctions": {\n\t\t\t"local": ["mem.read('key')", "mem.write('key', 'value')", "mem.remove('key')", "mem.print()"],\n\t\t\t"global": ["mem.read.global('key')", "mem.write.global('key', 'value')", "mem.remove.global('key')", "mem.print.global()"]\n\t\t},\n\t\t"Guide": "https://github.com/Bobby-McGonigle/Cisco-RoomDevice-Macro-Projects-Examples/tree/master/Macro%20Memory%20Storage"\n\t},\n\t"ExampleKey": "Example Value"\n}`
).then(() => {
sleep(500).then(() => {
xapi.Command.Macros.Runtime.Restart();
})
});
});
});
};
mem.read = function (key) {
return new Promise((resolve, reject) => {
xapi.command('Macros Macro Get', {
Content: 'True',
Name: "Memory_Storage"
}).then((event) => {
let raw = event.Macro[0].Content.replace(/var.*memory.*=\s*{/g, '{')
let store = JSON.parse(raw)
let temp;
if (store[mem.localScript] == undefined) {
store[mem.localScript] = {}
temp = store[mem.localScript]
} else {
temp = store[mem.localScript]
}
if (temp[key] != undefined) {
resolve(temp[key])
} else {
reject(new Error('Local Read Error. Object Key: "' + key + '" not found in \'' + "Memory_Storage" + '\' from script "' + mem.localScript + '"'))
}
})
});
}
mem.write = function (key, value) {
return new Promise((resolve) => {
xapi.command('Macros Macro Get', {
Content: 'True',
Name: "Memory_Storage"
}).then((event) => {
let raw = event.Macro[0].Content.replace(/var.*memory.*=\s*{/g, '{');
let store = JSON.parse(raw);
let temp;
if (store[mem.localScript] == undefined) {
store[mem.localScript] = {};
temp = store[mem.localScript];
} else {
temp = store[mem.localScript]
};
temp[key] = value;
store[mem.localScript] = temp;
let newStore = JSON.stringify(store, null, 4);
xapi.command('Macros Macro Save', {
Name: "Memory_Storage"
},
`var memory = ${newStore}`
).then(() => {
console.debug('Local Write Complete => "' + mem.localScript + '" : {"' + key + '" : "' + value + '"}');
resolve(value);
});
});
});
};