-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
457 lines (397 loc) · 14.4 KB
/
server.js
File metadata and controls
457 lines (397 loc) · 14.4 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
var express = require('express'),
app = express(),
http = require('http').createServer(app),
io = require('socket.io').listen(http);
http.listen(process.env.PORT || 3000)
app.get('/', function (req, res) {
res.sendfile(__dirname + '/public/index.html');
});
app.get('/QueueLists', function (req, res) {
res.sendfile(__dirname + '/public/queueLists.html');
});
app.get('/ReserveQueue', function (req, res) {
res.sendfile(__dirname + '/public/reserveQueue.html');
});
app.get('/CreateOrJoinCompany', function (req, res) {
res.sendfile(__dirname + '/public/createOrJoinCompany.html');
});
app.get('/NextQueue', function (req, res) {
res.sendfile(__dirname + '/public/nextQueue.html');
});
var _ = require('underscore');
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'us-cdbr-iron-east-02.cleardb.net',
user : 'b74609e180ce2b',
password : '87b88840',
database : 'heroku_ec961a3d2debbe8'
});
var customer_format = {
'Name': '',
'NumberOfSeats': 0,
'Id': '',
'SocketId': [],
'NextQueueFlag': false,
'QueuePosition' : 0,
'GroupColor' : ""
};
/*============================ Global Entity Start =================================*/
var GlobalCompany = function(){
this.allCompany = [];
};
GlobalCompany.prototype.getCompanyById = function(id){
for(var i =0; i<this.allCompany.length; i++){
if(this.allCompany[i].companyId == id){
return this.allCompany[i];
}
}
};
GlobalCompany.prototype.addCompany = function(company){
this.allCompany.push(company);
};
var globalCompany = new GlobalCompany();
/*============================ Global Entity End =================================*/
/*============================ Each Company Entity Start =================================*/
var Company = function(id){
this.companyId = id;
this.allCustomers = [];
//default table config
this.tableConfig = [{
'greater' : 1,
'less' : 3,
'customers' : [],
'latestQueuePosition' : 0,
'groupColor' : "red"
},
{
'greater' : 4,
'less' : 6,
'customers' : [],
'latestQueuePosition' : 0,
'groupColor' : "green"
},
{
'greater' : 7,
'less' : 9,
'customers' : [],
'latestQueuePosition' : 0,
'groupColor' : "blue"
},
{
'greater' : 10,
'less' : 100,
'customers' : [],
'latestQueuePosition' : 0,
'groupColor' : "black"
}];
this.callingQueue = [];
};
/*============================ Each Comapany Entity End =================================*/
io.sockets.on('connection', function(socket){;
console.log("=================" + socket.id + " connect ========================")
io.sockets.socket(socket.id).emit("socket id connection", {'SocketId': socket.id});
/*============================ Global Function Start =================================*/
socket.on('global create company', function(data){
var newCompany = new Company(data.CompanyId);
globalCompany.addCompany(newCompany);
io.sockets.emit('global update companies', globalCompany.allCompany);
});
socket.on('global request initial companies', function(data){
io.sockets.emit('global update companies', globalCompany.allCompany);
});
/*============================ Global Function end =================================*/
/*============================ Each Company Function Start =================================*/
socket.on('join company', function(data){
if(socket.companyId){
// leave the current room (stored in session)
socket.leave(socket.companyId);
}
// join this room id
socket.join(data.CompanyId);
socket.companyId = data.CompanyId;
});
socket.on('request reserve seats', function(data){
thisCompany = globalCompany.getCompanyById(socket.companyId);
if(thisCompany){
tableConfig = thisCompany.tableConfig;
allCustomers = thisCompany.allCustomers;
callingQueue = thisCompany.callingQueue;
var customer = _.clone(customer_format);
customer.Name = data.Name;
customer.NumberOfSeats = data.NumberOfSeats;
customer.Id = data.Id;
customer.SocketId = _.clone(customer_format.SocketId);
//Add customer into database
var start = new Date();
var start_sqlFormat = start.getFullYear() + "-" + (start.getMonth()+1) + "-" + start.getDate() + " " + start.getHours() + ":" + start.getMinutes() + ":" + start.getSeconds();
var post = {
qrcode: data.Id,
timestart: start_sqlFormat,
name: data.Name,
numseat: data.NumberOfSeats
};
/*pool.getConnection(function(err, connection){
var query = connection.query('INSERT INTO reservation SET ?', post, function(err, result) {
if (err) {
throw err;
}
});
connection.release();
});*/
//Add customer in tableConfig
for(var i =0; i<tableConfig.length; i++){
if(customer.NumberOfSeats >= tableConfig[i].greater && customer.NumberOfSeats <= tableConfig[i].less){
//check if this is the first customer in this category
if(tableConfig[i].customers.length == 0){
customer.NextQueueFlag = true;
}
tableConfig[i].latestQueuePosition = tableConfig[i].latestQueuePosition + 1;
customer.QueuePosition = tableConfig[i].latestQueuePosition;
customer.GroupColor = tableConfig[i].groupColor;
tableConfig[i].customers.push(customer);
break;
}
}
allCustomers.push(customer);
io.sockets.in(socket.companyId).emit('update table', allCustomers);
}
});
socket.on('next queue', function(data){
thisCompany = globalCompany.getCompanyById(socket.companyId);
if(thisCompany){
tableConfig = thisCompany.tableConfig;
allCustomers = thisCompany.allCustomers;
callingQueue = thisCompany.callingQueue;
var foundCustomerInTableConfig = false;
var tableConfigIndex = -1;
var customersIndex = -1;
var currentQueueCustomer;
var nextFirstQueueCustomer;
for(var i =0; i<tableConfig.length&& !foundCustomerInTableConfig; i++){
//find where is next queue call customer in table config
for(var j = 0; j<tableConfig[i].customers.length; j++){
if(data.Id == tableConfig[i].customers[j].Id){
tableConfigIndex = i;
customersIndex = j ;
foundCustomerInTableConfig = true;
currentQueueCustomer = tableConfig[i].customers[j];
//Report Error if calling customer is not the first queue in the same catagory group.
if(customersIndex != 0){
console.log("---------------- Error: Call Customer that is not the first queue in the same catagory group. ");
}
break;
}
}
}
if(tableConfigIndex != -1){
//Send out noticifation to all customers in same catagory
_.each(tableConfig[tableConfigIndex].customers, function(customer, idx) {
_.each(customer.SocketId, function(socketId){
if(socketId){
io.sockets.socket(socketId).emit("call queue", {'QueueNumber': idx});
}
});
});
tableConfig[tableConfigIndex].customers.splice(customersIndex, 1); //remove the first queue
// Find currentQueueCustomer index in allCustomers array
var customerIndex_InAllCustomers;
_.each(allCustomers, function(customer, idx) {
if (customer.Id == currentQueueCustomer.Id) {
customerIndex_InAllCustomers = idx;
return;
}
});
//Romove this customers
allCustomers.splice(customerIndex_InAllCustomers,1);
if(tableConfig[tableConfigIndex].customers.length > 0){
//set NextQueueFlag = true for next customer
nextFirstQueueCustomer = tableConfig[tableConfigIndex].customers[0];
nextFirstQueueCustomer.NextQueueFlag = true;
}
currentQueueCustomer.NextQueueFlag = false;
callingQueue.push(currentQueueCustomer);
//Update end Timestamp for currentQueueCustomer into database
var end = new Date();
var end_sqlFormat = end.getFullYear() + "-" + (end.getMonth()+1) + "-" + end.getDate() + " " + end.getHours() + ":" + end.getMinutes() + ":" + end.getSeconds();
var post = {
timeend: end_sqlFormat
};
/*pool.getConnection(function(err, connection){
var query = connection.query('UPDATE reservation SET ? WHERE qrcode = ?', [post, currentQueueCustomer.Id], function(err, result){
if (err) {
throw err;
}
});
connection.release();
});*/
//update Table information
io.sockets.in(socket.companyId).emit('update table', allCustomers);
io.sockets.in(socket.companyId).emit('update calling table', callingQueue);
}
}
});
socket.on('customer register code', function(data){
if(socket.companyId){
// leave the current room (stored in session)
socket.leave(socket.companyId);
}
//Check if data is string or object. If it is String, convert to object
if(typeof data === 'string'){
try {
data = JSON.parse(data);
} catch (e) {
return;
}
}
// join this room id
socket.join(data.CompanyId);
socket.companyId = data.CompanyId;
thisCompany = globalCompany.getCompanyById(socket.companyId);
if(thisCompany){
tableConfig = thisCompany.tableConfig;
allCustomers = thisCompany.allCustomers;
callingQueue = thisCompany.callingQueue;
//insert socket id for customer in table config
var foundCustomer = false;
var thisCustomer = undefined;
_.each(tableConfig, function(table, tableIdx) {
_.each(table.customers,function(customer,customerIdx){
if(customer.Id == data.Id){
customer.SocketId.push(data.SocketId);
foundCustomer = true;
thisCustomer = _.clone(customer);
thisCustomer.QueueNumber = customerIdx+1;
return;
}
});
});
if(!foundCustomer){
//insert socket id for customer in callingQueue
_.each(callingQueue, function(customer, idx) {
if (customer.Id == data.Id) {
customer.SocketId.push(data.SocketId);
foundCustomer = true;
thisCustomer = _.clone(customer);
thisCustomer.QueueNumber = 0;
return;
}
});
}
if(foundCustomer){
_.each(thisCustomer.SocketId, function(socketId){
if(socketId){
io.sockets.socket(socketId).emit("customer information", thisCustomer);
}
});
var post = {
doesusemobile: 'true'
};
/*pool.getConnection(function(err, connection){
var query = connection.query('UPDATE reservation SET ? WHERE qrcode = ?', [post, data.Id], function(err, result){
if (err) {
throw err;
}
});
connection.release();
});*/
}
//update Table information
io.sockets.in(socket.companyId).emit('update table', allCustomers);
io.sockets.in(socket.companyId).emit('update calling table', callingQueue);
}
});
//Find Customer in Next Queue
socket.on('request customer in next queue', function(data){
thisCompany = globalCompany.getCompanyById(socket.companyId);
if(thisCompany){
tableConfig = thisCompany.tableConfig;
allCustomers = thisCompany.allCustomers;
callingQueue = thisCompany.callingQueue;
if(data.customerType != undefined && typeof data.customerType === "number"){
//Current this server have only 0,1,2,3 customer type
if(data.customerType > 3){
data.customerType = 3;
}
if(tableConfig[data.customerType].customers.length > 0){
requestNextCustomer = tableConfig[data.customerType].customers[0];
io.sockets.in(socket.companyId).emit('respond customer in next queue', requestNextCustomer);
}else{
io.sockets.in(socket.companyId).emit('respond customer in next queue', "no more customer waiting in this customer group type");
}
}else{
io.sockets.in(socket.companyId).emit('respond customer in next queue', "require customerType parameter");
}
}
});
//Customer does attend
socket.on('customer attend', function(data){
thisCompany = globalCompany.getCompanyById(socket.companyId);
if(thisCompany){
tableConfig = thisCompany.tableConfig;
allCustomers = thisCompany.allCustomers;
callingQueue = thisCompany.callingQueue;
// Find data customer index in calling Queue array
var customerIndex_IncallingQueue;
_.each(callingQueue, function(customer, idx) {
if (customer.Id == data.Id) {
customerIndex_IncallingQueue = idx;
return;
}
});
var post = {
doesattend: "true"
};
/*pool.getConnection(function(err, connection){
var query = connection.query('UPDATE reservation SET ? WHERE qrcode = ?', [post, data.Id], function(err, result){
if (err) {
throw err;
}
});
connection.release();
});*/
//Romove this customers in calling queue
callingQueue.splice(customerIndex_IncallingQueue,1);
io.sockets.in(socket.companyId).emit('update table', allCustomers);
io.sockets.in(socket.companyId).emit('update calling table', callingQueue);
}
});
//Customer doesn't attend
socket.on('customer does not attend', function(data){
thisCompany = globalCompany.getCompanyById(socket.companyId);
if(thisCompany){
tableConfig = thisCompany.tableConfig;
allCustomers = thisCompany.allCustomers;
callingQueue = thisCompany.callingQueue;
// Find data customer index in calling Queue array
var customerIndex_IncallingQueue;
_.each(callingQueue, function(customer, idx) {
if (customer.Id == data.Id) {
customerIndex_IncallingQueue = idx;
return;
}
});
//Romove this customers in calling queue
callingQueue.splice(customerIndex_IncallingQueue,1);
io.sockets.in(socket.companyId).emit('update table', allCustomers);
io.sockets.in(socket.companyId).emit('update calling table', callingQueue);
}
});
//Send Initial Table
socket.on('request initial table', function(data){
thisCompany = globalCompany.getCompanyById(socket.companyId);
if(thisCompany){
tableConfig = thisCompany.tableConfig;
allCustomers = thisCompany.allCustomers;
callingQueue = thisCompany.callingQueue;
io.sockets.in(socket.companyId).emit('update table', allCustomers);
io.sockets.in(socket.companyId).emit('update calling table', callingQueue);
}
});
//Test Connection
socket.on('test connection', function(data){
io.sockets.emit('test connection back', data);
});
/*============================ Each Company Function End =================================*/
});
app.use(express.static(__dirname+'/public'));
app.use(express.static(__dirname+'/bower_components'));