-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotdesk-lock.js
169 lines (152 loc) · 5.39 KB
/
hotdesk-lock.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
/********************************************************
*
* Macro Author: William Mills
* Technical Solutions Specialist
* wimills@cisco.com
* Cisco Systems
*
* Version: 1-0-1
* Released: 06/17/23
*
* This is an example Webex Device macro which locks your
* device using a PIN code while in an active hotdesk session
*
* Full Readme, source code and license agreement available on Github:
* https://github.com/wxsd-sales/hotdesk-lock-macro
*
********************************************************/
import xapi from 'xapi';
const config = {
maxattempts: 3, // Max PIN attempts before auto sign out
button: {
name: 'Lock Device', // Name of Lock Button
color: '#eb4034', // Color of button if using non custom icon
icon: 'https://wxsd-sales.github.io/kiosk-demos/icons/lockpad.png' // Default Icon or a URL to a PNG for custom
},
panelId: 'lockButton' // Base Panel Id used for auto UI Creation and Processing
}
/*********************************************************
* Create Panel and Subscribe to Status Changes and Events
**********************************************************/
createPanel();
xapi.Event.UserInterface.Extensions.Panel.Clicked.on(processClicks)
xapi.Event.UserInterface.Message.TextInput.Response.on(processTextInputResponse)
xapi.Event.UserInterface.Message.TextInput.Clear.on(processTextInputClear)
xapi.Status.Webex.DevicePersonalization.Hotdesking.SessionStatus.on(processHotdeskSession);
xapi.Status.Standby.State.on(processStandbyChange)
/*********************************************************
* Main functions of the macro
**********************************************************/
let pin = '';
let attempts = 0;
function processClicks(event) {
if (event.PanelId != config.panelId) return;
if (pin === '') {
askForPIN(true)
} else {
xapi.Command.Standby.Halfwake();
}
}
function processTextInputResponse(event) {
switch (event.FeedbackId) {
case 'pin-code-response':
if (event.Text == pin) {
console.log('PIN valid')
attempts = 0;
return;
}
console.log('Invalid PIN')
if (attempts == config.maxattempts) {
console.log('Max PIN attempts reached, logging out of hotdesk session')
xapi.Command.Webex.Registration.Logout();
} else {
attempts = attempts + 1;
askForPIN();
}
break;
case 'pin-code-setup':
if (event.Text.length >= 4) {
pin = event.Text;
xapi.Command.Standby.Halfwake();
} else {
xapi.Command.UserInterface.Message.Alert.Display(
{ Duration: 8, Text: 'The PIN must be a minimum of 4-digits', Title: 'Invalid PIN' });
}
break;
}
}
function processTextInputClear(event) {
if (event.FeedbackId != 'pin-code-response') return;
xapi.Command.Standby.Halfwake();
}
function processHotdeskSession(state) {
switch (state) {
case 'Available':
console.log('Device Available - hiding lock button');
xapi.Command.UserInterface.Extensions.Panel.Update({ PanelId: config.panelId, Visibility: 'Hidden' });
break;
case 'Reserved':
console.log('Device Reserved - displaying lock button');
xapi.Command.UserInterface.Extensions.Panel.Update({ PanelId: config.panelId, Visibility: 'Auto' });
break;
}
pin = '';
}
async function processStandbyChange(state) {
if (state != 'Off') return;
if (pin === '') return;
const sessionStatus = await getHotdeskingStatus();
if (sessionStatus != 'Reserved') return;
askForPIN();
}
function getHotdeskingStatus(){
return xapi.Status.Webex.DevicePersonalization.Hotdesking.SessionStatus.get()
.catch(error => {return 'Available'})
}
function askForPIN(setup = false) {
const requestText = (attempts > 0) ?
`Please Enter PIN<br>Attempt (${attempts}/${config.maxattempts})` :
'Please Enter PIN';
xapi.Command.UserInterface.Message.TextInput.Display({
FeedbackId: setup ? 'pin-code-setup' : 'pin-code-response',
InputType: 'PIN',
Placeholder: setup ? 'Please set a new PIN' : 'Please Enter PIN',
SubmitText: 'Submit',
Text: setup ? 'Please set a PIN before the device can be locked<br>minimum 4-digits' : requestText,
Title: setup ? 'Create PIN' : 'Device Locked'
});
}
function getIcon(url) {
return xapi.Command.UserInterface.Extensions.Icon.Download({ Url: url })
.then(result => result.IconId)
.catch(error => {
console.log('Unable to download icon: ' + error.message)
return false
})
}
async function createPanel() {
let icon = '';
if (config.button.icon.startsWith('http')) {
const iconId = await getIcon(config.button.icon)
icon = `<Icon>Custom</Icon>
<CustomIcon>
<Id>${iconId}</Id>
</CustomIcon>`
} else {
icon = `<Icon>${config.button.icon}</Icon>`
}
const panel = `<Extensions>
<Panel>
<Location>HomeScreen</Location>
<Type>Home</Type>
${icon}
<Color>${config.button.color}</Color>
<Name>${config.button.name}</Name>
<ActivityType>Custom</ActivityType>
</Panel>
</Extensions>`;
xapi.Command.UserInterface.Extensions.Panel.Save({ PanelId: config.panelId }, panel)
.then(result => {
getHotdeskingStatus().then(state => processHotdeskSession(state))
})
}