-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
56 lines (49 loc) · 1.14 KB
/
index.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
const request = require('request');
const setAudioMute = require('./commands/setAudioMute.js');
const setAudioVolume = require('./commands/setAudioVolume.js');
const setPowerStatus = require('./commands/setPowerStatus.js');
class Control {
constructor(ip = '0.0.0.0', psk = '') {
this.ip = ip;
this.psk = psk;
this.body = {
version: '1.0',
id: 1,
};
}
/**
* HTTP Request Callback
*/
static callback(error, response, body) {
let output = {};
if (!error && response.statusCode === 200) {
output = body;
}
return output;
}
/**
* Execute command
*/
call(command) {
const headers = {
Origin: 'null',
'X-Auth-PSK': this.psk,
'User-Agent': 'sony-android-tv-control',
'Content-Type': 'text/plain;charset=UTF-8',
};
const stringBody = JSON.stringify(Object.assign({}, this.body, command.body));
const options = {
url: `http://${this.ip}${command.path}`,
method: 'POST',
headers,
body: stringBody,
};
request(options, this.callback);
}
}
module.exports = {
Control,
setAudioMute,
setAudioVolume,
setPowerStatus,
};