Custom Integration


ThingsBoard PE Feature


Only ThingsBoard Professional Edition supports Platform Integrations feature.

See ThingsBoard PE Installation Options to install ThingsBoard PE.

Introduction

Custom integration is only executed remotely from the main ThingsBoard instance. It allows to create integration with custom configuration that will use any transport protocol for communication with your devices.

This guide contains step-by-step instructions on how to create and launch ThingsBoard custom integration. For example, we will launch custom integration that uses TCP transport protocol to stream data from devices and pushes the converted data to cloud.thingsboard.io.

Before we start, you can find the full code of custom integration example that we will use in this guide here.

Prerequisites

We assume you already have a tenant administrator account on your own ThingsBoard PE v2.4.1+ instance or cloud.thingsboard.io.

Let’s assume that we have a sensor which is sending current temperature, humidity and battery level readings respectively in the following format: “25,40,94”.

Before setting up a custom integration, you need to create an Uplink and a Downlink converters.

Let’s create uplink converter.

image

NOTE: Although the Debug mode is very useful for development and troubleshooting, leaving it enabled in production mode may tremendously increase the disk space, used by the database, because all the debugging data is stored there. It is highly recommended to turn the Debug mode off when done debugging.

See the following script that is pasted to the Decoder function section:

/** Decoder **/

// decode payload to string
var decodedString = decodeToString(payload);
// remove unnecessary [\"] and split by [,] to get an array
var payloadArray = decodedString.replace(/\"/g, "").split(',');
var result = {
    deviceName: "Device A",
    deviceType: "type",
    telemetry: {
        // get each reading from the array and convert the string value to a number
        temperature: Number(payloadArray[0]),
        humidity: Number(payloadArray[1]),
        batteryLevel: Number(payloadArray[2])
    },
    attributes: {}
};

/** Helper functions **/

function decodeToString(payload) {
    return String.fromCharCode.apply(String, payload);
}

function decodeToJson(payload) {
   // convert payload to string.
   var str = decodeToString(payload);

   // parse string to JSON
   var data = JSON.parse(str);
   return data;
}

return result;

The purpose of the decoder function is to parse the incoming data and metadata to a format that ThingsBoard can consume. deviceName and deviceType are required, while attributes and telemetry are optional. Attributes and telemetry are flat key-value objects. Nested objects are not supported.

We will not use the Downlink converter in this guide so there is no need to create one. In case you have another use case, please refer to the following instructions.

Custom Integration Setup

Let’s create custom integration.

image

Notice that Execute remotely is enabled automatically when we choose Custom type and we enable Debug mode.

Integration class is used to create an instance of the integration using the Java reflective method.

The Integration JSON configuration is the custom configuration that has two fields in our case:

  • port, which will be used to bind the TCP server-client communication
  • msgGenerationIntervalMs, the interval between generating the messages

We will get back to this later in this guide.

Custom Integration Application

Download the sample application

Feel free to grab the code from the ThingsBoard repository and build the project with maven:

mvn clean install

Go ahead and add that maven project to your favorite IDE.

Dependencies review

Main dependencies that are used in the project:

<!-- Api ThingsBoard provides to create custom integration -->
<dependency>
    <groupId>org.thingsboard.common.integration</groupId>
    <artifactId>remote-integration-api</artifactId>
    <version>${thingsboard.version}</version>
</dependency>
<!-- Netty for TCP client-server implementation -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>${netty.version}</version>
</dependency>
<!-- Grpc transport between remote integration and ThingsBoard -->
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty</artifactId>
    <version>${grpc.version}</version>
</dependency>

Source code review

Main source code is the CustomIntegration Java class. Integration is expecting “Hello to ThingsBoard” message from the TCP client and replies with the “Hello from ThingsBoard!”. Once the client emulator receives “Hello from ThingsBoard!” , it will start sending auto-generated data to ThingsBoard in the following format: “25,40,94”. The Integration will pass the incoming message as-is to the uplink converter and push data to ThingsBoard.

Next steps

  • 入门指南 - 这些指南提供了ThingsBoard主要功能的快速概述。

  • 安装指南 - 了解如何在各种操作系统上安装ThingsBoard。

  • 数据看板 - 这些指南包含有关如何配置复杂的ThingsBoard仪表板的说明。

  • 数据处理 - 了解如何使用ThingsBoard规则引擎。

  • 数据分析 - 了解如何使用规则引擎执行基本的分析任务。

  • 硬件样品 - 了解如何将各种硬件平台连接到ThingsBoard。

  • 高级功能 - 了解高级ThingsBoard功能。

  • 开发指南 - 了解ThingsBoard中的贡献和开发。