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.

158 lines
5.3 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. use App\Jobs\TripComplete;
  10. class Api extends BaseController
  11. {
  12. public function account(Request $request) {
  13. $token = $request->input('token');
  14. if(!$token)
  15. return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
  16. $db = DB::table('databases')->where('write_token','=',$token)->first();
  17. if(!$db)
  18. return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
  19. return response(json_encode(['name' => $db->name]))->header('Content-Type', 'application/json');
  20. }
  21. public function query(Request $request) {
  22. $token = $request->input('token');
  23. if(!$token)
  24. return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
  25. $db = DB::table('databases')->where('read_token','=',$token)->first();
  26. if(!$db)
  27. return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
  28. $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
  29. if($request->input('tz')) {
  30. $tz = $request->input('tz');
  31. } else {
  32. $tz = 'America/Los_Angeles';
  33. }
  34. if($date=$request->input('date')) {
  35. $start = DateTime::createFromFormat('Y-m-d H:i:s', $date.' 00:00:00', new DateTimeZone($tz));
  36. $end = DateTime::createFromFormat('Y-m-d H:i:s', $date.' 23:59:59', new DateTimeZone($tz));
  37. } else {
  38. return response(json_encode(['error' => 'no date provided']))->header('Content-Type', 'application/json');
  39. }
  40. $results = $qz->queryRange($start, $end);
  41. $locations = [];
  42. $properties = [];
  43. $events = [];
  44. foreach($results as $id=>$record) {
  45. if(property_exists($record->data->properties, 'action')) {
  46. $rec = $record->data;
  47. $date = $record->date;
  48. $rec->properties->unixtime = (int)$date->format('U');
  49. $events[] = $rec;
  50. } else {
  51. #$record->date->format('U.u');
  52. $locations[] = $record->data;
  53. $props = $record->data->properties;
  54. $date = $record->date;
  55. $date->setTimeZone(new DateTimeZone($tz));
  56. $props->timestamp = $date->format('c');
  57. $props->unixtime = (int)$date->format('U');
  58. $properties[] = $props;
  59. }
  60. }
  61. if($request->input('format') == 'linestring') {
  62. $linestring = array(
  63. 'type' => 'LineString',
  64. 'coordinates' => array(),
  65. 'properties' => $properties
  66. );
  67. foreach($locations as $loc) {
  68. if(property_exists($loc, 'geometry'))
  69. $linestring['coordinates'][] = $loc->geometry->coordinates;
  70. else
  71. $linestring['coordinates'][] = null;
  72. }
  73. $response = array(
  74. 'linestring' => $linestring,
  75. 'events' => $events
  76. );
  77. } else {
  78. $response = [
  79. 'locations' => $locations
  80. ];
  81. }
  82. return response(json_encode($response))->header('Content-Type', 'application/json');
  83. }
  84. public function input(Request $request) {
  85. $token = $request->input('token');
  86. if(!$token)
  87. return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
  88. $db = DB::table('databases')->where('write_token','=',$token)->first();
  89. if(!$db)
  90. return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
  91. if(!is_array($request->input('locations')))
  92. 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');
  93. $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'w');
  94. $num = 0;
  95. $trips = 0;
  96. foreach($request->input('locations') as $loc) {
  97. if(array_key_exists('properties', $loc)) {
  98. if(array_key_exists('timestamp', $loc['properties'])) {
  99. try {
  100. if(preg_match('/^\d+\.\d+$/', $loc['properties']['timestamp']))
  101. $date = DateTime::createFromFormat('U.u', $loc['properties']['timestamp']);
  102. elseif(preg_match('/^\d+$/', $loc['properties']['timestamp']))
  103. $date = DateTime::createFromFormat('U', $loc['properties']['timestamp']);
  104. else
  105. $date = new DateTime($loc['properties']['timestamp']);
  106. if($date) {
  107. $num++;
  108. $qz->add($date, $loc);
  109. if(array_key_exists('type', $loc['properties']) && $loc['properties']['type'] == 'trip') {
  110. try {
  111. $job = (new TripComplete($db->id, $loc))->onQueue('compass');
  112. $this->dispatch($job);
  113. $trips++;
  114. } catch(Exception $e) {
  115. Log::warning('Received invalid trip');
  116. }
  117. }
  118. } else {
  119. Log::warning('Received invalid date: ' . $loc['properties']['timestamp']);
  120. }
  121. } catch(Exception $e) {
  122. Log::warning('Received invalid date: ' . $loc['properties']['timestamp']);
  123. }
  124. }
  125. }
  126. }
  127. return response(json_encode(['result' => 'ok', 'saved' => $num, 'trips' => $trips]))->header('Content-Type', 'application/json');
  128. }
  129. }