(9)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接4

导航链接

(1)香橙派+apache2与php+天猫精灵=自建平台语音支持--前言

(2)香橙派+apache2与php+天猫精灵=自建平台语音支持--香橙派操作系统安装

(3)香橙派+apache2与php+天猫精灵=自建平台语音支持--香橙派环境配置

(4)香橙派+apache2与php+天猫精灵=自建平台语音支持--apache2与php

(5)香橙派+apache2与php+天猫精灵=自建平台语音支持--MariaDB的安装

(6)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接1

(7)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接2

(8)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接3

(9)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接4

(10)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接5

(11)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接6

扫描二维码关注公众号,回复: 4824698 查看本文章

(12)香橙派+apache2与php+天猫精灵=自建平台语音支持--天猫精灵对接7

开发者网关地址

我们在天猫精灵的一项设置里需要填写开发者网管地址,那个是天猫精灵在得到第三方硬件平台认证后,post协议的地址,由此我们就根据POST的协议,拿到控制信息以及返回响应信息。我们在/var/www/html/aligenie中建一个aligenies_gate.php文件,用于解析POST过来的协议,先放一个天猫精灵的协议地址,http://doc-bot.tmall.com/docs/doc.htm?spm=0.0.0.0.MMVVlI&treeId=393&articleId=107454&docType=1

下面我们用一个时序图表示一下我们的文件调用关系。

现在我们先来逐条讲解gate中的代码。

<?php
error_log('----aligenies_gate.php in----');
require_once __DIR__.'/aligenies_handle.php';

$chars = md5(uniqid(mt_rand(), true));
$uuid  = substr($chars,0,8) . '-';
$uuid .= substr($chars,8,4) . '-';
$uuid .= substr($chars,12,4) . '-';
$uuid .= substr($chars,16,4) . '-';
$uuid .= substr($chars,20,12);

在文件的开头,每次获得执行这段文本前就会生成一个独一无二的uuid,这个就是协议文本中的

用于天猫平台跟踪返回指令用的,在最后也会告诉你这个,然后出错通过提供messageID进行天猫精灵服务端的判别

$poststr = file_get_contents("php://input");
$obj = json_decode($poststr);
$messageId = $uuid;

图中行1是用来获取POST过来的数据,这个是PHP获取http POST获取到的数据的一种方式,当然还有其他方式,大概有3种吧,感兴趣的可以去查一下。图的行2是把获得到的数据进行json解码放到obj中,json是http传送的一种格式。行3是获取本次请求的messageID值,会回传到天猫服务器的。

设备发现

下面我们来讲解设备发现的过程,先放上一个时序图。

switch ($obj->header->namespace) {
    case 'AliGenie.Iot.Device.Discovery': {
        error_log('----case in Discovery----');
        $result = device_discovery($messageId);
        break;
    }

这一段代码用来回复天猫服务器我们的设备都有哪些,我们可以参考一下天猫精灵的协议说明,这一段代码是天猫服务器POST后要响应的数据。

所有表格中要求返回的数据都必须返回,有一个不对,都会导致天猫精灵控制的时候找不到对应的设备。所以一定要好好填。然后我们进入device_discovery函数看一下,该函数在aligenies_handle.php文件中。

function device_discovery($messageId)
{
    error_log('----device_discovery in----');
    global $sqlFileName;

    if (($txtRes = fopen($sqlFileName, "r")) === false) {
        error_log('----gate_implement fopen failed----');
        $ret = new AliGenie\DiscoveryResponse(true);
        $ret->header->putResponseMessageId($messageId);
        $ret->payload->putResponseError("SERVICE_ERROR", "CANNOT_OPEN");
        $retJson = json_encode($ret);
        return $retJson;
    }
    $str = fread($txtRes, filesize($sqlFileName));
    error_log("str-->".$str);
    $obj = json_decode($str);

    $ret = new AliGenie\DiscoveryResponse();
    $ret->header->putResponseMessageId($messageId);

    foreach ($obj->dev_array as $devElement) {
        error_log('entity_id-->'.$devElement->entity_id);
        
        switch ($devElement->device_type) {
            case 'ceiling_lamp': {
                $dev = new AliGenie\DiscoveryDevice();
                $dev->putResponseDeviceInfo($devElement->entity_id, "吸顶灯", device_id_to_devicetype($devElement->entity_id), 
                        "https://www.rtplay.cn/icon/td.png");
                $dev->actions = array("TurnOn",
                                      "TurnOff",
                                      "SetBrightness",
                                      "SetColor",
                                      "AdjustUpBrightness",
                                      "AdjustDownBrightness",
                                      "QueryBrightness",
                                      "QueryPowerState",
                                      "QueryColor",
                                      "Query");
                $brightnessWhite = '';
                $brightnessYellow = '';
                foreach ($devElement->properties as $propertie) {
                    if ($propertie->name =='brightness_w') {
                        $brightnessWhite = $propertie->value;
                    } elseif ($propertie->name =='brightness_y') {
                        $brightnessYellow = $propertie->value;
                    } else {
                        $pro = new AliGenie\DiscoveryPropertie($propertie->name, $propertie->value);
                        $dev->putResponseProperties($pro);
                    }
                }
                $brightness = ($brightnessWhite>$brightnessYellow)?$brightnessWhite:$brightnessYellow;
                $pro = new AliGenie\DiscoveryPropertie("brightness", $brightness);
                $dev->putResponseProperties($pro);
                if ($brightnessWhite == 100 && $brightnessYellow == 0) {
                    $pro = new AliGenie\DiscoveryPropertie("color", "White");
                    $dev->putResponseProperties($pro);
                } elseif ($brightnessWhite == 0 && $brightnessYellow == 100) {
                    $pro = new AliGenie\DiscoveryPropertie("color", "Yellow");
                    $dev->putResponseProperties($pro);
                } elseif ($brightnessWhite == $brightnessYellow && $brightnessYellow != 0) {
                    $pro = new AliGenie\DiscoveryPropertie("mode", "自然模式");
                    $dev->putResponseProperties($pro);
                }
                $ext = new AliGenie\DiscoveryExtension();
                $dev->putResponseExtensions($ext);
                $ret->payload->putResponseDevices($dev);
                break;
            }
            case 'gateway_switch': {
                $dev = new AliGenie\DiscoveryDevice();
                $dev->putResponseDeviceInfo($devElement->entity_id, "插排", device_id_to_devicetype($devElement->entity_id), 
                        "https://www.rtplay.cn/icon/cz.png");
                $dev->actions = array("TurnOn",
                                      "TurnOff",
                                      "QueryPowerState",
                                      "Query");
                foreach ($devElement->properties as $propertie) {
                    $pro = new AliGenie\DiscoveryPropertie($propertie->name, $propertie->value);
                    $dev->putResponseProperties($pro);
                }
                $ext = new AliGenie\DiscoveryExtension();
                $dev->putResponseExtensions($ext);
                $ret->payload->putResponseDevices($dev);
                break;
            }
        }
    }

    $retJson = json_encode($ret);
    error_log('retJson-->'.$retJson);

    error_log('----device_discovery out----');
    return $retJson;
}

这里面主要做的就是读取sqldata文件,并将其中的json数据类型转换为天猫精灵需要的DiscoveryResponse回复类型。

猜你喜欢

转载自blog.csdn.net/andylauren/article/details/84933089
今日推荐