Example of using a TSL data parsing script
This topic shows you how to write a data parsing script for a Thing Specification Language (TSL) model. The example script parses uplink and downlink property data for a product that uses the pass-through/custom data format.
Step 1: Edit the script
- In the IoT Platform, create a product.
For more information, see Create a product.Note Data format to Pass-through/Custom.
- Define a TSL model for the product.
For more information, see Add a custom feature.
In this example, the following three properties are defined.
Identifier Data type Value range Read/Write type prop_float float -100 to 100 Read/Write prop_int16 int32 -100 to 100 Read/Write prop_bool bool 0: On; 1: Off Read/Write The data parsing script is based on the TSL model defined in this step and is used to parse uplink and downlink TSL model data.
- Define the device communication protocol as follows.
Table 1. Device data reporting request Field Bytes Frame type 1 byte Request ID 4 bytes Property prop_int16 2 bytes Property prop_bool 1 byte Property prop_float 4 bytes Table 2. Device data reporting response Field Bytes Frame type 1 byte Request ID 4 bytes Result code 1 byte Table 3. Set property request Field Bytes Frame type 1 byte Request ID 4 bytes Property prop_int16 2 bytes Property prop_bool 1 byte Property prop_float 4 bytes Table 4. Property setting response Field Bytes Frame type 1 byte Request ID 4 bytes Result code 1 byte - Write the script.
- On the product's Device Debugging page, find the Device-side Development section. In the Data Parsing step, click Edit Script to open the script editor.
- Select a scripting language. Then, enter the script in the text box under Edit Script.
Note The platform automatically generates a draft data parsing script. You can then define the functions in the script.
Three scripting languages are supported: JavaScript (ECMAScript 5), Python 2.7, and PHP 7.2.
The script must define the following two functions to parse uplink and downlink TSL model data.
- A function to transform Alink JSON data into the custom device data format:
- JavaScript (ECMAScript 5): protocolToRawData
- Python 2.7: protocol_to_raw_data
- PHP 7.2: protocolToRawData
- A function to transform the custom device data format into Alink JSON data:
- JavaScript (ECMAScript 5): rawDataToProtocol
- Python 2.7: raw_data_to_protocol
- PHP 7.2: rawDataToProtocol
For complete script examples, see JavaScript script example, Python script example, and PHP script example.
- A function to transform Alink JSON data into the custom device data format:
Step 2: Test the script online
After editing the script, test it online. Under Input Simulation, select a Simulation Type and enter the analog data.
- Simulate parsing property data reported by a device.
Set the Simulation Type to Device-reported data. Enter the following analog data, and then click Execute.
Note The following input parameter data applies only to the JavaScript script. For more examples, see JavaScript script example.For input parameter data for Python and PHP scripts, see Python script example and PHP script example.
You can use a string-to-hexadecimal converter to convert the input parameter from JSON to hexadecimal format. For example, if the converted data is
00002233441232013fa00000, enter the following data:0x00002233441232013fa00000The Data Parsing and Integration (DPI) engine parses the pass-through data into JSON format based on the rules in the script.
Click Execution Result to view the parsing results.
{ "method": "thing.event.property.post", "id": "2241348", "params": { "prop_float": 1.25, "prop_int16": 4658, "prop_bool": 1 }, "version": "1.0" } - Simulate parsing the response data sent from IoT Platform.
Set Simulation Type to Device-received data. Enter the following JSON data and click Execute.
{ "id": "12345", "version": "1.0", "code": 200, "method": "thing.event.property.post", "data": {} }The data parsing engine transforms JSON-formatted data into the following:
0x0200003039c8 - Simulate parsing the property setting data sent from IoT Platform.
Set Simulation Type to Device-received data. Enter the following JSON and click Execute.
{ "method": "thing.service.property.set", "id": "12345", "version": "1.0", "params": { "prop_float": 123.452, "prop_int16": 333, "prop_bool": 1 } }The Data Parsing Engine parses JSON data as follows:
0x0100003039014d0142f6e76d - Simulate parsing the property setting response data from the device.
Set Simulation Type to Device-reported data. Enter the following data and click Execute.
0x0300223344c8The DPI engine parses the pass-through data into the following JSON data:
{ "code": "200", "data": {}, "id": "2241348", "version": "1.0" }
Step 3: Submit the script
After you confirm that the script parses data correctly, click Submit. This submits the script to IoT Platform, which can then invoke it to parse uplink and downlink data for the device.
Step 4: Debug with a real device
Before you use the script in a production environment, use a real device to communicate with IoT Platform. This verifies that IoT Platform can successfully invoke the script to parse uplink and downlink data.
- Test reporting property data.
- Use the device to report property data, such as
0x00002233441232013fa00000. - In the IoT Platform console, go to for your product.
- Click View for the device. On the Device Details page, click the Running Status tab to view the property data.
- Use the device to report property data, such as
- Test sending property data.
- In the IoT Platform console, go to for your product.
- On the Online Debugging page, select the device to debug. Select Debug Real Device. For the feature, select the identifier of the property you want to debug, such as `prop_int16`. Set the method to Set. Enter the following data and click Send Instruction.
{ "method": "thing.service.property.set", "id": "12345", "version": "1.0", "params": { "prop_float": 123.452, "prop_int16": 333, "prop_bool": 1 } } - Check whether the device receives the property setting instruction.
- Device DetailsOn the page for the device, go to the tab. Check whether the device reports the current property data.
References
- For the template and an example of the JavaScript (ECMAScript 5) script, see JavaScript script example.
- For the template and an example of the Python 2.7 script, see Python script example.
- For the template and an example of the PHP 7.2 script, see PHP script example.
- For basic information, such as the data parsing flow, see Pass-through/Edit script.