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).
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).
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.
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.
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.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)