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-soap-api.js
59 lines (48 loc) 路 1.95 KB
/
samsung-tv-soap-api.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
"use strict";
const httpreq = require('httpreq');
const xml2js = require('xml2js');
const soapApiUrl = ":7676/smp_4_";
class SamsungTVSoapAPI {
constructor(ip) {
this.url = "http://" + ip + soapApiUrl;
}
getChannelListURL(callback) {
apiCall(this.url, 'GetChannelListURL', callback);
}
getSourceList(callback) {
apiCall(this.url, 'GetSourceList', callback);
}
getMBRDeviceList(callback) {
apiCall(this.url, 'GetMBRDeviceList', callback);
}
setMainTVSource(id, source, callback) {
const body = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetMainTVSource xmlns:u="urn:samsung.com:service:MainTVAgent2:1"><Source>' + source + '</Source><ID>' + id + '</ID><UiID>-1</UiID></u:SetMainTVSource></s:Body></s:Envelope>';
apiCall(this.url, 'SetMainTVSource', callback, body);
}
}
function apiCall(url, soapAction, callback, alternativeBody) {
let body = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:' + soapAction + ' xmlns:u="urn:samsung.com:service:MainTVAgent2:1"></u:' + soapAction + '></s:Body></s:Envelope>';
if (alternativeBody) {
body = alternativeBody;
}
httpreq.post(url, {
headers: {
'soapaction': '"urn:samsung.com:service:MainTVAgent2:1#' + soapAction + '"',
'user-agent': "DLNADOC/1.50 SEC_HHP_Homey/1.0"
},
body: body
}, (err, res) => {
if (err) {
Homey.log(err);
typeof callback === 'function' && callback(err, res);
return;
}
xml2js.parseString(res.body, function (err, result) {
if (err) {
Homey.log(err);
}
typeof callback === 'function' && callback(err, result);
});
});
}
module.exports = SamsungTVSoapAPI;