You can forward data that is processed by a parser script to another topic to achieve machine-to-machine (M2M) communication. This article describes the data forwarding process. In this example, a Thing Specification Language (TSL) communication topic is used as the source topic.
Prerequisites
A data source named DataSource is created and a TSL communication topic is added to the data source. For more information, see Create a data source.
Background information
The data forwarding feature of the rules engine allows you to forward data from Topic 1 to Topic 2.The following figure shows the data forwarding process.
Create a data destination
- Log on to the IoT Platform console.
On the Overview page, click All environment. On the All environment tab, find the instance that you want to manage and click the instance ID or instance name.
- In the left-side navigation pane, choose .
In the upper-right corner of the Data Forwarding page, click Go to New Version to go to the new version.
NoteIf you have performed this step, the Data Forwarding page of the new version appears after you choose Message Forwarding > Data Forwarding.
Click Data Destination. On the Data Destination tab, click Create Data Destination.
In the Create Data Destination dialog box, enter a data destination name. In this example, enter DataPurpose. Set the parameters and then click OK.
The following figure shows the parameters.
Parameter
Description
Select Operation
Select Publish to another Topic.
Product
Select the product to which the destination topic belongs.
You must use the
writeIotTopic(destinationId, topic, payload)
function to specify a topic in your parser script. For more information about how to use the function, see Functions.
Configure and start a parser
Create a parser named DataParser. For more information, see Create a parser.
On the Parser Details page, associate the parser with the created data source.
In the Data Source step of the wizard, click Associate Data Source.
In the dialog box that appears, select DataSource from the Data Source drop-down list, and then click OK.
On the Parser Details page, associate the parser with the created data destination.
Click Data Destination in the wizard. In the Data Destination section, click Associate Data Destination.
In the dialog box that appears, select DataPurpose from the Data Destination drop-down list, and then click OK.
In the Data Destination section, view and save the data destination ID. In this example, the ID is 1000.
When you write the parser script, you must use the data destination ID.
On the Parser Details page, click Parser.
In the code editor, enter a script. For more information about how to modify a script, see Script syntax.
For more information about the function parameters, see Functions.
// Use the payload() function to obtain the data that is submitted by devices and convert the data by using the JSON format. var data = payload("json"); // Forward submitted TSL data. writeIotTopic(1000, "/sys/a1I***/room3/thing/service/property/set", data);
Debugging
The following figure shows the parameters.
The following result indicates that the script is implemented.
Click Publish.
Go to the Parser tab of the Data Forwarding page. Find the DataParser parser and click Start in the Actions column to start the parser.