-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (54 loc) · 1.52 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
const YaesuRotatorController = require("./gs232b");
const GS = new YaesuRotatorController("/dev/ttyUSB0", 9600, true, 2);
const http = require("http");
const socketIo = require("socket.io");
const server = http.createServer();
const io = socketIo(server, {
cors: {
origin: "http://localhost",
methods: ["GET", "POST"],
},
});
async function updateRotatorStatus(socket) {
try {
const status = await GS.getRotatorStatus();
socket.emit("status", status);
} catch (error) {
console.error("Error getting rotator status:", error);
}
}
io.on("connection", socket => {
console.log("Client connected");
updateRotatorStatus(socket);
socket.on("set-azimuth", (value) => {
GS.setAzimuth(value)
.then(() => {
console.log("Ok!")
updateRotatorStatus(io);
})
.catch(error => {
console.error("Error setting azimuth:", error);
});
});
socket.on("disconnect", () => {
console.log("Client disconnected");
});
socket.on("get-status", () => {
updateRotatorStatus(socket);
});
socket.on("move-left", () => {
console.log("moveLeft");
GS.moveLeft();
});
socket.on("move-right", () => {
console.log("moveRight");
GS.moveRight();
});
socket.on("move-stop", () => {
console.log("Stop!");
GS.moveStop();
});
});
server.listen(3001, () => {
console.log("Server listening on port 3001");
});