forked from CiscoDevNet/roomdevices-macros-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppleTV.js
86 lines (74 loc) · 2.54 KB
/
AppleTV.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
const xapi = require('xapi');
const CEC_LOGICAL_ADDRESS_FOR_APPLETV = 4;
const CODEC_CONNECTOR_ID_WHERE_APPLE_TV_IS_CONNECTED = 2;
const signinsequence = [ 'Right','Right','Right','Right', 'Ok', 'Left', 'Left', 'Left','Ok'];
function sendCEC(key){
var cecstring = 'Video CEC Input KeyClick ConnectorId: ' + CODEC_CONNECTOR_ID_WHERE_APPLE_TV_IS_CONNECTED + ' LogicalAddress:' + CEC_LOGICAL_ADDRESS_FOR_APPLETV + ' NamedKey: ' + key;
xapi.command('Video CEC Input KeyClick', {ConnectorId: CODEC_CONNECTOR_ID_WHERE_APPLE_TV_IS_CONNECTED, LogicalAddress:CEC_LOGICAL_ADDRESS_FOR_APPLETV, NamedKey: key});
console.log(`CEC command sent:` + cecstring);
}
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
// Usage!
function sendCECSequence(params){
let WAIT = 500;
let offset = 0;
console.log(`waiting:` + WAIT);
for (var i=0; i<=params.length; i++) {
sleep(offset+=WAIT).then(() => { sendCEC(params[i]);});
console.log(`waited:` + WAIT);
}
}
xapi.event.on('UserInterface Extensions Page Action', (event) => {
if(event.PageId == 'AppleTV'){
if(event.Type == 'Opened'){
console.log(`AppleTV was opened`);
xapi.command('Presentation Start', {ConnectorId: CODEC_CONNECTOR_ID_WHERE_APPLE_TV_IS_CONNECTED});
}
else{
console.log(`AppleTV was closed`);
xapi.command('Presentation Stop');
}
}
});
xapi.event.on('UserInterface Extensions Widget Action', (event) => {
if(event.WidgetId == 'appletv_navigator'){
if(event.Type == 'pressed'){
switch(event.Value){
case 'right':
sendCEC('Right');
break;
case 'left':
sendCEC('Left');
break;
case 'up':
sendCEC('Up');
break;
case 'down':
sendCEC('Down');
break;
case 'center':
sendCEC('Ok');
break;
default:
console.log(`Unhandled Navigation`);
}
}
}
else if(event.WidgetId == 'appletv_menu'){
if(event.Type == 'clicked'){
sendCEC('Back');
}
}
else if(event.WidgetId == 'appletv_play'){
if(event.Type == 'clicked'){
sendCEC('Play');
}
}
else if(event.WidgetId == 'appletv_signin'){
if(event.Type == 'clicked'){
sendCECSequence.apply(this, signinsequence);
}
}
});