Tous les produits
Search
Centre de documentation

IoT Platform:Convertir le format des données du modèle TSL

Dernière mise à jour :Aug 10, 2026

Les formats de données du modèle Thing Specification Language (TSL) d'Alibaba Cloud et de la plateforme AEP de China Telecom diffèrent. Vous devez donc convertir les données sur IoT Platform. Cette rubrique explique comment utiliser un script Python pour cette conversion.

Procédure

  1. Revenez à votre instance dédiée sur la console IoT Platform. Dans le volet de navigation de gauche, accédez à Devices > Devices. Recherchez l'appareil passerelle (15008261001) et cliquez sur View. Sur la page des détails de l'appareil, accédez à la section Device Log et cliquez sur View.

  2. Sous l'onglet Cloud run log, repérez un message envoyé par l'appareil vers le cloud et cliquez sur View dans la colonne Actions.

  3. Dans la liste déroulante Content, sélectionnez Hex. Copiez le contenu et enregistrez-le dans un fichier local afin de valider ultérieurement le script d'analyse des données.

  4. Dans le volet de navigation de gauche, accédez à Devices > Products. Cliquez sur le produit NBProduct1. Sur la page des détails du produit, cliquez sur l'onglet Message Analysis. Sélectionnez Python 2.7, supprimez le script existant, puis collez le script suivant.

    Le script de cet exemple est fourni à titre indicatif uniquement. Modifiez-le selon votre modèle TSL. Pour plus d'informations, consultez

    Soumettre un script pour analyser les données TSL

    .

    # -*- 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. Sous Input Simulation, définissez Simulation Type sur Upstreamed Device Data. Collez le Content copié à l'étape 3 dans l'éditeur, puis cliquez sur Run.

  6. Une fois le fonctionnement du script confirmé, cliquez sur Submit pour le déployer sur IoT Platform.

  7. Revenez à la plateforme AEP de China Telecom. Sur la page AEP-Online Debugging de l'appareil nbdevice, signalez à nouveau les données de consommation d'eau.

    Dans la zone

    Device Behavior Simulation

    , définissez

    Service Type

    sur

    Data Reporting

    et

    Service

    sur

    Business Data Reporting

    . Définissez

    Water Consumption

    sur

    0,90

    et signalez les données. Un message de réussite s'affiche dans le

    Device Log

    à droite.

  8. Revenez à votre instance dédiée sur la console IoT Platform. Accédez à Devices > Devices, recherchez l'appareil 15008261001 et cliquez sur View dans la colonne Actions.

    Sur la page des détails de l'appareil, accédez à l'onglet

    TSL Data > Status

    . Les données sont désormais converties au format de données du modèle TSL pour IoT Platform. La valeur actuelle de la propriété

    water_consumption

    est

    0,9

    m³.