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.

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