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.

51 lines
1.5 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use DB, Log;
  4. use App\Jobs\Job;
  5. use Illuminate\Contracts\Bus\SelfHandling;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use DateTime, DateTimeZone;
  8. class NotifyOfNewLocations extends Job implements SelfHandling, ShouldQueue
  9. {
  10. private $_dbid;
  11. public function __construct($dbid) {
  12. $this->_dbid = $dbid;
  13. }
  14. public function handle() {
  15. $db = DB::table('databases')->where('id','=',$this->_dbid)->first();
  16. $urls = preg_split('/\s+/', $db->ping_urls);
  17. $location = [
  18. 'location' => json_decode($db->last_location, true)
  19. ];
  20. if($db->current_trip)
  21. $location['trip'] = json_decode($db->current_trip, true);
  22. $location = json_encode($location, JSON_UNESCAPED_SLASHES);
  23. foreach($urls as $url) {
  24. if(trim($url)) {
  25. $ch = curl_init($url);
  26. curl_setopt($ch, CURLOPT_POST, true);
  27. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  28. 'Content-Type: application/json',
  29. 'Authorization: Bearer '.$db->read_token,
  30. 'Compass-Url: '.env('BASE_URL').'api/last?token='.$db->read_token.'&geocode=1'
  31. ]);
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, $location);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  34. curl_exec($ch);
  35. $timestamp = '';
  36. if($db->last_location) {
  37. $timestamp = json_decode($db->last_location)->properties->timestamp;
  38. }
  39. Log::info("Notifying ".$url." with current location: ".$timestamp);
  40. }
  41. }
  42. }
  43. }