Write custom scripts

Updated at:

When the source node's topic data format is raw data in a data parsing task, you can use custom scripts to parse the data. IoT Platform supports JavaScript (ECMAScript 5), Python 2.7, and PHP 7.2 scripts. The following sections provide templates and examples for custom topic data parsing scripts.

JavaScript script example

Script template

/**
 * Converts device-sent data to a custom topic into JSON format upon submission to the IoT Platform.
 * Input parameter: data, byte[] array, must not be empty.
 * Output parameter: jsonObj, object, must not be empty.
 */
function executeScript(data) {
    var jsonObj = {};
    return jsonObj;
}

Script example

/*
  Sample data:
  Custom topic:
     /user/update, to which data is sent.
  Input parameter:
     data: 0x000000000100320100000000
  Output parameter:
    {
        "prop_float": 0,
        "prop_int16": 50,
        "prop_bool": 1
    }
 */
function executeScript(data) {
    var uint8Array = new Uint8Array(data.length);
    for (var i = 0; i < data.length; i++) {
        uint8Array[i] = data[i] & 0xff;
    }
    var dataView = new DataView(uint8Array.buffer, 0);
    var jsonMap = {};
    jsonMap['prop_int16'] = dataView.getInt16(5);
    jsonMap['prop_bool'] = uint8Array[7];
    jsonMap['prop_float'] = dataView.getFloat32(8);

    return jsonMap;
}

Python script example

Script template

# Converts device-sent data to a custom topic into JSON format upon submission to the IoT Platform.
# Input parameter: data, list, elements must be integers and the list cannot be empty.
# Output parameter: jsonObj, dictionary.
def execute_script(data):
   jsonObj = {}
   return jsonObj

Script example

# coding=UTF-8

#  Sample data:
#  Custom topic: /user/update, to which data is sent.
#  Input parameter:
#     data: 0x000000000100320100000000
#  Output parameter:
#  {
#     "prop_float": 0,
#     "prop_int16": 50,
#     "prop_bool": 1
#   }
def execute_script(data):
    uint8Array = []
    for byteValue in data:
        uint8Array.append(byteValue & 0xff)

    jsonMap = {}
    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

# Converts a byte array into an integer.
def bytes_to_int(bytes):
    result = ''.join(['%02X' % i for i in bytes])
    return int(result, 16)

PHP script example

Script template

<?php
/**
 * Converts device-sent data to a custom topic into JSON format upon submission to the IoT Platform.
 * Input parameter: $data, regular array, elements must be integers.
 * Output parameter: $jsonObj, associative array, keys should be strings and not numeric characters like "10", must not be empty.
 */
function executeScript($data)
{
    $jsonObj = array();
    return $jsonObj;
}
    Important

    Do not use global or static variables because they may produce inconsistent results.

    The script processes data by using two's complement. Values in the range of [-128,127] are converted to [0,255]. For example, -1 becomes 255 (decimal).

    The input parameter of the executeScript function is an integer array. Each element must undergo a bitwise AND operation with 0xFF to obtain its unsigned value. The function returns an associative array whose key must be a non-numeric string. For example, a key of "10" causes PHP to interpret it as the integer 10.

    The PHP execution environment uses strict exception handling. Errors throw exceptions and halt subsequent code execution. To ensure robustness, catch and handle exceptions appropriately.

Script example

<?php
/*
  Sample data:
  Custom topic:
     /user/update, to which data is sent.
  Input parameter:
     data: 0x000000000100320100000000
  Output parameter:
  {
     "prop_float": 0,
     "prop_int16": 50,
     "prop_bool": 1
   }
 */
function executeScript($bytes)
{
    $data = array();
    $length = count($bytes);
    for ($i = 0; $i < $length; $i++) {
        $data[$i] = $bytes[$i] & 0xff;
    }

    $jsonMap = array();
    $jsonMap['prop_int16'] = getInt16($data, 5);
    $jsonMap['prop_bool'] = $data[7];

    return $jsonMap;
}

function getInt16($bytes, $index)
{
    $array = array_slice($bytes, $index, 2);
    return hexdec(implode('', array_map('dechex', $array)));
}

function byteArrayToHexString($data)
{
    return implode('', array_map(function($byte) {
        return str_pad(dechex($byte), 2, '0', STR_PAD_LEFT);
    }, $data));
}