Quantified-Self Application

From AGILE IoT Wiki
Revision as of 07:21, 13 March 2018 by Dpapadimatos (talk | contribs) (Created page with " == Quantified-Self Application == The Quantified-Self Application is a Node js application which uses the AGILE software stack to provide connectivity to supported Bluetooth...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Quantified-Self Application

The Quantified-Self Application is a Node js application which uses the AGILE software stack to provide connectivity to supported Bluetooth Low Energy Devices. The Agile sdk is used for access.

Features

Implementation

Using Agile SDK

Follow the steps needed [1] to install and then setup access to the Agile stack

var agile = require('agile-sdk')({
  api: 'http://agile.local:8080',
  idm: 'http://agile.local:3000',
  token: "LQCL7C14y84Ayqedjmbm1LuIes1TsSyn5Cv"
})

Javascript promises [2] can then be used to asynchronously access the stack.

An example flow is as follows

  • Turn on Device Discovery
agile.protocolManager.discovery.start()
	.then(function() {
  		console.log('General discovery is on');
		return agile.protocolManager.devices();
	})
	.then(function(devices) {
           //do something
	}).catch(function(err){
	console.log("discovery error:"+ err);
	});
  • Check for discovered devices
agile.protocolManager.devices().then(function(devices) {
       devices.forEach(function(device){
         //do something
       })
}).catch(function(err){
       console.log("available devices err :"+ err);
})
  • Register a device
  • Check if it registered correctly and connect
var registeredDevices =  agile.deviceManager.get().then(devices => {
if (devices.length != 0 ){
   devices.forEach(function(device){
         agile.deviceManager.get([deviceId])
	.then(function(device, onRejected) {
	if (onRejected != null) {
		console.log("rejected: "+ onRejected);
	}
	  agile.device.connect([deviceId]).then(function(){
		resolve();
	   })
	}).catch ( err => {
	    console.log ("Device not defined: " + err);
	    reject(err);
	});
}).catch(err => {
	console.log("Device "+device+" not registered " + err);
	reject(err);
});
  • Subscribe to data
agile.device.subscribe(deviceId, componentId).then(stream => {
  stream.onerror = (error) => {
     console.log('Websocket Connection Error ' + JSON.stringify(error));
  };
  stream.onopen = () => {
  };
  stream.onclose = () => {
  };
  stream.onmessage = (e) => {
     if (typeof e.data === 'string') {
     console.log("Received: '" + e.data + "'");
     // do something
  };
}).catch(err => {
  console.log("Can't subscribe " + err);
});

<! === Integrating new device drivers === >