Guides

Explore NDU tutorials and guides.

RPC Reply With data from Related Device

In this tutorial, we will explain how to work with RPC call reply Rule Node and also how to:

Intro

We have 2 devices - Controller and Thermostat. We want to initiate RPC call from Controller and request related Thermostat current temperature value. RPC call will have 2 properties:

Model definition

There is a room where 2 devices are installed: Thermostat and Controller.

We want to initiate RPC request from Controller A and ask the latest temperature of the Thermostat in the same room (Thermostat A)

Configure Rule Chain

Go to Rule Chains -> Add new Rule Chain

Configuration:

image

New Rule Chain is created. Press Edit button and configure Chain.

Add Related attributes node and connect it to the Input node.

This node will load temperature attribute of related Thermostat and save it in Message metadata with name temp.

Configuration:

image

Add Transform Script node

Add Transform Script node and connect it to the Related attributes node.

This node will transform an original message into RPC reply message. RPC call reply node sends Message payload as the response to the request, so we need to construct proper payload in Transformation node.

Configuration:

1
 msg = {"temperature" : metadata.temp} return {msg: msg, msgType: msgType}; 

image

Add RPC call reply node

RPC call reply node takes RPC request ID from message metadata. This ID used to identify incoming RPC call.

This node takes message payload and sends it as the response to the Message Originator.

Configuration:

image



This Rule chain is ready and we should save it. Here is how Related thermostat temperature Rule Chain should look like:

image

Connect Rule Chains

Now we will connect our new chain with the Root Chain. We want to route incoming RPC requests with method property equals getTemperature to our new rule chain (Related thermostat temperature).

Let’s return to the Root Rule Chain, press Edit button and make required changes.

Add Filter Script node

Add Filter Script node and connect it to the Message Type Switch node with relation type RPC Request.

Configuration:

1
 return msg.method === 'getTemperature'; 

image

After this, all incoming messages with Message Type RPC Request will be routed to this node. Inside this node, function will filter only allowed RPC requests with method = getTemperature

Add Rule Chain node

Add Rule Chain node with True relation type to the previous Filter Script node (filter getTemperature).

Configuration:

image

Now, all messages that satisfy configured filter will be routed to Related thermostat temperature Rule Chain

Log unknown request

We also want to log all other RPC requests if they are unknown. We need to add Log node with relation type False to the Filter Script node (filter getTemperature).

All incoming RPC requests with method NOT EQUALS getTemperature will be passed from Filter Script to the Log node.

Configuration:

1
 return 'Unexpected RPC call request message:\n' + JSON.stringify(msg) + '\metadata:\n' + JSON.stringify(metadata); 

image



Changes in the Root Rule Chain are finished and we should save it. Here is how Root Rule Chain should look like:

image

Verify configuration

Configuration is finished and we can verify that Rule Chain works as we expect.

We will use REST RPC API for emulating Controller A device.

For sending HTTP request, we will use curl utility.

For triggering RPC request, we need to:

1
curl -X POST -d '{"method": "getTemperature", "params":{}}' http://localhost:8080/api/v1/IAkHBb9N7kKD9ieLRMFN/rpc --header "Content-Type:application/json"

Responce:

1
{"temperature":"52"}

It is expected result. Controller A sends RPC call to the Thingsboard with method getTemperature. Message was routed via configured Rule Chain and attribute of the related thermostat were fetched and returned in the responce.

If we try to submit request with unknown method we will see message in the Thingsboard log file:

1
curl -X POST -d '{"method": "UNKNOWN", "params":{}}' http://localhost:8080/api/v1/IAkHBb9N7kKD9ieLRMFN/rpc --header "Content-Type:application/json"

[pool-35-thread-3] INFO o.t.rule.engine.action.TbLogNode - Unexpected RPC call request message: {"method":"UNKNOWN","params":{}}metadata: {"deviceType":"Controller","requestId":"0","deviceName":"Controller A"}



For more details how RPC works in the Thignsboard, please read RPC capabilities Article.