Bu konuda, özel konulara gönderilen verileri ayrıştırmak için kullanabileceğiniz bir JavaScript betik şablonu ve örnek bir JavaScript betiği sunulmaktadır.
Betik şablonu
var SELF_DEFINE_TOPIC_UPDATE_FLAG = '/user/update' // Define the following custom topic: /user/update.
var SELF_DEFINE_TOPIC_ERROR_FLAG = '/user/update/error' // Define the following custom topic: /user/update/error.
/**
* Convert data that is sent by a device to a custom topic to JSON data when the device submits the data to IoT Platform.
* topic: an input parameter that specifies the topic to which the device sends messages. The value is a string.
* rawData: the input parameter. The value is a byte array that cannot be empty.
* jsonObj: the output parameter. The value is a JSON object that cannot be empty.
*/
function transformPayload(topic, rawData) {
var jsonObj = {};
return jsonObj;
}
Örnek betik
Not
Aşağıdaki örnek betik yalnızca özel konulara gönderilen mesajları ayrıştırır. Ürünün
Data Type
parametresini
Custom
olarak ayarlarsanız Thing Specification Language (TSL) mesajlarını ayrıştırmak için de bir betik yazmanız gerekir. Daha fazla bilgi için bkz.
TSL model mesajı ayrıştırma betiği gönderme
.
custom data formats hakkında bilgi için bkz. Ürün oluşturma.
var SELF_DEFINE_TOPIC_UPDATE_FLAG = '/user/update' // Define the following custom topic: /user/update.
var SELF_DEFINE_TOPIC_ERROR_FLAG = '/user/update/error' // Define the following custom topic: /user/update/error.
/*
Sample data:
Use the following custom topic to submit data:
/user/update.
Input:
topic: /${productKey}/${deviceName}/user/update
bytes: 0x000000000100320100000000
Output:
{
"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 (var 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;
}