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.

120 lines
3.1 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. class TripComplete extends Job implements SelfHandling, ShouldQueue
  11. {
  12. private $_dbid;
  13. private $_data;
  14. public function __construct($dbid, $data) {
  15. $this->_dbid = $dbid;
  16. $this->_data = $data;
  17. }
  18. public function handle() {
  19. print_r($this->_data);
  20. $db = DB::table('databases')->where('id','=',$this->_dbid)->first();
  21. if(!$db->micropub_endpoint) {
  22. Log::info('No micropub endpoint configured for database ' . $db->name);
  23. return;
  24. }
  25. $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
  26. // Build the GeoJSON for this trip
  27. $geojson = [
  28. 'type' => 'FeatureCollection',
  29. 'features' => [
  30. [
  31. 'type' => 'Feature',
  32. 'geometry' => $this->_data['geometry'],
  33. 'properties' => []
  34. ]
  35. ]
  36. ];
  37. $file_path = tempnam(sys_get_temp_dir(), 'compass');
  38. file_put_contents($file_path, json_encode($geojson));
  39. // Reverse geocode the start and end location to get an h-adr
  40. $startAdr = [
  41. 'type' => 'h-adr',
  42. 'properties' => [
  43. 'latitude' => $this->_data['geometry']['coordinates'][1],
  44. 'longitude' => $this->_data['geometry']['coordinates'][0],
  45. ]
  46. ];
  47. $endAdr = [
  48. 'type' => 'h-adr',
  49. 'properties' => [
  50. 'latitude' => $this->_data['geometry']['coordinates'][1],
  51. 'longitude' => $this->_data['geometry']['coordinates'][0],
  52. ]
  53. ];
  54. $distance = 10;
  55. $duration = 100;
  56. $params = [
  57. 'h' => 'entry',
  58. 'created' => $this->_data['properties']['end'],
  59. 'published' => $this->_data['properties']['end'],
  60. 'route' => [
  61. 'type' => 'h-route',
  62. 'properties' => [
  63. 'activity' => $this->_data['properties']['mode'],
  64. 'start-location' => $startAdr,
  65. 'end-location' => $endAdr,
  66. 'distance' => [
  67. 'type' => 'h-measure',
  68. 'properties' => [
  69. 'num' => $distance,
  70. 'unit' => 'meter'
  71. ]
  72. ],
  73. 'duration' => [
  74. 'type' => 'h-measure',
  75. 'properties' => [
  76. 'num' => $duration,
  77. 'unit' => 'second'
  78. ]
  79. ],
  80. // TODO: avgpace
  81. // TODO: avgspeed
  82. ]
  83. ]
  84. ];
  85. $multipart = new Multipart();
  86. $multipart->addArray($params);
  87. $multipart->addFile('geojson', $file_path, 'application/json');
  88. $httpheaders = [
  89. 'Authorization: Bearer ' . $db->micropub_token,
  90. 'Content-type: ' . $multipart->contentType()
  91. ];
  92. // Post to the Micropub endpoint
  93. $ch = curl_init($db->micropub_endpoint);
  94. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  95. curl_setopt($ch, CURLOPT_POST, true);
  96. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
  97. curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart->data());
  98. curl_setopt($ch, CURLOPT_HEADER, true);
  99. $response = curl_exec($ch);
  100. echo "========\n";
  101. echo $response."\n========\n";
  102. echo "\n";
  103. }
  104. }