透傳/自定義產(chǎn)品的數(shù)據(jù)解析腳本需包含自定義Topic數(shù)據(jù)解析腳本和物模型數(shù)據(jù)解析腳本。本文提供完整的PHP語言示例腳本。

<?php
/*
示例數(shù)據(jù):
設(shè)備上報數(shù)據(jù):
傳入?yún)?shù):
    0x0000000001003201
輸出結(jié)果:
    {"method":"thing.event.property.post","id":"1","params":{"prop_int16":50,"prop_bool":1},"version":"1.0"}

屬性設(shè)置的返回結(jié)果:
傳入?yún)?shù):
    0x0300223344c8
輸出結(jié)果:
    {"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]; //command字段。
    if ($fHead == 0x00) {
        $jsonMap['method'] = 'thing.event.property.post'; //ALink JSON格式,屬性上報topic。
        $jsonMap['version'] = '1.0'; //ALink JSON格式,協(xié)議版本號固定字段。
        $jsonMap['id'] = '' . getInt32($data, 1); //ALink JSON格式,標(biāo)示該次請求id值。
        $params = [];
        $params['prop_int16'] = getInt16($data, 5); //對應(yīng)產(chǎn)品屬性中prop_int16。
        $params['prop_bool'] = $data[7]; //對應(yīng)產(chǎn)品屬性中prop_bool。
        $jsonMap['params'] = $params; //ALink JSON格式,params標(biāo)準(zhǔn)字段。
    } else if ($fHead == 0x03) {
        $jsonMap['version'] = '1.0'; //ALink JSON格式,協(xié)議版本號固定字段。
        $jsonMap['id'] = '' . getInt32($data, 1); //ALink JSON格式,標(biāo)示該次請求id值。
        $jsonMap['code'] = getInt8($data, 5);
    }

    return $jsonMap;
}

/*
示例數(shù)據(jù):
屬性設(shè)置:
傳入?yún)?shù):
    {"method":"thing.service.property.set","id":"12345","version":"1.0","params":{"prop_int16":333, "prop_bool":1}}
輸出結(jié)果:
    0x013039014d01

設(shè)備上報的返回結(jié)果:
傳入數(shù)據(jù):
    {"method":"thing.event.property.post","id":"12345","version":"1.0","code":200,"data":{}}
輸出結(jié)果:
    0x023039c8
*/
function protocolToRawData($json)
{
    $method = $json['method'];
    $id = $json['id'];
    $version = $json['version'];
    $payloadArray = [];
    if ($method == 'thing.service.property.set') //屬性設(shè)置。
    {
        $params = $json['params'];
        $prop_int16 = $params['prop_int16'];
        $prop_bool = $params['prop_bool'];
        //按照自定義協(xié)議格式拼接rawData。
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex(0x01))); //command字段。
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex(intval($id)))); //ALink JSON格式'id'。
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex($prop_int16))); //屬性'prop_int16'的值。
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex($prop_bool))); //屬性'prop_bool'的值。
    } else if ($method == 'thing.event.property.post') { //設(shè)備上報數(shù)據(jù)返回結(jié)果。
        $code = $json['code'];
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex(0x02))); //command字段。
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex(intval($id)))); //ALink JSON格式'id'。
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex($code)));
    } else { //未知命令,對于這些命令不做處理。
        $code = $json['code'];
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex(0xff))); //command字段。
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex(intval($id)))); //ALink JSON格式'id'。
        $payloadArray = concat($payloadArray, hexStringToByteArray(toHex($code)));
    }
    return $payloadArray;
}

/*
  示例數(shù)據(jù):
  自定義Topic:
     /user/update,上報數(shù)據(jù)。
  輸入?yún)?shù):
     topic: /{productKey}/{deviceName}/user/update
     bytes: 0x000000000100320100000000
  輸出參數(shù):
  {
     "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;
}