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)
Buy Linux Magazine
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.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.