This topic describes how to write scripts to parse Thing Specification Language (TSL) data for products whose Data Format is Custom. A sample script is provided for parsing upstream and downstream property data.

Step 1: Submit a script

  1. Create a product in the IoT Platform console.
    For more information about how to create a product, see Create a product.
    Note Set Data Format to Custom.
  2. Define the TSL model for the product. For more information about custom properties, see Define features.

    In this example, the following three properties are defined.

    Identifier Data type Valid values Read/write type
    prop_float float -100 to 100 Read/write
    prop_int16 int32 -100 to 100 Read/write
    prop_bool bool 0: enabled, 1: disabled Read/write

    The scripts for parsing upstream and downstream TSL data are defined based on the TSL that is defined here.

  3. Define the communication protocol as follows:
    Table 1. Requests for data reported by devices
    Field Number of bytes
    Frame type 1
    Request ID 4
    Property prop_int16 2
    Property prop_bool 1
    Property prop_float 4
    Table 2. Responses for data reported by devices
    Field Number of bytes
    Frame type 1
    Request ID 4
    Return code 1
    Table 3. Requests for setting properties
    Field Number of bytes
    Frame type 1
    Request ID 4
    Property prop_int16 2
    Property prop_bool 1
    Property prop_float 4
    Table 4. Responses for setting properties
    Field Number of bytes
    Frame type 1
    Request ID 4
    Return code 1
  4. Write the script.
    1. On the Product page, find the target product and click View.
    2. On Product Details page, select the Data Parsing tab.
    3. Select a scripting language. In the Edit Script text box, enter the script.

      Currently, JavaScript (ECMAScript 5), Python 2.7, and PHP 7.2 are supported.

      The following functions must be called in the script to parse the upstream and downstream TSL data.

      • The function that converts Alink JSON data to custom data:
        • JavaScript (ECMAScript 5): protocolToRawData
        • Python 2.7: protocol_to_raw_data
        • PHP 7.2: protocolToRawData
      • The function that converts custom data to Alink JSON data:
        • JavaScript (ECMAScript 5): rawDataToProtocol
        • Python 2.7: raw_data_to_protocol
        • PHP 7.2: rawDataToProtocol

      For complete sample scripts, see JavaScript example, Python example, and PHP example.

      Note You also need to write a script for uploading parsed data to a custom topic. For more information about writing scripts, see Overview.

      For complete sample scripts of data parsing involving custom topic data and TSL data, see Complete sample script in JavaScript, Complete sample script in Python, and Complete sample script in PHP.

Step 2: Test the script online

After the script is edited, on the Input Simulation tab, select Simulation Type, and enter test script and simulation data to test the script.

  • Parse the property data reported by the device.

    Set the simulation type to Upstreamed Device Data, enter the following simulation data, and then click Run.

    0x00002233441232013fa00000

    The data parsing engine converts the pass-through data to JSON data according to the rules defined in the script.

    Click Parsing Results to view the parsed data.

    {
        "method": "thing.event.property.post", 
        "id": "2241348", 
        "params": {
            "prop_float": 1.25, 
            "prop_int16": 4658, 
            "prop_bool": 1
        }, 
        "version": "1.0"
    }
  • Parse the response returned by IoT Platform.

    Set the simulation type to Received Device Data, enter the following JSON data, and then click Run.

    {
      "id": "12345",
      "version": "1.0",
      "code": 200,
      "method": "thing.event.property.post",
      "data": {}
    }

    The data parsing engine converts the JSON data to the following data.

    0x0200003039c8
  • Parse the data for setting properties sent by IoT Platform.

    Set the simulation type to Received Device Data, enter the following JSON data, and then click Run.

    {
        "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 converts the JSON data to the following data.

    0x0100003039014d0142f6e76d
  • Parse the result data returned by the device after setting properties.

    Set the simulation type to Upstreamed Device Data, enter the following simulation data, and then click Run.

    0x0300223344c8

    The data parsing engine converts the pass-through data to 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 to submit the script to the backend system of IoT Platform.

Note Only submitted scripts can be called by IoT Platform. Scripts that are in the draft state cannot be called.

Step 4: Use a physical device for debugging

Before you use the script, use a real device to communicate with IoT Platform. Make sure that IoT Platform can call the script and parse upstream and downstream data.

  • Test upstream property data.
    1. Use the property data reported by the device, for example, 0x00002233441232013fa00000.
    2. In the IoT Platform console, choose Devices > Devices.
    3. Find the target device and click View. On the Status tab of the Device Details page, check whether the corresponding property data exists.
  • Test downstream property data.
    1. In the IoT Platform console, choose Maintenance > Online Debug.
    2. Select the product and device to debug. Select Debug Physical Device. Set the debug feature to the identifier of the property, for example, prop_int16. Set the method to Set. Enter the following data and click Dispatch Command.
      {
          "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 data for setting properties.
    4. On the Status tab of the Device Details page for the device, check whether the device has reported the current property data.

Related topics

  • For more information about JavaScript (ECMAScript 5) script templates and examples, see JavaScript example.
  • For more information about PHP 7.2 script templates and examples, see Python example.
  • For more information about Python 2.7 script templates and examples, see PHP example.
  • For more information about the data parsing process, see What is data parsing?.
  • For more information about how to parse data involving custom topics, see Overview.