This repository has been archived by the owner on Dec 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
samsung-tv-smp2.js
52 lines (42 loc) 路 1.53 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
"use strict";
const httpreq = require('httpreq');
const xml2js = require('xml2js');
const soapApiUrl = ":7676/smp_2_";
class SamsungTVSmp2 {
constructor(ip) {
this.url = "http://" + ip + soapApiUrl;
}
getTVInfo(callback, timeout) {
if (!timeout) {
timeout = 5000;
}
httpreq.get(this.url, {timeout: timeout}, (err, res) => {
if (err) {
Homey.log("[GET TV INFO] " + err);
typeof callback === 'function' && callback(err, null);
return;
}
xml2js.parseString(res.body, (err, result) => {
if (err) {
Homey.log("[GET TV INFO] " + err);
typeof callback === 'function' && callback(err, null);
return;
}
try {
const tvInfo = {};
const rawTvInfo = result['root']['device'][0];
for (const key in rawTvInfo) {
if (rawTvInfo.hasOwnProperty(key)) {
tvInfo[key] = rawTvInfo[key][0];
}
}
typeof callback === 'function' && callback(err, tvInfo);
} catch (err) {
Homey.log("[GET TV INFO] Result loaded and xml parsed, but does not have expected nodes.");
typeof callback === 'function' && callback(err, null);
}
});
});
}
}
module.exports = SamsungTVSmp2;