-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.js
84 lines (79 loc) · 1.98 KB
/
request.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
const http = require("http");
const fs = require('fs');
const path = require("path");
const par = require("./parameters.json");
var debug = false;
const commands = {
OK: "ok",
UP: "up",
DOWN: "down",
LEFT: "left",
RIGHT: "right",
PLAY: "play",
BACK: "back",
INFO: "info",
VOLUME_UP: "volume_up",
VOLUME_DOWN: "volume_down",
POWER: "power",
CHANNEL_UP: "channel_up",
CHANNEL_DOWN: "channel_down"
}
/**
*
* @param {boolean} value Enable debug?
*/
function setDebug(value){
debug = value;
}
/**
*
* @param {String} command Command string. Use request.command enum to avoid errors.
*/
function createRequest(command) {
var hash, binary;
try {
hash = par.hash[command];
var binaryPath = path.join(__dirname, 'binaries', command);
binary = fs.readFileSync(binaryPath);
} catch (error) {
console.log("Unable to make request for IPTV: " + error);
return;
}
const options = {
"method": "POST",
"hostname": par.ip,
"port": par.port,
"path": `/companion?hash=${hash}&cid=${par.cid}&seq=${par.seq}`,
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "EPG/166 CFNetwork/1206 Darwin/20.1.0",
"Content-Length": binary.length
},
"timeout": 2000
};
const req = http.request(options, function (res) {
const chunks = [];
if(res.statusCode != 204){
console.log("IPTV Communication error. Status code is "+res.statusCode+", should be 204.")
}
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
const body = Buffer.concat(chunks);
if(body.toString()!=="")
console.log("Response from IPTV is not empty, indicates denial of control.")
});
res.on("error", function () {
console.log("IPTV Response Error");
});
});
if(debug)console.log("Writing " + command)
req.write(binary);
req.end();
}
module.exports = {
createRequest: createRequest,
setDebug: setDebug,
commands: commands
}