Browse Source

send the actual location when pinging an external URL

pull/14/merge
Aaron Parecki 7 years ago
parent
commit
e72f0bf55d
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 25 additions and 12 deletions
  1. +5
    -1
      compass/app/Http/Controllers/Api.php
  2. +17
    -10
      compass/app/Jobs/NotifyOfNewLocations.php
  3. +3
    -1
      compass/app/Jobs/TripComplete.php

+ 5
- 1
compass/app/Http/Controllers/Api.php View File

@ -269,6 +269,10 @@ class Api extends BaseController
} }
} }
if($request->input('current')) {
$last_location = $request->input('current');
}
$response = [ $response = [
'result' => 'ok', 'result' => 'ok',
'saved' => $num, 'saved' => $num,
@ -289,7 +293,7 @@ class Api extends BaseController
// Notify subscribers that new data is available // Notify subscribers that new data is available
if($db->ping_urls) { if($db->ping_urls) {
$job = (new NotifyOfNewLocations($db->id))->onQueue('compass');
$job = (new NotifyOfNewLocations($db->id, $last_location))->onQueue('compass');
$this->dispatch($job); $this->dispatch($job);
} }
} }

+ 17
- 10
compass/app/Jobs/NotifyOfNewLocations.php View File

@ -1,8 +1,7 @@
<?php <?php
namespace App\Jobs; namespace App\Jobs;
use DB;
use Log;
use DB, Log;
use App\Jobs\Job; use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling; use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
@ -11,22 +10,30 @@ use DateTime, DateTimeZone;
class NotifyOfNewLocations extends Job implements SelfHandling, ShouldQueue class NotifyOfNewLocations extends Job implements SelfHandling, ShouldQueue
{ {
private $_dbid; private $_dbid;
private $_last_location;
public function __construct($dbid) {
public function __construct($dbid, $last_location) {
$this->_dbid = $dbid; $this->_dbid = $dbid;
$this->_last_location = $last_location;
} }
public function handle() { public function handle() {
$db = DB::table('databases')->where('id','=',$this->_dbid)->first(); $db = DB::table('databases')->where('id','=',$this->_dbid)->first();
$urls = preg_split('/\s+/', $db->ping_urls); $urls = preg_split('/\s+/', $db->ping_urls);
foreach($urls as $url) { foreach($urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'url' => env('BASE_URL').'api/last?token='.$db->read_token.'&geocode=1'
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
if(trim($url)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer '.$db->read_token,
'Compass-Url: '.env('BASE_URL').'api/last?token='.$db->read_token.'&geocode=1'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->_last_location));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
Log::info("Notifying ".$url." with current location");
}
} }
} }
} }

+ 3
- 1
compass/app/Jobs/TripComplete.php View File

@ -238,7 +238,9 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue
$response = curl_exec($ch); $response = curl_exec($ch);
Log::info("Done!"); Log::info("Done!");
Log::info($response);
if(preg_match('/Location: (.+)/', $response, $match)) {
Log::info($match[1]);
}
// echo "========\n"; // echo "========\n";
// echo $response."\n========\n"; // echo $response."\n========\n";

Loading…
Cancel
Save