-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.js
155 lines (139 loc) · 4.43 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
var express = require('express');
var YouBike = require('./YouBike');
var mongoose = require('mongoose');
var constants = require("./constants");
var app = express();
var jsonArr = [];
mongoose.connect('mongodb://localhost/youbike');
console.log("===========================Server is starting===========================");
app.use(express.static('web_interface'));
// Home page
app.get('/', function(request, response) {
console.log("This is /");
});
app.get('/list',function(request, response) {
YouBike.find().distinct('sno', function(error, stationId) {
if (!error){
jsonArr = [];
console.log(stationId);
for(var i=0; i< stationId.length; i++) {
YouBike.findOne({
sno: stationId[i]
}).exec(function(err, stationNode) {
if(!err) {
if(stationNode != null) {
console.log(stationNode.sno);
console.log(stationNode.sna);
jsonArr.push({
sno: stationNode.sno, sna: stationNode.sna
});
if(jsonArr.length === stationId.length) {
sortByKey(jsonArr, 'sno');
console.log(JSON.stringify(jsonArr));
response.contentType('application/json');
response.send(JSON.stringify(jsonArr));
response.end();
}
}
}
});
}
}
});
});
// Route for /youbike/:staionId
app.route('/youbike/:staionId')
//===========================GET /youbike/:staionId===========================
.get(function(request, response) {
YouBike.findOne({
sno: request.params.staionId
}).sort({_id : -1}).exec(function(err, found_file) {
response.send(found_file);
});
})
// Route for /nearYouBike/:staionId
app.route('/nearYouBike/:staionId')
//===========================GET /nearYouBike/:staionId===========================
.get(function(request, response) {
YouBike.findOne({
sno: request.params.staionId
}).exec(function(err, found_file) {
jsonArr = [];
var lat1 = found_file.lat;
var lng1 = found_file.lng;
console.log(lat1);
console.log(lng1);
for(var i=1; i<= constants.Total_Station_Num; i++) {
var targetId = addZero(i, 4);
if(targetId !== request.params.staionId) {
YouBike.findOne({
sno: addZero(i, 4)
}).exec(function(err, stationNode) {
if(!err) {
if(stationNode != null) {
var lat2 = stationNode.lat;
var lng2 = stationNode.lng;
var distance = GetDistance(lat1,lng1,lat2,lng2);
console.log(lat2);
console.log(lng2);
console.log("---distance: " + distance);
jsonArr.push({
sno: stationNode.sno, sna: stationNode.sna, dist: distance,
sbi: stationNode.sbi, bemp: stationNode.bemp
});
if(jsonArr.length === constants.Total_Station_Num - 1) {
sortByKey(jsonArr, 'dist');
console.log(JSON.stringify(jsonArr));
response.contentType('application/json');
response.send(JSON.stringify(jsonArr));
response.end();
}
}
}
});
}
}
});
})
app.listen(8080);
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
/**
* Function for searching
*/
/*
* Return radian by degrees
*/
function Rad(d){
return d * Math.PI / 180.0;
}
/*
* Distance Calculation
* lat1 and lng2 are the latitude and longtitude of point1
* lat2 and lng2 are the latitude and longtitude of point2
*/
function GetDistance(lat1,lng1,lat2,lng2){
var radLat1 = Rad(lat1);
var radLat2 = Rad(lat2);
var a = radLat1 - radLat2;
var b = Rad(lng1) - Rad(lng2);
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s *6378.137 ; // EARTH_RADIUS;
//s = Math.round(s * 10000) / 10000; // Unit: km
s = Math.round(s * 10000) / 10; // Unit: m
//s=s.toFixed(4);
return s;
}
function addZero(num, n) {
var len = num.toString().length;
while (len < n) {
num = "0" + num;
len++;
}
return num;
}