-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
262 lines (209 loc) · 7.27 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
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
var http = require('http')
var port = process.env.PORT || 1337;
var express = require('express');
var app = express(); //init
var qs = require('querystring');
var cors = require('cors');
var fs = require('fs');
///////////////////////////////////////////
// Hackster IO Hackathon!
var rider_waiting = false;
var driver_incoming = false;
var rider_id = 1;
var rider_details = [];
var car_location = { lat: "47.6154164", long: "-122.3460138", date: "2015" };
var debugcar = "";
app.use(express.static(__dirname + '/public'));
app.get('/', gethome);
app.use(cors())
function findWeatherAndReturn(rider, response) {
path = "http://api.wunderground.com/api/73de21f5b11ba67d/history_";
path += (rider.date + "/q/" + rider.state + "/" + rider.city + ".json")
console.log(path)
//do the http request to get the weather
var options = {
hostname: 'api.wunderground.com',
path: path
};
http.get(path, function (http_res) {
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function () {
// you can use res.send instead of console.log to output via express
console.log(data);
data = JSON.parse(data)
try{
weather = data['history']['dailysummary'][0]['meantempi']
}
catch(ex){
weather = 50
}
rider['weather'] = weather
car_location = rider
response.send(rider);
});
});
}
function gethome(req, res) {
res.render('index.html')
/*
res.send('<html><body>' +
'<h1>Hackster IO -- Tuber App!</h1>' +
'<br/>' +
'</body></html>');
*/
}
function findGeoAndReturn(rider, res){
path = "http://nominatim.openstreetmap.org/search?q="
path += ( rider.address + "," + rider.state + "," + rider.city + "&format=json&polygon=1&addressdetails=1")
console.log(path)
http.get(path, function (http_res) {
var data = "";
var lat = "";
var lon = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function () {
// you can use res.send instead of console.log to output via express
console.log(data);
data = JSON.parse(data)
try{
lat = data[0].lat || ''
lon = data[0].lon || ''
}
catch(ex){
lat = 47
lon = -122
}
to_send_data = {'lat': lat, 'lon': lon}
res.send(to_send_data);
});
});
}
app.post('/hack/requestride', function (req, res) {
// mobile app requests a ride
var body = '';
req.on('data', function (data) {
body += data;
// Too much POST data, kill the connection!
if (body.length > 1e6)
req.connection.destroy();
});
req.on('end', function () {
req.body = qs.parse(body);
console.log(req.body)
// TODO: make this pose, accept name and date and address
if (!req.body) return res.sendStatus(400)
rider_waiting = true;
var id = rider_id;
rider_id++;
rider = {
'name': req.body.name || "",
'date': (req.body.date || "").replace('/', ''),
'address': req.body.address || "",
'state': req.body.state || "",
'city': req.body.city || ""
}
rider_details.push(rider)
findGeoAndReturn(rider, res)
// res.send('welcome, ' + req.body.name)
});
});
app.get('/hack/rideavailable', function (req, res) {
// edison pings, asking if rider is waiting
driver_incoming = true;
//find the weather.
if( rider_details.length > 0){
rider = rider_details.pop()
rider.result = true
findWeatherAndReturn(rider, res)
}
else {
res.send(JSON.stringify({'result':false}))
}
});
app.get('/hack/rideaccepted', function (req, res) {
// driver pushed button on edison
});
app.get('/hack/rideprogress', function (req, res) {
// mobile app wants to know where driver is
res.send(car_location);
});
app.post('/hack/setdriverlocation', function (req, res) {
// driver sending us updates on the year
var body = '';
req.on('data', function (data) {
body += data;
console.log("got data! " + data);
// Too much POST data, kill the connection!
if (body.length > 1e6)
req.connection.destroy();
});
req.on('end', function () {
debugcar = body;
try {
req.body = JSON.parse(body);
} catch (ex) {
return res.send("{error:'cant parse json'}")
}
// TODO: make this pose, accept name and date and address
if (!req.body) { return res.sendStatus(400); }
car_location = {
'date': (req.body.date || ""),
'lat': req.body.lat || "",
'long': req.body.long || "",
}
res.send("{}");
});
});
app.get('/hack/getdriverlocation', function (req, res) {
// mobile app wants to know where driver is
res.send(car_location);
});
app.get('/hack/admin', function (req, res) {
// mobile app wants to know where driver is
res.send("" +
"<h1>admin console</h1>" +
"<br/>" +
"rider_waiting = " +
JSON.stringify(rider_waiting) +
"<br/>" +
"driver_incoming = " +
JSON.stringify(driver_incoming) +
"<br/>" +
"rider_id " +
JSON.stringify(rider_id) +
"<br/>" +
"rider_details "+
JSON.stringify(rider_details) +
"<br/>" +
"car_location " +
JSON.stringify(car_location) +
"<br/>" +
"debugcar " +
JSON.stringify(debugcar) +
"<br/>" +
"<button onclick='setdriverlocation()'>setdriverlocation</button>" +
"<br/>" +
"<button onclick='requestride()'>requestride</button>" +
"<br/>" +
"<br/>" +
'<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"i></script>'+
'<script> function requestride() { $.ajax({ type:"POST", url:"/hack/requestride", data: {name:"max",date:"date",address:"address",city:"city",state:"state"} });}</script>' +
'<script> function setdriverlocation() { $.ajax({ type:"POST", headers: {"Content-Type":"application/json"}, url:"/hack/setdriverlocation", data: JSON.stringify({lat:"lat", long:"long",date:"date"}) });}</script>' +
"...fuck yea." +
"");
});
// Start server!
var server = app.listen(port, function serverstartup() {
console.log('NodeExpress server listening on port %d', server.address().port);
});