forked from casperboone/homey-samsung-smart-tv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamsung-tv-smp2.js
82 lines (54 loc) · 2.19 KB
/
samsung-tv-smp2.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
var httpreq = require('httpreq');
var xml2js = require('xml2js');
var soapApiUrl = ":7676/smp_2_";
function SamsungTVSmp2(ip) {
Homey.log("smp2 constr: "+ ip);
this.url = "http://" + ip + soapApiUrl;
}
/**
* Returns array (if you're lucky) with:
* deviceType
* manufacturer
* manufacturerURL
* modelDescription
* modelName
* modeURL
* serialNumber
* UDN
* UPC
* sec:deviceID
* sec:ProductCap
*/
SamsungTVSmp2.prototype.getTVInfo = function(callback, timeout) {
if(!timeout) {
timeout = 5000;
}
httpreq.get(this.url, { timeout: timeout }, function (err, res){
if (err){
Homey.log("[GET TV INFO] " +err);
typeof callback === 'function' && callback(err, null);
} else {
xml2js.parseString(res.body, function (err, result) {
if (err) {
Homey.log("[GET TV INFO] " + err);
typeof callback === 'function' && callback(err, null);
} else {
if (result != null && result['root']['device'][0]) {
var tvInfo = {};
var rawTvInfo = result['root']['device'][0];
for (var key in rawTvInfo) {
if (rawTvInfo.hasOwnProperty(key)) {
tvInfo[key] = rawTvInfo[key][0];
}
}
typeof callback === 'function' && callback(err, tvInfo);
} else {
Homey.log("[GET TV INFO] Result loaded and xml parsed, but does not have expected nodes.");
typeof callback === 'function' && callback("XML does not contain expected nodes", null);
}
}
});
}
});
}
module.exports = SamsungTVSmp2;