-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (99 loc) · 2.68 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
var express = require('express');
var bodyParser = require('body-parser');
var config = require('./config.json');
var MongoClient = require('mongodb').MongoClient
var app = express();
app.set('port', (process.env.PORT || 3000));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
var mongoConnection = 'mongodb://' + config.mongoip + ":" + config.mongoport + '/app3DB'
MongoClient.connect(mongoConnection, (err, database) => {
if (err) return console.log(err);
db = database;
app.listen(app.get('port'), function() {
console.log(`running on 0.0.0.0:${app.get('port')}`);
});
});
app.get('/', function(req, res){
res.render("login.ejs");
});
var collectionCount = [];
var collectionsArray;
app.get('/trolleys', function(req, res){
db.listCollections().toArray(function(err, collections){
collectionsArray = collections;
function getNumber(i,bay) {
var x;
db.collection("master", function(err, collection) {
collection.find({"bayid": bay, "outtime": "x"}).toArray(function(err, result) {
if (err) {
throw err;
} else {
x=result.length;
}
collectionCount[i] = x;
});
});
return x;
}
getNumber(0, "pear");
getNumber(1, "apple");
getNumber(2, "banana");
res.render('index.ejs', {
bay: collectionCount
});
});
});
app.get('/update', function(req, res){
db.listCollections().toArray(function(err, collections){
db.collection("master", function(err, collection) {
collection.find({"outtime": "x"}).toArray(function(err, result) {
if (err) {
res.status(500).send({ error: 'something blew up' });
return;
}
res.json(generateResponseJSON(result));
});
});
});
});
//returns a json object to respond with
//result is JSON object.
//This generates a JSON object that holds a bay name as a key
//and an object containing the bay's capcity and count as a value.
//The JSON object can also have multiple keys, each for a different bay.
function generateResponseJSON(result)
{
//bayCount = {
// bayname:{
// capacity: 0;
// count:0;
// },
//};
//This use
bay = {};
console.log("result: ");
console.log(result);
for (i = 0;i < result.length;i++)
{
var record = result[i];
console.log(record);
if (bay.hasOwnProperty(record.bayid))
{
//bay already registered add one omre to count
console.log("already exists");
bay[record.bayid].count++;
}
else
{
console.log("make new");
bay[record.bayid] = {
count:1,
capacity:10,
};
}
}
console.log("bay");
console.log(bay);
return bay;
}