-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (41 loc) · 1.13 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
const RemoteBox = require('./RemoteBox');
const RB = new RemoteBox('/dev/ttyACM0', 38400); //Default RemoteBox 38400Bd
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 updateAntennaStatus(socket) {
try {
const status = await RB.getAntennaStatus();
socket.emit('status', status);
} catch (error) {
console.error('Error getting antenna status:', error);
}
}
io.on('connection', (socket) => {
console.log('Client connected');
updateAntennaStatus(socket);
socket.on('set-antenna', (trx, index, on) => {
RB.setRadioAntenna(trx, index + 1, on)
.then(() => {
updateAntennaStatus(io);
})
.catch((error) => {
console.error('Error setting antenna:', error);
});
});
socket.on('disconnect', () => {
console.log('Client disconnected');
});
socket.on('get-status', () => {
updateAntennaStatus(socket);
});
});
server.listen(3001, () => {
console.log('Server listening on port 3001');
});