In this tutorial, we will explain how to work with RPC call reply Rule Node and also how to:
- Create and connect different Rule Chains using Rule Chain node
- Filter messages using Script node
- Transform incoming messages with Script node
- Fetch attributes of related entities with Related Attributes node
- Process RPC Calls from devices with RPC call reply node
-
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:
method: getTemperature
-
Model definition
There is a room where 2 devices are installed: Thermostat and Controller.
The Thermostat is represented as Device with the name Thermostat A and type Thermostat.
- The Controller is represented as Device with name Controller A and type Controller.
- Create relation from Controller A to Thermostat A via relation Thermostat
- Add the attribute, with server scope, to the deviceThermostat A.
- Attribute name: temperature
- Attribute value: 52
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
Create new Rule Chain Related thermostat temperature
Go to Rule Chains -> Add new Rule Chain
Configuration:
- Name : Related thermostat temperature
New Rule Chain is created. Press Edit button and configure Chain.
Add Related attributes node
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:
- Name: get related temperature
- Direction: From
- Max relation level: 1
- Relation type : Thermostat
- Entity type : Device
- Latest telemetry : false
- Source attribute : temperature
- Target attribute : temp
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:
- Name: build responce
Script:
msg = {"temperature" : metadata.temp} return {msg: msg, msgType: msgType};
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:Name : send responce
- Request ID : requestId
This Rule chain is ready and we should save it. Here is how Related thermostat temperature Rule Chain should look like:
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:
- Name : filter getTemperature
Script:
return msg.method === 'getTemperature';
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 = getTemperatureAdd Rule Chain node
Add Rule Chain node with True relation type to the previous Filter Script node (filter getTemperature).
Configuration:Rule Chain: Related thermostat temperature
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:
- Name : log others
- Script :
return 'Unexpected RPC call request message:\n' + JSON.stringify(msg) + '\metadata:\n' + JSON.stringify(metadata);
Changes in the Root Rule Chain are finished and we should save it. Here is how Root Rule Chain should look like:
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:
- Take Controller A device API token. We can copy token from Device page. In this tutorial it is IAkHBb9N7kKD9ieLRMFN but it is unique and you need to copy your device token.
- Make POST request to the Thingsboard URL - http://localhost:8080/api/v1/**$ACCESS_TOKEN**/rpc with content type = application/json and payload
{"method": "getTemperature", "params":{}}
Responce:curl -X POST -d '{"method": "getTemperature", "params":{}}' http://localhost:8080/api/v1/IAkHBb9N7kKD9ieLRMFN/rpc --header "Content-Type:application/json"
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.{"temperature":"52"}
If we try to submit request with unknown method we will see message in the Thingsboard log file: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.