-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
190 lines (137 loc) · 5.48 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
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
var express = require( 'express' ),
http = require( 'http' ),
config = require( 'dotenv' ).config(),
app = express(),
server = http.createServer( app ),
io = require( 'socket.io' ).listen( server ),
port = 8080,
browser_sockets = {},
controller_sockets = {},
repl = require( "repl" ),
///for Arduino configuration
SerialPort = require( 'serialport' ),
defaults = {
baudRate: 9600
},
///for correct display of data from Serial port
Readline = SerialPort.parsers.Readline;
server.listen( port );
// Serve static files under the /public directory
app.use( "/public", express.static( __dirname + '/public' ) );
// Set up index
app.get( '/', function ( req, res ) {
res.sendFile( __dirname + '/index.html' );
} );
// Log that the servers running
console.log( "Server running on port: " + port );
// On any connection to the socket
io.sockets.on( 'connection', function ( socket ) {
console.log( "socket connetion innitiated" );
// ///ARDUINO SETUP
// portArduino = new SerialPort("/dev/cu.usbmodem14111", defaults);
// //const portArduino = new SerialPort('/dev/tty-usbserial1');
// var parser = new Readline();
// When a browser connects, store its socket id
socket.on( 'browser_connect', function () {
console.log( "browser connected" );
// Store the browser socket
browser_sockets[ socket.id ] = {
socket: socket,
controller_id: undefined
};
// Notify the browser of a successful connection
socket.emit( "browser_connected", process.env.DEVICE_IP );
// portArduino.pipe(parser);
//parser.on('data', console.log);
//portArduino.write('ROBOT PLEASE RESPOND\n');
// portArduino.on("open", function() {
// console.log('Serial Port Arduino Opend');
//
//
// parser.on("data", function(data) {
// // Coerce data into a number
// //console.log(data);
// //dataDigital = parseInt(data, 8);
// //data = +data;
// // If data is worth reading and processing
// if (data && data > 1) {
// // Create a new Array() whose length equals data
// // then join to create output visualization
// // Print the data value along with visualization of data
// //console.log(data)
// socket.emit('arduinoUpdate', data);
// }
//
// });
//
//
//
// });
// Create new serialport pointer
// portArduino.on("error", function(msg) {
// console.log("error: " + msg);
// });
//
//
// repl.start("=>");
} );
// When a controller attempts to connect it will send the id of the browser
// socket it is trying to connect to
socket.on( 'controller_connect', function ( browser_socket_id ) {
// If the browser socket, that this controller is attempting to connect to, exists
if ( browser_sockets[ browser_socket_id ] ) {
console.log( "Controller connected" );
// Store the controller socket and its relevant browser socket
controller_sockets[ socket.id ] = {
socket: socket,
browser_id: browser_socket_id
};
// Notify the controller of a successful connection
socket.emit( "controller_connected", true );
// Set the controller id on the relevant browser socket object
browser_sockets[ browser_socket_id ].controller_id = socket.id;
// Notify relevant browser socket that a controller has connected successfully
browser_sockets[ browser_socket_id ].socket.emit( "controller_connected", true );
// Forward the changes onto the relative browser socket
socket.on( 'controller_state_change', function ( data ) {
if ( browser_sockets[ browser_socket_id ] ) {
// Notify relevant browser socket of controller state change
browser_sockets[ browser_socket_id ].socket.emit( "controller_state_change", data )
}
} );
} else {
console.log( "Controller failed to connect" );
// Notify the controller of a failed connection
socket.emit( "controller_connected", false );
}
} );
// When a socket disconnects
socket.on( 'disconnect', function () {
// If it's a browser socket
if ( browser_sockets[ socket.id ] ) {
console.log( "browser disconnected" );
// If this browser has a controller connected to it
if ( controller_sockets[ browser_sockets[ socket.id ].controller_id ] ) {
// Notify relevant controller socket that the browser has disconnected
controller_sockets[ browser_sockets[ socket.id ].controller_id ].socket.emit( "controller_connected", false );
// Remove browser id from the relevant controller socket
controller_sockets[ browser_sockets[ socket.id ].controller_id ].browser_id = undefined;
}
// Delete it
delete browser_sockets[ socket.id ];
}
// If it's a controller socket
if ( controller_sockets[ socket.id ] ) {
console.log( "Controller disconnected" );
// If this controller is connected to a browser socket
if ( browser_sockets[ controller_sockets[ socket.id ].browser_id ] ) {
// Notify relevant browser socket that the controller has disconnected
browser_sockets[ controller_sockets[ socket.id ].browser_id ].socket.emit( "controller_connected", false );
// Remove controller id from the relevant browser socket
browser_sockets[ controller_sockets[ socket.id ].browser_id ].controller_id = undefined;
}
// Delete it
delete controller_sockets[ socket.id ];
}
} );
} );