-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
66 lines (51 loc) · 1.64 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
const inquirer = require('inquirer');
const SerialPortController = require('./bin/SerialPortController');
const SocketsController = require('./bin/SocketsController');
const { emitColor } = require('./bin/ServerHandlers');
const http = require('http');
const { Server } = require('socket.io');
const dotenv = require('dotenv');
dotenv.config({ path: '.env.example' });
function getQuestions(postsList) {
const choices = postsList.map(port => {
return {
name: `${port.Name} ==> ${port.Port}`,
value: port.Port
};
});
return {
type: 'list',
message: 'Select the port where the Arduino is connected',
name: 'Ports',
choices: choices
};
}
SerialPortController.SearchPorts().then(async ports => {
const { Ports: SelectedPort } = await inquirer.prompt(getQuestions(ports));
const SerialController = new SerialPortController(SelectedPort);
const app = require('./app');
const server = http.createServer(app);
const io = new Server(server);
SocketsController(io, SerialController);
server.listen(process.env.NODE_APPLICATION_PORT, () => {
console.log('Web server Listening!');
});
SerialController.on('ready', () => {
console.log('Port Connected');
io.sockets.emit('SerialConnected');
});
SerialController.on('reconnected', async () => {
console.log('Port Reconnected');
io.sockets.emit('SerialReconnected');
setTimeout(async () => {
await emitColor(io.sockets, SerialController);
}, 1500);
});
SerialController.on('closed', (msg) => {
console.error(msg);
io.sockets.emit('SerialDisconnected');
});
SerialController.on('error', (msg) => {
console.error(msg);
});
}).catch(error => { console.error(error); });