-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vehicle: add basic support for querying data elements and sampling
This commit adds basic support in the library and in the CLI for sampling vehicle data from the device (not streaming at the moment). Here's an example showing the items available and their current values using the CLI. Note that unavailable items (there are many) are ignored in this case using grep: ``` $ wva vehicle list --value | grep -v Unavailable AccelPedalLowIdle = 3 AccelPedalPosition = 92.0 BatteryPotential = 690.549988 CruiseControlSetSpeed = 255.0 CruiseControlStatus = 7 EngineCoolantTemp = 162.0 EngineManifoldPressure = 414.0 EngineOilPressure = 808.0 EnginePercentLoadAtCurrentSpeed = 100.0 EngineSpeed = 6425.5 FuelEconomy = 42.620319 FuelLevel = 84.800003 FuelRate = 227.100006 PTOStatus = 31 ParkingBrake = 0 Throttle = 102.0 TotalDistance = 3750.0 TotalEngineHours = 950.0 TripDistance = 1875.0 VehicleSpeed = 178.195618 ```
- Loading branch information
Paul Osborne
authored and
Paul Osborne
committed
Mar 20, 2015
1 parent
ef6f8bd
commit b167bae
Showing
6 changed files
with
171 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
six==1.9.0 | ||
requests==2.6.0 | ||
click==3.3 | ||
arrow==0.5.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# Copyright (c) 2015 Digi International Inc. All Rights Reserved. | ||
|
||
import datetime | ||
from dateutil.tz import tzutc | ||
from wva.test.test_utilities import WVATestBase | ||
|
||
|
||
class TestVehicleDataElement(WVATestBase): | ||
def test_get_vehicle_data_elements(self): | ||
self.prepare_json_response("GET", "/ws/vehicle/data", | ||
{ | ||
'data': [ | ||
'vehicle/data/ParkingBrake', | ||
'vehicle/data/VehicleSpeed', # real one has way more... | ||
] | ||
}) | ||
els = self.wva.get_vehicle_data_elements() | ||
self.assertEqual(len(els), 2) | ||
self.assertEqual(set(els.keys()), {'ParkingBrake', 'VehicleSpeed'}) | ||
self.assertEqual(els["ParkingBrake"].name, "ParkingBrake") | ||
self.assertEqual(els["ParkingBrake"].name, "ParkingBrake") | ||
|
||
def test_sample(self): | ||
self.prepare_json_response("GET", "/ws/vehicle/data/VehicleSpeed", | ||
{'VehicleSpeed': | ||
{ | ||
'timestamp': '2015-03-20T20:11:10Z', | ||
'value': 170.664856 | ||
} | ||
}) | ||
el = self.wva.get_vehicle_data_element("VehicleSpeed") | ||
sample = el.sample() | ||
self.assertAlmostEqual(sample.value, 170.664856) | ||
self.assertEqual(sample.timestamp, datetime.datetime(2015, 3, 20, 20, 11, 10, tzinfo=tzutc())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# Copyright (c) 2015 Digi International Inc. All Rights Reserved. | ||
|
||
from collections import namedtuple | ||
import arrow | ||
|
||
VehicleDataSample = namedtuple('VehicleDataSample', ['value', 'timestamp']) | ||
|
||
|
||
class VehicleDataElement(object): | ||
"""Provides access to a particular vehicle data element""" | ||
|
||
def __init__(self, http_client, element_name): | ||
self.name = element_name | ||
self._http_client = http_client | ||
|
||
def sample(self): | ||
"""Get the current value of this vehicle data element | ||
The returned value will be a namedtuple with 'value' and | ||
'timestamp' elements. Example:: | ||
speed_el = wva.get_vehicle_data_element('VehicleSpeed') | ||
for i in xrange(10): | ||
speed = speed_el.sample() | ||
print("Speed: %0.2f @ %s" % (speed.value, speed.timestamp)) | ||
time.sleep(1) | ||
""" | ||
# Response: {'VehicleSpeed': {'timestamp': '2015-03-20T18:00:49Z', 'value': 223.368515}} | ||
data = self._http_client.get("vehicle/data/{}".format(self.name))[self.name] | ||
dt = arrow.get(data["timestamp"]).datetime | ||
value = data["value"] | ||
return VehicleDataSample(value, dt) |