-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
206 lines (175 loc) · 4.79 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var express = require('express'),
app = express();
var path = require('path'),
http = require('http').Server(app),
serialport = require("serialport"),
SerialPort = serialport.SerialPort;
var io = require('socket.io')(http);
var routes = require('./routes/index');
var connected = false,
color_array = ["label-warning", "label-success", "label-primary"],
spirit_array = ["Vodka", "Gin", "Rum"],
mixer_array = ["Cranberry Juice", "Lemon Lime Soda", "Orange Juice"];
process.env.PWD = process.cwd();
var port_object = {};
var port = process.env.PORT || process.env.NODE_PORT || 3000;
// view engine setup
app.use(express.static(path.join(process.env.PWD, 'public')));
app.set('views', path.resolve(path.join(process.env.PWD, 'views')));
app.set('view engine', 'jade');
app.get('/', function(req, res){
// the html string being sent
var filepath = path.resolve(__dirname + '/views/index.html');
res.sendFile(filepath);
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/////////////////////////////////////
//SOCKET IO STUFF
/////////////////////////////////////
io.on('connection', function(socket){
//Handles begin event from client
socket.on('begin', function(data){
console.log(data.port);
begin_drink_response(port_object[data.port]);
});
//Handles ready event from client
socket.on('ready', function(data){
console.log(data.port);
ready_drink_response(port_object[data.port]);
});
});
// Loop over all available serial ports looking for open-able data ports
serialport.list(function (err, ports) {
non_bluetooth_ports = ports.filter(function (el) {
console.log(el.comName);
if (el.comName.indexOf("Bluetooth") > 0) return false;
else return true;
});
console.log(non_bluetooth_ports);
non_bluetooth_ports.forEach(function(port) {
console.log(port.comName);
p = new SerialPort(port.comName, {
baudrate: 9600,
parser: serialport.parsers.readline("\r\n")
});
port_object[port.comName] = p;
p.on('error', function(e) { console.log(e); });
p.open(function (error) {
if (error) {
console.log("Couldn't connect to: " + port.comName);
}
else {
p.on('data', function (data) {
console.log(data);
if (is_a_scanner(data)) {
close_response(p);
}
else if (is_drink_order(data)) {
var drink = JSON.parse(data).drink;
// send drink to the view with socket.io
var processed_data = convert_drink_response(port, drink);
console.log(processed_data);
close_response(p);
io.emit("new", processed_data);
}
else if (was_drink_delivered(data)) {
var finish = true;
// send finish to the view with socket.io
io.emit('end', { id: port_to_id(port) } );
close_response(p);
}
});
}
});
});
});
/** EMIT THESE
* New
* End
* io.emit('end', data);
*/
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// Helper Methods
// send a byte to the serial port to tell it that we're done
function close_response(port) {
port.flush();
port.write('$');
}
function begin_drink_response(port){
port.flush();
port.write('[');
}
function ready_drink_response(port){
port.flush();
port.write(']');
}
function port_to_id(port) {
return port.comName.split("/").pop().split(".").pop();
}
function convert_drink_response(port, data) {
return { "id": port_to_id(port),
"port": port.comName,
"spirit": spirit_array[data[0]],
"spirit_class": color_array[data[0]],
"mixer": mixer_array[data[1]],
"mixer_class": color_array[data[1]]};
}
function is_a_scanner(data) {
if (data === "connect") {
return true;
}
else {
return false;
}
}
function is_drink_order(data) {
try {
json_res = JSON.parse(data);
}
catch (e) {
return false;
}
if (typeof json_res.drink !== "undefined") {
return true;
}
else {
return false;
}
}
function was_drink_delivered(data) {
if (data === "delivered") {
return true;
}
else {
return false;
}
}
module.exports = app;
http.listen(8000, function(){
console.log('listening on *:3000');
});