-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
124 lines (99 loc) · 3.08 KB
/
server.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const express = require("express");
const http = require("http");
const WebSocket = require("ws");
const cors = require("cors");
const mdns = require("mdns-js");
const axios = require("axios");
const bodyParser = require("body-parser");
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const port = 3001;
let initialUniqueDeviceId = [];
app.use(cors());
app.use(bodyParser.json());
async function getElgatoInfo(addresses, port) {
try {
const settingsCall = await axios
.get(`http://${addresses}:${port}/elgato/lights/settings`)
.then((res) => res.data);
const infoCall = await axios
.get(`http://${addresses}:${port}/elgato/accessory-info`)
.then((res) => res.data);
const optionsCall = await axios
.get(`http://${addresses}:${port}/elgato/lights`)
.then((res) => res.data);
return {
settingsCall,
infoCall,
optionsCall,
};
} catch (e) {
console.error(e);
return null;
}
}
function startDiscovery(ws, ip) {
const browser = mdns.createBrowser(mdns.tcp("http"));
browser.on("ready", () => {
console.log(`MDNS browser ready for ${ip}`);
});
browser.on("update", async (service) => {
const { addresses, fullname, port } = service;
if (!fullname) return;
const isElgato = fullname.includes("Elgato");
const isPhilips = fullname.includes("Hue Bridge");
if (isElgato || isPhilips) {
let keyLight;
if (isElgato) {
keyLight = await getElgatoInfo(addresses, port);
}
const device = {
isElgato,
isPhilips,
ip: addresses,
networkAddress: `${addresses}:${port}`,
keyLight,
};
if (!initialUniqueDeviceId.includes(device.networkAddress)) {
initialUniqueDeviceId.push(device.networkAddress);
ws.send(JSON.stringify(device));
}
}
});
browser.on("error", (err) => {
console.error(err);
});
browser.discover();
}
wss.on("connection", (ws) => {
console.log("WebSocket client connected");
ws.on("close", () => {
console.log("WebSocket client disconnected");
initialUniqueDeviceId = [];
});
startDiscovery(ws, "192.168.1.0");
});
app.put("/elgato/lights", (req, res) => {
// Clone the original request and modify the headers
const clonedReq = {
method: req.method,
url: `http://${req.body.targetUrl}/elgato/lights`,
data: req.body,
headers: { ...req.headers },
};
axios(clonedReq)
.then((response) => {
res.sendStatus(response.status);
})
.catch((error) => {
console.log(error);
res.sendStatus(500);
});
});
server.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
process.on("SIGINT", () => {
process.exit();
});