All Products
Search
Document Center

IoT Platform:Convert TSL model data format

Last Updated:Jun 20, 2026

Since the Thing Specification Language (TSL) model data formats for Alibaba Cloud and the China Telecom AEP platform differ, you must convert the data on IoT Platform. This topic explains how to use a Python script for this conversion.

Procedure

  1. Return to your exclusive instance on the IoT Platform console. In the left-side navigation pane, choose Devices > Devices. Find the gateway device (15008261001) and click View. On the device details page, go to the Device Log section and click View.
  2. On the Cloud run log tab, find a message sent from the device to the cloud and click View in the Actions column.
  3. From the Content drop-down list, select Hex. Copy the content and save it to a local file to validate the data parsing script later.
  4. In the left-side navigation pane, choose Devices > Products. Click the product NBProduct1. On the product details page, click the Message Analysis tab. Select Python 2.7, delete the existing script, and then paste the following script.
    The script in this example is for demonstration only. You can modify the script based on your TSL model. For more information, see Submit a script to parse TSL data.
    # -*- coding: UTF-8 -*-
    # The following is a template for Alink-based products. You can write your script based on this template.
    # Converts data from a custom topic to the JSON format when a device reports data to IoT Platform.
    # Input parameter: topic (string). The topic to which the device reports messages.
    # Input parameter: rawData (list). A list of integers. Cannot be empty.
    # Output parameter: jsonObj (dictionary).
    def transform_payload(topic, rawData):
       jsonObj = {}
       return jsonObj
    import json
    import common_util
    ALINK_PROP_REPORT_METHOD = 'thing.event.property.post'
    COMMAND_REPORT = 0x00  # Report a property.
    COMMAND_SET = 0x01  # Set a property.
    COMMAND_REPORT_REPLY = 0x02  # The result returned for data reporting.
    COMMAND_SET_REPLY = 0x03  # The result returned by the device for a property setting.
    COMMAD_UNKOWN = 0xff  # Unknown command.
    ALINK_PROP_SET_METHOD = 'thing.service.property.set'  # The IoT Platform topic that is used to send property setting commands from the cloud to a device.
    ALINK_PROP_SET_REPLY_METHOD = 'thing.service.property.set'  # The IoT Platform topic that a device uses to report the result of a property setting to the cloud.
    SELF_DEFINE_TOPIC_UPDATE_FLAG = '/user/update'  # Custom topic: /user/update.
    SELF_DEFINE_TOPIC_ERROR_FLAG = '/user/update/error' # Custom topic: /user/update/error.
    # Converts raw data from a custom format to the Alink protocol format.
    # Input parameter: rawData (list). A list of integers. Cannot be empty.
    # Output parameter: jsonObj (dictionary). Cannot be empty.
    def raw_data_to_protocol(rawData):
        output = []
        for iters in rawData:
            output.append(chr(iters))
        inStr = ''.join(output)
        try:
            json_object = json.loads(inStr)
            # matched_out = json_object['notifyType']
            deviceId = json_object['deviceId']
            water_consumption = json_object['payload']
            running_data = water_consumption['water_consumption']
            # gatewayId = json_object['gatewayId']
            # service = json_object['service']
            # data_object = service['data']
            # imei = data_object['IMEI']
            # MeasureValue = json_object['MeasureValue']
            # Temperature = json_object['Temperature']
            # BatteryVoltage = data_object['BatteryVoltage']
        except ValueError, e:
            MeasureValue = 'err'
            # imei = 'err'
        jsonMap = {}
        jsonMap['method'] = ALINK_PROP_REPORT_METHOD
        jsonMap['version'] = '1.0'
        jsonMap['id'] = '123'
        params = {}
        # params['IMEI'] = imei
        params['deviceId'] = deviceId
        params['water_consumption']= running_data
        # params['gatewayId'] = gatewayId
        # params['MeasureValue'] = MeasureValue
        # params['BatteryVoltage'] = BatteryVoltage
        # params['Temperature'] = Temperature
        jsonMap['params'] = params
        return jsonMap
    # Convert an 8-bit integer to a byte array.
    def int_8_to_byte(value):
        t_value = '%02X' % value
        if len(t_value) % 2 != 0:
            t_value += '0'
        return hex_string_to_byte_array(t_value)
    # Convert a 32-bit integer to a byte array.
    def int_32_to_byte(value):
        t_value = '%08X' % value
        if len(t_value) % 2 != 0:
            t_value += '0'
        return hex_string_to_byte_array(t_value)
    # Convert a 16-bit integer to a byte array.
    def int_16_to_byte(value):
        t_value = '%04X' % value
        if len(t_value) % 2 != 0:
            t_value += '0'
        return hex_string_to_byte_array(t_value)
    # Convert a hexadecimal string to a byte array.
    def hex_string_to_byte_array(str_value):
        if len(str_value) % 2 != 0:
            return None
        cycle = len(str_value) / 2
        pos = 0
        result = []
        for i in range(0, cycle, 1):
            temp_str_value = str_value[pos:pos + 2]
            temp_int_value = int(temp_str_value, base=16)
            result.append(temp_int_value)
            pos += 2
        return result
    # Converts data from the Alink protocol format to a device-readable format.
    # Input parameter: jsonData (dictionary). Cannot be empty.
    # Output parameter: rawdata (list). A list of integers from 0 to 255. Cannot be empty.
    def protocol_to_raw_data(myjson):
        payload_array = []
        in_params = myjson.get("params")
        test = in_params.get('test', None)
        method = myjson.get('method')
        print(method);
        if method == ALINK_PROP_SET_METHOD:
            params = myjson.get('params')
            params = {}
            params['test'] = test
            content= {}
            content['params']=params
            content['serviceIdentifier'] = 'test'
            mystr = json.dumps(content)
            for ch in mystr:
                    print(ch)
                    payload_array = payload_array + int_8_to_byte(ord(ch))
        elif method == ALINK_PROP_REPORT_METHOD:
            code = json.get('code', None)
            payload_array = payload_array + int_8_to_byte(COMMAND_REPORT_REPLY)
            payload_array = payload_array + int_32_to_byte(int(id))
            payload_array = payload_array + int_8_to_byte(code)
        else:
            code = json.get('code')
            payload_array = payload_array + int_8_to_byte(COMMAD_UNKOWN)
            payload_array = payload_array + int_32_to_byte(int(id))
            payload_array = payload_array + int_8_to_byte(code)
        return payload_array
  5. Under Input Simulation, set Simulation Type to Upstreamed Device Data. Paste the Content that you copied in Step 3 into the editor, and then click Run.
  6. After you confirm that the script works as expected, click Submit to deploy it to the IoT Platform.
  7. Return to the China Telecom AEP platform. On the AEP-Online Debugging page for the nbdevice device, report the water consumption data again.
    In the Device Behavior Simulation area, set Service Type to Data Reporting and Service to Business Data Reporting. Set Water Consumption to 0.90 and report the data. A success message appears in the Device Log on the right.
  8. Return to your exclusive instance on the IoT Platform console. Choose Devices > Devices, find the device 15008261001, and click View in the Actions column.
    On the device details page, navigate to the TSL Data > Status tab. The data is now converted to the TSL model data format for IoT Platform. The current value of the water_consumption property is 0.9 m³.