Browse Source

add a button to send a trip to the micropub endpoint for the visible time range

pull/5/head
Aaron Parecki 8 years ago
parent
commit
9f38b837a7
6 changed files with 79 additions and 23 deletions
  1. +36
    -0
      compass/app/Http/Controllers/Api.php
  2. +1
    -15
      compass/app/Http/routes.php
  3. +4
    -1
      compass/app/Jobs/TripComplete.php
  4. +13
    -0
      compass/public/assets/map.js
  5. +2
    -0
      compass/resources/views/layouts/map.blade.php
  6. +23
    -7
      compass/resources/views/map.blade.php

+ 36
- 0
compass/app/Http/Controllers/Api.php View File

@ -255,4 +255,40 @@ class Api extends BaseController
return response(json_encode(['result' => 'ok', 'saved' => $num, 'trips' => $trips]))->header('Content-Type', 'application/json');
}
public function trip_complete(Request $request) {
$token = $request->input('token');
if(!$token)
return response(json_encode(['error' => 'no token provided']))->header('Content-Type', 'application/json');
$db = DB::table('databases')->where('write_token','=',$token)->first();
if(!$db)
return response(json_encode(['error' => 'invalid token']))->header('Content-Type', 'application/json');
if($request->input('tz')) {
$tz = new DateTimeZone($request->input('tz'));
} else {
$tz = new DateTimeZone('UTC');
}
$start = new DateTime($request->input('start'), $tz);
$end = new DateTime($request->input('end'), $tz);
$loc = [
'properties' => [
'mode' => $request->input('mode'),
'start' => $start->format('c'),
'end' => $end->format('c'),
]
];
try {
$job = (new TripComplete($db->id, $loc))->onQueue('compass');
$this->dispatch($job);
Log::info('Got a manual trip record: '.$start->format('c').' '.$end->format('c'));
} catch(Exception $e) {
Log::warning('Received invalid trip');
}
return response(json_encode(['result' => 'ok']))->header('Content-Type', 'application/json');
}
}

+ 1
- 15
compass/app/Http/routes.php View File

@ -1,16 +1,5 @@
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$app->get('/', 'Controller@index');
$app->post('/auth/start', 'IndieAuth@start');
@ -27,7 +16,4 @@ $app->get('/api/query', 'Api@query');
$app->get('/api/last', 'Api@last');
$app->get('/api/input', 'Api@account');
$app->post('/api/input', 'Api@input');
// Event::listen('illuminate.query', function($query){
// Log::debug($query);
// });
$app->post('/api/trip-complete', 'Api@trip_complete');

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

@ -44,9 +44,10 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue
$results = $qz->queryRange($start, $end);
$features = [];
foreach($results as $id=>$record) {
// Don't include app action tracking data
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
// Remove some of the app-specific tracking keys from each record
return !in_array($k, ['locations_in_payload','desired_accuracy','significant_change','pauses','deferred']);
}, ARRAY_FILTER_USE_KEY);
$features[] = $record->data;
@ -182,6 +183,7 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue
$endTime = strtotime($features[count($features)-1]->properties['timestamp']);
$duration = $endTime - $startTime;
$params['trip']['properties']['duration']['properties']['num'] = $duration;
$params['trip']['properties']['duration']['properties']['unit'] = 'second';
Log::debug("Overriding duration to $duration");
$points = array_map(function($f){
@ -198,6 +200,7 @@ class TripComplete extends Job implements SelfHandling, ShouldQueue
}
if($distance) {
$params['trip']['properties']['distance']['properties']['num'] = $distance;
$params['trip']['properties']['distance']['properties']['unit'] = 'meter';
Log::debug("Overriding distance to $distance");
}
}

+ 13
- 0
compass/public/assets/map.js View File

@ -164,6 +164,19 @@ jQuery(function($){
}
});
$("#trip-create").click(function(){
$("#trip-create").addClass("loading");
$.post('/api/trip-complete', {
start: $("#range-from").val(),
end: $("#range-to").val(),
tz: $("#timezone").val(),
mode: $("#trip-mode").val(),
token: $("#database").data("write-token")
}, function(response) {
$("#trip-create").removeClass("loading");
});
});
if($("#range-from").val() == "") {
console.log("Autoselecting calendar day");
$(".calendar a[data-date="+((new Date()).toISOString().slice(0,10))+"]").focus().click();

+ 2
- 0
compass/resources/views/layouts/map.blade.php View File

@ -18,6 +18,8 @@
<script src="/assets/leaflet-0.7.3/leaflet.js"></script>
<script src="/assets/leaflet-0.7.3/esri-leaflet.js"></script>
<script src="/assets/highcharts/js/highcharts.js"></script>
<script src="/assets/semantic/semantic.min.js"></script>
<script src="/assets/semantic/components/dropdown.min.js"></script>
<script src="/assets/extensions.js"></script>
<script src="/assets/speed.js"></script>
<script src="/assets/events.js"></script>

+ 23
- 7
compass/resources/views/map.blade.php View File

@ -5,12 +5,28 @@
@include('partials/logged-in')
<div id="daterange"><div class="in">
<div class="ui form"><div class="fields">
<div class="five wide field"><input type="text" id="range-from" placeholder="from" value="<?= $range_from ?>"></div>
<div class="five wide field"><input type="text" id="range-to" placeholder="to" value="<?= $range_to ?>"></div>
<div class="five wide field"><input type="text" id="timezone" value="<?= $range_tz ?>"></div>
<button class="ui submit button" id="range-go">Go</button>
</div></div>
<div class="ui form">
<div class="fields">
<div class="five wide field"><input type="text" id="range-from" placeholder="from" value="<?= $range_from ?>"></div>
<div class="five wide field"><input type="text" id="range-to" placeholder="to" value="<?= $range_to ?>"></div>
<div class="five wide field"><input type="text" id="timezone" value="<?= $range_tz ?>"></div>
<button class="ui submit button" id="range-go">Go</button>
</div>
</div>
@if($database->micropub_endpoint)
<div class="ui form" style="margin-top: 4px;">
<div class="fields">
<div style="display: flex; margin-right: 4px;">
<select id="trip-mode">
@foreach(['bicycle', 'car2go', 'drive', 'train', 'walk', 'run', 'plane', 'train', 'bus'] as $mode)
<option value="{{ $mode }}">{{ $mode }}</option>
@endforeach
</select>
</div>
<button class="ui submit button" id="trip-create">Create Trip</button>
</div>
</div>
@endif
</div></div>
<div id="calendar">
@ -44,6 +60,6 @@
<div id="battery-chart" width="800" height="160"></div>
</div>
<div id="database" data-name="{{ $database->name }}" data-token="{{ $database->read_token }}"></div>
<div id="database" data-name="{{ $database->name }}" data-token="{{ $database->read_token }}" data-write-token="{{ $database->write_token }}"></div>
@endsection

Loading…
Cancel
Save