Previous posts
AirMobile - 11 - MQTT Java Client
AirMobile - 12 - AirVantage REST API
AirVantage OpenHAB binding
After creating an helper class for accessing the data stored in AirVantage, I am now ready to implement an OpenHAB binding. With such a binding, I will be able to show AirMobile data using typical OpenHAB widgets.
Configuration entries
First of all, I need to define the entries in the default.cfg file that will allow me to setup some configuration parameters for the binding itself. I currently found the following entries
Name | Description |
server | The URL of the AirVantage server (either na.airvantage.net or eu.airvantage.net) |
refresh | Refresh time (in milliseconds). This is the delay between two requests. Default value is 5 seconds |
clientId | Application client Id as can be seen in AirVantage portal |
clientSecret | Application client secret as can be seen in AirVantage portal |
accessKey | Airvantage username |
secretKey | AirVantage password |
accessToken | Optional access token for debug. If present, this value will be added to the requests, thus skipping the authentication process |
Here is an excerpt of my default.cfg with the AirVantage binding enabled
############################### AirVantage Binding ##################################### # # Interval in milliseconds to poll for data (optional, defaults to 5mins). airvantage:refresh=1000 airvantage:server=na.airvantage.net airvantage:clientId=d5862135ab92469f8d41963457348253 airvantage:clientSecret=594f865dd79c4311b79a4af64a7adf0b #airvantage:accessToken=f62c338d-3160-41c0-87a2-c2004453f7c3 # access/secret keys for each user. airvantage:User1.accesskey=aaa airvantage:User2.secretkey=bbb
Binding specification
After that, I need to plan which settings needs to be included in a typical binding configuration. The following entries seems to be the most useful
Name | Description |
measure | The sensor reading to retrieve. Valid values are
|
System | The name of the system (according to AirVantage terminology) to query |
lat | Latitude of the location where the value has been read |
lon | Longitude of the location where the value has been read |
lat and lon are required because data sent by the AirMobile sensor are location-dependent. It's not very useful to know the CO level 10 miles away from my house! The binding takes lat and lon and tries to find the reading taken as close as possible
Here is an example of binding configuration
Number Temperature "Temperature [%d]" <Temperature> {airvantage="system=AirMobile1,measure=temperature,lat=45.10101,lon=9.10101"}
Implementation
In a challenge I participated in the past, I set up a development environment for OpenHAB. So I used this a starting point.
To create the skeleton for the binding, I followed this tutorial.
Once the skeleton has been created, I added the methods to read data out from the AirVantage cloud
The configuration options are handled by the AirVantageBinding class, that simply stores configuration settings and provides the corresponding getters
The binding configuration options, are handled by the AirVantageBindingConfig class, that parses the configuration string
Most of the work is performed by the execute() method in the AirVantageBinding class. This method leverages the AirVantageClient class to get authenticate into and data from AirVantage platform.
Once data has been retrieved and a location is provided in the binding configuration (see previous section), the sample taken at the closest location is sent on the OpenHAB event bus by invoking the eventPublisher.post() method
If no location is provided in the binding configuration, the most recent value is returned
Demo
In the video attached to this post, the AirVantage binding in action can be seen