From d783301af44265c08bceef16216f0a9ced11521d Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Tue, 29 Dec 2015 13:25:36 -0800 Subject: [PATCH] include route data as a geojson file also include timezone in start/end times, and reverse geocoded place name --- compass/app/Jobs/TripComplete.php | 126 +++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 35 deletions(-) diff --git a/compass/app/Jobs/TripComplete.php b/compass/app/Jobs/TripComplete.php index d073553..880b3f7 100644 --- a/compass/app/Jobs/TripComplete.php +++ b/compass/app/Jobs/TripComplete.php @@ -8,39 +8,53 @@ use p3k\Multipart; use App\Jobs\Job; use Illuminate\Contracts\Bus\SelfHandling; use Illuminate\Contracts\Queue\ShouldQueue; +use DateTime, DateTimeZone; class TripComplete extends Job implements SelfHandling, ShouldQueue { private $_dbid; private $_data; - + public function __construct($dbid, $data) { $this->_dbid = $dbid; $this->_data = $data; } - + public function handle() { - print_r($this->_data); - + // echo "Job Data\n"; + // echo json_encode($this->_data)."\n"; + if(!is_array($this->_data)) return; + $db = DB::table('databases')->where('id','=',$this->_dbid)->first(); - + + Log::info("Starting job for ".$db->name); + if(!$db->micropub_endpoint) { Log::info('No micropub endpoint configured for database ' . $db->name); return; } - + $qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r'); - + + // Load the data from the start and end times + $start = new DateTime($this->_data['properties']['start']); + $end = new DateTime($this->_data['properties']['end']); + $results = $qz->queryRange($start, $end); + $features = []; + foreach($results as $id=>$record) { + if(!property_exists($record->data->properties, 'action')) { + $record->data->properties = array_filter((array)$record->data->properties, function($k){ + // Remove some of the app-specific tracking keys + return !in_array($k, ['locations_in_payload','desired_accuracy','significant_change','pauses','deferred']); + }, ARRAY_FILTER_USE_KEY); + $features[] = $record->data; + } + } + // Build the GeoJSON for this trip $geojson = [ 'type' => 'FeatureCollection', - 'features' => [ - [ - 'type' => 'Feature', - 'geometry' => $this->_data['geometry'], - 'properties' => [] - ] - ] + 'features' => $features ]; $file_path = tempnam(sys_get_temp_dir(), 'compass'); file_put_contents($file_path, json_encode($geojson)); @@ -49,60 +63,88 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue $startAdr = [ 'type' => 'h-adr', 'properties' => [ - 'latitude' => $this->_data['geometry']['coordinates'][1], - 'longitude' => $this->_data['geometry']['coordinates'][0], + 'latitude' => $this->_data['properties']['start-coordinates'][1], + 'longitude' => $this->_data['properties']['start-coordinates'][0], ] ]; $endAdr = [ 'type' => 'h-adr', 'properties' => [ - 'latitude' => $this->_data['geometry']['coordinates'][1], - 'longitude' => $this->_data['geometry']['coordinates'][0], + 'latitude' => $this->_data['properties']['end-coordinates'][1], + 'longitude' => $this->_data['properties']['end-coordinates'][0], ] ]; + Log::info('Looking up start and end locations'); + $start = self::geocode($this->_data['properties']['start-coordinates'][1], $this->_data['properties']['start-coordinates'][0]); + if($start) { + $startAdr['properties']['locality'] = $start->locality; + $startAdr['properties']['region'] = $start->region; + $startAdr['properties']['country'] = $start->country; + Log::info('Found start: '.$start->full_name.' '.$start->timezone); + } + $end = self::geocode($this->_data['properties']['end-coordinates'][1], $this->_data['properties']['end-coordinates'][0]); + if($end) { + $endAdr['properties']['locality'] = $end->locality; + $endAdr['properties']['region'] = $end->region; + $endAdr['properties']['country'] = $end->country; + Log::info('Found end: '.$end->full_name.' '.$end->timezone); + } - $distance = 10; - $duration = 100; + // Set the timezone of the dates based on the location + $startDate = new DateTime($this->_data['properties']['start']); + if($start && $start->timezone) { + $startDate->setTimeZone(new DateTimeZone($start->timezone)); + } + $endDate = new DateTime($this->_data['properties']['end']); + if($end && $end->timezone) { + $endDate->setTimeZone(new DateTimeZone($end->timezone)); + } $params = [ 'h' => 'entry', - 'created' => $this->_data['properties']['end'], - 'published' => $this->_data['properties']['end'], - 'route' => [ - 'type' => 'h-route', + 'created' => $endDate->format('c'), + 'trip' => [ + 'type' => 'h-trip', 'properties' => [ - 'activity' => $this->_data['properties']['mode'], + 'mode-of-transport' => $this->_data['properties']['mode'], + 'start' => $startDate->format('c'), + 'end' => $endDate->format('c'), 'start-location' => $startAdr, 'end-location' => $endAdr, 'distance' => [ 'type' => 'h-measure', 'properties' => [ - 'num' => $distance, + 'num' => round($this->_data['properties']['distance']), 'unit' => 'meter' ] ], 'duration' => [ 'type' => 'h-measure', 'properties' => [ - 'num' => $duration, + 'num' => round($this->_data['properties']['duration']), 'unit' => 'second' ] ], + 'route' => 'route.json' // TODO: avgpace // TODO: avgspeed ] ] ]; - + + // echo "Micropub Params\n"; + // print_r($params); + $multipart = new Multipart(); $multipart->addArray($params); - $multipart->addFile('geojson', $file_path, 'application/json'); + $multipart->addFile('route.json', $file_path, 'application/json'); $httpheaders = [ 'Authorization: Bearer ' . $db->micropub_token, 'Content-type: ' . $multipart->contentType() ]; - + + Log::info('Sending to the Micropub endpoint: '.$db->micropub_endpoint); // Post to the Micropub endpoint $ch = curl_init($db->micropub_endpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -110,11 +152,25 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders); curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart->data()); curl_setopt($ch, CURLOPT_HEADER, true); - $response = curl_exec($ch); - - echo "========\n"; - echo $response."\n========\n"; + $response = curl_exec($ch); - echo "\n"; + Log::info("Done!"); + Log::info($response); + + // echo "========\n"; + // echo $response."\n========\n"; + // + // echo "\n"; + } + + public static function geocode($lat, $lng) { + $ch = curl_init(env('ATLAS_BASE').'api/geocode?latitude='.$lat.'&longitude='.$lng); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 8); + $response = curl_exec($ch); + if($response) { + return json_decode($response); + } } + }