Forward data to other topics

更新时间:
复制 MD 格式

A device cannot subscribe to or publish messages to the topics of another device. Data forwarding enables device-to-device (M2M) communication by routing processed messages from one device topic to another through a parser script. This topic uses a Thing Specification Language (TSL) model data reporting topic as an example.

How it works

Data forwarding routes data from Topic 1 to Topic 2. The following diagram shows the data flow.

image

Data forwarding routes messages from Topic 1 of Device 1 to Topic 2 of Device 2. If Device 2 has subscribed to Topic 2 with the required permissions, it receives messages from Topic 1 of Device 1, enabling communication between different devices.

In the preceding diagram:

Scenarios

  • The destination topic for data forwarding is a custom topic:

    • Communication between different devices that belong to the same product.

    • Communication between two devices that belong to different products.

  • The destination topic for data forwarding is the topic for sending TSL model data: /sys/${productKey}/${deviceName}/thing/service/property/set.

    A device receives forwarded data from this topic and sets its property values accordingly. This scenario applies when the destination device needs to update its properties based on the forwarded data.

Limits

  • The destination device must have permission to subscribe to the destination topic and must have subscribed to it. Otherwise, it cannot receive the forwarded message.

  • Cloud gateway products and devices do not support cross-product data forwarding. Data can be forwarded only to a communication topic that belongs to the current product.

  • MQTT-based instances support only one product. Therefore, cross-product data forwarding is not supported by default.

Prerequisites

You must add a data source for the device topic. For example, you can create a data source named DataSource and add the TSL model data reporting topic of a specific device. For more information, see Add a data source.

Step 1: Create a data destination

  1. Log on to the IoT Platform console.

  2. On the Overview page, find the instance that you want to manage and click the instance ID or instance name.

  3. In the left-side navigation pane, choose Message Forwarding > Data Forwarding.

  4. On the Data Forwarding page, click Go to New Version in the upper-right corner.

    Note

    If you have performed this operation before, you are automatically redirected to the new version of the Data Forwarding page.

  5. Click the Data destination tab, and then click Create data destination.

  6. In the Create Data Destination dialog box, enter a data destination name, such as DataPurpose, configure the parameters according to the descriptions below, and click OK.

    The following figure shows a sample configuration.选择操作

    Parameter

    Description

    Select Operation

    Select Publish To Another Topic.

    Product

    Select the product to which the destination topic belongs.

    The specific topic must be set in the parser script using the writeIotTopic(destinationId, topic, payload) function. For more information about the function, see Data forwarding functions.

Step 2: Configure and start the parser

  1. Create a parser, such as DataParser. For more information, see Step 1: Create a parser.

  2. On the Resolver Details page, associate the data source.

    1. In the configuration wizard, under Data Source, click Associate Data Source.

    2. In the dialog box, select DataSource from the Data Source drop-down list and click OK.

  3. On the Resolver Details page, you can associate data destinations.

    1. In the configuration wizard, click Data Destination. Then, in the upper-right corner of the Data Destination list, click Associate Data Destination.

    2. In the dialog box that appears, from the Data Destination drop-down list, select DataPurpose and click OK.

    3. In the data destination list, find the Data Destination ID and record it. For example, 1000.

      You will need this Data Destination ID in the parsing script.

  4. On the parser details page, click the Parser Script tab.

  5. In the script editor, enter a parser script.

    The parser script syntax is similar to JavaScript. For more information about how to write a script, see Script syntax.

    To forward data to another topic, use the writeIotTopic(destinationId, topic, payload) function. For more information about the function parameters, see Function list.

    // Use the payload() function to obtain the message that is reported by the device and convert the message to the JSON format.
    var data = payload("json");
    // Forward the TSL model data.
    writeIotTopic(1000, "/sys/a1I***/room3/thing/service/property/set", data);
  6. Click Debug, select a product and device, enter the Topic and Payload data, and verify that the script is executable.

    The following figure shows sample parameters:调试示例

    The script executed successfully, as shown in the following output.

    运行结果

  7. Click Publish.

  8. Return to the Resolver tab on the Data Forwarding page and click Start for the DataParser resolver.

Operation example

M2M communication between devices based on message forwarding

References

FAQ