Example of using a TSL data parsing script

Updated at:

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

  1. In the IoT Platform, create a product.
    For more information, see Create a product.
    Note Data format to Pass-through/Custom.
  2. 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.

  3. 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
  4. Write the script.
    1. 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.
    2. 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.

Step 2: Test the script online

Important The hexadecimal strings and JSON data used as input and output in the following examples are for demonstration purposes only. Do not use them directly for debugging in your scenario.

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:

    0x00002233441232013fa00000

    The 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.

    0x0300223344c8

    The 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

Important IoT Platform can invoke only submitted scripts. Scripts that are in a draft state cannot be invoked.

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.
    1. Use the device to report property data, such as 0x00002233441232013fa00000.
    2. In the IoT Platform console, go to for your product.
    3. Click View for the device. On the Device Details page, click the Running Status tab to view the property data.
  • Test sending property data.
    1. In the IoT Platform console, go to for your product.
    2. 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
          }
      }
    3. Check whether the device receives the property setting instruction.
    4. Device DetailsOn the page for the device, go to the tab. Check whether the device reports the current property data.

References