In this article, we’ll explain how easy it is to connect Facebook users to Internet of Things (IoT) using AirVantage and Parse.
Connect your objects to AirVantage
We assume you already connected your objects to AirVantage. If not, you can use one of the following tutorials:
- Using REST APIs on Raspberry Pi (available soon)
- Enable BeagleBone Black connection to AirVantage using MQTT (available soon)
- ALEOS AF with AirVantage Tutorial
- AirVantage Agent plugin for Open AT Application Framework
Once your objects are connected to AirVantage, you can operate the solution and interact the objects using AirVantage User Interface. You can also create a custom business web application using AirVantage API.
But perhaps one of your objectives is to give this solution to end users. In that case, the AirVantage User Interface will probably be too complex. Maybe you just want to create a mobile application granting end users to access only to their objects and a subset of these objects data. In order to do so, you can use Parse.com to manage user authentication using Facebook, the Parse Android SDK to develop your mobile application and Parse Cloud Code to connect Parse.com to AirVantage.
Parse.com
Parse.com is a Backend-As-A-Service (BAAS) providing user management, data storage, social network integration, push notifications, cloud code hosting and several SDK for Android, iOS, .NET, Javascript, Unity, etc.
We also assume you have a valid Parse.com account. If not, you can create one here.
Initialize an application on Parse.com
- Go to your dashboard on Parse.com
- Create a new application
Connect users using Facebook
In this example, we’ll rely completely on Parse.com for user management. It offers all you need to manage users and connect them to their Facebook account. Each SDK have convenient methods to initialize an account connected to Facebook.
Follow the different steps described in the Parse.com documentation to learn how to connect Parse.com users with Facebook using one of the SDKs.
Now you have users in Parse.com and objects in AirVantage. The next step is to allow Parse.com to access to AirVantage API.
Connect Parse.com to AirVantage API using Cloud code
Initialize cloud code
Parse.com offers to host Javascript code in order to define custom methods available through a simple API. Please read the documentation to initialize Cloud code for your application.
Import AirVantage module
Import the AirVantage Cloud Module airvantage.js in the “cloud” folder initialized during the previous step. This module implements a subset of methods available in AirVantage API.
Create an API Client for Parse
- In Airvantage, go to Develop - API Clients.
- Create a new API Client for your Parse application
- Put a random URL as the Redirect URL, it won’t be used in this example
- Copy the client id and the secret key, we’ll use it in the next step
- As Parse.com cannot handle the OAuth Authorization Code Flow, we need to use the Resource Owner Flow. Now you need to create a user in AirVantage. This user will be used to authenticate Parse.com
- Go to Administration - Users
- Create a new user
- Assign or create a profile with rights required by your application
- Copy the email and password, we’ll use them in the next step
Create a simple cloud function
- Create a new file
main.js
in thecloud/
folder - Copy the following code in this file and change the values with what you get from previous step
var airvantage = require('cloud/airvantage.js');
airvantage.initialize('{client_id}', '{secret_key}', '{user_email}', '{user_password}');
Parse.Cloud.define("get", function(request, response) {
if (!request.params.uid) {
throw 'Missing initialization parameter';
}
else {
airvantage.system({
uid: request.params.uid
},
{
success: function(system) {
response.success(system);
}
});
}
});
This function basically returns the details of the system matching the uid
parameter sent in the request.
Deploy your cloud code on Parse.com and then you can call this function (after replacing the placeholders for the application id, the REST API key and the system uid):
Using command line:
curl -X POST \
-H "X-Parse-Application-Id: {parse_application_id}" \
-H "X-Parse-REST-API-Key: {parse_rest_api_key}" \
-H "Content-Type: application/json" \
-d '{"uid":"{system_uid}"}' \
https://api.parse.com/1/functions/get
Using the Android SDK:
Parse.initialize(this, {parse_application_id}, {parse_client_key});
Map<String, String> params = new HashMap<String, String>();
params.put("uid", "{system_uid}");
Object result = ParseCloud.callFunction("get", params);
You can now access Airvantage objects using cloud code and your users are managed by Parse.com. The next step is to allow Parse to associate objects with users and do minimal controls.
Associate objects to current user
Create a new class on Parse.com
To associate objects with users, we are going to create a table in Parse.com to link object uid with users.
- Go to you application data browser in Parse.com
- Create a new custom class called “Thing”
- Create a new column on this class called “uid” of type “String”. It will be used to store the uid of the AirVantage system.
- Create a new column called “owner” of type “Pointer” (pointing on _User). It will be used to store a reference to the user owning object .
Add a new cloud function to associate an object to a user
Paste the following code in your main.js
file:
Parse.Cloud.define("add", function(request, response) {
if (!request.params.uid || !request.user) {
throw 'Missing initialization parameter';
}
else {
var currentUser = Parse.User.current();
var Thing = Parse.Object.extend("Thing");
var newThing = new Thing();
newThing.set("uid", system.uid);
newThing.set("owner", currentUser);
newThing.save(null, {
success: function(thing) {
response.success("Thing '" + system.uid + "' added to the current user");
},
error: function(thing, error) {
response.error("Failed to add Thing '" + system.uid + "' to the current user");
}
});
}
});
This code will create a new entry in the “Thing” table of your Parse application with given uid and current user. You can add more logic to perform more validation or more complex queries.
Update the “get” method
Now objects are linked to users we can update the get
cloud function in order to validate the user have access to this object.
Update the get
function in your main.js file:
Parse.Cloud.define("get", function(request, response) {
if (!request.params.uid || !request.user) {
throw 'Missing initialization parameter';
}
else {
var currentUser = Parse.User.current();
var Thing = Parse.Object.extend("Thing");
var query = new Parse.Query(Thing);
query.equalTo("owner", currentUser);
query.first({
success: function(object) {
if (!object) {
response.error("The current user doesn't have access to the thing '" + request.params.uid + "'");
}
else {
airvantage.system({
uid: request.params.uid
},
{
success: function(system) {
response.success(system);
}
});
}
},
error: function(error) {
response.error("Unable to retrieve things for the current user.");
}
});
}
});
A complete example?
A complete example of an Android application linking Facebook users to Airvantage is available on GitHub.