-
Notifications
You must be signed in to change notification settings - Fork 21
/
lgtv.js
516 lines (468 loc) · 18.5 KB
/
lgtv.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* jshint -W097 */
/* jshint strict:false */
/* global require */
/* global RRule */
/* global __dirname */
/* jslint node: true */
'use strict';
var fs = require('fs'); // for storing client key
var utils = require('@iobroker/adapter-core');
var adapter;
var LGTV = require('lgtv2');
var wol = require('wol');
var pollTimerChannel = null;
var pollTimerOnlineStatus = null;
var pollTimerInput = null;
var pollTimerGetSoundOutput = null;
var isConnect = false;
var lgtvobj;
function startAdapter(options) {
options = options || {};
Object.assign(options,{
name: "lgtv",
stateChange: function (id, state) {
if (id && state && !state.ack) {
id = id.substring(adapter.namespace.length + 1);
adapter.log.debug('State change "' + id + '" - VALUE: ' + state.val);
switch (id) {
case 'states.popup':
adapter.log.debug('Sending popup message "' + state.val + '" to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.notifications/createToast', {message: state.val}, function (err, val) {
if (!err) adapter.setState('states.popup', state.val, true);
});
break;
case 'states.turnOff':
adapter.log.debug('Sending turn OFF command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system/turnOff', {message: state.val}, function (err, val) {
if (!err) adapter.setState('states.turnOff', state.val, true);
});
break;
case 'states.power':
adapter.log.debug('Sending turn OFF command to WebOS TV: ' + adapter.config.ip);
if(!state.val){
sendCommand('ssap://system/turnOff', {message: state.val}, function (err, val){
if (!err) adapter.setState('states.power', state.val, true);
});
} else {
adapter.getState(adapter.namespace + '.states.mac', function (err, state){
adapter.log.debug('GetState mac: ' + JSON.stringify(state));
if(state){
wol.wake(state.val, function (err, res){
if (!err) adapter.log.debug('Send WOL to MAC: {' + state.val + '} OK');
});
} else {
adapter.log.error('Error get MAC address TV. Please turn on the TV manually first!');
}
});
}
break;
case 'states.mute':
adapter.log.debug('Sending mute ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://audio/setMute', {mute: state.val}, function (err, val) {
if (!err) adapter.setState('states.mute', state.val, true);
});
break;
case 'states.volume':
adapter.log.debug('Sending volume change ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://audio/setVolume', {volume: state.val}, function (err, val) {
if (!err){}
});
break;
case 'states.volumeUp':
adapter.log.debug('Sending volumeUp ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://audio/volumeUp', null, function (err, val) {
if (!err) adapter.setState('states.volumeUp', !!state.val, true);
});
break;
case 'states.volumeDown':
adapter.log.debug('Sending volumeDown ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://audio/volumeDown', null, function (err, val) {
if (!err) adapter.setState('states.volumeDown', !!state.val, true);
});
break;
case 'states.channel':
adapter.log.debug('Sending switch to channel ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://tv/openChannel', {channelNumber: state.val}, function (err, val) {
if (!err)
adapter.setState('states.channel', state.val, true);
else
adapter.log.debug('Error in switching to channel: ' + err);
});
break;
case 'states.channelUp':
adapter.log.debug('Sending channelUp ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://tv/channelUp', null, function (err, val) {
if (!err) adapter.setState('states.channelUp', !!state.val, true);
});
break;
case 'states.channelDown':
adapter.log.debug('Sending channelDown ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://tv/channelDown', null, function (err, val) {
if (!err) adapter.setState('states.channelDown', !!state.val, true);
});
break;
case 'states.mediaPlay':
adapter.log.debug('Sending mediaPlay ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://media.controls/play', null, function (err, val) {
if (!err) adapter.setState('states.mediaPlay', !!state.val, true);
});
break;
case 'states.mediaPause':
adapter.log.debug('Sending mediaPause ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://media.controls/pause', null, function (err, val) {
if (!err) adapter.setState('states.mediaPause', !!state.val, true);
});
break;
case 'states.openURL':
adapter.log.debug('Sending open ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/open', {target: state.val}, function (err, val) {
if (!err) adapter.setState('states.openURL', state.val, true);
});
break;
case 'states.mediaStop':
adapter.log.debug('Sending mediaStop ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://media.controls/stop', null, function (err, val) {
if (!err) adapter.setState('states.mediaStop', !!state.val, true);
});
break;
case 'states.mediaFastForward':
adapter.log.debug('Sending mediaFastForward ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://media.controls/fastForward', null, function (err, val) {
if (!err) adapter.setState('states.mediaFastForward', !!state.val, true);
});
break;
case 'states.mediaRewind':
adapter.log.debug('Sending mediaRewind ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://media.controls/rewind', null, function (err, val) {
if (!err) adapter.setState('states.mediaRewind', !!state.val, true);
});
break;
case 'states.3Dmode':
adapter.log.debug('Sending 3Dmode ' + state.val + ' command to WebOS TV: ' + adapter.config.ip);
switch (state.val) {
case true:
sendCommand('ssap://com.webos.service.tv.display/set3DOn', null, function (err, val) {
if (!err) adapter.setState('states.3Dmode', !!state.val, true);
});
break;
case false:
sendCommand('ssap://com.webos.service.tv.display/set3DOff', null, function (err, val) {
if (!err) adapter.setState('states.3Dmode', !!state.val, true);
});
break;
}
break;
case 'states.launch':
adapter.log.debug('Sending launch command ' + state.val + ' to WebOS TV: ' + adapter.config.ip);
switch (state.val) {
case 'livetv':
adapter.log.debug('Switching to LiveTV on WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/launch', {id: "com.webos.app.livetv"}), function (err, val) {
if (!err) adapter.setState('states.launch', state.val, true);
};
break;
case 'smartshare':
adapter.log.debug('Switching to SmartShare App on WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/launch', {id: "com.webos.app.smartshare"}), function (err, val) {
if (!err) adapter.setState('states.launch', state.val, true);
};
break;
case 'tvuserguide':
adapter.log.debug('Switching to TV Userguide App on WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/launch', {id: "com.webos.app.tvuserguide"}), function (err, val) {
if (!err) adapter.setState('states.launch', state.val, true);
};
break;
case 'netflix':
adapter.log.debug('Switching to Netflix App on WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/launch', {id: "netflix"}), function (err, val) {
if (!err) adapter.setState('states.launch', state.val, true);
};
break;
case 'youtube':
adapter.log.debug('Switching to Youtube App on WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/launch', {id: "youtube.leanback.v4"}), function (err, val) {
if (!err) adapter.setState('states.launch', state.val, true);
};
break;
case 'prime':
adapter.log.debug('Switching to Amazon Prime App on WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/launch', {id: "lovefilm.de"}), function (err, val) {
if (!err) adapter.setState('states.launch', state.val, true);
};
break;
case 'amazon':
adapter.log.debug('Switching to Amazon Prime App on WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/launch', {id: "amazon"}), function (err, val) {
if (!err) adapter.setState('states.launch', state.val, true);
};
break;
default:
//state.val = '"' + state.val + '"';
adapter.log.debug('Opening app ' + state.val + ' on WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://system.launcher/launch', {id: state.val}), function (err, val)
{
if (!err)
adapter.setState('states.launch', state.val, true);
else adapter.log.debug('Error opening app ' + state.val + ' on WebOS TV: ' + adapter.config.ip);
};
break;
}
break;
case 'states.input':
adapter.log.debug('Sending switch to input "' + state.val + '" command to WebOS TV: ' + adapter.config.ip);
sendCommand('ssap://tv/switchInput', {inputId: state.val}, function (err, val) {
if (!err) adapter.setState('states.input', state.val, true);
});
break;
case 'states.raw':
adapter.log.debug('Sending raw command api "' + state.val + '" to WebOS TV: ' + adapter.config.ip);
sendCommand(state.val[url], state.val[cmd], function (err, val) {
if (!err) adapter.setState('states.raw', state.val, true);
});
break;
case 'states.youtube':
var uri = state.val;
if(!~uri.indexOf('http')){
uri = 'https://www.youtube.com/watch?v=' + uri;
}
sendCommand('ssap://system.launcher/launch', {id: 'youtube.leanback.v4', contentId: uri}, function (err, val) {
if (!err) adapter.setState('states.youtube', state.val, true);
});
break;
case 'states.drag':
// The event type is 'move' for both moves and drags.
var vals = state.val.split(",");
var dx = parseInt(vals[0]);
var dy = parseInt(vals[1]);
sendCommand('move', { dx: dx, dy: dy, drag: vals[2] === 'drag' ? 1 : 0}, function (err, val) {
if (!err) adapter.setState(id, state.val, true);
});
break;
case 'states.scroll':
var vals = state.val.split(",");
var dx = parseInt(vals[0]);
var dy = parseInt(vals[1]);
sendCommand('scroll', { dx: dx, dy: dy }, function (err, val) {
if (!err) adapter.setState(id, state.val, true);
});
break;
case 'states.click':
sendCommand('click', {}, function (err, val) {
if (!err) adapter.setState(id, state.val, true);
});
break;
case 'states.soundOutput':
sendCommand('ssap://com.webos.service.apiadapter/audio/changeSoundOutput', { output: state.val}, function (err, val) {
if (!err) adapter.setState(id, state.val, true);
});
break;
default:
if(~id.indexOf('remote')){
adapter.log.error('State change "' + id + '" - VALUE: ' + JSON.stringify(state));
var ids = id.split(".");
var key = ids[ids.length - 1].toString().toUpperCase();
sendCommand('button', { name: key }, function (err, val) {
if (!err) adapter.setState(id, state.val, true); // ?
});
}
break;
}
}
},
unload: function (callback) {
lgtvobj.disconnect();
callback();
},
ready: function () {
main();
}
});
adapter = new utils.Adapter(options);
return adapter;
}
function connect(cb){
lgtvobj = new LGTV({
url: 'ws://' + adapter.config.ip + ':3000',
timeout: adapter.config.timeout,
reconnect: true,
keyfile: 'lgtvkeyfile'
});
lgtvobj.on('connecting', function (host){
adapter.log.debug('Connecting to WebOS TV: ' + host);
adapter.setState('info.connection', false, true);
clearIntervals();
});
lgtvobj.on('close', function (e){
adapter.log.debug('Connection closed: ' + e);
adapter.setState('info.connection', false, true);
});
lgtvobj.on('prompt', function (){
adapter.log.debug('Waiting for pairing confirmation on WebOS TV ' + adapter.config.ip);
});
lgtvobj.on('error', function (error){
adapter.log.debug('Error on connecting or sending command to WebOS TV: ' + error);
adapter.setState('info.connection', false, true);
});
lgtvobj.on('connect', function (error, response){
adapter.log.debug('WebOS TV Connected');
isConnect = true;
clearIntervals();
adapter.setState('info.connection', true, true);
lgtvobj.subscribe('ssap://audio/getVolume', function(err, res){
adapter.log.debug('audio/getVolume: ' + JSON.stringify(res));
if (~res.changed.indexOf('volume')) {
adapter.setState('states.volume', parseInt(res.volume), true);
}
if (~res.changed.indexOf('muted')) {
adapter.setState('states.mute', res.muted, true);
}
});
if (parseInt(adapter.config.interval, 10)) {
pollTimerChannel = setInterval(pollChannel, parseInt(adapter.config.interval, 10));
pollTimerOnlineStatus = setInterval(pollOnlineStatus, parseInt(adapter.config.interval, 10));
pollTimerInput = setInterval(pollInputAndCurrentApp, parseInt(adapter.config.interval, 10));
pollTimerGetSoundOutput = setInterval(pollGetSoundOutput, parseInt(adapter.config.interval, 10));
}
sendCommand('ssap://api/getServiceList', null, function (err, val) {
if (!err) adapter.log.debug('Service list: ' + JSON.stringify(val));
});
sendCommand('ssap://com.webos.service.update/getCurrentSWInformation', null, function (err, val) {
if (!err) {
adapter.log.debug('getCurrentSWInformation: ' + JSON.stringify(val));
adapter.setState('states.mac', val.device_id, true);
}
});
sendCommand('ssap://system/getSystemInfo', null, function (err, val) {
if (!err) {
adapter.log.debug('getSystemInfo: ' + JSON.stringify(val));
adapter.setState('states.model', val.modelName, true);
}
});
cb && cb();
});
}
function sendCommand(cmd, options, cb) {
if(isConnect){
sendPacket(cmd, options, function(_error, response){
cb && cb(_error, response);
});
}
}
function sendPacket(cmd, options, cb){
if (~cmd.indexOf('ssap:') || ~cmd.indexOf('com.')){
lgtvobj.request(cmd, options, function (_error, response){
if (_error){
adapter.log.debug('ERROR! Response from TV: ' + (response ? JSON.stringify(response) :_error));
}
cb && cb(_error, response);
});
} else {
lgtvobj.getSocket('ssap://com.webos.service.networkinput/getPointerInputSocket', function(err, sock){
if(!err){
sock.send(cmd, options);
}
});
}
}
function pollChannel() {
adapter.log.debug('Polling channel');
sendCommand('ssap://tv/getCurrentChannel', null, function (err, channel) {
var JSONChannel, ch;
JSONChannel = JSON.stringify(channel);
adapter.log.debug('DEBUGGING CHANNEL POLLING: ' + JSONChannel);
if (JSONChannel) {
ch = JSONChannel.match(/"channelNumber":"(\d+)"/m);
}
if (!err && ch) {
adapter.setState('states.channel', ch[1], true);
} else {
adapter.setState('states.channel', '', true);
}
if (JSONChannel) ch = JSONChannel.match(/.*"channelId":"(.*?)"/m);
if (!err && ch) {
adapter.setState('states.channelId', ch[1], true);
} else {
adapter.setState('states.channelId', '', true);
}
});
}
function pollOnlineStatus() {
adapter.log.debug('Polling OnlineStatus');
sendCommand('com.webos.applicationManager/getForegroundAppInfo', null, function (err, OnlineStatus) {
adapter.log.debug('DEBUGGING pollOnlineStatus: ' + JSON.stringify(OnlineStatus));
if (!err && OnlineStatus) {
adapter.setState('states.on', true, true);
adapter.setState('states.power', true, true);
} else {
adapter.setState('states.on', false, true);
adapter.setState('states.power', false, true);
adapter.setState('info.connection', false, true);
}
});
}
function pollInputAndCurrentApp() {
adapter.log.debug('Polling Input and current App');
sendCommand('ssap://com.webos.applicationManager/getForegroundAppInfo', null, function (err, Input) {
if (!err && Input) {
var JSONInput, CurrentInputAndApp;
JSONInput = JSON.stringify(Input);
adapter.log.debug('DEBUGGING pollInputAndCurrentApp: ' + JSONInput);
if (JSONInput) {
CurrentInputAndApp = JSONInput.match(/.*"appId":"(.*?)"/m);
if (CurrentInputAndApp) {
adapter.setState('states.currentApp', CurrentInputAndApp[1], true);
var ins = CurrentInputAndApp[1].split(".");
var input = ins[ins.length - 1].toString();
adapter.setState('states.input', input, true);
}
}
} else {
adapter.log.debug('ERROR on polling input and app: ' + err);
}
});
}
function pollGetSoundOutput() {
adapter.log.debug('Polling current sound output');
sendCommand('ssap://com.webos.service.apiadapter/audio/getSoundOutput', null, function (err, Output) {
if (!err && Output) {
var JSONOutput, CurrentSoundOutput;
JSONOutput = JSON.stringify(Output);
adapter.log.debug('DEBUGGING pollGetSoundOutput: ' + JSONOutput);
if (JSONOutput){
CurrentSoundOutput = JSONOutput.match(/.*"soundOutput":"(.*?)"/m);
if (CurrentSoundOutput){
adapter.setState('states.soundOutput', CurrentSoundOutput[1], true);
}
}
} else {
adapter.log.debug('ERROR on Polling get current sound output: ' + err);
}
});
}
function clearIntervals(){
clearInterval(pollTimerChannel);
clearInterval(pollTimerOnlineStatus);
clearInterval(pollTimerInput);
clearInterval(pollTimerGetSoundOutput);
}
function main() {
adapter.log.info('Ready. Configured WebOS TV IP: ' + adapter.config.ip);
adapter.subscribeStates('*');
connect(function (){});
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
startAdapter();
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}