TSL model data parsing examples

更新时间:
复制 MD 格式

This topic provides script examples for parsing uplink and downlink property data. You will learn how to write a data parsing script based on a Thing Specification Language (TSL) model for a product that uses the pass-through/custom data format.

Step 1: Edit the script

  1. Create a product in the Link Living platform.

    For more information about how to create a product, see Create a product.

    Note

    Data Format to Pass-through/Custom.

  2. Define a TSL model for the product.

    For more information about how to define custom properties, see Add a custom feature.

    This example defines the following three properties.

    IdentifierData typeValue rangeRead/Write type
    prop_floatfloat-100 to 100Read/Write
    prop_int16int32-100 to 100Read/Write
    prop_boolbool0: On; 1: OffRead/Write

    The data parsing script is written based on this TSL model to parse uplink and downlink TSL model data.

  3. Add a TSL model.

    This example defines the following three properties:

    IdentifierData typeValue rangeRead/Write type
    prop_floatfloat-100 to 100Read/Write
    prop_int16int32-100 to 100Read/Write
    prop_boolbool0: On; 1: OffRead/Write

    The message parsing script is written based on the TSL model defined here. The script parses data from mobile originated and mobile terminated TSL model messages.

    The parameter value lengths for communication in this example are defined as follows:

    • Device data reporting request
      FieldBytes
      Frame type1 byte
      Request ID4 bytes
      Property prop_int162 bytes
      Property prop_bool1 byte
      Property prop_float 4 bytes
    • Device data reporting response
      FieldBytes
      Frame type1 byte
      Request ID4 bytes
      Result code 1 byte
    • Set property request
      FieldBytes
      Frame type1 byte
      Request ID4 bytes
      Property prop_int162 bytes
      Property prop_bool1 byte
      Property prop_float 4 bytes
    • Set property response
      FieldBytes
      Frame type1 byte
      Request ID4 bytes
      Result code 1 byte
  4. Write the script.

    1. On the Products page, find the product and click View.

    2. On the Product Details page, click the Data Parsing tab.

    3. Select a scripting language. Then, enter the script in the text box under Edit Script.

      Two scripting languages are supported: JavaScript (ECMAScript 5) and Python 2.7.

      You must define the following two functions in the script to parse uplink and downlink TSL model data.

      Supported scripting languageFunction to transform custom device data format to Alink JSON format (mobile originated communication)Function to transform Alink JSON format to custom device data format (mobile terminated communication)Sample code
      JavaScript (ECMAScript 5)rawDataToProtocolprotocolToRawDataJavaScript script sample
      Python 2.7raw_data_to_protocolprotocol_to_raw_dataPython script sample
      PHP 7.2rawDataToProtocolprotocolToRawDataPHP script sample

      For complete script examples, see JavaScript script examples, Python script examples, and PHP script examples.

Step 2: Test the script online

Important The hexadecimal strings and JSON data passed in and returned in the following examples are for reference only. Do not use them for testing in your actual scenario.

After you edit the script, in the Input Simulation section, select a Simulation Type and enter simulated data to test the script online.

  • Simulate parsing property data reported by a device.

    Select Data reported by device as the simulation type, enter the following simulated data, and then click Execute.

    Note The following simulated request parameter data is for JavaScript scripts. For more sample content, see JavaScript script sample.

    For simulated request parameter data for Python and PHP scripts, see Python script sample and PHP script sample.

    You can use a string-to-hexadecimal converter to convert the JSON request parameter data to hexadecimal data. For example, if the converted data is 00002233441232013fa00000, enter the following data.

    0x00002233441232013fa00000

    The DPI engine transforms the pass-through data into JSON data based on the script rules.

    Click Running Result to view the parsing result.

    {
        "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 by IoT Platform.

    Select Device Accepts Data as the impersonation type, enter the following data in JSON format, and then click Execute.

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

    The data parsing engine transforms JSON data into the following:

    0x0200003039c8
  • Simulate parsing the property setting data sent by IoT Platform.

    Select Device Accepts Data as the impersonation type, enter the following data in JSON format, and then 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 transforms JSON data into the following:

    0x0100003039014d0142f6e76d
  • Simulate parsing the property setting response data returned by the device.

    For Impersonation type, select Device-reported data, enter the following data, and then click Execute.

    0x0300223344c8

    The DPI engine transforms the pass-through data into the following JSON data:

    {
      "code": "200",
      "data": {},
      "id": "2241348",
      "version": "1.0"
    }

Step 3: Submit the script

Important Only submitted scripts can be called by IoT Platform. Scripts in draft status cannot be called.

After you confirm that the script parses data correctly, click Submit. The script is submitted to IoT Platform. IoT Platform calls the script to parse data during mobile originated and mobile terminated communication.

Step 4: Test with a real device

Before you use the script in a production environment, use a real device to communicate with IoT Platform to verify that the script can successfully parse mobile originated and mobile terminated data.

You can use the Link SDK provided by IoT Platform to develop a device and connect it to IoT Platform. For more information, see Link SDK.

  • Test reporting property data.
    1. Use the device to report property data, such as 0x00002233441232013fa00000.
    2. In the IoT Platform console, choose Device Management > Devices.
    3. Find the device and click View. On the Device Details page, click the TSL Data > Status tab and verify that the corresponding property data is displayed.
  • Test sending property data.
    1. In the IoT Platform console, choose Monitoring and Maintenance > Online Debug.
    2. Select the product and device that you want to test. Select Default Module. For Function, select the identifier of the property that you want to test, such as Property (prop_int16). Set Method to Set. Enter the following data and click Send Command.
      {
          "method": "thing.service.property.set", 
          "id": "12345", 
          "version": "1.0", 
          "params": {
              "prop_float": 123.452, 
              "prop_int16": 333, 
              "prop_bool": 1
          }
      }
    3. Verify that the device receives the property setting instruction.
    4. On the Device Details page of the device, click the TSL Data > Status tab and verify that the device reports the current property data.

References