<?php
require_once __DIR__.'/DiscoveryResponse.php';
require_once __DIR__.'/ControlResponse.php';
require_once __DIR__.'/QueryResponse.php';
$sqlFileName = "../sqldata.txt";
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 DuerOS\DiscoveryResponse(true);
$ret->header->putResponseMessageId($messageId);
$retJson = json_encode($ret);
return $retJson;
}
$str = fread($txtRes, filesize($sqlFileName));
error_log("str-->".$str);
$obj = json_decode($str);
$ret = new DuerOS\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 DuerOS\DiscoveryAppliances();
$dev->putResponseApplianceTypes("LIGHT");
$dev->putResponseDeviceInfo($devElement->entity_id, "展现给用户的详细介绍", "吸顶灯",
"RT-home", "RT-CL0.1", "du0.001.00");
$dev->putResponseIsReachable(true);
$dev->actions = array("turnOn",
"turnOff",
"setBrightnessPercentage",
"setColor",
"incrementBrightnessPercentage",
"decrementBrightnessPercentage");
$dev->putResponseApplianceDetails(NULL);
$att = new DuerOS\DiscoveryAttributes("name", "吸顶灯", "", 1496741861, 0);
$dev->putResponseAttributes($att);
$att = new DuerOS\DiscoveryAttributes("connectivity", "REACHABLE", "", 1496741861, 0);
$dev->putResponseAttributes($att);
$attcv = new DuerOS\DiscoveryAttributesColorValue(350.5, 0.7138, 0.6524);
$att = new DuerOS\DiscoveryAttributes("color", $attcv, "", 1496741861, 10);
$dev->putResponseAttributes($att);
$brightnessWhite = '';
$brightnessYellow = '';
$powerstate = '';
foreach ($devElement->properties as $propertie) {
if ($propertie->name =='brightness_w') {
$brightnessWhite = $propertie->value;
} elseif ($propertie->name =='brightness_y') {
$brightnessYellow = $propertie->value;
} elseif ($propertie->name =='powerstate') {
$powerstate = strtoupper($propertie->value);
}
}
$att = new DuerOS\DiscoveryAttributes("powerState", $powerstate, "", 1496741861, 0);
$dev->putResponseAttributes($att);
$brightness = ($brightnessWhite>$brightnessYellow)?$brightnessWhite:$brightnessYellow;
$att = new DuerOS\DiscoveryAttributes("brightness", $brightness, "", 1496741861, 100);
$dev->putResponseAttributes($att);
$ret->payload->putResponseAppliances($dev);
break;
}
}
}
$retJson = json_encode($ret);
error_log('retJson-->'.$retJson);
error_log('----device_discovery out----');
return $retJson;
}
function device_control($obj, $messageId)
{
error_log('----device_control in----');
global $sqlFileName;
$applianceId = $obj->payload->appliance->applianceId;
$action = $obj->header->name;
$deviceType = '';//Customize device type
$deviceType = device_id_to_type($applianceId);
error_log("deviceType-->".$deviceType);
error_log("action-->".$action);
if ($action == "" || $deviceType == "") {
error_log('----get_control_action failed----');
$ret = new DuerOS\ControlResponse(true);
$ret->header->putResponseMessageId($messageId);
$ret->payload->putResponseError("SERVICE_ERROR",
"action or device not support,name:".$obj->header->name." device:".substr($applianceId,0,stripos($applianceId,".")));
$retJson = json_encode($ret);
return $retJson;
}
$validArray = array (
"deviceId" => $applianceId,
"deviceType" => $deviceType,
"actionName" => $obj->header->name,
"action" => $action
);
//-----------------------------------
if (($txtRes = fopen($sqlFileName, "r+")) === false) {
error_log('----get_control fopen failed----');
$ret = new DuerOS\ControlResponse(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);
$sqlObj = json_decode($str);
foreach ($sqlObj->dev_array as $devElement) {
error_log('entity_id-->'.$devElement->entity_id);
if ($devElement->entity_id == $validArray['deviceId']) {
switch ($devElement->device_type) {
case 'ceiling_lamp': {
switch ($validArray['action']) {
case 'TurnOnRequest': {
$ret = new DuerOS\ControlResponse();
$ret->header->putResponseMessageId($messageId);
$ret->header->putResponseName("TurnOnConfirmation");
foreach ($devElement->properties as $propertie) {
error_log('name-->'.$propertie->name);
if ($propertie->name == "powerstate") {
$propertie->value = "on";
break;
}
}
break;
}
case 'TurnOffRequest': {
$ret = new DuerOS\ControlResponse();
$ret->header->putResponseMessageId($messageId);
$ret->header->putResponseName("TurnOffConfirmation");
foreach ($devElement->properties as $propertie) {
error_log('name-->'.$propertie->name);
if ($propertie->name == "powerstate") {
$propertie->value = "off";
break;
}
}
break;
}
case 'SetBrightnessPercentageRequest': {
$preValue = 0;
foreach ($devElement->properties as $propertie) {
error_log('name-->'.$propertie->name);
if ($propertie->name == "brightness_w" && $propertie->value != 0) {
$preValue = ($propertie->value > $preValue)?$propertie->value:$preValue;
$propertie->value = $obj->payload->brightness->value;
} else if ($propertie->name == "brightness_y" && $propertie->value != 0) {
$preValue = ($propertie->value > $preValue)?$propertie->value:$preValue;
$propertie->value = $obj->payload->brightness->value;
}
}
$ret = new DuerOS\ControlResponse(SetBrightnessPercentageRequest, $preValue, $obj->payload->brightness->value);
没有合适的资源?快使用搜索试试~ 我知道了~
dueros.zip
共11个文件
php:11个
4星 · 超过85%的资源 需积分: 9 9 下载量 180 浏览量
2019-07-23
21:54:04
上传
评论
收藏 12KB ZIP 举报
温馨提示
在与小度智能音箱对接时需要使用的php代码,这套代码是完全可以运行的,稍加改动就可以使用,但是距离实际应用还差很多,这个代码只是初期学习阶段使用。
资源推荐
资源详情
资源评论
收起资源包目录
dueros.zip (11个子文件)
dueros
dueros_handle.php 18KB
dueros_gate.php 985B
dueros_token.php 594B
dueros_authorize.php 2KB
DiscoveryResponse.php 8KB
dueros_server.php 1KB
color
RGB.php 4KB
HSLTest.php 2KB
HSV.php 3KB
HSL.php 3KB
ControlResponse.php 6KB
共 11 条
- 1
资源评论
- liyongbing19852024-03-14少了几个文件跑不起来呢
andylauren
- 粉丝: 788
- 资源: 49
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功