forked from casperboone/homey-samsung-smart-tv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
397 lines (243 loc) · 7.43 KB
/
app.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
"use strict";
var SamsungTVChannels = require("./samsung-tv-channels.js");
var SamsungTVRemote = require("./samsung-tv-remote.js");
var SamsungTVSoapAPI = require("./samsung-tv-soap-api.js");
var SamsungTVSources = require("./samsung-tv-sources.js");
var SamsungTVSmp2 = require("./samsung-tv-smp2.js");
var self = module.exports = {
init: function() {
self.tvs = {};
// Update status of devices every 10 seconds
// A flow card based on this can be added later,
// then maybe the interval time should be decreased
setInterval(devicesUpdateAvailability, 10000);
/**
* FLOW: Set Channel
*/
Homey.manager('flow').on('action.set_channel', function( callback, args ){
if( typeof args.tv == 'undefined' ) return callback( "Select a TV" );
var tv = self.tvs[ args.tv.id ];
if( typeof tv == 'undefined' ) return callback( "TV not connected" );
deviceAvailable(tv.ip, function(result) {
if(result) {
tv.remote.setChannel(args.channel.channelNumber);
callback(null, true);
} else {
callback( "TV offline" );
}
});
});
/**
* FLOW: [autocomplete] Set Channel
*/
Homey.manager('flow').on('action.set_channel.channel.autocomplete', function( callback, args ){
if( typeof args.args.tv == 'undefined' ) return callback( "Select a TV" );
var tv = self.tvs[ args.args.tv.id ];
if( typeof tv == 'undefined' ) return callback( "TV not connected" );
deviceAvailable(tv.ip, function(result) {
if(result) {
var channelsAutoComplete = [];
for (var key in tv.channels.all()) {
if (tv.channels.all().hasOwnProperty(key)) {
var channel = tv.channels.get(key);
channelsAutoComplete.push({
name: channel.number + '. ' + channel.name,
channelNumber: channel.number
});
}
}
channelsAutoComplete = channelsAutoComplete.filter(function(channel){
return channel.name.toLowerCase().indexOf(args.query.toLowerCase()) > -1;
})
callback( null, channelsAutoComplete );
} else {
callback( "TV offline" );
}
});
});
/**
* FLOW: Set Source
*/
Homey.manager('flow').on('action.set_source', function( callback, args ){
if( typeof args.tv == 'undefined' ) return callback( "Select a TV" );
var tv = self.tvs[ args.tv.id ];
if( typeof tv == 'undefined' ) return callback( "TV not connected" );
deviceAvailable(tv.ip, function(result) {
if(result) {
tv.soapApi.setMainTVSource(args.source.id, args.source.source, function (err, res) {
if(err) {
callback(null, false);
} else {
callback(null, true);
}
});
} else {
callback( "TV offline" );
}
});
});
/**
* FLOW: [autocomplete] Set Source
*/
Homey.manager('flow').on('action.set_source.source.autocomplete', function( callback, args ){
if( typeof args.args.tv == 'undefined' ) return callback( "Select a TV" );
var tv = self.tvs[ args.args.tv.id ];
if( typeof tv == 'undefined' ) return callback( "TV not connected" );
deviceAvailable(tv.ip, function(result) {
if(result) {
var sourcesAutoComplete = [];
for(var key in tv.sources.all()) {
if (tv.sources.all().hasOwnProperty(key)) {
var source = tv.sources.get(key);
sourcesAutoComplete.push({
name: source.description,
id: source.id,
source: source.source
});
}
}
sourcesAutoComplete = sourcesAutoComplete.filter(function(source){
return source.name.toLowerCase().indexOf(args.query.toLowerCase()) > -1;
})
callback( null, sourcesAutoComplete );
} else {
callback( "TV offline" );
}
});
});
/**
* FLOW: Toggle Mute
*/
Homey.manager('flow').on('action.toggle_mute', function( callback, args ){
if( typeof args.tv == 'undefined' ) return callback( "Select a TV" );
var tv = self.tvs[ args.tv.id ];
if( typeof tv == 'undefined' ) return callback( "TV not connected" );
deviceAvailable(tv.ip, function(result) {
if(result) {
tv.remote.mute();
callback(null, true);
} else {
callback( "TV offline" );
}
});
});
/**
* FLOW: Volume Up
*/
Homey.manager('flow').on('action.volume_up', function( callback, args ){
if( typeof args.tv == 'undefined' ) return callback( "Select a TV" );
var tv = self.tvs[ args.tv.id ];
if( typeof tv == 'undefined' ) return callback( "TV not connected" );
deviceAvailable(tv.ip, function(result) {
if(result) {
tv.remote.volumeUp();
callback(null, true);
} else {
callback( "TV offline" );
}
});
});
/**
* FLOW: Volume Down
*/
Homey.manager('flow').on('action.volume_down', function( callback, args ){
if( typeof args.tv == 'undefined' ) return callback( "Select a TV" );
var tv = self.tvs[ args.tv.id ];
if( typeof tv == 'undefined' ) return callback( "TV not connected" );
deviceAvailable(tv.ip, function(result) {
if(result) {
tv.remote.volumeDown();
callback(null, true);
} else {
callback( "TV offline" );
}
});
});
/**
* FLOW: Power Off
*/
Homey.manager('flow').on('action.power_off', function( callback, args ){
if( typeof args.tv == 'undefined' ) return callback( "Select a TV" );
var tv = self.tvs[ args.tv.id ];
if( typeof tv == 'undefined' ) return callback( "TV not connected" );
deviceAvailable(tv.ip, function(result) {
if(result) {
tv.remote.powerOff();
callback(null, true);
} else {
callback( "TV offline" );
}
});
});
},
addDevice: function(ip, callback) {
// Add to internal device array
self.tvs[ip] = {
id: ip,
ip: ip
};
deviceAvailable(ip, callback)
}
}
/**
* Returns true device is available.
*/
function deviceAvailable(ip, callback) {
// Was it available in the last check, then we assume it still is
if (self.tvs[ip].available) {
callback(true);
} else {
deviceUpdateAvailability(ip, function(res) {
Homey.log("CALLED BACK! WITH" + res);
callback(res);
});
}
}
/**
* Update the availability of a device by trying to get details about it.
*/
function deviceUpdateAvailability(ip, callback) {
Homey.log("DUA:step0");
var tv = self.tvs[ip];
var smp2 = new SamsungTVSmp2(ip);
smp2.getTVInfo(function(err, data) {
Homey.log("DUA:step1");
if(err) {
Homey.log("DUA:step1.5");
tv.available = false;
Homey.manager('drivers').getDriver('tv').setUnavailable({id: ip, ip: ip});
callback(false);
} else {
Homey.log("DUA:step2");
if(!tv.available) {
Homey.log("DUA:step3");
tv.available = true;
Homey.manager('drivers').getDriver('tv').setAvailable({id: ip, ip: ip});
// Set remote, smp2 and soap api instance
tv.remote = new SamsungTVRemote(ip);
tv.soapApi = new SamsungTVSoapAPI(ip);
tv.smp2 = smp2;
// Retrieve sources
tv.sources = new SamsungTVSources(ip);
Homey.log("DUA:step4");
// Retrieve channels by first making sure channel list is available
tv.soapApi.getChannelListURL(function () {
tv.channels = new SamsungTVChannels(ip, function() {
Homey.log("CHANNELS CALLED BACK");
callback(true);
});
});
}
}
}, 2000); // Time it out after 2 seconds
}
/**
* Update availability status of all devices.
*/
function devicesUpdateAvailability() {
for (var key in self.tvs) {
if (self.tvs.hasOwnProperty(key)) {
deviceUpdateAvailability(self.tvs[key].ip, function() {});
}
}
}