You can use the data forwarding feature of the rules engine to forward data to Function Compute. Then, Function Compute uses function scripts to process business data. This article describes the data forwarding process. In this example, a Thing Specification Language (TSL) communication topic is used as the source topic.
- An IoT Platform instance whose data can be forwarded to Function Compute is created in a region that supports data forwarding to Function Compute.
Supported instance Supported region Enterprise Edition instances: Standard and Video
- China (Shanghai)
- Japan (Tokyo)
- China (Shanghai)
- Japan (Tokyo)
- A data source named DataSource is created and a TSL communication topic is added the data source. For more information, see Create a data source.
- A Function Compute service and a function are created. The function is configured and can run as expected after verification. For more information, see the Function Compute documentation. Important
- When you create the function, you can set the Function Type parameter only to Event Function.
- If you use an IoT Platform Enterprise Edition instance, Function Compute must be activated in the same region where the Enterprise Edition instance resides.
The rules engine uses the data forwarding feature to forward device data to Function Compute. Then, Function Compute runs business-specific function scripts to implement various business features.
The following figure shows the data forwarding process.
You can use the data forwarding feature of the new version or the old version to forward data to Function Compute. For information about examples on how to use the data forwarding feature of the old version, see the Forward data to Function Compute topic of the previous version.
Create a data destination
- Log on to the IoT Platform console.
- On the Overview page, find the instance that you want to manage and click the instance name to go to the Instance Details page. Important Enterprise Edition instances are available in the China (Shanghai) and Japan (Tokyo) regions. If the Enterprise Edition instances are unavailable in the region that you select, skip this step.
- In the left-side navigation pane, choose .
- Optional:On the Data Forwarding page, click Go to New Version in the upper-right corner to go to the new version. Note If you have performed this step, the Data Forwarding page of the new version appears after you choose Rules > Data Forwarding.
- Click the Data Destination tab. On this 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.
Parameter Description Operation Select Send Data to Function Compute. Region Select the region where your Function Compute service is deployed. Service Select a Function Compute service.
You can click Create Service to go to the Function Compute console and create a service. For more information, see Manage services.
Function Version The scope of the update. Valid values:
- Use Default Version: Use the default version of the Function Compute service. The default version is LATEST.
- Select Version: Select the service version that you published.
You can click Create Version to go to the Function Compute console and create a version. For more information, see Manage versions.
- Select Alias: Select the alias that you specified for the version of the Function Compute service.
You can click Create Alias to go to the Function Compute console and create an alias. For more information, see Manage aliases.
Function Select the function that you want to use to receive data.
You can click Create Function to go to the Function Compute console and create a function. For more information, see Manage functions.
Authorization Grant IoT Platform the permissions to write data to Function Compute.
If no RAM roles exist, click Create RAM Role to go to the Resource Access Management (RAM) console, create a RAM role, and then grant permissions to the RAM role. For more information, see Create a RAM role.
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 write a script, see Sample script.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. writeFc(1000, data)
- Click Debugging. In the dialog box that appears, select a product and a device, specify a topic, and then enter payload data to check whether the script runs as expected.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.
- Log on to the Function Compute console. On the details page of the function, the Logs tab displays the execution records of the function. In the upper-right corner of the details page, click Monitoring in the upper-right corner to view the monitoring statistics on the function. Important The monitoring statistics on a function are generated 5 minutes after the function runs.