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.

176 lines
5.7 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use DB;
  4. use Log;
  5. use Quartz;
  6. use p3k\Multipart;
  7. use App\Jobs\Job;
  8. use Illuminate\Contracts\Bus\SelfHandling;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use DateTime, DateTimeZone;
  11. class TripComplete extends Job implements SelfHandling, ShouldQueue
  12. {
  13. private $_dbid;
  14. private $_data;
  15. public function __construct($dbid, $data) {
  16. $this->_dbid = $dbid;
  17. $this->_data = $data;
  18. }
  19. public function handle() {
  20. // echo "Job Data\n";
  21. // echo json_encode($this->_data)."\n";
  22. if(!is_array($this->_data)) return;
  23. $db = DB::table('databases')->where('id','=',$this->_dbid)->first();
  24. Log::info("Starting job for ".$db->name);
  25. if(!$db->micropub_endpoint) {
  26. Log::info('No micropub endpoint configured for database ' . $db->name);
  27. return;
  28. }
  29. $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
  30. // Load the data from the start and end times
  31. $start = new DateTime($this->_data['properties']['start']);
  32. $end = new DateTime($this->_data['properties']['end']);
  33. $results = $qz->queryRange($start, $end);
  34. $features = [];
  35. foreach($results as $id=>$record) {
  36. if(!property_exists($record->data->properties, 'action')) {
  37. $record->data->properties = array_filter((array)$record->data->properties, function($k){
  38. // Remove some of the app-specific tracking keys
  39. return !in_array($k, ['locations_in_payload','desired_accuracy','significant_change','pauses','deferred']);
  40. }, ARRAY_FILTER_USE_KEY);
  41. $features[] = $record->data;
  42. }
  43. }
  44. // Build the GeoJSON for this trip
  45. $geojson = [
  46. 'type' => 'FeatureCollection',
  47. 'features' => $features
  48. ];
  49. $file_path = tempnam(sys_get_temp_dir(), 'compass');
  50. file_put_contents($file_path, json_encode($geojson));
  51. // Reverse geocode the start and end location to get an h-adr
  52. $startAdr = [
  53. 'type' => 'h-adr',
  54. 'properties' => [
  55. 'latitude' => $this->_data['properties']['start-coordinates'][1],
  56. 'longitude' => $this->_data['properties']['start-coordinates'][0],
  57. ]
  58. ];
  59. $endAdr = [
  60. 'type' => 'h-adr',
  61. 'properties' => [
  62. 'latitude' => $this->_data['properties']['end-coordinates'][1],
  63. 'longitude' => $this->_data['properties']['end-coordinates'][0],
  64. ]
  65. ];
  66. Log::info('Looking up start and end locations');
  67. $start = self::geocode($this->_data['properties']['start-coordinates'][1], $this->_data['properties']['start-coordinates'][0]);
  68. if($start) {
  69. $startAdr['properties']['locality'] = $start->locality;
  70. $startAdr['properties']['region'] = $start->region;
  71. $startAdr['properties']['country'] = $start->country;
  72. Log::info('Found start: '.$start->full_name.' '.$start->timezone);
  73. }
  74. $end = self::geocode($this->_data['properties']['end-coordinates'][1], $this->_data['properties']['end-coordinates'][0]);
  75. if($end) {
  76. $endAdr['properties']['locality'] = $end->locality;
  77. $endAdr['properties']['region'] = $end->region;
  78. $endAdr['properties']['country'] = $end->country;
  79. Log::info('Found end: '.$end->full_name.' '.$end->timezone);
  80. }
  81. // Set the timezone of the dates based on the location
  82. $startDate = new DateTime($this->_data['properties']['start']);
  83. if($start && $start->timezone) {
  84. $startDate->setTimeZone(new DateTimeZone($start->timezone));
  85. }
  86. $endDate = new DateTime($this->_data['properties']['end']);
  87. if($end && $end->timezone) {
  88. $endDate->setTimeZone(new DateTimeZone($end->timezone));
  89. }
  90. $params = [
  91. 'h' => 'entry',
  92. 'created' => $endDate->format('c'),
  93. 'trip' => [
  94. 'type' => 'h-trip',
  95. 'properties' => [
  96. 'mode-of-transport' => $this->_data['properties']['mode'],
  97. 'start' => $startDate->format('c'),
  98. 'end' => $endDate->format('c'),
  99. 'start-location' => $startAdr,
  100. 'end-location' => $endAdr,
  101. 'distance' => [
  102. 'type' => 'h-measure',
  103. 'properties' => [
  104. 'num' => round($this->_data['properties']['distance']),
  105. 'unit' => 'meter'
  106. ]
  107. ],
  108. 'duration' => [
  109. 'type' => 'h-measure',
  110. 'properties' => [
  111. 'num' => round($this->_data['properties']['duration']),
  112. 'unit' => 'second'
  113. ]
  114. ],
  115. 'route' => 'route.json'
  116. // TODO: avgpace
  117. // TODO: avgspeed
  118. ]
  119. ]
  120. ];
  121. // echo "Micropub Params\n";
  122. // print_r($params);
  123. $multipart = new Multipart();
  124. $multipart->addArray($params);
  125. $multipart->addFile('route.json', $file_path, 'application/json');
  126. $httpheaders = [
  127. 'Authorization: Bearer ' . $db->micropub_token,
  128. 'Content-type: ' . $multipart->contentType()
  129. ];
  130. Log::info('Sending to the Micropub endpoint: '.$db->micropub_endpoint);
  131. // Post to the Micropub endpoint
  132. $ch = curl_init($db->micropub_endpoint);
  133. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  134. curl_setopt($ch, CURLOPT_POST, true);
  135. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
  136. curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart->data());
  137. curl_setopt($ch, CURLOPT_HEADER, true);
  138. $response = curl_exec($ch);
  139. Log::info("Done!");
  140. Log::info($response);
  141. // echo "========\n";
  142. // echo $response."\n========\n";
  143. //
  144. // echo "\n";
  145. }
  146. public static function geocode($lat, $lng) {
  147. $ch = curl_init(env('ATLAS_BASE').'api/geocode?latitude='.$lat.'&longitude='.$lng);
  148. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  149. curl_setopt($ch, CURLOPT_TIMEOUT, 8);
  150. $response = curl_exec($ch);
  151. if($response) {
  152. return json_decode($response);
  153. }
  154. }
  155. }