-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (141 loc) · 4.43 KB
/
index.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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require('mongoose');
const app = express();
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.static("public"));
// Database Connection
mongoose.connect("mongodb://localhost:27017/greenmileDB", { useNewUrlParser: true , useUnifiedTopology: true });
// For identifying the current active user
var activeUserID;
//Scheme and model for three collections in the greenMile DB
const dustbinSchema = mongoose.Schema({
_dustbinID : String,
driverID: String,
driverName : String,
driverPhoneNo : Number,
driverLisenceNo : Number,
driverPhotoLink : String,
driverMunipalHeadName : String,
driverMunipalHeadPhoneNo : Number,
lat : Number,
lng : Number,
lastPickup : Date,
percentFilled : Number,
channelID : Number,
graphLink : String
});
const dustbinData = mongoose.model("dustbin", dustbinSchema);
const driverLoginSchema = mongoose.Schema({
_driverID : String,
name : String,
email : String,
password : String
});
const driverLoginData = mongoose.model("driverLogin", driverLoginSchema);
const driverInfoSchema = mongoose.Schema({
driverID : String,
name : String,
email : String,
phoneNo : Number,
head : String,
lisenceNo : Number,
dustbin : [dustbinSchema]
});
const driverInfoData = mongoose.model("driverInfo", driverInfoSchema);
app.get("/", function(req, res){
res.sendFile(__dirname + "/landPage.html");
});
// send dustbin info data to /data route so can we can read in the map through frontend
// javascript using XML mainframe.js
app.get("/data", function(req, res){
dustbinData.find(function(err, items){
if(err)
console.log(err);
else
res.send(items);
});
})
app.get("/map", function(req, res){
res.sendFile(__dirname + "/map.html");
});
// if the user refreshes the mainframe page he will be taken to the home page
app.get("/mainframe", function(req, res){
if(activeUserID!==''){
res.sendFile(__dirname + "/mainframe.html");
}else{
res.redirect("/");
}
});
// posting active user data to the route to be used by frontend js mainframe.js
app.get("/activeUserData", function(req, res){
driverInfoData.findOne({ driverID : activeUserID }, function(err, doc){
if(err)
console.log(err);
else{
res.send(doc);
}
});
})
app.get("/change/:channelID/:field1", function(req, res){
const channel_id = req.params.channelID;
const field = req.params.field1;
dustbinData.updateOne({ channelID : channel_id }, {$set : { percentFilled : field } }, function(err, doc){
if(err)
console.log(err);
else{
res.send("1");
}
});
console.log(channel_id);
if(activeUserID!=='') {
driverInfoData.updateOne( {
'driverID' : activeUserID,
'dustbin.channelID': channel_id
},
{ $set: { "dustbin.$.percentFilled" : field } },
function(err, c){
if(err)
console.log(err);
else
console.log(c);
});
}
});
app.get("/logout", function(req, res){
activeUserID = "";
res.redirect("/");
})
// check the login credentials
app.post("/", function(req, res){
driverLoginData.findOne({ _driverID: req.body.userID }, function (err, docs) {
if (err){
console.log(err);
}
else{
if(docs.email===req.body.email && docs.password===req.body.password){
activeUserID = req.body.userID;
res.redirect("/mainframe");
}
else
console.log("NOT FOUND");
}
});
});
//An attempt to add dustbin which hasn't been picked in 3 days
// app.post("/pickup", function(req, res){
// console.log(req.body);
// console.log(activeUserID);
// function toISOStringLocal(d) {
// function z(n){return (n<10?'0':'') + n}
// return d.getFullYear() + '-' + z(d.getMonth()+1) + '-' +
// z(d.getDate()) + 'T' + z(d.getHours()) + ':' +
// z(d.getMinutes()) + ':' + z(d.getSeconds())
// }
// driverInfoData.updateOne( { "driverID" : activeUserID , 'dustbin.dustbinID' : req.body.dustbinID }, {'$set': {
// 'dustbin.$.lastPickup': toISOStringLocal(new Date())
// }});
// })
app.listen(3000, function(){
console.log("Server is up and running");
});