forked from estbeetoo/node-red-contrib-kodi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkodi.js
271 lines (242 loc) · 9.81 KB
/
kodi.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
/**
* Created by aborovsky on 27.08.2015.
*/
var util = require('util');
var DEBUG = false;
var connectionFSM = require('./lib/connectionFSM.js');
module.exports = function (RED) {
/**
* ====== Kodi-controller ================
* Holds configuration for kodijs host+port,
* initializes new kodijs connections
* =======================================
*/
function KodiControllerNode(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
this.host = config.host;
this.port = config.port;
this.kodi = null;
var node = this;
/**
* Initialize an kodijs socket, calling the handler function
* when successfully connected, passing it the kodijs connection
*/
this.initializeKodiConnection = function (handler) {
if (node.kodi) {
DEBUG && RED.comms.publish("debug", {
name: node.name,
msg: 'already configured connection to Kodi player at ' + config.host + ':' + config.port
});
if (handler && (typeof handler === 'function')) {
if (node.kodi.connection)
handler(node.kodi);
else
node.kodi.on('connected', function () {
handler(node.kodi);
});
}
return node.kodi;
}
node.log('configuring connection to Kodi player at ' + config.host + ':' + config.port);
node.kodi = new connectionFSM({
host: config.host,
port: config.port,
debug: DEBUG
});
if (handler && (typeof handler === 'function')) {
node.kodi.on('connected', function () {
handler(node.kodi);
});
}
node.kodi.connect();
DEBUG && RED.comms.publish("debug", {
name: node.name,
msg: 'Kodi: successfully connected to ' + config.host + ':' + config.port
});
return node.kodi;
};
this.on("close", function () {
node.log('disconnecting from kodijs server at ' + config.host + ':' + config.port);
node.kodi && node.kodi.disconnect && node.kodi.disconnect();
node.kodi = null;
});
}
RED.nodes.registerType("kodi-controller", KodiControllerNode);
/**
* ====== Kodi-out =======================
* Sends outgoing Kodi player from
* messages received via node-red flows
* =======================================
*/
function KodiOut(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
var controllerNode = RED.nodes.getNode(config.controller);
this.unit_number = config.unit_number;
this.kodicommand = config.kodicommand;
var node = this;
//node.log('new Kodi-out, config: ' + util.inspect(config));
//
this.on("input", function (msg) {
DEBUG && RED.comms.publish("debug", {
name: node.name,
msg: 'kodiout.onInput msg[' + util.inspect(msg) + ']'
});
//node.log('kodiout.onInput msg[' + util.inspect(msg) + ']');
if (!(msg && msg.hasOwnProperty('payload'))) return;
var payload = msg.payload;
if (typeof(msg.payload) === "object") {
payload = msg.payload;
} else if (typeof(msg.payload) === "string") {
try {
payload = JSON.parse(msg.payload);
if (typeof (payload) === 'number')
payload = {cmd: msg.payload.toString()};
} catch (e) {
payload = {cmd: msg.payload.toString()};
}
}
if (payload == null) {
node.log('kodiout.onInput: illegal msg.payload!');
return;
}
if (node.kodicommand && node.kodicommand !== 'empty') {
try {
payload = JSON.parse(node.kodicommand);
if (typeof (payload) === 'number')
payload = {cmd: node.kodicommand.toString()};
} catch (e) {
payload = {cmd: node.kodicommand.toString()};
}
}
node.send(payload, function (err) {
if (err) {
node.error('send error: ' + err);
}
if (typeof(msg.cb) === 'function')
msg.cb(err);
});
});
this.on("close", function () {
node.log('kodiOut.close');
});
node.status({fill: "yellow", shape: "dot", text: "inactive"});
function nodeStatusConnected() {
node.status({fill: "green", shape: "dot", text: "connected"});
}
function nodeStatusDisconnected() {
node.status({fill: "red", shape: "dot", text: "disconnected"});
}
function nodeStatusReconnect() {
node.status({fill: "yellow", shape: "ring", text: "reconnecting"});
}
function nodeStatusConnecting() {
node.status({fill: "green", shape: "ring", text: "connecting"});
}
controllerNode.initializeKodiConnection(function (fsm) {
if (fsm.connected)
nodeStatusConnected();
else
nodeStatusDisconnected();
fsm.off('connecting', nodeStatusConnecting);
fsm.on('connecting', nodeStatusConnecting);
fsm.off('connected', nodeStatusConnected);
fsm.on('connected', nodeStatusConnected);
fsm.off('disconnected', nodeStatusDisconnected);
fsm.on('disconnected', nodeStatusDisconnected);
fsm.off('reconnect', nodeStatusReconnect);
fsm.on('reconnect', nodeStatusReconnect);
});
this.send = function (data, callback) {
DEBUG && RED.comms.publish("debug", {name: node.name, msg: 'send data[' + JSON.stringify(data) + ']'});
//node.log('send data[' + data + ']');
// init a new one-off connection from the effectively singleton KodiController
// there seems to be no way to reuse the outgoing conn in adreek/node-kodijs
controllerNode.initializeKodiConnection(function (fsm) {
try {
DEBUG && RED.comms.publish("debug", {name: node.name, msg: "send: " + JSON.stringify(data)});
data.cmd = data.cmd || data.method;
data.args = data.args || data.params;
fsm.connection.run(data.cmd, data.args).then(function () {
callback && callback();
}, function (err) {
callback && callback(err);
});
}
catch (err) {
node.error('error calling send: ' + err);
callback(err);
}
});
}
}
//
RED.nodes.registerType("kodi-out", KodiOut);
/**
* ====== Kodi-IN ========================
* Handles incoming Global Cache, injecting
* json into node-red flows
* =======================================
*/
function KodiIn(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
this.connection = null;
var node = this;
//node.log('new KodiIn, config: %j', config);
var controllerNode = RED.nodes.getNode(config.controller);
/* ===== Node-Red events ===== */
function nodeStatusConnecting() {
node.status({fill: "green", shape: "ring", text: "connecting"});
}
function nodeStatusConnected() {
node.status({fill: "green", shape: "dot", text: "connected"});
}
function nodeStatusDisconnected() {
node.status({fill: "red", shape: "dot", text: "disconnected"});
}
function nodeStatusReconnect() {
node.status({fill: "yellow", shape: "ring", text: "reconnecting"});
}
function bindNotificationListeners(connection) {
function getListenerForNotification(notification) {
return function (data) {
node.receiveNotification(notification, data);
}
}
Object.keys(connection.schema.schema.notifications).forEach(function (method) {
connection.schema.schema.notifications[method](getListenerForNotification(method));
});
}
node.receiveNotification = function (notification, data) {
DEBUG && RED.comms.publish("debug", {
name: node.name,
msg: 'kodi event data[' + JSON.stringify(data) + ']'
});
node.send({
topic: 'kodi',
payload: {
'notification': notification,
'data': data
}
});
};
controllerNode.initializeKodiConnection(function (fsm) {
bindNotificationListeners(fsm.connection);
if (fsm.connected)
nodeStatusConnected();
else
nodeStatusDisconnected();
fsm.off('connecting', nodeStatusConnecting);
fsm.on('connecting', nodeStatusConnecting);
fsm.off('connected', nodeStatusConnected);
fsm.on('connected', nodeStatusConnected);
fsm.off('disconnected', nodeStatusDisconnected);
fsm.on('disconnected', nodeStatusDisconnected);
fsm.off('reconnect', nodeStatusReconnect);
fsm.on('reconnect', nodeStatusReconnect);
});
}
RED.nodes.registerType("kodi-in", KodiIn);
}