View on GitHub

amber-javascript-sdk

Amber javascript SDK

amber-javascript-sdk

Boon Amber Javascript SDK

An SDK for Boon Amber sensor analytics

Installation

The Boon Amber Javascript SDK is a nodejs project but is not yet published.

npm install git+ssh://git@github.com/boonlogic/amber-javascript-sdk.git

Credentials setup

Note: An account in the Boon Amber cloud must be obtained from Boon Logic to use the Amber SDK.

The username and password should be placed in a file named ~/.Amber.license whose contents are the following:

{
    "default": {
        "username": "AMBER-ACCOUNT-USERNAME",
        "password": "AMBER-ACCOUNT-PASSWORD",
        "server": "https://amber.boonlogic.com/v1"
    }
}

The ~/.Amber.license file will be consulted by the Amber SDK to find and authenticate your account credentials with the Amber server. Credentials may optionally be provided instead via the environment variables AMBER_USERNAME and AMBER_PASSWORD.

Connectivity test

The following javascript provides a basic proof-of-connectivity:

connect-example.js

const MyClient = require('amber-javascript-sdk')

// create amber instance
let amberInstance = new MyClient.AmberClient()

amberInstance.listSensors().then(function (data) {
    console.log("listSensorsResponse: %o", data)
}).catch(error => {
    console.error(error)
})

Running the connect-example.js script should yield output like the following:

$ node examples/connect-example.js
{
  idToken: 'eyJraWQiOiJMSElFb3JXWTVaY1lJeTZOTThMbXVBUHpWYWxUOEdTS2w3UGxiS2pTdkFRPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiI1MjZlMzM2Yi00ZmY1LTQwNjQtYTY1ZS02MDBmZTA5NmMyYmEiLCJhdWQiOiIzbWFoZHZtMmU2dTlsdm5rYms2OW1hajloMCIsImNvZ25pdG86Z3JvdXBzIjpbInByb2R1Y3Rpb24iXSwiZXZlbnRfaWQiOiI1MjNmMzgzMC01Yzc3LTQ5NTYtODgwYy04YmMzZDU1OTkyOTgiLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTYwMDM5NTU5MSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLnVzLWVhc3QtMS5hbWF6b25hd3MuY29tXC91cy1lYXN0LTFfdDNuMmZoUEVsIiwiY29nbml0bzp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kac46ml7Su9fJXaBGax6gOBNL.SrEwhUF_Qo-l3PVkj23y0w',
  tokenType: 'Bearer'
}

where the dictionary {} lists all sensors that currently exist under the given Boon Amber account.

Full Example

The following javascript will demonstrate each API call in the Amber Javascript SDK.

full-example.js

const MyClient = require('amber-javascript-sdk')

// create amber instance
let amberInstance = new MyClient.AmberClient()

// basic walkthrough of available api calls
amberInstance.createSensor("Sensor-1-4002").then(function (data) { // create new sensor
    console.log("createSensorResponse: %o", data)
    return amberInstance.listSensors()  // get list of sensors
}).then(function (data) {
    console.log("listSensorsResponse: %o", data)
    return amberInstance.getSensor(data[0].sensorId) // get sensor details
}).then(function (data) {
    console.log("getSensorResponse: %o", data.response)
    return amberInstance.updateLabel(data.sensorId, "newLabel") // update sensor label
}).then(function (data) {
    console.log("updateLabelResponse: %o", data.response)
    return amberInstance.configureSensor(data.sensorId, 1, 25) // configure sensor
}).then(function (data) {
    console.log("configureSensorResponse: %o", data.response)
    return amberInstance.getConfig(data.sensorId) // get sensor configuration
}).then(function (data) {
    console.log("getConfigResponse: %o", data.response)
    return amberInstance.streamSensor(data.sensorId, "0.05,1.0,2.5,0.9") // post streaming data
}).then(function (data) {
    console.log("streamSensorResponse: %o", data.response)
    return amberInstance.getStatus(data.sensorId) // get sensor analytic status
}).then(function (data) {
    console.log("getStatusResponse: %o", data.response)
    return amberInstance.deleteSensor(data.sensorId) // delete sensor
}).then(function (data) {
    console.log("deleteSensorResponse: %o", data.response)
    return data
}).catch(error => {
    console.error(error)
})

Sample CSV file processor

The following will process a file named data.csv residing in the examples directory of this javascript. Each row will be fed to an Amber instance with SI analytics being displayed.

stream-example.js
data.csv

const fs = require('fs')
const MyClient = require('amber-javascript-sdk')

// create amber instance
let amberInstance = new MyClient.AmberClient()

amberInstance.createSensor("sensor-1-999").then(function (data) {
    console.log("createSensorResponse: %o", data)
    return amberInstance.configureSensor(data.sensorId, 1, 25) // configure sensor
}).then(function (data) {
    console.log("configureSensorResponse: %o", data.response)

    const filedata = fs.readFileSync('data.csv', 'UTF-8')

    // split the contents by new line
    const lines = filedata.split(/\r?\n/)

    // stream data line by line using synchronous pattern.
    // amber results are received before next call is made
    let chain = Promise.resolve()
    for (let line of lines) {
        chain = chain.then(() => amberInstance.streamSensor(data.sensorId, line).then(function (data) {
            console.log("%o, %o", line, data.response)
        }))
    }
}).catch(error => {
    console.error(error)
})