-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbmc-client.js
138 lines (121 loc) · 4 KB
/
xbmc-client.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
// Generated by CoffeeScript 1.4.0
(function() {
var Emitter, XbmcClient, buttons,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Emitter = require('emitter');
buttons = {
Player: ['PlayPause', 'Stop'],
Input: ['Up', 'Down', 'Left', 'Right', 'Select', 'Back', 'Home']
};
XbmcClient = (function(_super) {
var arr, button, method, _fn, _i, _len,
_this = this;
__extends(XbmcClient, _super);
function XbmcClient(ip, port) {
this.ip = ip;
this.port = port;
this.requests = {};
this.id = 0;
this.player = null;
this.connect(this.ip, this.port);
this.listen();
}
XbmcClient.prototype.connect = function(ip, port) {
var _this = this;
this.ws = new WebSocket('ws://' + ip + ':' + port + '/jsonrpc');
return this.ws.addEventListener('open', function() {
return _this.getActivePlayers();
});
};
XbmcClient.prototype.sendRequest = function(data, cb) {
this.id++;
if (cb != null) {
this.requests[this.id] = cb;
}
data.id = this.id;
data.jsonrpc = '2.0';
return this.ws.send(JSON.stringify(data));
};
XbmcClient.prototype.listen = function() {
var _this = this;
return this.ws.addEventListener('message', function(e) {
var data, _ref, _ref1, _ref2;
try {
data = JSON.parse(e.data);
if (data.result != null) {
if (data.id !== void 0) {
if (_this.requests[_this.id] !== void 0) {
_this.requests[_this.id](data.result);
delete _this.requests[_this.id];
}
}
}
switch (data.method) {
case 'Player.OnPlay':
_this.player = (_ref = data.params) != null ? (_ref1 = _ref.data) != null ? (_ref2 = _ref1.player) != null ? _ref2.playerid : void 0 : void 0 : void 0;
return _this.emit('play');
case 'Player.OnStop':
_this.player = null;
return _this.emit('stop');
case 'Player.OnPause':
return _this.emit('pause');
}
} catch (ex) {
throw ex;
}
});
};
XbmcClient.prototype.getActivePlayers = function() {
var _this = this;
return this.sendRequest({
method: 'Player.GetActivePlayers'
}, function(data) {
if (data.length > 0) {
return _this.player = data[0].playerid;
} else {
return _this.player = null;
}
});
};
XbmcClient.prototype.sendPlayerRequest = function(data, cb) {
if (data.params === void 0) {
data.params = {};
}
if (this.player != null) {
data.params.playerid = this.player;
}
return this.sendRequest(data, cb);
};
XbmcClient.prototype.sendPlayerButton = function(button, cb) {
return this.sendPlayerRequest({
method: 'Player.' + button
}, cb);
};
XbmcClient.prototype.sendButton = function(method, button, cb) {
return this.sendRequest({
method: method + "." + button
}, cb);
};
for (method in buttons) {
arr = buttons[method];
_fn = function(method, button) {
var str;
str = button.toLowerCase().substr(0, 1) + button.substr(1);
return XbmcClient.prototype[str] = function(cb) {
if (method === 'Player') {
return this.sendPlayerButton(button, cb);
} else {
return this.sendButton(method, button, cb);
}
};
};
for (_i = 0, _len = arr.length; _i < _len; _i++) {
button = arr[_i];
_fn(method, button);
}
}
return XbmcClient;
}).call(this, Emitter);
module.exports = XbmcClient;
}).call(this);