In this tutorial, we will explain how to work with RPC call reply Rule Node and also how to:
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:
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)
Go to Rule Chains -> Add new Rule Chain
Configuration:
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:
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:
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:
This Rule chain is ready and we should save it. Here is how Related thermostat temperature Rule Chain should look like:
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 and connect it to the Message Type Switch node with relation type RPC Request.
Configuration:
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 with True relation type to the previous Filter Script node (filter getTemperature).
Configuration:
Now, all messages that satisfy configured filter will be routed to Related thermostat temperature Rule Chain
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:
Changes in the Root Rule Chain are finished and we should save it. Here is how Root Rule Chain should look like:
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:
{"method": "getTemperature", "params":{}}
Responce:
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:
[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.