-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
155 lines (123 loc) · 4.9 KB
/
Copy pathserver.js
File metadata and controls
155 lines (123 loc) · 4.9 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
var express = require('express');
var session = require('express-session');
var http = require('http');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var port = 8080;//<----- Control the port no from here
var server = require('http').Server(app);// needed to deploy on server
var fetchAction = require('node-fetch');
var fs = require('fs');
var timestamp = require('time-stamp');
var request = require('request');
var cors = require('cors');
var mongoose = require('mongoose');
var MongoClient = require('mongodb').MongoClient,assert = require('assert');
// setting up express server
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// routes of the server
app.get('/',(req,res,next) => {
res.json({"message":"Welcome to the Speed limit detection System :\nServer is up and running sucessfully"});
console.log("Request made to the server landing page");
});
app.post('/Speed_stream',(req,res,next) => {
// ---------------------------------------------------------------------------------------------------
const request_option_overspeed = {
uri: "https://roads.googleapis.com/v1/speedLimits?path="+JSON.stringify(req.body.Location)+"&key=AIzaSyDSehieJnAXKODJZCcibjeJNSUVeHdOSWw",
method: "GET",
headers: {
"Content-Type": "application/json"
},
}
request(request_option_overspeed,(err,response,result11)=>{
//if(!err && res.statusCode==200){<-------------------add this once api active
if(!err && response.statusCode==403){
var result = JSON.parse(result11)
console.log(result11);
// -----------------------------------------------------------------------------------------------------
//if((result.speedLimits[0].speedLimit - req.body.Speed) < 0)<--------correct this once api ready
if((200 - req.body.Speed) < 0)
{
// Connceting to google location api
const request_option_location= {
//uri: "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + JSON.stringify(result.speedLimits[0].placeId) + "&key=AIzaSyDSehieJnAXKODJZCcibjeJNSUVeHdOSWw",
uri: "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJJ4vQRudkJA0RpednU70A-5M&key=AIzaSyDSehieJnAXKODJZCcibjeJNSUVeHdOSWw",
method: "GET",
headers: {
"Content-Type": "application/json"
},
}
request(request_option_location,(err2,response2,result2)=>{
//if(!err && res.statusCode==200){ // <-----------------add this once api works
if(!err && response2.statusCode==200){
var result1 = JSON.parse(result2)
console.log(result1);
// connecting to mongodb
// ---------------------------------------------------------------------------------
MongoClient.connect('mongodb://ankit:1234567890@ds219879.mlab.com:19879/speed_analysis', (err3,db)=> {
assert.equal(null,err);
console.log("Sucessfully connected to the mongodb client");
// sending the information
db.collection(req.body.Car_Number).insertOne({
"Car_Number": req.body.Car_Number,
"Location": JSON.stringify(result1.result.formatted_address),
"Speed": req.body.Speed,
"Time-Stamp_key": timestamp('YYYY/MM/DD'),
"Time-Stamp": timestamp('YYYY/MM/DD:mm:ss'),
//"Overspeed_Limit": result.speedLimits[0].speedLimit + " " + result.speedLimits[0].units, //activate once api active
"Overspeed_Limit": 200 + " " + "KMPH",
//"Speed_above_overspeed_limit": (req.body.Speed - result.speedLimits[0].speedLimit) // activate once api active
"Speed_above_overspeed_limit": (req.body.Speed - 200)
})
.then(function(result) {
// process result
res.json({"code": 200 ,"message": "Data uploaded successfully on Car overspeeding "});
})
if(err3){
res.json({"code": 500 , "message": "Data upload unsuccessfull"})
}
});
//google api location else ending here
//------------------------------------------------------------------------
}
else
{
console.log("Overspeed_data=" + res.statusCode);
}
});
//-----------------------------------------------------------------------
}
else
{
res.json({"code": 200 ,"message": "Car Not overspedding right now"});
}
// else ending for the google speed api request
//--------------------------------------------------------------
}
else
{
console.log("Overspeed_data=" + res.statusCode);
}
// ------------------------------------------------------------------
});
});
// ----------------------------------------------------------------------------
app.get('/speed_violation_details/:Car_Number', (req,res,next)=>{
MongoClient.connect('mongodb://ankit:1234567890@ds219879.mlab.com:19879/speed_analysis', (err,db)=> {
assert.equal(null,err);
console.log("Sucessfully connected to the mongodb client");
// sending the information
db.collection(req.params.Car_Number).find({}).toArray((err,result)=>{
if(result == null)
{
res.json({"code": 404,"message":"This Car does not exist"})
}
res.json({"code": 200,result});
});
});
});
// Server Started
//app.listen(port);
app.listen(process.env.port || 8080);
console.log('Test Server Started ! on port ' + port);