-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
201 lines (174 loc) · 5.62 KB
/
server.js
File metadata and controls
201 lines (174 loc) · 5.62 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//@ts-check
const logger = require("npmlog");
const app = require("express")();
// @ts-ignore
const server = require("http").Server(app);
// @ts-ignore
const io = require("socket.io")(server);
const qrcode = require("qrcode-terminal");
const network = require("./utils/network");
const config = require("./utils/config");
const dev = config["NODE_ENV"] !== "production";
const hostname = config["HOSTNAME"] || "0.0.0.0";
const port = config["PORT"] || 3000;
logger.level = config["LOG_LEVEL"] || "debug";
const next = require("next");
// @ts-ignore
const nextapp = next({ dev, hostname, port });
const nextHandler = nextapp.getRequestHandler();
const ips = network.getLocalIpAdress();
const clients = {};
nextapp.prepare().then(() => {
app.get("*", (req, res) => {
return nextHandler(req, res);
});
server.listen(port, (err) => {
if (err) throw err;
for (const ip of ips) {
logger.info("Server", `Ready on http://${ip}:${port}`);
qrcode.generate(`http://${ip}:${port}`, { small: true });
}
});
});
io.on("connection", (socket) => {
logger.info(`[SIO][--]`, `[${socket.id}] is connected`);
// TODO: add doc + better params name
socket.on("ui-client-connected", (uuid) => {
logger.info(`[SIO][UI]`, `[${socket.id}][${uuid}] is authenticated`);
clients[socket.id] = { socket: socket, uuid: uuid, type: "UI", car: "" };
socket.join("UI");
const py_clients = getPyClients();
emitNotification(
"info",
`Welcome to the dashboard. You can now take the control of ${py_clients.length} available cars.`,
false
);
});
// TODO: add doc + better params name
socket.on("py-client-connected", (uuid) => {
logger.info(`[SIO][PY]`, `[${socket.id}][${uuid}] is authenticated`);
clients[socket.id] = {
socket: socket,
uuid: uuid,
type: "PY",
};
socket.join("PY");
updateCars();
emitNotification("success", "A new car is available", true);
});
// client to server
// TODO: add doc + better params name
socket.on("disconnect", () => {
if (clients[socket.id]) {
const uuid = clients[socket.id].uuid;
const type = clients[socket.id].type;
delete clients[socket.id];
logger.info(`[SIO][${type}]`, `[${socket.id}][${uuid}] is disconnected`);
if (type === "PY") {
io.to(`PY_${socket.id}`).socketsLeave(`PY_${socket.id}`);
updateCars();
emitNotification(
"warning",
`Car with ID [${socket.id}] is not available anymore`,
true
);
}
} else {
logger.info(`[SIO][--]`, `[unknown] is disconnected`);
}
});
// ui to server
// TODO: add doc + better params name
socket.on("GET_CARS", () => {
if (clients[socket.id]) {
const uuid = clients[socket.id].uuid;
logger.info(`[SIO][UI]`, `[${socket.id}][${uuid}] requested cars`);
} else {
logger.info(`[SIO][--]`, `[unknown] requested cars`);
}
const py_clients = getPyClients();
const cars = py_clients.map((client) => client.socket.id);
socket.emit("GET_CARS", cars);
});
// ui to server
// TODO: add doc + better params name
socket.on("SET_CAR", (new_car) => {
const ui_client = clients[socket.id];
if (ui_client) {
const uuid = clients[socket.id].uuid;
logger.info(
`[SIO][UI]`,
`[${socket.id}][${uuid}] choose [${new_car}] as main car`
);
socket.leave(`PY_${ui_client.car}`);
if (new_car !== "") {
ui_client.car = new_car;
socket.join(`PY_${ui_client.car}`);
}
}
});
// py to ui
// TODO: add doc + better params name
socket.on("GET_MEMORY", (data) => {
socket.to(`PY_${socket.id}`).emit("GET_MEMORY", data);
});
// py to ui
// TODO: add doc + better params name
socket.on("GET_LOGS", (data) => {
socket.to(`PY_${socket.id}`).emit("GET_LOGS", data);
});
// ui to py
// TODO: add doc + better params name
socket.on("GET_SETTINGS", (car_id) => {
if (car_id !== "") {
socket.join(`PY_${car_id}_SETTINGS`);
socket.to(car_id).emit("GET_SETTINGS_ASK");
}
});
// py to ui
// TODO: add doc + better params name + async
socket.on("GET_SETTINGS_ANS", async (data) => {
const room = `PY_${socket.id}_SETTINGS`;
socket.to(room).emit("GET_SETTINGS", data);
io.to(room).socketsLeave(room);
});
// TODO: add doc + better params name
socket.on("SET_SETTINGS", (settings) => {
const car = clients[socket.id].car;
socket.to(car).emit("SET_SETTINGS", settings);
emitNotification("success", `Settings updated for ${car}`, true);
});
// TODO: add doc + better params name
socket.on("RESTART", (car_id) => {
const car = clients[socket.id].car;
socket.to(car).emit("RESTART");
emitNotification("success", `Restarting ${car}`, true);
});
// TODO: add doc + better params name
socket.on("STOP", (car_id) => {
const car = clients[socket.id].car;
socket.to(car).emit("STOP");
emitNotification("success", `Stoping ${car}`, true);
});
// TODO: add doc
function emitNotification(severity, message, everyone = false) {
if (everyone) {
io.to("UI").emit("GET_NOTIFICATIONS", { severity, message });
} else {
socket.emit("GET_NOTIFICATIONS", { severity, message });
}
}
function getPyClients() {
return Object.values(clients).filter((c) => c.type === "PY");
}
// TODO: remove this function
function getUiClients() {
return Object.values(clients).filter((c) => c.type === "UI");
}
// TODO: add doc
function updateCars() {
const py_clients = getPyClients();
const cars = py_clients.map((client) => client.socket.id);
io.to("UI").emit("GET_CARS", cars);
}
});