このトピックでは、Thing Specification Language (TSL) データを解析する JavaScript のスクリプトテンプレートとサンプルスクリプトを示します。

スクリプトテンプレート

次の JavaScript スクリプトテンプレートに基づいて、データ解析スクリプトを記述できます。

このテンプレートは、データ形式カスタムのプロダクトにのみ適用できます。
/**
 * Converts Alink data to a data format that can be recognized by devices before data is sent to devices from IoT Platform.
 * Input: A jsonObj object. It cannot be empty
 * Output: A rawData byte[] array. It cannot be empty
 *
 */
function protocolToRawData(jsonObj) {
    return rawdata;
}

/**
 * Converts custom data from devices to Alink protocol data. This function is called when devices report data to IoT Platform.
 * Input: A rawData byte [] array. It cannot be empty
 * Output: A jsonObj dictionary. It cannot be empty.
 */
function rawDataToProtocol(rawData) {
    return jsonObj;
}
			

追加の考慮事項

  • グローバル変数を使用しないでください。 グローバル変数を使用すると、実行結果に不整合が生じる可能性があります。
  • スクリプトでは、2 つの補数演算を使用してデータを処理します。 範囲 [-128,127] の値の補数は [0,255] です。 たとえば、-1 の補数は 10 進数の 255 です。
  • rawDataToProtocol 関数の入力は整数配列です。 ビット単位の AND 演算 には、0xFF を使用して補数を取得します。
  • protocolToRawData 関数は、IoT Platform から送信されたデータを解析し、結果を配列で返します。 配列要素は 0〜255 の整数です。

次のスクリプトは、「TSL データの解析例」で定義されているプロパティとプロトコルに基づいています。

var COMMAND_REPORT = 0x00; // Property data reported by devices
var COMMAND_SET = 0x01; // Property setting command data from the cloud
var COMMAND_REPORT_REPLY = 0x02;  // Response data for devices reporting properties
var COMMAND_SET_REPLY = 0x03;  // Response data for setting device properties
var COMMAD_UNKOWN = 0xff;  // Unknown commands
var ALINK_PROP_REPORT_METHOD = 'thing.event.property.post'; // The topic for devices to report current property data to the cloud
var ALINK_PROP_SET_METHOD = 'thing.service.property.set';  // The topic for IoT platform to send setting property commands to control devices
var ALINK_PROP_SET_REPLY_METHOD = 'thing.service.property.set';  // The topic for devices to report property setting results to IoT platform
/*
Sample data:
The device reports property data
Input parameters->
    0x000000000100320100000000
Output result->
    {"method":"thing.event.property.post","id":"1","params":{"prop_float":0,"prop_int16":50,"prop_bool":1},"version":"1.0"}

The device responds after setting properties
Input parameters->
    0x0300223344c8
Output result->
    {"code":"200","data":{},"id":"2241348","version":"1.0"}
*/
function rawDataToProtocol(bytes) {
    var uint8Array = new Uint8Array(bytes.length);
    for (var i = 0; i < bytes.length; i++) {
        uint8Array[i] = bytes[i] & 0xff;
    }
    var dataView = new DataView(uint8Array.buffer, 0);
    var jsonMap = new Object();
    var fHead = uint8Array[0]; // The command prefix
    if (fHead == COMMAND_REPORT) {
        jsonMap['method'] = ALINK_PROP_REPORT_METHOD; // The topic for reporting properties in the ALink JSON
        jsonMap['version'] = '1.0'; // The fixed protocol version in the ALink JSON
        jsonMap['id'] = ''+ dataView.getInt32(1); // The request ID in the ALink JSON
        var params = {};
        params['prop_int16'] = dataView.getInt16(5); // The value of the prop_int16 property
        params['prop_bool'] = uint8Array[7]; // The value of the prop_bool property
        params['prop_float'] = dataView.getFloat32(8); // The value of the prop_float property
        jsonMap['params'] = params; // The params field in the ALink JSON
    } else if(fHead == COMMAND_SET_REPLY) {
        jsonMap['version'] = '1.0'; // The fixed protocol version in the ALink JSON
        jsonMap['id'] = ''+ dataView.getInt32(1); // The request ID in the ALink JSON
        jsonMap['code'] = ''+ dataView.getUint8(5);
        jsonMap['data'] = {};
    }

    return jsonMap;
}
/*
Sample data:
IoT Platform pushes a command for setting properties to the device
Input parameters->
    {"method":"thing.service.property.set","id":"12345","version":"1.0","params":{"prop_float":123.452, "prop_int16":333, "prop_bool":1}}
Output result->
    0x0100003039014d0142f6e76d
IoT Platform responds after the device reports properties
Input data->
    {"method":"thing.event.property.post","id":"12345","version":"1.0","code":200,"data":{}}
Output result->
    0x0200003039c8
*/
function protocolToRawData(json) {
    var method = json['method'];
    var id = json['id'];
    var version = json['version'];
    var payloadArray = [];
    if (method == ALINK_PROP_SET_METHOD) // Sets properties
    {
        var params = json['params'];
        var prop_float = params['prop_float'];
        var prop_int16 = params['prop_int16'];
        var prop_bool = params['prop_bool'];
        // Raw data is concatenated based on the custom protocol format.
        payloadArray = payloadArray.concat(buffer_uint8(COMMAND_SET)); // The command field
        payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // The id in the ALink JSON
        payloadArray = payloadArray.concat(buffer_int16(prop_int16)); // The value of the prop_int16 property
        payloadArray = payloadArray.concat(buffer_uint8(prop_bool)); // The value of the prop_bool property
        payloadArray = payloadArray.concat(buffer_float32(prop_float)); // The value of the prop_float property
    } else if (method == ALINK_PROP_REPORT_METHOD) {// The response of the reported data
        var code = json['code'];
        payloadArray = payloadArray.concat(buffer_uint8(COMMAND_SET)); // The command field
        payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // The id in the ALink JSON
        payloadArray = payloadArray.concat(buffer_uint8(code));
    } else {// Unknown commands. Some commands are not processed.
        var code = json['code'];
        payloadArray = payloadArray.concat(buffer_uint8(COMMAND_UNKOWN)); // The command field
        payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // The id in the ALink JSON
        payloadArray = payloadArray.concat(buffer_uint8(code));
    }
    return payloadArray;
}
// The following are some helper functions.
function buffer_uint8(value) {
    var uint8Array = new Uint8Array(1);
    var dv = new DataView(uint8Array.buffer, 0);
    dv.setUint8(0, value);
    return [].slice.call(uint8Array);
}
function buffer_int16(value) {
    var uint8Array = new Uint8Array(2);
    var dv = new DataView(uint8Array.buffer, 0);
    dv.setInt16(0, value);
    return [].slice.call(uint8Array);
}
function buffer_int32(value) {
    var uint8Array = new Uint8Array(4);
    var dv = new DataView(uint8Array.buffer, 0);
    dv.setInt32(0, value);
    return [].slice.call(uint8Array);
}
function buffer_float32(value) {
    var uint8Array = new Uint8Array(4);
    var dv = new DataView(uint8Array.buffer, 0);
    dv.setFloat32(0, value);
    return [].slice.call(uint8Array);
}