Adding custom microservice

This page will help you add your custom microservice to the Kaa IoT Platform. You will be able to process data from endpoints in your own way and then send the processed data back to the platform.

Guide overview

Pods status Kaa IoT Platform microservices provide NATS and REST API interfaces for inter-service comunication. Read more about Kaa IoT Platform architecture. We’ll use the example of NATS subscriber and publisher that can consume data from endpoints and write to EPTS. The example is written in JavaScript for Node.JS.

Prerequisites

  1. The next Kaa platform services are up and running: KPC, CM, EPR, DCX, EPTS.
  2. EPTS is configured with the Temperature time series as below.
  3. Node.js and npm are installed.
  4. Download sample code used in this tutorial here.
  5. In terminal go to the directory custom-microservice where code located by executing this commands from the folder with the downloaded materials:
    unzip tutorials
    cd tutorials/custom-microservice/
    

Steps

  1. Initialize npm:
    npm init
    
  2. Install needed dependencies:
    npm install -D nats avsc
    
  3. Don’t forget to forward a NATS port if you’re running this example locally:
    kubectl port-forward {nats-pod-name} 4222:4222
    

    You can get {nats-pod-name} from output of command:

    kubectl get pods
    
  4. To run the example, execute the following command:
    node Nats-example.js
    

What’s going on in the code?

Avro schema for TSTP protocol: This schema is from schema.js in the sample.

const schema = avroSchema;

Connecting to NATS:

const nats = NATS.connect({'url': 'nats://localhost:4222', 'preserveBuffers': true});

Avro schema will be used for parsing message:

const type = avro.parse(JSON.stringify(schema), {wrapUnions: true});

Subscribe

Subscribe to NATS subject where EPTS sends data to:

let subjectForSubscription = `kaa.v1.events.epts.endpoint.data-collection.data-points-received.Temperature`;

Listen for messages from EPTS:

nats.subscribe(subjectForSubscription, function(msg) {
    console.log('Message received: ', type.toString(type.fromBuffer(msg)));
});

Publish

temperature-transformer part from the publish subject below stands for the name of the TSTP transmitter instance. Refer to the below EPTS configuration.

let subjectForPublish = `kaa.v1.events.temperature-transformer.endpoint.data-collection.data-points-received.Temperature`;

Message fields correspond to the avro schema:

const message = {
    correlationId: Math.random().toString(36).substring(7),
    timestamp: 0,
    appVersionName: 'demo_application_v1',
    endpointId: 'qwerty12345678',
    timeSeriesName: 'Temperature',
    dataPoints: [{
        timestamp: 1875943753,
        values: new Map([['value','1']]),
    }]
};

Publish the message to EPTS:

const buf = type.toBuffer(message);
nats.publish(subjectForPublish, buf);

EPTS time series configuration

EPTS must have the next time series configuration:

kaa:
  applications:
    smart_kettle:
      versions:
        smart_kettle_v1:

  tstp.receiver:
    from:
      temperature-transformer:
        time-series:
        - Temperature
        - Logs

Resources

All tutorial resources are located on GitHub.

Next steps