JavaScript Quickstart SDK for DevicePilot IoT Analytics

DevicePilot SDK for JavaScript

www.devicepilot.com

DevicePilot is the analytics, visualization, and automation tool for IoT. Plug in your data and get instant, powerful insights to track all the important metrics that your business depends on.

This library helps you quickly get started posting your device telemetry so you can begin exploring your IoT data in DevicePilot.

Set-up

  • Sign up for a DevicePilot account if you haven’t already!

You can book a demo at devicepilot.com

  • Get your POST or KPI token

Find your token in Settings > Tokens

  • Add the DevicePilot library to your project:
npm install devicepilot

Getting Started

const DevicePilot = require('devicepilot');

const dp = DevicePilot({
  postToken: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // Required for post requests
  kpiToken: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // Required for kpi requests
});

APIs

  • dp.post: takes a telemetry record or an array of records and posts it to DevicePilot
  • dp.kpi.getResults: takes a KPI identifier and returns its current results

Examples

Post
async function post() {
  await dp
    .post({
      $id: 'unique-device-id', // this is used to identify your device
      // any valid json body will be converted into key:value telemetry:
      ledColour: 'blue',
      switchedOn: true,
      temperature: 20,
      // an array of record objects can also be provided
    })
    .catch((error) => {
      // an error occurred and the post was not successful.
      console.log('post was not successful');
      throw error;
    });
  console.log('posted one record successfully');
}
post();
Get KPI Results
// You can find the ID of a KPI from the Tokens page.
// See documentation for further information.
const kpiId = '12345678-abcd-1234-abcd-1234567890ab';
async function get() {
  const kpiResult = await dp.kpi
    .getResults(kpiId)
    .catch((error) => {
      // an error occurred fetching results
    });
  const {
    error, // if provided, an error occurred when calculating the kpi
    data, // data required to display a KPI result, e.g. x and y for a scatter chart
    meta, // additional information about the data returned, e.g. the type of the y axis
  } = kpiResult;
  if (error) throw new Error(error);
  console.log(JSON.stringify(data, null, 2));
}
get();

Breaking Changes

Version 2.0 introduces dp.kpi.getResults and makes devicepilot isomorphic (the package can now safely be used in browser and server deployments).

To achieve this we have dropped support for token configuration from the environment, and instead added the postToken and kpiToken to the constructor.

Version 3.0 requires node 10 or above for server environments.

Documentation

For more information about using DevicePilot, check out: https://help.devicepilot.com/

Download Details:

Author: DevicePilot

Demo: https://www.devicepilot.com/

Source Code: https://github.com/DevicePilot/devicepilot-js

#node #nodejs #javascript

JavaScript Quickstart SDK for DevicePilot IoT Analytics
1.70 GEEK