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.

265 lines
8.8 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. Log::debug(json_encode($this->_data));
  26. if(!$db->micropub_endpoint) {
  27. Log::info('No micropub endpoint configured for database ' . $db->name);
  28. return;
  29. }
  30. $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
  31. // Load the data from the start and end times
  32. $start = new DateTime($this->_data['properties']['start']);
  33. $end = new DateTime($this->_data['properties']['end']);
  34. if($end->format('U') - $start->format('U') < 15) {
  35. Log::info("Skipping trip since it was too short");
  36. return;
  37. }
  38. $results = $qz->queryRange($start, $end);
  39. $features = [];
  40. foreach($results as $id=>$record) {
  41. // Don't include app action tracking data
  42. if(!property_exists($record->data->properties, 'action')) {
  43. // Ignore locations with accuracy worse than 5000m
  44. if(property_exists($record->data->properties, 'horizontal_accuracy') && $record->data->properties->horizontal_accuracy <= 5000) {
  45. $record->data->properties = array_filter((array)$record->data->properties, function($k){
  46. // Remove some of the app-specific tracking keys from each record
  47. return !in_array($k, ['locations_in_payload','desired_accuracy','significant_change','pauses','deferred']);
  48. }, ARRAY_FILTER_USE_KEY);
  49. $features[] = $record->data;
  50. }
  51. }
  52. }
  53. // Build the GeoJSON for this trip
  54. $geojson = [
  55. 'type' => 'FeatureCollection',
  56. 'features' => $features
  57. ];
  58. $file_path = tempnam(sys_get_temp_dir(), 'compass');
  59. file_put_contents($file_path, json_encode($geojson));
  60. // If there are no start/end coordinates in the request, use the first and last coordinates
  61. if(count($features)) {
  62. if(!array_key_exists('start_location', $this->_data['properties'])) {
  63. $this->_data['properties']['start_location'] = $features[0];
  64. }
  65. if(!array_key_exists('end_location', $this->_data['properties'])) {
  66. $this->_data['properties']['end_location'] = $features[count($features)-1];
  67. }
  68. }
  69. $startAdr = false;
  70. if(array_key_exists('start_location', $this->_data['properties'])) {
  71. // Reverse geocode the start and end location to get an h-adr
  72. $startAdr = [
  73. 'type' => 'h-adr',
  74. 'properties' => [
  75. 'latitude' => $this->_data['properties']['start_location']['geometry']['coordinates'][1],
  76. 'longitude' => $this->_data['properties']['start_location']['geometry']['coordinates'][0],
  77. ]
  78. ];
  79. Log::info('Looking up start location');
  80. $start = self::geocode($startAdr['properties']['latitude'], $startAdr['properties']['longitude']);
  81. if($start) {
  82. $startAdr['properties']['locality'] = $start->locality;
  83. $startAdr['properties']['region'] = $start->region;
  84. $startAdr['properties']['country'] = $start->country;
  85. Log::info('Found start: '.$start->full_name.' '.$start->timezone);
  86. }
  87. } else {
  88. $start = false;
  89. }
  90. $endAdr = false;
  91. if(array_key_exists('end_location', $this->_data['properties'])) {
  92. $endAdr = [
  93. 'type' => 'h-adr',
  94. 'properties' => [
  95. 'latitude' => $this->_data['properties']['end_location']['geometry']['coordinates'][1],
  96. 'longitude' => $this->_data['properties']['end_location']['geometry']['coordinates'][0],
  97. ]
  98. ];
  99. Log::info('Looking up end location');
  100. $end = self::geocode($endAdr['properties']['latitude'], $endAdr['properties']['longitude']);
  101. if($end) {
  102. $endAdr['properties']['locality'] = $end->locality;
  103. $endAdr['properties']['region'] = $end->region;
  104. $endAdr['properties']['country'] = $end->country;
  105. Log::info('Found end: '.$end->full_name.' '.$end->timezone);
  106. }
  107. } else {
  108. $end = false;
  109. }
  110. // Set the timezone of the dates based on the location
  111. $startDate = new DateTime($this->_data['properties']['start']);
  112. if($start && $start->timezone) {
  113. $startDate->setTimeZone(new DateTimeZone($start->timezone));
  114. }
  115. $endDate = new DateTime($this->_data['properties']['end']);
  116. if($end && $end->timezone) {
  117. $endDate->setTimeZone(new DateTimeZone($end->timezone));
  118. }
  119. $params = [
  120. 'h' => 'entry',
  121. 'published' => $endDate->format('c'),
  122. 'trip' => [
  123. 'type' => 'h-trip',
  124. 'properties' => [
  125. 'mode-of-transport' => $this->_data['properties']['mode'],
  126. 'start' => $startDate->format('c'),
  127. 'end' => $endDate->format('c'),
  128. 'route' => 'route.json'
  129. ]
  130. ]
  131. ];
  132. if($startAdr) {
  133. $params['trip']['properties']['start-location'] = $startAdr;
  134. }
  135. if($endAdr) {
  136. $params['trip']['properties']['end-location'] = $endAdr;
  137. }
  138. if(array_key_exists('distance', $this->_data['properties'])) {
  139. $params['trip']['properties']['distance'] = [
  140. 'type' => 'h-measure',
  141. 'properties' => [
  142. 'num' => round($this->_data['properties']['distance']),
  143. 'unit' => 'meter'
  144. ]
  145. ];
  146. }
  147. if(array_key_exists('duration', $this->_data['properties'])) {
  148. $params['trip']['properties']['duration'] = [
  149. 'type' => 'h-measure',
  150. 'properties' => [
  151. 'num' => round($this->_data['properties']['duration']),
  152. 'unit' => 'second'
  153. ]
  154. ];
  155. }
  156. if(array_key_exists('cost', $this->_data['properties'])) {
  157. $params['trip']['properties']['cost'] = [
  158. 'type' => 'h-measure',
  159. 'properties' => [
  160. 'num' => round($this->_data['properties']['cost'], 2),
  161. 'unit' => 'USD'
  162. ]
  163. ];
  164. }
  165. // If there is trip data, recalculate the distance and duration based on the actual data
  166. if(count($features)) {
  167. $startTime = strtotime($features[0]->properties['timestamp']);
  168. $endTime = strtotime($features[count($features)-1]->properties['timestamp']);
  169. $duration = $endTime - $startTime;
  170. $params['trip']['properties']['duration']['type'] = 'h-measure';
  171. $params['trip']['properties']['duration']['properties']['num'] = $duration;
  172. $params['trip']['properties']['duration']['properties']['unit'] = 'second';
  173. Log::debug("Overriding duration to $duration");
  174. $points = array_map(function($f){
  175. return $f->geometry->coordinates;
  176. }, $features);
  177. $simple = \p3k\geo\ramerDouglasPeucker($points, 0.0001);
  178. $last = false;
  179. $distance = 0;
  180. foreach($simple as $p) {
  181. if($last) {
  182. $distance += \p3k\geo\gc_distance($p[1], $p[0], $last[1], $last[0]);
  183. }
  184. $last = $p;
  185. }
  186. if($distance) {
  187. $params['trip']['properties']['distance']['type'] = 'h-measure';
  188. $params['trip']['properties']['distance']['properties']['num'] = $distance;
  189. $params['trip']['properties']['distance']['properties']['unit'] = 'meter';
  190. Log::debug("Overriding distance to $distance");
  191. }
  192. }
  193. // TODO: avgpace for runs
  194. // TODO: avgspeed for bike rides
  195. // TODO: avg heart rate if available
  196. // echo "Micropub Params\n";
  197. // print_r($params);
  198. $multipart = new Multipart();
  199. $multipart->addArray($params);
  200. $multipart->addFile('route.json', $file_path, 'application/json');
  201. $httpheaders = [
  202. 'Authorization: Bearer ' . $db->micropub_token,
  203. 'Content-type: ' . $multipart->contentType()
  204. ];
  205. Log::info('Sending to the Micropub endpoint: '.$db->micropub_endpoint);
  206. // Post to the Micropub endpoint
  207. $ch = curl_init($db->micropub_endpoint);
  208. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  209. curl_setopt($ch, CURLOPT_POST, true);
  210. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
  211. curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart->data());
  212. curl_setopt($ch, CURLOPT_HEADER, true);
  213. $response = curl_exec($ch);
  214. Log::info("Done!");
  215. if(preg_match('/Location: (.+)/', $response, $match)) {
  216. Log::info($match[1]);
  217. }
  218. // echo "========\n";
  219. // echo $response."\n========\n";
  220. //
  221. // echo "\n";
  222. }
  223. public static function geocode($lat, $lng) {
  224. $ch = curl_init(env('ATLAS_BASE').'api/geocode?latitude='.$lat.'&longitude='.$lng);
  225. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  226. curl_setopt($ch, CURLOPT_TIMEOUT, 8);
  227. $response = curl_exec($ch);
  228. if($response) {
  229. return json_decode($response);
  230. }
  231. }
  232. }