Introduction
Build web apps for the Connected Home using the power of AngularJS and Lelylan, a new platform to monitor and control your devices through a simple, open and robust REST API.
Build web apps for the Connected Home using the power of AngularJS and Lelylan, a new platform to monitor and control your devices through a simple, open and robust REST API.
Install lelylan-ng using Bower.
$ bower install lelylan-ng --save
This example shows you how the client works.
{{device.name}}
To fully understand how it works read the next section.
In this section you will learn how to create a web app showing the list of your devices.
To build our app we'll use Yeoman, a collection of tools and frameworks helping developers to quickly build web applications.
With a recent version of Node.js installed, install the yo
package.
In this way you have Yo, Grunt and Bower and can run them directly from the command-line.
$ npm install -g yo
With Yeoman you can install additional generators with npm. For this tutorial you need to install the AngularJS generator.
$ npm install -g generator-angular
To begin, go to the terminal, make a new directory and cd into it.
$ mkdir new-project && cd $_
You can now kick-start your AngularJS app.
$ yo angular
It will also ask you if you would like to include Twitter Bootstrap and other stuff. Once you've decided, just hit Enter. It will take a while to complete.
To preview what the app looks like run the serve command.
$ grunt serve
The server supports LiveReload, meaning you can fire up a text editor, edit a custom element and the browser will reload on save.
Install lelylan-ng using Bower.
$ bower install lelylan-ng --save
Now you have oauth-ng
and all its dependencies ready to be used.
Restart the server to automatically add them to your index page.
$ grunt serve
The setup is now completed.
Add the Lelylan client to your AngularJS app.
// app/scripts/app.js
angular.module('newProjectApp', ['lelylan.client', ... ])
Lelylan uses an OAuth 2.0 server for authentication and authorization. Register a new application and get your Client ID and Client Secret.
The easiest way to get an authorization token is to use
oauth-ng, an
AngularJS directive for OAuth 2.0. Open your main.html view, place the
oauth
tag and set the Client ID and Client Secret with the ones
previously created.
Check out the oauth-ng docs if you want to better understand how it works.
To get your devices use the Device#all
method. Open the
main controller and replace the existing code with the one below.
angular.module('newProjectApp')
.controller('MainCtrl', function ($scope, Device) {
Device.all()
.success(function(data) { $scope.devices = data })
.error(function(data) { $scope.error = 'Unauthorized request. Login first.' })
});
In this snippet of code we inject the Device
service and call
the Device.all()
method to get all of your devices returning a promise
with two $http specific methods: success and error.
Supposing you have created some devices in Lelylan
(if not create one),
now you need to show them. Open the main.html
view and list your devices by showing their names.
{{device.name}}
You're done!
Open your index page, click to the Login link and authorize your application to get
a new access token. You are now ready to see the name of all your devices.
This is just a first simple example of what you can do. Learn how to monitor and
control your devices using the device directive.
for more interesting use cases.
Implemented services
All methods are fully described in the Dev Center.
Requests
Every request returns a
promise
with two $http specific methods: success
and error
.
Device.all().
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Subscription services
When using the
subscriptions API
the access token is not required. In this case the authentication is done by setting the
client ID and the client Secret.
Subscription.auth({
clientId: 'CLIENT_ID_HERE',
clientSecret: 'CLIENT_SECRET_HERE'
});
Subscription.all().success(function(data) {
scope.subscriptions = data
})
Caching
Every request can be cached adding the
cache option.
// makes the http request enabling the cache
Device.all({}, { cache: true }).
success(function(data, status, headers, config) { ... }).
// get the cached request
var $cache = $cacheFactory.get('$http');
var resource = $cache.get('http://api.lelylan.com/devices');
Next time you ask for a previously cached resource, the HTTP request is
not fired and the cached value is returned.
Configurations
Lelylan client accepts the following options.
endpoint
- string representing the API endpoint
(default to http://api.lelylan.com
).
Here a configuration example.
angular.module('app').config(function(lelylanClientConfigProvider) {
lelylanClientConfigProvider.configure({ endpoint: 'http://localhost:9000' });
});