Generic Adapter – The Node-red case

As we presented in the blog entry dedicated to AURORAL Adapters, it is necessary to provide a piece of software to connect smart hubs or infrastructures of sensors with the AURORAL software.

To minimise the effort, the bAvenir team wants to provide a framework to quickly develop adapters and where lots of support and automations can get a heavy load out of the way. For this purpose Node-red environment has been chosen and added as an official extension of the AURORAL Node, that can be downloaded and install at the same time as the Node.


The framework: Node-RED

Node-RED is an open source programming framework, which is accesible through browser editor an enable users with little or none programming experience to create applications. It can be used also for connecting things to AURORAL platform, because it has wide list of supporting platforms and protocols. For simplifying somunication with Auroral agent we developed a plugin for Node-RED. It can be installed using Node-RED built in Palette manager, but if you install Node-RED using our AURORAL Node cli, it will be included.

Registering first thing and publishing property

After installing and running the AURORAL Node (Selecting the Node-RED extension), you will be able to access the Node-RED dashboard. Visit http://localhost:1880 to access the Node-RED dashboard.

Node-RED is on first startup empty and you need to setup a connection to Auroral agent.

Empty Node-RED

Device edit dialog - empty

Agent connection dialog

You need to provide a valid Thing Description and specify wether you plan to use your Device for listening to events or read properties from remote devices.

 Device edit dialog

To be able to provide data using this device, you need to connect the 'properties' output of the device to your custom flow to read data from your infrastructure (this flow needs to end with a auroralResponse block). Your custom flow needs to do the following things:

{
"_auroralReqId": "id needed to process the request",
"pid": "unique property identificator",
"oid": "unique object identificator",
"adapterId": "unique identificato in your infrastructure"
}

An example with static data can be seen below: TestDevice flow

{
  "title": "Sensor1",
  "@context": "https://www.w3.org/2019/wot/td/v1",
  "@type": [
    "Ontology:DeviceType"
  ],
  "securityDefinitions": {
    "nosec_sc": {
      "scheme": "nosec"
    }
  },
  "description": "Sensor example for the node-red adapter",
  "properties": {
    "temp": {
      "@type": "Ontology:PropertyType",
      "description": "Retrieve temperature of my device",
      "forms": [
        {
          "href": "http://localhost:1250/api/property/000-111-222/temp",
          "op": "readproperty"
        }
      ],
      "title": "temp",
      "type": "number"
    },
    "status": {
      "@type": "Ontology:PropertyType",
      "description": "Retrieve status of the device",
      "forms": [
        {
          "href": "http://localhost:1250/api/property/000-111-222/status",
          "op": "readproperty"
        }
      ],
      "title": "status",
      "type": "string"
    }
  },
  "security": "nosec_sc"
}

TestDevice flow

Do not forget to enable device in auroral NM (https://auroral.dev.bavenir.eu/nm), otherwise you will receive error.

Publishing event

{
  "@context": "https://www.w3.org/2019/wot/td/v1",
  "@type": "Ontology:DeviceType",
  "title": "EventSource",
  "description": "Sensor example for the node-red adapter",
  "securityDefinitions": {
    "nosec_sc": {
      "scheme": "nosec"
    }
  },
  "security": "nosec_sc",
  "events": {
    "alert": {
      "title": "alert",
      "@type": "Ontology:PropertyType",
      "description": "Notifications that are pushed to AURORAL from my sensor",
      "forms": [
        {
          "op": [
            "subscribeevent"
          ],
          "href": "http://localhost:1250/api/event/000-111-222/alert"
        }
      ]
    }
  }
}

TestDevice flow – When sending event, messege needs to contain these attributes: – msg.type = 'event'msg.eid = 'alert'msg.payload = data sent through event

Subscribing to event

{
  "@context": "https://www.w3.org/2019/wot/td/v1",
  "@type": "Ontology:DeviceType",
  "title": "EventSource",
  "description": "Sensor example for the node-red adapter",
  "securityDefinitions": {
    "nosec_sc": {
      "scheme": "nosec"
    }
  },
  "security": "nosec_sc"
}

Event subscription location – When subscription is enabled, there is option to specify array of oid + eid. It needs to be in strict format. It's example can be seen in next picture. Event subscription detail

Retrieving property from other device

{
  "@context": "https://www.w3.org/2019/wot/td/v1",
  "@type": "Ontology:DeviceType",
  "title": "EventSource",
  "description": "Sensor example for the node-red adapter",
  "securityDefinitions": {
    "nosec_sc": {
      "scheme": "nosec"
    }
  },
  "security": "nosec_sc"
}

Flow - Subscribing to event

Note: It is also possible to send a JSON body with information for updating if the remote device supports that operation. Just add the body as msg.payload = { ... }

Getting registrations

Flow - Getting registered items

{
  "payload": [
    {
      "type": "Device",
      "adapterId": "d4t34fre",
      "name": "Sensor1",
      "privacy": "Private",
      "properties": [
        "temp",
        "status"
      ],
      "events": [
        "alert"
      ],
      "oid": "54d846cd-6318-4bc5-a82f-bff105c68694"
    }
  ]
}

Brought to you by the AURORAL community, keep posted! 💪