Reading driving data via the on-board diagnostic port

Dancing with OAuth

How can a user authorize a script to pick up private data from the Automatic server on their behalf? Version 0.05 or newer of the CPAN OAuth::Cmdline module offers both helpers for providers such as Google Drive or Spotify, and an interface for Automatic.com. To guide the user through the token dance in a web browser, users first need to register the application on the Automatic developer site (Figure 7).

Figure 7: Before API access, you need to register the script with Automatic.

Under My Apps, press the + button and enter the following URL in the OAuth redirect URL field next to the name of the app (Perlsnapshot in this case)

http://localhost:8082/callback

so that the Automatic server, after completing the work, will guide the command-line client back to its own web server and store the access token it has received locally on the hard disk.

Automatic insists on release checking commercial applications before commissioning, but in test mode you can manage up to 25 user accounts without this inquisition. Automatic confirms the registration of the app with two keys: the client ID and the client secret. You need to drop both into the .automatic.yml file in YAML format in your home directory:

~/.automatic.yml
client_id: <XXXXX>
client_secret: <YYYY>

In the CPAN OAuth::Cmdline module's eg directory, you will find a script named automatic-token-init; when launched, it outputs a message indicating that it has started a web server on port 8082 of the localhost. If you then point your browser at http://localhost:8082, it shows you a link titled Login on Automatic, which takes you to the login page of the Automatic server at the push of a button (Figure 8).

Figure 8: The locally launched web server takes the user to the login window of the Automatic website in order to authorize the Perlsnapshot application.

Access Approved

Once you have logged in and agreed to let the new application, whose name is displayed on the site, retrieve user data (Figure 9), the Automatic server returns to the web server controlled by the init script, which then stores some other parameters, such as the access token in the ~/.automatic.yml file, allowing scripts to pick up driving data in the future without requiring the user to enter a password.

Figure 9: The Perlsnapshot app has been successfully integrated with the account.

The CPAN module provides the http_get() method for retrieving authenticated web requests. Behind the scenes, this then retrieves the access token from the YAML file and then neatly adds it to the request header, convincing the server to release the user's private data without much ado.

A detail on the fringe: Unlike other OAuth implementations, Automatic does not offer a refresh token; once issued, an access token remains valid for a whole year, after which the user has to do the token dance once again.

Painting by Numbers

The trip data recorded by the Automatic app contains other informational tidbits. The path field contains a cryptic string that logs the vehicle's current location every few seconds during a trip as the latitude and longitude values in what is known as polyline encoding [3]:

qzjeFb|hjVDkBh@gAY]hBiDrAwBt@_A...

Using the CPAN Algorithm::GooglePolylineEncoding module, you can quickly decipher the string of characters:

(37.74393, -122.43922), (37.74369, -122.43832), ...

The first value specifies the longitude, and the second value specifies the latitude of geographic locations through which the car moved.

During a 10-minute test drive to my go-to place for freshly baked rolls on weekends (the supermarket in San Francisco's Diamond Heights neighborhood), a total of 38 data points were collected; Listing 2 decodes these and prepares them for visualization.

Listing 2

trip-path

01 #!/usr/local/bin/perl -w
02 use strict;
03 use OAuth::Cmdline::Automatic;
04 use JSON qw( from_json to_json );
05 use YAML qw( Dump );
06 use Algorithm::GooglePolylineEncoding;
07
08 my $oa = OAuth::Cmdline::Automatic->new();
09 $oa->raise_error( 1 );
10
11 my $trips = from_json $oa->http_get(
12   "https://api.automatic.com/trip", [] );
13
14 my @locs = Algorithm::GooglePolylineEncoding::decode_polyline(
15     $trips->{ results }->[ 0 ]->{ path } );
16
17 print Dump ( \@locs );

But how can you transfer the measured values to a map like the one shown in Figure 10? The Google Maps API provides a convenient interface; I actually used it in a previous column [4], where I introduced the map-draw script, which accepts latitude/longitude points in YAML format and produces JavaScript from them. It can also tell the browser to draw these points on a map via the Google Maps API.

Figure 10: Morning drive for freshly baked rolls in San Francisco, drawn using the Google Maps API.

Listing 2 thus only needs to produce YAML with latitude/longitude pairs. You can then call

./trip-path | ./map-draw >map.html

to create an HTML page, which you can easily load in your browser with file:///…map.html in the URL field to view an interactive map of the route.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Spotify

    For a monthly fee, the Spotify streaming service beams music onto your desktop or phone. To intensify the groove, Perlmeister Mike Schilli archived his Spotify playlists for eternity using an OAuth-protected web API.

  • Patterns in the Archive

    To help him check his Google Drive files with three different pattern matchers, Mike builds a command-line tool in Go to maintain a meta cache.

  • Perl: Google Drive

    Armed with a Chinese guillotine and a scanner with an automatic document feeder, Mike Schilli gives his books some special treatment, courtesy of Google Drive, which offers 5GB of storage space – room enough to start an online PDF collection.

  • Book Collector

    Mike Schilli does not put books on the shelf; instead, he scans them and saves the PDFs in Google Drive. A command-line Go program then rummages through the digitized books and downloads them as required.

  • Perl – Security Snapshots

    When the Perlmeister is on the road, he likes to know what's going on at home. Armed with just two scripts, he draws on the Tumblr API to store snapshots cyclically from his home security camera.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News