When the source node's topic data format is raw data in a data parsing task, custom scripts can be utilized for parsing. The IoT Platform supports JavaScript (ECMAScript 5), Python 2.7, and PHP 7.2 scripts. This topic provides templates and examples for writing 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;
}
Avoid using global or static variables as they may lead to inconsistent results.
The script processes data using two's complement operation. Values in the range of [-128,127] are complemented to [0,255]. For instance, -1 is complemented to 255 (decimal).
The input parameter for the custom protocol parsing function, executeScript, is an integer array. Each element must undergo a bitwise AND operation with 0xFF to derive its complement. The function returns an associative array where the key should contain non-array characters (for instance, a key of "10" results in the PHP array interpreting it as the integer 10).
Strict exception handling is required in the PHP execution environment. Errors throw exceptions, halting subsequent code execution. To ensure robustness, catch and manage 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));
}