-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge-scanner-macro.js
87 lines (73 loc) · 2.73 KB
/
badge-scanner-macro.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
/********************************************************
*
* Macro Author: William Mills
* Technical Solutions Specialist
* Cisco Systems
*
* Version: 1-0-0
* Released: 05/28/24
*
* This is an example macro which processes Key Action events
* from an USB-HID RFID card scanner. It displays an alert
* of a successful badge scan and if in a call also attempts
* to send the data to the far end.
*
* Full Readme, source code and license details are available here:
* https://github.com/wxsd-sales/badge-scanner
*
********************************************************/
import xapi from 'xapi';
let keyCombo = []; // Buffer for storing key combos: eg. Shift + A
let keySequence = []; // Buffer for storing key sequence until an 'Enter' key is received
// Enable USB keyboard input support
xapi.Config.Peripherals.InputDevice.Mode.set('On');
// Subscribe to Key Action Events and Process them
xapi.Event.UserInterface.InputDevice.Key.Action.on(processKeyAction);
// Process Key Action events from RFID Scanner Keyboard Emulation key press sequences
function processKeyAction(event) {
console.debug(event)
const key = event.Key.split('_').pop().toLowerCase();
// Store all new keys except an 'enter' key to the keyCombo array
if (event.Type == 'Pressed' && key != 'enter') {
keyCombo.push(key)
return
}
// Transfer keyCombos to KeySequence and handle uppercasing
if (event.Type == 'Released' && key != 'enter') {
if (keyCombo.length == 2 && keyCombo[0].endsWith('shift')) {
console.log('Storing Key:', key)
keySequence.push(keyCombo.pop().toUpperCase());
keyCombo = [];
} else {
console.log('Adding to sequence Key:', keyCombo)
keySequence.push(keyCombo.pop());
}
return
}
// Once 'enter' released detected, output the collected key sequence and reset buffer
if (event.Type == 'Released' && key == 'enter') {
const keyCode = keySequence.join('')
sendScan(keyCode)
//alert('Key Sequence: ' + keyCode)
keySequence = [];
}
}
// Logs and displays alerts to the devices UI
function notification() {
console.log(text);
xapi.Command.UserInterface.Message.Alert.Display(
{ Duration: 10, Text: text, Title: 'RFID Scanner Alert' });
}
async function sendScan(keyCode) {
const call = await xapi.Status.Call.get();
const inCall = call?.[0]?.Status == 'Connected'
xapi.Command.Audio.Sound.Play({ Sound: 'Announcement' });
if (!inCall) {
console.log('Not in call - not sending badge scan');
return
}
xapi.Command.Call.FarEndMessage.Send({ CallId: call.id, Text: keyCode, Type: 'badgescan' })
.then(() => notification)
.catch(error => console.log('FarEndMessage Send Not Available'))
}