You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.9 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Laravel\Lumen\Routing\Controller as BaseController;
  4. use Illuminate\Http\Request;
  5. use DB;
  6. use Quartz;
  7. use Log;
  8. use DateTime, DateTimeZone;
  9. class Api extends BaseController
  10. {
  11. public function query(Request $request) {
  12. $token = $request->input('token');
  13. if(!$token)
  14. return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
  15. $db = DB::table('databases')->where('read_token','=',$token)->first();
  16. if(!$db)
  17. return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
  18. $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
  19. if($date=$request->input('date')) {
  20. if($request->input('tz')) {
  21. $tz = $request->input('tz');
  22. } else {
  23. $tz = 'America/Los_Angeles';
  24. }
  25. $start = DateTime::createFromFormat('Y-m-d H:i:s', $date.' 00:00:00', new DateTimeZone($tz));
  26. $end = DateTime::createFromFormat('Y-m-d H:i:s', $date.' 23:59:59', new DateTimeZone($tz));
  27. } else {
  28. return response(json_encode(['error' => 'no date provided']))->header('Content-Type', 'application/json');
  29. }
  30. $results = $qz->queryRange($start, $end);
  31. $locations = [];
  32. foreach($results as $id=>$record) {
  33. $record->date->format('U.u');
  34. $locations[] = $record->data;
  35. }
  36. if($request->input('format') == 'linestring') {
  37. $response = array(
  38. 'type' => 'LineString',
  39. 'coordinates' => array()
  40. );
  41. foreach($locations as $loc) {
  42. $response['coordinates'][] = $loc->geometry->coordinates;
  43. }
  44. } else {
  45. $response = [
  46. 'locations' => $locations
  47. ];
  48. }
  49. return response(json_encode($response))->header('Content-Type', 'application/json');
  50. }
  51. public function input(Request $request) {
  52. $token = $request->input('token');
  53. if(!$token)
  54. return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
  55. $db = DB::table('databases')->where('write_token','=',$token)->first();
  56. if(!$db)
  57. return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
  58. if(!is_array($request->input('locations')))
  59. 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');
  60. $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'w');
  61. foreach($request->input('locations') as $loc) {
  62. if(array_key_exists('properties', $loc) && array_key_exists('timestamp', $loc['properties'])) {
  63. $date = DateTime::createFromFormat('U', $loc['properties']['timestamp']);
  64. $line = $qz->add($date, $loc);
  65. }
  66. }
  67. return response(json_encode(['result' => 'ok']))->header('Content-Type', 'application/json');
  68. }
  69. }