npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jonaphael/iotagent-nodejs

v0.0.4-beta

Published

Base iotagent functionality for dojot

Downloads

1

Readme

dojot IoT agent base library

This library contains all common operations that a IoT agent might need from dojot. This includes receiving notifications related to device operations, such as creation, deletion and updates, as well as methods for sending device data to other components.

How to install

Just execute

npm install @dojot/iotagent-nodejs

How to use

This library has one main function, which is updateAttr. Furthermore, it also generated events "device.create", "device.remove", "device.update", and "device.configure" in "iotagent.device" subject. The updateAttr function and all events are shown in the folloing code.

var iotagentLib = require('@dojot/iotagent-nodejs');
var logger = require("@dojot/dojot-module-logger").logger;
var util = require("util");

logger.debug("Initializing IoT agent...");
var iotagent = new iotagentLib.IoTAgent();
iotagent.init().then(() => {
    logger.debug("... IoT agent was initialized");

    logger.debug("Registering callbacks for device events...");
    let deviceId = undefined;
    iotagent.on('iotagent.device', 'device.create', (tenant, event) => {
        logger.debug(`Got device creation message. Tenant is ${tenant}.`);
        logger.debug(`Data is: ${util.inspect(event)}`);
        logger.debug('Got configure event from Device Manager', event)
        // This is just to get one valid device ID to be used in
        // updateAttr sample.
        deviceId = event.data.id;
    });

    iotagent.on('iotagent.device', 'device.configure', (tenant, event) => {
        logger.debug(`Got device actuation message. Tenant is ${tenant}.`);
        logger.debug(`Data is: ${util.inspect(event)}`);
        logger.debug('Got configure event from Device Manager', event)
    });

    iotagent.on('iotagent.device', 'device.remove', (tenant, event) => {
        logger.debug(`Got device removal message. Tenant is ${tenant}.`);
        logger.debug(`Data is: ${util.inspect(event)}`);
        logger.debug('Got configure event from Device Manager', event)
    });

    iotagent.on('iotagent.device', 'device.update', (tenant, event) => {
        logger.debug(`Got device update message. Tenant is ${tenant}.`);
        logger.debug(`Data is: ${util.inspect(event)}`);
        logger.debug('Got configure event from Device Manager')
    });

    logger.debug("... callbacks for device events were registered.")

    // If there is any configured device, the callback associated to "device.create"
    // event will be called.
    logger.debug("Requesting library to generate event for each device...")
    iotagent.messenger.generateDeviceCreateEventForActiveDevices();
    logger.debug("... event generation was requested.")

    let i = 0;
    const sendMessage = () => {
      i++;
      let msg = {
          "attr1": `this is a sample reading: ${i}`,
      };
      let metadata = {

      };
      if (deviceId != undefined) {
        iotagent.updateAttrs(deviceId, "admin", msg, metadata);
      }
      setTimeout(() => {
        sendMessage();
      }, 2000);
    };

    sendMessage();
});

Device events have the following format:

  • device.create:
{
  "event": "create",
  "meta": {
    "service": "admin"
  },
  "data": {
    "id": "efac",
    "label" : "Device 1",
    "templates" : [ 1, 2, 3],
    "attrs" : {

    },
    "created" : "2018-02-06T10:43:40.890330+00:00"
  }
}
  • device.update:
{
  "event": "update",
  "meta": {
    "service": "admin"
  },
  "data": {
    "id": "efac",
    "label" : "Device 1",
    "templates" : [ 1, 2, 3],
    "attrs" : {

    },
    "created" : "2018-02-06T10:43:40.890330+00:00"
  }
}
  • device.remove:
{
  "event": "remove",
  "meta": {
    "service": "admin"
  },
  "data": {
    "id": "efac"
  }
}
  • device.configure:
{
  "event": "configure",
  "meta": {
    "service": "admin"
  },
  "data" : {
    "id" : "efac",
    "attrs": {
      "reset" : 1,
      "step-motor" : "+45"
    }
  }
}

In order to execute this code, the following environment variables can be set:

# These are the default values
export KAFKA_HOSTS="kafka:9092"
export KAFKA_GROUP_ID="iotagent"
export DATA_BROKER_URL="http://data-broker"
export AUTH_URL="http://auth:5000"
export DEVICE_MANAGER_URL="http://device-manager:5000"

It is important to notice that KAFKA_GROUP_ID variable must be set to something different, as this is used by Kafka to select which clients will receive messages from a particular topic. If not set, all services that uses the same ID will be selected at random to receive messages, which is probably not what you want.