I device Oak pubblicano stato, variabili e funzioni su Particle.io.
Particle fornisce sia una console web che un set di API REST.
Documentazione API – https://docs.particle.io/reference/api/
Esempi con curl
Autenticazione
Per prima cosa bisogna autenticarsi al Particle ed ottenere un access_token, che verrà usato per tutte le successive request
curl https://api.particle.io/oauth/token -u particle:particle -d grant_type=password -d username=simone.zabberoni@gmail.com -d password=PasswordMoltoSicura
{ "token_type": "bearer", "access_token": "stringa_access_token", "expires_in": 7776000, "refresh_token": "stringa_refresh_token" }
Elenco dispositivi
curl https://api.particle.io/v1/devices\?access_token\=stringa_access_token
[{ "id": "12345678910", "name": "Zab Oak 2", "last_app": null, "last_ip_address": "91.44.31.217", "last_heard": "2017-02-09T22:31:52.070Z", "product_id": 82, "connected": false, "platform_id": 82, "cellular": false, "status": "normal" }, { "id": "12345678911", "name": "Zab Oak", "last_app": null, "last_ip_address": "91.44.31.217", "last_heard": "2017-02-09T22:31:05.237Z", "product_id": 82, "connected": false, "platform_id": 82, "cellular": false, "status": "normal" }]
Dettagli singolo device
Si usa la stessa API indicando in url il device id
curl https://api.particle.io/v1/devices/12345678910\?access_token\=stringa_access_token
{ "id": "12345678910", "name": "Zab Oak 2", "last_app": null, "last_ip_address": "91.44.31.217", "last_heard": "2017-02-09T22:31:52.070Z", "product_id": 82, "connected": false, "platform_id": 82, "cellular": false, "status": "normal" }
Oak Mean Manager – NodeJS Web Interface
La documentazione ufficiale – https://docs.particle.io/reference/javascript/
Oak Mean Manager (http://ec2-52-51-240-188.eu-west-1.compute.amazonaws.com:8081/) – codice demo e non ottimizzato ma funzionante
Features attuali
- elenco devices
- dettagli per device
- stato delle variabili pubblicate
- elenco ed utlizzo delle funzioni pubblicate
Source
Repo su GitHub – https://github.com/Simone-Zabberoni/oak-mean
Screenshots
Code snippets
Rotta Express per l’autenticazione
apiRouter.post('/authenticate', function(req, res) { particle.login({username: req.body.username, password: req.body.password}) .then( function(data){ console.log('Login success! Token: ', data.body.access_token); res.cookie('loggedUser', req.body.username) // auth ok, send the json .json({ success: true, message: 'Authentication successful.', token: data.body.access_token }); }, function(err) { console.log('API call completed on promise fail: ', err); res.json({ success: false, message: 'Authentication failed:'+err }); } ); });
Rotta Express di richiesta dettaglio di un singolo device
apiRouter.route('/devices/:deviceId') .get(function(req, res) { var token = req.body.token || req.query.token || req.headers['x-access-token']; var devicesPr = particle.getDevice({ deviceId: req.params.deviceId, auth: token }); devicesPr.then( function(device){ console.log('Device attrs retrieved successfully:', device); res.json({ success: true, message: 'Device information retrieval success.', device: device.body }); }, // cut
Angular Device Service and Factory – mappatura funzioni JS UI su rotte Express
angular.module('deviceService', []).factory('Device', function($http) { var deviceFactory = {}; // create auth factory object // get a single device deviceFactory.get = function(id) { return $http.get('/api/devices/' + id); }; // get all devices deviceFactory.all = function() { return $http.get('/api/devices/'); }; // get variable value deviceFactory.variable = function(id, variableName) { return $http.get('/api/variable/' + id + '/' + variableName); }; // send a value to a function deviceFactory.func = function(id, funcData) { return $http.post('/api/function/' + id, funcData); }; // return auth factory object return deviceFactory; });