This repository was archived by the owner on Jan 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
63 lines (49 loc) · 1.51 KB
/
main.ts
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
import * as express from "express";
import * as net from "net";
import * as https from "https";
import * as fs from "fs";
const PORT = 55381;
const CERT_PATH = `${process.env.HOME}/.certs`;
const ENCODING = "utf8";
const app = express();
app.get("/avr/volume", (req, res) => {
const queryVol = parseInt(req.query["number"]);
if (req.query["key"] !== "Key1" || !queryVol || queryVol >= 1000) {
res.status(400).send("BAD REQUEST\n");
return;
}
const targetVol = queryVol < 10 ? queryVol * 10 : queryVol;
console.log(`Setting avr volume to ${targetVol}`);
const client = new net.Socket();
client.setTimeout(2000);
client.on("timeout", () => {
client.end();
res.status(500).send("TELENT SOCKET TIMEOUT\n");
});
client.on("data", data => {
console.log("Response: " + data.toString());
client.destroy();
res.status(200).send("OK\n");
});
client.connect(
23,
"Denon-AVR.home.lasath.org",
() => {
const msg = `MV${targetVol}`;
console.log(`Connected to AVR. Sending '${msg}'...`);
client.write(msg, () => {
console.log("Data written");
});
}
);
});
const httpsServer = https.createServer(
{
cert: fs.readFileSync(`${CERT_PATH}/fullchain.pem`, ENCODING),
key: fs.readFileSync(`${CERT_PATH}/privkey.pem`, ENCODING)
},
app
);
httpsServer.listen(PORT, () =>
console.log(`Initialized. Listening on ${PORT}`)
);