-
Notifications
You must be signed in to change notification settings - Fork 30
/
Api.php
442 lines (370 loc) · 15.2 KB
/
Api.php
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use DB, Log, Cache;
use Quartz;
use DateTime, DateTimeZone, DateInterval;
use App\Jobs\TripComplete;
use App\Jobs\TripStarted;
use App\Jobs\NotifyOfNewLocations;
class Api extends BaseController
{
public function account(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('write_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
return response(json_encode(['name' => $db->name]))->header('Content-Type', 'application/json');
}
public function query(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('read_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
$qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
if($request->input('tz')) {
$tz = $request->input('tz');
} else {
$tz = 'UTC';
}
if($date=$request->input('date')) {
$start = DateTime::createFromFormat('Y-m-d H:i:s', $date.' 00:00:00', new DateTimeZone($tz));
$end = DateTime::createFromFormat('Y-m-d H:i:s', $date.' 23:59:59', new DateTimeZone($tz));
} elseif(($start=$request->input('start')) && ($end=$request->input('end'))) {
$start = new DateTime($start, new DateTimeZone($tz));
$end = new DateTime($end, new DateTimeZone($tz));
} else {
return response(json_encode(['error' => 'no date provided']))->header('Content-Type', 'application/json');
}
$results = $qz->queryRange($start, $end);
$locations = [];
$properties = [];
$events = [];
if($request->input('format') == 'linestring') {
foreach($results as $id=>$record) {
// When returning a linestring, separate out the "event" records from the "location" records
if($record->data) {
if(property_exists($record->data->properties, 'action')) {
$rec = $record->data;
# add a unixtime property
$rec->properties->unixtime = (int)$record->date->format('U');
$events[] = $rec;
} else {
#$record->date->format('U.u');
// Ignore super inaccurate locations
if(!property_exists($record->data->properties, 'horizontal_accuracy')
|| $record->data->properties->horizontal_accuracy <= 5000) {
$locations[] = $record->data;
$props = $record->data->properties;
$date = $record->date;
$date->setTimeZone(new DateTimeZone($tz));
$props->timestamp = $date->format('c');
$props->unixtime = (int)$date->format('U');
$properties[] = $props;
}
}
}
}
$linestring = array(
'type' => 'LineString',
'coordinates' => [],
'properties' => $properties
);
foreach($locations as $loc) {
if(property_exists($loc, 'geometry'))
$linestring['coordinates'][] = $loc->geometry->coordinates;
else
$linestring['coordinates'][] = null;
}
$response = array(
'linestring' => $linestring,
'events' => $events
);
} else {
foreach($results as $id=>$record) {
if($record->data) {
$locations[] = $record->data;
}
}
$response = [
'locations' => $locations
];
}
return response(json_encode($response))->header('Content-Type', 'application/json');
}
public function last(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('read_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
if($request->input('tz')) {
$tz = $request->input('tz');
} else {
$tz = 'UTC';
}
if($input=$request->input('before')) {
// If a specific time was requested, look up the data in the filesystem
$qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/', $input)) {
// If the input date is given in YYYY-mm-dd HH:mm:ss format, interpret it in the timezone given
$date = DateTime::createFromFormat('Y-m-d H:i:s', $input, new DateTimeZone($tz));
} else {
// Otherwise, parse the string and use the timezone in the input
$date = new DateTime($input);
$date->setTimeZone(new DateTimeZone($tz));
}
if(!$date) {
return response(json_encode(['error' => 'invalid date provided']))->header('Content-Type', 'application/json');
}
/* ********************************************** */
// TODO: move this logic into QuartzDB
// Load the shard for the given date
$shard = $qz->shardForDate($date);
// If the shard doesn't exist, check one day before
if(!$shard->exists()) {
$date = $date->sub(new DateInterval('PT86400S'));
$shard = $qz->shardForDate($date);
}
// Now if the shard doesn't exist, return an empty result
if(!$shard->exists()) {
return response(json_encode([
'data'=>null
]));
}
// Start iterating through the shard and look for the last line that is before the given date
$shard->init();
$record = false;
foreach($shard as $r) {
if($r->date > $date)
break;
$record = $r;
}
/* ********************************************** */
if(!$record) {
return response(json_encode([
'data'=>null
]));
}
$response = [
'data' => $record->data,
];
} else {
// If no specific time was requested, use the cached location from the database
$response = [
'data' => json_decode($db->last_location),
];
}
if($request->input('geocode') && property_exists($response['data'], 'geometry') && property_exists($response['data']->geometry, 'coordinates')) {
$coords = $response['data']->geometry->coordinates;
$params = [
'latitude' => $coords[1],
'longitude' => $coords[0],
'date' => $response['data']->properties->timestamp
];
$geocode = self::geocode($params);
if($geocode) {
$response['geocode'] = $geocode;
} else {
$response['geocode'] = null;
}
}
return response(json_encode($response))->header('Content-Type', 'application/json');
}
public function trip(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('read_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
if($db->current_trip) {
$response = [
'trip' => json_decode($db->current_trip)
];
} else {
$response = [
'trip' => null
];
}
return response(json_encode($response))->header('Content-Type', 'application/json');
}
public function input(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('write_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
if(!is_array($request->input('locations')))
return response(json_encode(['error' => 'invalid input', 'error_description' => 'parameter "locations" must be an array of GeoJSON data with a "timestamp" property']))->header('Content-Type', 'application/json');
$qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'w');
$num = 0;
$trips = 0;
$last_location = false;
foreach($request->input('locations') as $loc) {
if(array_key_exists('properties', $loc)) {
if(array_key_exists('timestamp', $loc['properties'])) {
try {
if(preg_match('/^\d+\.\d+$/', $loc['properties']['timestamp']))
$date = DateTime::createFromFormat('U.u', $loc['properties']['timestamp']);
elseif(preg_match('/^\d+$/', $loc['properties']['timestamp']))
$date = DateTime::createFromFormat('U', $loc['properties']['timestamp']);
else
$date = new DateTime($loc['properties']['timestamp']);
if($date) {
$cacheKey = 'compass::'.$db->name.'::'.$date->format('U');
// Skip adding if the timestamp is already in the cache.
// Helps prevent writing duplicate data when the HTTP request is interrupted.
if(!env('CACHE_DRIVER') || !Cache::has($cacheKey)) {
$num++;
$qz->add($date, $loc);
if(env('CACHE_DRIVER'))
Cache::put($cacheKey, 1, 360); // cache this for 6 hours
$last_location = $loc;
}
if(array_key_exists('type', $loc['properties']) && $loc['properties']['type'] == 'trip') {
try {
$job = (new TripComplete($db->id, $loc))->onQueue('compass');
$this->dispatch($job);
$trips++;
Log::info('Got a trip record');
} catch(Exception $e) {
Log::warning('Received invalid trip');
}
}
} else {
Log::warning('Received invalid date: ' . $loc['properties']['timestamp']);
}
} catch(Exception $e) {
Log::warning('Received invalid date: ' . $loc['properties']['timestamp']);
}
}
}
}
if($request->input('current')) {
$last_location = $request->input('current');
Log::info('Device sent current location');
}
$response = [
'result' => 'ok',
'saved' => $num,
'trips' => $trips
];
if($last_location) {
/*
// 2017-08-22 Don't geocode cause it takes too long. Maybe make a separate route for this later.
$geocode = self::geocode(['latitude'=>$last_location['geometry']['coordinates'][1], 'longitude'=>$last_location['geometry']['coordinates'][0]]);
$response['geocode'] = [
'full_name' => $geocode->full_name,
'locality' => $geocode->locality,
'country' => $geocode->country
];
*/
$response['geocode'] = null;
// Cache the last location in the database
if(isset($last_location['properties']['timestamp'])) {
if(preg_match('/^\d+\.\d+$/', $last_location['properties']['timestamp']))
$date = DateTime::createFromFormat('U.u', $last_location['properties']['timestamp']);
elseif(preg_match('/^\d+$/', $last_location['properties']['timestamp']))
$date = DateTime::createFromFormat('U', $last_location['properties']['timestamp']);
else
$date = new DateTime($last_location['properties']['timestamp']);
$date->setTimeZone(new DateTimeZone('UTC'));
$date = $date->format('Y-m-d H:i:s');
} else {
$date = null;
}
DB::table('databases')->where('id', $db->id)
->update([
'last_location' => json_encode($last_location, JSON_UNESCAPED_SLASHES+JSON_PRETTY_PRINT),
'last_location_date' => $date,
]);
// Notify subscribers that new data is available
if($db->ping_urls) {
$job = (new NotifyOfNewLocations($db->id))->onQueue('compass');
$this->dispatch($job);
}
}
if($request->input('trip')) {
$existing_trip = $db->current_trip;
DB::table('databases')->where('id', $db->id)
->update([
'current_trip' => json_encode($request->input('trip'), JSON_UNESCAPED_SLASHES+JSON_PRETTY_PRINT)
]);
if(!$existing_trip && $db->ping_urls) {
$job = (new TripStarted($db->id))->onQueue('compass');
$this->dispatch($job);
}
} else {
DB::table('databases')->where('id', $db->id)->update(['current_trip' => null]);
}
return response(json_encode($response))->header('Content-Type', 'application/json');
}
public function trip_complete(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('write_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
if($request->input('tz')) {
$tz = new DateTimeZone($request->input('tz'));
} else {
$tz = new DateTimeZone('UTC');
}
$start = new DateTime($request->input('start'), $tz);
$end = new DateTime($request->input('end'), $tz);
$loc = [
'properties' => [
'mode' => $request->input('mode'),
'start' => $start->format('c'),
'end' => $end->format('c'),
]
];
try {
$job = (new TripComplete($db->id, $loc))->onQueue('compass');
$this->dispatch($job);
Log::info('Got a manual trip record: '.$start->format('c').' '.$end->format('c'));
} catch(Exception $e) {
Log::warning('Received invalid trip');
}
return response(json_encode(['result' => 'ok']))->header('Content-Type', 'application/json');
}
public static function geocode($params) {
$ch = curl_init(env('ATLAS_BASE').'api/geocode?'.http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$response = curl_exec($ch);
if($response) {
return json_decode($response);
}
}
public function share(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('write_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
$expires_at = time() + $request->input('duration');
$share_token = str_random(15);
$share_id = DB::table('shares')->insertGetId([
'database_id' => $db->id,
'created_at' => date('Y-m-d H:i:s'),
'expires_at' => date('Y-m-d H:i:s', $expires_at),
'token' => $share_token,
]);
$share_url = env('BASE_URL').'s/'.$share_token;
return response(json_encode([
'url' => $share_url
]), 201)->header('Content-Type', 'application/json')
->header('Location', $share_url);
}
}