This topic provides a sample script for parsing data from custom topics in JavaScript.

Description

  • Parsing data from custom topics is supported only for the China (Shanghai) region.
  • When you configure devices, attach the ? _sn=default tag to custom topics. IoT Platform only parses data from custom topics that are attached with the tag.

    For example, if you need to parse the data that is sent by a device to the /${productKey}/${deviceName}/user/update topic into JSON data, you must define the topic as follows when configuring the device: /${productKey}/${deviceName}/user/update? _sn=default.

    Note When you create a custom topic in the IoT Platform console, define the topic without adding the tag.
  • IoT Platform parses only data submitted by devices to the cloud, and does not parse downstream data sent from the cloud.
  • IoT Platform parses only payloads submitted to the cloud and returns the parsed payloads.
  • Topics in which data resides do not change before or after parsing. For example, a device sends data to the /${productKey}/${deviceName}/user/update topic, and the parsed data still resides in this topic.

Script Template

var SELF_DEFINE_TOPIC_UPDATE_FLAG = '/user/update'  // Custom topic: /user/update
var SELF_DEFINE_TOPIC_ERROR_FLAG = '/user/update/error'  // Custom topic: /user/update/error
/**
 * Converts data from devices to JSON data. The function is called when devices report data through a custom topic to IoT Platform.
 * Input: A topic string. It is the topic for devices reporting messages.    
 * Input: A rawData byte [] array. It cannot be empty.
 * Output: A jsonObj dictionary. It cannot be empty.
 */
function transformPayload(topic, rawData) {
var jsonObj = {};
return jsonObj;
}

Sample script

Note The following script is used only to parse custom topic data. If the Data Format of the product is Custom, you also need to write a script for parsing TSL data. For more information about parsing TSL data, see Example for parsing TSL data.
var SELF_DEFINE_TOPIC_UPDATE_FLAG = '/user/update'  // Custom topic: /user/update
var SELF_DEFINE_TOPIC_ERROR_FLAG = '/user/update/error'  // Custom topic: /user/update/error
/*
  Sample data
  Custom Topic:/user/update for reporting data
Input parameters: topic:/{productKey}/{deviceName}/user/update, and bytes: 0x000000000100320100000000
  Output parameters:
  {
     "prop_float": 0,
     "prop_int16": 50,
     "prop_bool": 1,
     "topic": "/{productKey}/{deviceName}/user/update"
   }
 */
function transformPayload(topic, bytes) {
    var uint8Array = new Uint8Array(bytes.length);
    for (int i = 0; i < bytes.length; i++) {
        uint8Array[i] = bytes[i] & 0xff;
    }
    var dataView = new DataView(uint8Array.buffer, 0);
    var jsonMap = {};

    if(topic.includes(SELF_DEFINE_TOPIC_ERROR_FLAG)) {
        jsonMap['topic'] = topic;
        jsonMap['errorCode'] = dataView.getInt8(0)
    } else if (topic.includes(SELF_DEFINE_TOPIC_UPDATE_FLAG)) {
        jsonMap['topic'] = topic;
        jsonMap['prop_int16'] = dataView.getInt16(5);
        jsonMap['prop_bool'] = uint8Array[7];
        jsonMap['prop_float'] = dataView.getFloat32(8);
    }

    return jsonMap;
}