-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
191 lines (165 loc) · 5.53 KB
/
app.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
191
/***********************************************************************
* App.js
* by David Hodges, Outermost Software, LLC, 2019
* This is the main function of the NodeJS server program for the Phidgets Rover
* It serves up the user's web page (public/index.html) with it's built in web server
* It communicates with the user's web page and thumbstick using the sockets.io protocol
* It communicates with the Rover interface in phidgetServer.js asynchronously using the pubsub.js protocol
***********************************************************************/
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const app = express();
const pubsub = require('pubsub-js');
const global = require('./constants');
const phidget = require('./phidgetServer');
const math = require('mathjs'); // for accurate math
var debug = require('debug')('stalker:server');
const url = require('url');
const fs = require('fs');
const port = 3001
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, '/public/')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
module.exports = app;
var http = require('http');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', function () {
console.log('Listening to http://localhost:', port);
});
// handlers for socket server for 2 way communication with web page
const socketServer = function () {
//
// socket server events
//
io.on('connection', function (socket) {
console.log('user connected');
socket.on('connectRover', function (data) {
if (data == 'true') {
console.log("connection request received");
pubsub.publish(global.roverconnection_command, "connect");
}
else {
console.log("disconnect request received");
pubsub.publish(global.rovervelocity_command, "0");
pubsub.publish(global.roverconnection_command, "disconnect");
}
});
socket.on('velocity', function (data) {
// this event comes from the slider on the web page. Values are from -100 to 100, with steps in value of 1. .
// 0 is stopped. Negative numbers mean reverse direction. Positive numbers mean forwards direction.
var v = math.round(data, 2);
pubsub.publish(global.rovervelocity_command, v);
});
socket.on('steering', function (data) {
// this event comes from the slider on the web page. Values are from -10.0 to 10.0, with steps of .1
var v = math.round(data, 2);
pubsub.publish(global.roversteering_command, v);
});
socket.on('ThumbStick', function (data) {
var TSTransport = JSON.parse(data);
//console.log(`Got a ThumbStick socket request. X: ${TSTransport.X} Y: ${TSTransport.Y}`);
pubsub.publish(global.roverthumbstick_command, TSTransport);
});
pubsub.subscribe(global.roverconnection_status, function (msg, data) {
if (data == "connected") {
socket.emit('connectionStatus', 'Rover is connected');
}
else if (data == "disconnected") {
socket.emit('connectionStatus', 'Rover is not connected');
}
});
pubsub.subscribe(global.errorreport, function (msg, data) {
var responseArray = data;
var jsonResponse = JSON.stringify(responseArray);
socket.emit('errorReport', jsonResponse);
});
pubsub.subscribe(global.telemetry, function (msg, data) {
var jsonTelemetry = JSON.stringify(data);
socket.volatile.emit('telemetry', jsonTelemetry);
});
});
var compareThumbStickValues = function(X, Y)
{
if (TSTransportSave.X == X && TSTransportSave.Y == Y)
{
return true;
}
return false;
}
} // end of socket handlers
const setConnectionStatus = function (status) {
if (status == "on") {
io.on('connection', function (socket) {
{
socket.emit('connectionStatus', 'Rover is connected');
}
});
}
}
//
// start up socket server for communication with web page
//
var io = require('socket.io').listen(server);
socketServer();
// for test:
//setConnectionStatus("on");
//
// startup phidget interface for communication with rover
//
phidget.phidgetServer();
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}