Thing Specification Language (TSL) mesajlarını ayrıştırmak için bir PHP betik şablonu ve örnek betik sağlar.
Betik şablonu
TSL mesajlarını ayrıştıran bir PHP betiği yazmak için aşağıdaki şablonu kullanın:
Bu betik şablonu yalnızca
Data Type
parametresi
Custom
olarak ayarlanmış ürünler için geçerlidir.
<?php
/**
* Convert Alink JSON data to data that can be recognized by a device when IoT Platform sends downstream data.
* $jsonObj: the input parameter. The value is a JSON object that indicates an association array.
* $rawData: the output parameter. The value is an array of integers within a value range of [0,255]. Array elements cannot be empty.
*/
function protocolToRawData($jsonObj)
{
$rawData = array();
return $rawData;
}
/**
* Convert custom data of a device to Alink JSON data when the device submits data to IoT Platform.
* $rawData: the input parameter. The value is an array of integers.
* $jsonObj: the output parameter. The value is a JSON object that indicates an association array. The value of each key must be a string. However, the string cannot consist of only digits such as 10. Key values cannot be empty.
*/
function rawDataToProtocol($rawData)
{
$jsonObj = array();
return $jsonObj;
}
/**
* Convert the data that is sent by a device to a custom topic to JSON data when the device submits data to IoT Platform.
* $topic: the input parameter that specifies the topic to which the device sends messages. The value is a string.
* $rawData: the input parameter. The value is an array of integers.
* $jsonObj: the output parameter. The value is a JSON object that indicates an association array. The value of each key must be a string. However, the string cannot consist of only digits such as 10. Key values cannot be empty.
*/
function transformPayload($topic, $rawData)
{
$jsonObj = array();
return $jsonObj;
}
Betik yazımına ilişkin notlar
Genel değişkenler veya statik değişkenler kullanmayın. Aksi takdirde sonuçlar tutarsız olabilir.
Betik, verileri iki tümleyen işlemi kullanarak işler. [-128,127] aralığındaki değerler için tümleyen aralığı [0,255]'tir. Örneğin, -1'in ondalık tabandaki tümleyeni 255'tir.
rawDataToProtocol işlevi, cihazlar tarafından gönderilen mesajları ayrıştırır. İşlevin giriş parametresi bir tamsayı dizisidir. Tümleyeni elde etmek için bitsel VE işlemi olarak
0xFFkullanın. Dönen sonuç bir ilişkilendirilmiş dizidir. Her bir anahtar değeri dizi olmayan karakterler içermelidir. Örneğin, bir anahtar değeri 10 ise PHP dizisi 10 tamsayısını alır.protocolToRawData işlevi, IoT Platform tarafından gönderilen aşağı yönlü mesajları ayrıştırır ve bir dizi döndürür. Dizi, standart bir PHP dizisi olmalıdır. Dizideki her öğe [0,255] aralığında bir tamsayı olmalıdır.
transformPayload işlevi, özel konulara gönderilen verileri ayrıştırır. İşlevin giriş parametresi bir tamsayı dizisidir. Tümleyeni elde etmek için bitsel VE işlemi olarak
0xFFkullanın. Dönen sonuç bir ilişkilendirilmiş dizidir. Her bir anahtar değeri dizi olmayan karakterler içermelidir. Örneğin, bir anahtar değeri 10 ise PHP dizisi 10 tamsayısını alır.PHP çalışma zamanı ortamı, istisna işlemeye karşı katıdır. Bir hata oluşursa sonraki mantık çalıştırılmaz. Kodun dayanıklılığını sağlamak için istisnaları yakalayın ve işleyin.
Örnek betik
Aşağıdaki örnek betik, TSL model mesajı ayrıştırma betiği gönderme konusunda tanımlanan özelliklere ve protokole göre yazılmıştır:
TSL modelleri tarafından desteklenen veri türleri hakkında daha fazla bilgi için bkz. Desteklenen veri türleri. Bir cihaz TSL özelliği verilerini veya olay verilerini gönderdikten sonra IoT Platform, verileri Alink JSON biçiminde olan bir yanıt oluşturur. IoT Platform bu yanıtı cihaza göndermeden önce verilerin biçimini cihazın tanıyabileceği bir biçime dönüştürmek için betiği kullanır. Alink JSON biçimi hakkında daha fazla bilgi için bkz. Cihaz özellikleri, olayları ve hizmetleri.
<?php
/*
Sample data:
Device data submission
Input:
0x0000000001003201
Output:
{"method":"thing.event.property.post","id":"1","params":{"prop_int16":50,"prop_bool":1},"version":"1.0"}
Result of property setting
Input:
0x0300223344c8
Output:
{"code":"200","id":"2241348","version":"1.0"}
*/
function rawDataToProtocol($bytes)
{
$data = [];
$length = count($bytes);
for ($i = 0; $i < $length; $i++) {
$data[$i] = $bytes[$i] & 0xff;
}
$jsonMap = [];
$fHead = $data[0]; // The command field.
if ($fHead == 0x00) {
$jsonMap['method'] = 'thing.event.property.post '; // The topic that is used to submit properties. Data format: Alink JSON.
$jsonMap['version'] = '1.0'; // The version of the protocol. This value is fixed. Data format: Alink JSON.
$jsonMap['id'] = '' . getInt32($data, 1); // The ID of the request. Data format: Alink JSON.
$params = [];
$params['prop_int16'] = getInt16($data, 5); // The value of the prop_int16 property of the product.
$params['prop_bool'] = $data[7]; // The value of the prop_bool property of the product.
$jsonMap['params'] = params; // The params field. Data format: Alink JSON.
} else if ($fHead == 0x03) {
$jsonMap['version'] = '1.0'; // The version of the protocol. This value is fixed. Data format: Alink JSON.
$jsonMap['id'] = '' . getInt32($data, 1); // The ID of the request. Data format: Alink JSON.
$jsonMap['code'] = getInt8($data, 5);
}
return $jsonMap;
}
/*
Sample data:
Property setting
Input:
{"method":"thing.service.property.set","id":"12345","version":"1.0","params":{"prop_int16":333, "prop_bool":1}}
Output:
0x013039014d01
Result of device data submission:
Input:
{"method":"thing.event.property.post","id":"12345","version":"1.0","code":200,"data":{}}
Output:
0x023039c8
*/
function protocolToRawData($json)
{
$method = $json['method'];
$id = $json['id'];
$version = $json['version'];
$payloadArray = [];
if ($method == 'thing.service.property.set ') // Configure properties.
{
$params = $json['params'];
$prop_int16 = $params['prop_int16'];
$prop_bool = $params['prop_bool'];
// Concatenate raw data based on the custom protocol format.
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex(0x01))); // The command field.
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex(intval($id)))); // The ID of the request. Data format: Alink JSON.
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex($prop_int16))); // The value of the prop_int16 property.
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex($prop_bool))); // The value of the prop_bool property.
} else if ($method == 'thing.event.property.post ') { // The response.
$code = $json['code'];
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex(0x02))); // The command field.
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex(intval($id)))); // The ID of the request. Data format: Alink JSON.
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex($code)));
} else { // Unknown commands. The commands are not run.
$code = $json['code'];
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex(0xff))); // The command field.
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex(intval($id)))); // The ID of the request. Data format: Alink JSON.
$payloadArray = concat($payloadArray, hexStringToByteArray(toHex($code)));
}
return $payloadArray;
}
/*
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)
{
$data = array();
$length = count($bytes);
for ($i = 0; $i < $length; $i++) {
$data[$i] = $bytes[$i] & 0xff;
}
$jsonMap = array();
if (strpos($topic, '/user/update/error') !== false) {
$jsonMap['topic'] = $topic;
$jsonMap['errorCode'] = getInt8($data, 0);
} else if (strpos($topic, '/user/update') !== false) {
$jsonMap['topic'] = $topic;
$jsonMap['prop_int16'] = getInt16($data, 5);
$jsonMap['prop_bool'] = $data[7];
}
return $jsonMap;
}
function getInt32($bytes, $index)
{
$array = array($bytes[$index], $bytes[$index + 1], $bytes[$index + 2], $bytes[$index + 3]);
return hexdec(byteArrayToHexString($array));
}
function getInt16($bytes, $index)
{
$array = array($bytes[$index], $bytes[$index + 1]);
return hexdec(byteArrayToHexString($array));
}
function getInt8($bytes, $index)
{
$array = array($bytes[$index]);
return hexdec(byteArrayToHexString($array));
}
function byteArrayToHexString($data)
{
$hexStr = '';
for ($i = 0; $i < count($data); $i++) {
$hexValue = dechex($data[$i]);
$tempHexStr = strval($hexValue);
if (strlen($tempHexStr) === 1) {
$hexStr = $hexStr . '0' . $tempHexStr;
} else {
$hexStr = $hexStr . $tempHexStr;
}
}
return $hexStr;
}
function hexStringToByteArray($hex)
{
$result = array();
$index = 0;
for ($i = 0; $i < strlen($hex) - 1; $i += 2) {
$result[$index++] = hexdec($hex[$i] . $hex[$i + 1]);
}
return $result;
}
function concat($array, $data)
{
return array_merge($array, $data);
}
function toHex($data)
{
$var = dechex($data);
$length = strlen($var);
if ($length % 2 == 1) {
$var = '0' . $var;
}
return $var;
}