-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLGTVController.js
409 lines (333 loc) · 11.6 KB
/
LGTVController.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
/** A Reactor controller for LG TVs.
* Copyright (c) 2022/2023 Daniele Bochicchio, All Rights Reserved.
* LGTVController is offered under MIT License - https://mit-license.org/
* More info: https://github.com/dbochicchio/reactor-lgtv
*
* Disclaimer: This is beta software, so quirks and bugs are expected. Please report back.
*/
const version = 24313;
const className = "lgtv";
const ns = "x_lgtv"
const ignoredValue = "@@IGNORED@@"
const Controller = require("server/lib/Controller");
const Capabilities = require("server/lib/Capabilities");
const LGTV = require('lgtv2');
const Logger = require("server/lib/Logger");
Logger.getLogger('LGTVController', 'Controller').always("Module LGTVController v%1", version);
const util = require("server/lib/util");
const TimerBroker = require("server/lib/TimerBroker");
const debounce = (callback, wait) => {
let timeoutId = null;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
callback.apply(null, args);
}, wait);
};
}
var impl = false; /* Implementation data, one copy for all instances, will be loaded by start() later */
module.exports = class LGTVController extends Controller {
constructor(struct, id, config) {
super(struct, id, config); /* required *this.*/
this.failures = 0;
this.lgtv = undefined;
this.stopping = false; /* Flag indicates we're stopping */
this.connected = false;
// we do not need the group, since it's a one man band show here
this.removeControllerGroup();
}
/** Start the controller. */
async start() {
if (!this.config.host) {
this.log.err("%1 No host configured", this);
return Promise.reject("No host configured");
}
/** Load implementation data if not yet loaded. Remove this if you don't
* use implementation data files.
*/
if (false === impl) {
impl = await this.loadBaseImplementationData(className, __dirname);
}
this.log.debug(5, "%1 starting", this);
this.stopping = false;
this.run();
return this;
}
/* Stop the controller. */
async stop() {
this.log.debug(5, "%1 stopping", this);
this.stopping = true;
/* Required ending */
return await super.stop();
}
/* run() is called when Controller's single-simple timer expires. */
run() {
this.log.debug(5, "%1 running", this);
this.startClient();
}
/* startClient() load status and creates the entities */
startClient() {
if (this.stopping) return;
const e = this.system;
var that = this;
var log = this.log;
log.debug(5, "%1 [refreshStatus] - startClient: %2", that, that.config.host);
this.mapSystemDevice(that, false);
that.online();
this.lgtv = new LGTV({
url: (that.config.secure ?? true) ? `wss://${that.config.host}:3001` : `ws://${that.config.host}:3000`,
timeout: that.config.timeout || 15000,
keyFile: `${__dirname}/client.key`, // TODO: path from config/code
wsconfig: {
keepalive: true,
keepaliveInterval: 10000,
dropConnectionOnKeepaliveTimeout: false,
keepaliveGracePeriod: 5000,
tlsOptions: {
rejectUnauthorized: false
}
}
});
this.lgtv.on('error', function (err) {
if (err.code === 'EHOSTUNREACH' || err.code === 'ETIMEDOUT') {
log.notice("%1 Soft error: %2", that, err.code);
}
else
that.onError(that, err);
// update connection status
that.connected = false;
that.updateConnection(e, that.connected);
});
this.lgtv.on('connecting', function () {
log.debug(5, "%1 Connecting to %2", that, that.config.host);
// update connection status
that.connected = false;
that.updateConnection(e, that.connected);
});
this.lgtv.on('connect', function () {
that.online();
log.notice("%1 Connected: %2", that, that.config.host);
// update connection status
that.connected = true;
that.updateConnection(e, that.connected);
that.lgtv.subscribe('ssap://audio/getVolume', function (err, res) {
log.debug(5, "%1 getVolume: %2 - %3", that, that.config.host, res);
var attributes = {};
if (res.volumeStatus?.volume !== undefined) attributes["volume.level"] = res.volumeStatus.volume / 100;
if (res.volumeStatus?.muteStatus !== undefined) attributes["muting.state"] = res.volumeStatus.muteStatus;
if (res.volumeStatus?.soundOutput !== undefined) attributes["_ns_.output"] = res.volumeStatus.soundOutput;
if (res.changed !== undefined && res.changed.indexOf('volume') !== -1) attributes["volume.level"] = res.volume / 100;
if (res.changed !== undefined && res.changed.indexOf('muted') !== -1) attributes["muting.state"] = res.muted;
// update attributes with debounce
debounce(function () {
log.debug(5, "%1 debounce: %2", that, attributes);
that.updateEntityAttributes(e, attributes);
}, 2000)();
});
var liveTVReady = false;
that.lgtv.subscribe('ssap://com.webos.applicationManager/getForegroundAppInfo', function (err, res) {
log.debug(5, "%1 getForegroundAppInfo: %2 - %3", that, that.config.host, res);
// update attributes
that.updateEntityAttributes(e, { "_ns_.input": res.appId });
// live TV support
if (res.appId === 'com.webos.app.livetv') {
if (!liveTVReady) {
liveTVReady = true;
setTimeout(() => {
that.lgtv.subscribe('ssap://tv/getCurrentChannel', (err, res) => {
log.debug(5, "%1 getCurrentChannel: %2 - %3", that, that.config.host, res);
if (err) {
that.onError(that, err);
return;
}
var attributes = {};
attributes["_ns_.channel_id"] = res.channelNumber;
that.updateEntityAttributes(e, attributes);
});
}, 3000);
}
}
});
});
this.lgtv.on('prompt', function () {
log.warn("%1 prompt: %2", that, that.config.host);
that.sendWarning("LG TV needs your authorization to run: check your TV and approve the request");
});
this.lgtv.on('close', function () {
log.debug(5, "%1 Connection closed: %2", that, that.config.host);
// update connection status
that.connected = false;
that.updateConnection(e, that.connected);
log.debug(5, "%1 Connection closed", that);
});
}
/* mapSystemDevice() is used to create the system device */
mapSystemDevice(that, state) {
that.mapDevice("system", that.config.name ?? "LG TV",
["volume", "muting", "power_switch", "toggle", "sys_system", ns], "power_switch.state",
{
"power_switch.state": state,
"toggle.state": state,
"_ns_.online": state,
});
}
/* updateConnection() is used to update the connection status */
updateConnection(e, state) {
var attributes = {
"power_switch.state": state,
"toggle.state": state,
"_ns_.online": state,
};
// update attributes
this.updateEntityAttributes(e, attributes);
}
/* offline() is used to handle the offline status */
onError(that, err) {
console.log(err);
that.log.err("%1 Error: %2", that, err);
try {
that.startDelay(Math.min(120_000, (that.config.error_interval || 5_000) * Math.max(1, ++that.failures - 12)));
}
catch {
// soft warning
}
if (that.failures >= 3) {
that.offline();
}
}
/* performOnEntity() is used to implement actions on entities */
async performOnEntity(e, actionName, params) {
this.log.debug(5, "%1 [performOnEntity] %3 - %2 - %4", this, actionName, e, params);
if (!this.connected && !actionName.startsWith('sys_system')) {
this.log.warn("%1 LG TV %2 is offline - can't execute: %3", this, this.config.host, actionName);
return;
}
switch (actionName) {
case `${ns}.sendnotification`:
case `${ns}.send_notification`:
if (params?.text == undefined) {
this.log.warn("%1 LG TV %2- text param is mandatory and must be specified", this, this.config.host);
}
else {
this.lgtv?.send("request", "ssap://system.notifications/createToast", {
message: params.text
});
}
return;
case 'power_switch.on':
this.lgtv?.request("ssap://system/turnOn");
return;
case 'power_switch.off':
this.lgtv?.request("ssap://system/turnOff");
return;
case 'toggle.toggle':
var state = e.getAttribute('power_switch.state') === true;
this.performOnEntity(e, state ? 'power_switch.off' : 'power_switch.on');
return;
case 'volume.increase':
var currentVolume = e.getAttribute("volume.level") ?? -1;
if (currentVolume == -1) {
this.lgtv?.request("ssap://audio/volumeUp");
}
else {
var volume = ((params?.amount || 0) + currentVolume) * 100;
this.lgtv?.request("ssap://audio/setVolume", { volume: parseInt(volume, 10) } || 0);
}
return;
case 'volume.decrease':
var currentVolume = e.getAttribute("volume.level") ?? -1;
if (currentVolume == -1) {
this.lgtv?.request("ssap://audio/volumeDown");
}
else {
var volume = ((params?.amount || 0) - currentVolume) * 100;
this.lgtv?.request("ssap://audio/setVolume", { volume: parseInt(volume, 10) } || 0);
}
return;
case 'volume.relative':
var volume = (params?.amount || 0) * 100;
this.lgtv?.request("ssap://audio/setVolume", { volume: parseInt(volume, 10) } || 0);
return;
case 'volume.setdb':
case 'volume.set':
var volume = (params?.value || params?.db || 0) * 100;
this.lgtv?.request("ssap://audio/setVolume", { volume: parseInt(volume, 10) } || 0);
return;
case 'muting.mute':
case 'muting.unmute':
case 'muting.toggle':
case 'muting.set':
var payload = params?.muting === 'true' || actionName == 'muting.mute';
// TODO: special case for toggle
this.lgtv?.request('ssap://audio/setMute', { mute: payload });
return;
case 'sys_system.restart':
this.lgtv = undefined;
this.stopping = false;
this.startClient();
return;
}
return super.performOnEntity(e, actionName, params);
}
/* Maps a device into a MSR entity */
mapDevice(id, name, capabilities, defaultAttribute, attributes) {
this.log.debug(5, "%1 mapDevice(%2, %3, %4, %5, %6)", this, id, name, capabilities, defaultAttribute, attributes);
var isNew = false;
let e = this.findEntity(id);
try {
if (!e) {
this.log.notice("%1 Creating new entity for %2", this, name);
e = this.getEntity(className, id);
e.setName(name);
e.setType(className);
isNew = true;
}
e.deferNotifies(true);
e.markDead(false);
// capabilities
if (capabilities) {
this.log.debug(5, "%1 [%2] adding capabilities: %3", this, id, capabilities);
e.extendCapabilities(capabilities);
// Check controller and system capabilities versions for changes
const vinfo = { ...Capabilities.getSysInfo(), controller: version };
const hash = util.hash(JSON.stringify(vinfo));
if (e._hash !== hash) {
e.refreshCapabilities();
e._hash = hash;
}
}
this.updateEntityAttributes(e, attributes);
if (defaultAttribute)
e.setPrimaryAttribute(defaultAttribute);
if (isNew)
this.sendNotice('Discovered new device {0:q} ({1}) on controller {2:q}', name, id, this);
} catch (err) {
this.log.err("%1 [mapDevice] error: %2", this, err);
} finally {
e.deferNotifies(false);
}
}
updateEntityAttributes(e, attributes) {
if (e && attributes) {
e.deferNotifies(true);
e.markDead(false);
for (const attr in attributes) {
var newValue = attributes[attr];
// skip ignored values
if (ignoredValue != newValue) {
// check if value has changed
var attrName = attr.replace(/_ns_/g, ns);
var value = e.getAttribute(attrName);
// check for and skip unchanged values
var changed = value != newValue && JSON.stringify(value) != JSON.stringify(newValue);
if (changed) {
var id = e.getCanonicalID();
this.log.debug(5, "%1 [%2] %3: %4 => %5", this, id, attrName, newValue, value);
e.setAttribute(attrName, newValue);
}
}
}
e.deferNotifies(false);
}
}
};