-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
292 lines (270 loc) · 9.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// ## About
// **keg.io** is a techonology-laden kegerator, developed by VNC employees, to
// satisfy their nerdiest beer-drinking needs. It's built on node.js, and utilizes
// an arduino microcontroller for interfacing with the actual keg HW and sensors.
//
// It's got several cool features, including:
//
// * Gravatar support
// * Twitter integration
/*
* **keg.io** accepts two types of clients: web browser and kegerator.
*
* A web browser client connects to keg.io to view its primary GUI.
* A kegerator client connects to keg.io to send and receive sensor information.
*
* Keg.io can accept multiple connections from both web browsers and kegerators.
*
*/
// ##Setup dependencies
//require(__dirname + "/lib/setup").ext( __dirname + "/lib");
var fs = require('fs')
, sys = require('util')
, http = require('http')
, url = require('url')
, querystring = require('querystring')
, io = require('socket.io')
, files = require('node-static')
, router = require('choreographer').router()
, keg_io = require('./lib/keg.io/keg.io')
, keg = new keg_io.Keg()
, log4js = require('log4js');
function printUsage(logger)
{
logger.error("USAGE:");
logger.error("node server.js <path_to_config_file>");
logger.error("\tWhere <path_to_config_file> is the path to a JSON configuration file");
logger.error("\t containing all the required keg.io config options. Optionally, the");
logger.error("\t config file may contain C-style comments (/* */).");
}
// Setup our logging
// We're using [**log4js**](http://log4js.berlios.de/) for all of our logging
// The logging verbosity (particularly to the console for debugging) can be changed via the
// **conf/log4js.json** configuration file, using standard log4js log levels:
//
// * OFF
// * FATAL
// * ERROR
// * WARN
// * INFO
// * DEBUG
// * TRACE
// * ALL
log4js.configure('conf/log4js.json');
var logger = log4js.getLogger('default');
// Make sure we got all the required cmdline args
if (process.argv.length < 3)
{
printUsage(logger);
process.exit(1);
}
// echo all the args
logger.debug("ARGS:");
process.argv.forEach(function (val, index, array) {
logger.debug(index + ": " + val);
});
// Load our commented JSON configuration file, and echo it
var config = JSON.parse(
fs.readFileSync(process.argv[2]).toString().replace(
new RegExp("\\/\\*(.|\\r|\\n)*?\\*\\/", "g"),
"" // strip out C-style comments (/* */)
)
);
logger.debug("CONFIG:");
for(var prop in config) {
if(config.hasOwnProperty(prop))
logger.debug(prop + ": " + config[prop]);
}
//logger.debug(sys.inspect(config, true, null));
// initialize serial port connection to kegerator
keg.init(logger,
config.device,
config.db_name,
config.twitter_enabled,
config.twitter_consumer_key,
config.twitter_consumer_secret,
config.twitter_access_token_key,
config.twitter_access_token_secret,
config.admin_ui_password,
config.high_temp_threshold);
//
// Create several node-static server instances to serve the './public' folder
//
var base = new(files.Server)('./static');
var css = new(files.Server)('./static/css');
var css2 = new(files.Server)('./static/css/ui-lightness');
var css3 = new(files.Server)('./static/css/ui-lightness/images');
var images = new(files.Server)('./static/images');
var coaster_images = new(files.Server)('./static/images/coasters');
var js = new(files.Server)('./static/js');
var js2 = new(files.Server)('./static/js/profiling');
var docs = new(files.Server)('./static/docs');
// ##Routes
// We're using [**choreographer**](https://github.com/laughinghan/choreographer) for our request routing
router.ignoreCase = true;
/////// ADD ALL YOUR ROUTES HERE /////////
router.get('/', function(req, res) {
base.serveFile('/index.html', 200, {}, req, res);
})
.get('/old', function(req, res) {
base.serveFile('/index_old.html', 200, {}, req, res);
})
.get('/socketPort.json', function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(config.socket_client_connect_port);
})
.get('/currentTemperature.json', function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify({ name: 'temp', value: keg.getLastTemperature() }));
})
.get('/temperatureHistory.json', function(req, res) {
keg.getTemperatureTrend(function(result) {
// For some reason, the following line doesn't work with the highcharts.
//res.send(result, {'Content-Type': 'text/json'}, 200);
//console.log(result);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(result);
});
})
.get('/currentPercentRemaining.json', function(req, res) {
keg.getPercentRemaining(function(percent) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify({ name: 'remaining', value: percent + "" }));
});
})
.get('/lastDrinker.json', function(req, res) {
keg.getLastDrinker(function(result) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify({ name: 'pour', value: result }));
});
})
.get('/lastDrinkerCoasters.json', function(req, res) {
keg.getLastDrinkerCoasters(function(result) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify({ name: 'coaster', value: result }));
});
})
.get('/recentHistory.json', function(req, res) {
keg.getRecentHistory(function(result) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(result));
});
})
.get('/pourHistory.json', function(req, res) {
keg.getPourTrend(function(result) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(result);
});
})
.get('/pourHistoryAllTime.json', function(req, res) {
keg.getPourTrendAllTime(function(result) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(result);
});
})
.get('/kegInfo.json', function(req, res) {
keg.getKegInfo(function(result) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(result);
});
})
.get('/addUser.json',function(req,res){
keg.addUser(
querystring.parse(req.url.split('?')[1])
,function(result){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(result);
});
})
.get('/css/ui-lightness/*', function(req, res, file) {
css2.serveFile(file, 200, {}, req, res);
})
.get('/css/ui-lightness/images/*', function(req, res, file) {
css3.serveFile(file, 200, {}, req, res);
})
.get('/css/*', function(req, res, file) {
css.serveFile(file, 200, {}, req, res);
})
.get('/images/coasters/*', function(req, res, file) {
coaster_images.serveFile(file, 200, {}, req,res);
})
.get('/images/*', function(req, res, file) {
images.serveFile(file, 200, {}, req,res);
})
.get('/js/profiling/*', function(req, res, file) {
js2.serveFile(file, 200, {}, req, res);
})
.get('/js/*', function(req, res, file) {
js.serveFile(file, 200, {}, req,res);
})
.get('/docs/*', function(req, res, file) {
docs.serveFile(file, 200, {}, req, res);
});
// Create an HTTP server
var server = http.createServer(router);
server.listen(config.http_port);
// Create a server for the web socket
// Depending on the port assignments, this
// could be the same server that we use for HTTP
var socketServer = server;
if (config.http_port != config.socket_listen_port)
{
socketServer = http.createServer();
socketServer.listen(config.socket_listen_port);
}
// Setup Socket.IO
var socket = io.listen(socketServer);
socket.sockets.on('connection', function(client){
logger.info('Web browser client connected');
keg.on('temp', function(data) {
if (data) {
client.send(JSON.stringify({ name: 'temp', value: data }));
}
});
keg.on('tag', function(data) {
if (data) {
client.send(JSON.stringify({ name: 'tag', value: data }));
}
});
keg.on('flow', function(data) {
if (data) {
client.send(JSON.stringify({ name: 'flow', value: data }));
}
});
keg.on('pour', function(data){
if (data)
{
client.send(JSON.stringify({name: 'pour', value: data }));
}
});
keg.on('deny', function(data){
if (data)
{
client.send(JSON.stringify({name: 'deny', value: data }));
}
});
keg.on('remaining', function(data){
if (data)
{
client.send(JSON.stringify({name: 'remaining', value: data }));
}
});
keg.on('coaster', function(data){
if (data)
{
client.send(JSON.stringify({name: 'coaster', value: data }));
}
});
keg.on('coaster_earned', function(data){
if (data)
{
client.send(JSON.stringify({name: 'coaster_earned', value: data }));
}
});
client.on('disconnect', function(){
logger.info('Web browser client disconnected.');
});
});
logger.info('Listening for HTTP requests on http://0.0.0.0:' + config.http_port );
logger.info('Listening for web socket requests on http://0.0.0.0:' + config.socket_listen_port );
logger.info('Telling clients to connect web socket on port: ' + config.socket_client_connect_port );