-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (59 loc) · 1.86 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
import express from 'express';
import helmet from 'helmet';
import compression from 'compression';
import cors from 'cors';
import bodyParser from 'body-parser';
import winston from 'winston';
import configRoutes from './app/routes/main';
import socketIO from 'socket.io';
import http from 'http';
import net from 'net';
let app = express();
let server = http.createServer(app);
let io = socketIO.listen(server);
let currentArduino = null;
const SERVER_HTTP_PORT = 3000;
const SERVER_TCPIP_PORT = 1337;
const SERVER_HTTP_IP = '0.0.0.0';
const SERVER_TCPIP_IP = '0.0.0.0';
configRoutes(app);
app.use(compression());
app.use(helmet());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/public', express.static('./app/assets'));
app.set('view engine', 'pug');
app.set('views', './app/views');
//Http server
server.listen(SERVER_HTTP_PORT, SERVER_HTTP_IP, () => {
winston.log('info', `HTTP Server listering on port: ${SERVER_HTTP_PORT} and IP: ${SERVER_HTTP_IP}`);
});
//TCP server
net.createServer((sock) => {
sock.on('data', (data) => {
winston.log('info', 'New message from TCP client');
const json = JSON.parse(data.toString().trim());
winston.log('info', json);
});
}).listen(SERVER_TCPIP_PORT, SERVER_TCPIP_IP, () => {
winston.log('info', `TCP/IP Server listering on port: ${SERVER_TCPIP_PORT} and IP: ${SERVER_HTTP_IP}`);
}).on('connection', (socket) => {
currentArduino = socket;
winston.log('info', 'New client is now connected');
});
//Socket io server
io.on('connection', (client) => {
winston.log('info', 'New client io connected');
client.on('message', (message) => {
winston.log('info', 'New client io message');
winston.log('info', message);
if (currentArduino) {
const arduinoMessage = {
pin: message
};
currentArduino.write(JSON.stringify(arduinoMessage));
}
});
});
export default app;