Quantified-Self Application

From AGILE IoT Wiki
Revision as of 11:05, 13 March 2018 by Dpapadimatos (talk | contribs)
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

  • Stream data from sensors and cloud-based data sources
  • Define goals and monitor progress

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');
	})
       .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 (agile management user interface can be used)
  • 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

New Devices can be added by implementing functionality in agile-devicefactory

  • Clone agile-core and agile-dev
  • Create a new device instance in agile-core/org.eclipse.agail.DeviceFactory/src/main/java/org/eclipse/agail/device/instance and implement the required device functionality. An example can be found at [3]
  • in the agile-stack directory create/modify docker-compose.override.yml. Modify the path to the agile-core directory accordingly
 agile-devicefactory:
    command: [ "bash", "/usr/src/app/scripts/start.sh", "DeviceFactory" ]
    #image: agileiot/agile-core-armv7l:v0.2.12
    build: 
       context: ../../agile-core
       dockerfile: Dockerfile
    depends_on:
      - agile-dbus
    volumes:
      - agile-dbus-dir:/usr/src/app/.agile_bus
      - agile-data-core-plugins:/usr/src/app/plugins
    environment:
      DBUS_SESSION_BUS_ADDRESS: "unix:path=/usr/src/app/.agile_bus/agile_bus_socket"
    restart: always
    privileged: true
  • Deploy to a device according to instructions in [4]