This article provides a template and a sample script in Python to parse data that is sent to custom topics.

Script template

SELF_DEFINE_TOPIC_UPDATE_FLAG = '/user/update' # The custom topic /user/update. 
SELF_DEFINE_TOPIC_ERROR_FLAG = '/user/update/error' # The custom topic /user/update/error. 

# Call a function to convert the data in a custom topic into JSON-formatted data when the device submits data to IoT Platform. 
# topic: an input parameter that specifies the topic to which the device sends messages. The value is a string.    
# rawData: an input parameter. The value is a list of integers that cannot be empty. 
# jsonObj: an output parameter. The value is a JSON object that indicates a dictionary.     
def transform_payload(topic, rawData):
   jsonObj = {}
   return jsonObj

Sample script

Note The following sample script can be used only to parse data that is sent to custom topics. If the Data Type parameter of the product is set to Custom, you also need to write a script to parse Thing Specification Language (TSL) data. For information about how to write a script to parse TSL data, see Example for parsing TSL data.

For information about the custom data format, see Create a product.

# coding=UTF-8
SELF_DEFINE_TOPIC_UPDATE_FLAG = '/user/update'# Custom topic: /user/update.
SELF_DEFINE_TOPIC_ERROR_FLAG = '/user/update/error'# Custom topic: /user/update/error.

# Sample data:
# Custom topic: /user/update. The topic is used to submit data.
# Input parameters:
# topic:  /{productKey}/{deviceName}/user/update
#     bytes:  0x00000000100320100000000
# Output parameters:
# {
# "prop_float": 0,
#     "prop_int16":  50,
#     "prop_bool": 1,
#     "topic": "/{productKey}/{deviceName}/user/update"
#   }
def transform_payload(topic, bytes):
    uint8Array = []
    for byteValue in bytes:
        uint8Array.append(byteValue & 0xff)

    jsonMap = {}
    if SELF_DEFINE_TOPIC_ERROR_FLAG in topic:
        jsonMap['topic'] = topic
        jsonMap['errorCode'] = bytes_to_int(uint8Array[0:1])

    elif SELF_DEFINE_TOPIC_UPDATE_FLAG in topic:
        jsonMap['topic'] = topic
        jsonMap['prop_int16'] = bytes_to_int(uint8Array[5:7])
        jsonMap['prop_bool'] = bytes_to_int(uint8Array[7: 8])
        jsonMap['prop_float'] = bytes_to_int(uint8Array[8:])

    return jsonMap

# Convert a byte array to integers.
def bytes_to_int(bytes):
    data = ['%02X' % i for i in bytes]
    return int(''.join(data), 16) 50,
#     "prop_bool": 1,
#     "topic": "/{productKey}/{deviceName}/user/update"
#   }
def transform_payload(topic, bytes):
    uint8Array = []
    for byteValue in bytes:
        uint8Array.append(byteValue & 0xff)

    jsonMap = {}
    if SELF_DEFINE_TOPIC_ERROR_FLAG in topic:
        jsonMap['topic'] = topic
        jsonMap['errorCode'] = bytes_to_int(uint8Array[0:1])

    elif SELF_DEFINE_TOPIC_UPDATE_FLAG in topic:
        jsonMap['topic'] = topic
        jsonMap['prop_int16'] = bytes_to_int(uint8Array[5:7])
        jsonMap['prop_bool'] = bytes_to_int(uint8Array[7: 8])
        jsonMap['prop_float'] = bytes_to_int(uint8Array[8:])

    return jsonMap

# Convert a byte array to integers. 
def bytes_to_int(bytes):
    data = ['%02X' % i for i in bytes]
    return int(''.join(data), 16)