Google Home Bether Working
This commit is contained in:
parent
8b39b727e1
commit
23aa83ec9a
@ -42,10 +42,22 @@ class ErrorHandler {
|
|||||||
'message' => $exception->getMessage(),
|
'message' => $exception->getMessage(),
|
||||||
];
|
];
|
||||||
echo json_encode($message);
|
echo json_encode($message);
|
||||||
|
|
||||||
|
$apiLogManager = new LogManager('../logs/'. date("Y-m-d").'.log');
|
||||||
|
$apiLogManager->write("[APACHE] ERROR\n" . json_encode($message, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
set_exception_handler("ErrorHandler::exception");
|
set_exception_handler("ErrorHandler::exception");
|
||||||
|
|
||||||
|
$json = file_get_contents('php://input');
|
||||||
|
$obj = json_decode($json, true);
|
||||||
|
|
||||||
|
$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
|
||||||
|
|
||||||
|
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||||
|
$apiLogManager->write("[API] POST body\n" . json_encode($_POST, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||||
|
$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||||
|
|
||||||
//Debug
|
//Debug
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
ini_set( 'display_errors','1');
|
ini_set( 'display_errors','1');
|
||||||
|
@ -22,7 +22,7 @@ $router->post('/api/devices', 'DevicesApi@getAllDevices');
|
|||||||
$router->post('/api/login', 'AuthApi@login');
|
$router->post('/api/login', 'AuthApi@login');
|
||||||
|
|
||||||
$router->get('/api/HA/auth', 'GoogleHomeApi@autorize');
|
$router->get('/api/HA/auth', 'GoogleHomeApi@autorize');
|
||||||
$router->post('/api/HA', 'GoogleHomeApi@response');
|
$router->any('/api/HA', 'GoogleHomeApi@response');
|
||||||
|
|
||||||
// examples
|
// examples
|
||||||
$router->any('/api/example', 'ExampleApi@example');
|
$router->any('/api/example', 'ExampleApi@example');
|
||||||
|
@ -1,32 +1,64 @@
|
|||||||
<?php
|
<?php
|
||||||
class GoogleHomeApi {
|
class GoogleHomeApi {
|
||||||
function response()
|
public static function response()
|
||||||
{
|
{
|
||||||
|
set_time_limit (5);
|
||||||
$json = file_get_contents('php://input');
|
$json = file_get_contents('php://input');
|
||||||
$obj = json_decode($json, true);
|
$obj = json_decode($json, true);
|
||||||
|
|
||||||
$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
|
|
||||||
|
|
||||||
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
|
||||||
$apiLogManager->write("[API] POST body\n" . json_encode($_POST, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
|
||||||
$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
switch ($obj['inputs'][0]['intent']) {
|
switch ($obj['inputs'][0]['intent']) {
|
||||||
case 'action.devices.SYNC':
|
case 'action.devices.SYNC':
|
||||||
self::sync($obj['requestId']);
|
self::sync($obj['requestId']);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'action.devices.QUERY':
|
case 'action.devices.QUERY':
|
||||||
|
self::query($obj['requestId'], $obj['inputs'][0]['payload']);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'action.devices.EXECUTE':
|
case 'action.devices.EXECUTE':
|
||||||
self::execute($obj['requestId'], $obj['inputs'][0]['payload']);
|
self::execute($obj['requestId'], $obj['inputs'][0]['payload']);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function query($requestId, $payload){
|
||||||
|
|
||||||
|
foreach ($payload['devices'] as $deviceId) {
|
||||||
|
$subDeviceData = SubDeviceManager::getSubDevice($deviceId['id']);
|
||||||
|
if ($subDeviceData['type'] != "on/off") continue;
|
||||||
|
|
||||||
|
$state = false;
|
||||||
|
if (RecordManager::getLastRecord($deviceId['id'])['value'] == 1){
|
||||||
|
$state = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$online = false;
|
||||||
|
if (RecordManager::getLastRecord($deviceId['id'])['execuded'] == 1){
|
||||||
|
$online = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$devices[] = [
|
||||||
|
$deviceId['id'] => [
|
||||||
|
'on' => $state,
|
||||||
|
'online' => $online,
|
||||||
|
'status'=> 'SUCCESS',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$response = [
|
||||||
|
'requestId' => $requestId,
|
||||||
|
'payload' => [
|
||||||
|
'devices' => $devices,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
|
||||||
|
$apiLogManager->write("[API] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||||
|
echo json_encode($response);
|
||||||
|
}
|
||||||
static function sync($requestId){
|
static function sync($requestId){
|
||||||
$devices = [];
|
$devices = [];
|
||||||
|
|
||||||
@ -53,49 +85,67 @@ class GoogleHomeApi {
|
|||||||
$response = [
|
$response = [
|
||||||
'requestId' => $requestId,
|
'requestId' => $requestId,
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'agentUserId'=>'simple-Home',
|
'agentUserId'=>'651351531531',
|
||||||
'devices' => $devices,],
|
'devices' => $devices,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
|
$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
|
||||||
|
|
||||||
$apiLogManager->write("[API] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
$apiLogManager->write("[API] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||||
echo json_encode($response);
|
echo json_encode($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function execute($subdeviceId, $payload){
|
static function execute($requestId, $payload){
|
||||||
$commands = [
|
$commands = [];
|
||||||
'ids' =>
|
|
||||||
];
|
|
||||||
foreach ($payload['commands'] as $key => $command) {
|
foreach ($payload['commands'] as $key => $command) {
|
||||||
foreach ($command['devices'] as $key => $device) {
|
foreach ($command['devices'] as $key => $device) {
|
||||||
$executionCommand = $command['execution'][$key];
|
$executionCommand = $command['execution'][0];
|
||||||
$subDeviceId = $device['id'];
|
if (isset($command['execution'][$key])) {
|
||||||
|
$executionCommand = $command['execution'][$key];
|
||||||
switch ($executionCommand) {
|
|
||||||
case 'action.devices.commands.OnOff':
|
|
||||||
if ($executionCommand['on'] == true){
|
|
||||||
//turn ddeivce on
|
|
||||||
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
# code...
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$subDeviceId = $device['id'];
|
||||||
|
|
||||||
|
switch ($executionCommand['command']) {
|
||||||
|
case 'action.devices.commands.OnOff':
|
||||||
|
$value = 0;
|
||||||
|
if ($executionCommand['params']['on']) $value = 1;
|
||||||
|
|
||||||
|
RecordManager::createWithSubId($subDeviceId, $value);
|
||||||
|
|
||||||
|
$timeout = 0;
|
||||||
|
while(RecordManager::getLastRecord($subDeviceId)['execuded'] == 0 && $timeout < 5 ){
|
||||||
|
sleep(1);
|
||||||
|
$timeout++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$commandTemp = [
|
||||||
|
'ids' => [$subDeviceId],
|
||||||
|
'status' => 'SUCCESS',
|
||||||
|
'states' => [
|
||||||
|
'on' => $executionCommand['params']['on'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($timeout >= 5){
|
||||||
|
$commandTemp['status'] = "ERROR";
|
||||||
|
}
|
||||||
|
$commands[] = $commandTemp;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = [
|
$response = [
|
||||||
'requestId' => $requestId,
|
'requestId' => $requestId,
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'commands' => $commands,],
|
'commands' => $commands,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
|
$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
|
||||||
|
|
||||||
$apiLogManager->write("[API] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
$apiLogManager->write("[API] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||||
|
|
||||||
echo json_encode($response);
|
echo json_encode($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,21 +161,21 @@ class GoogleHomeApi {
|
|||||||
//tel: zemanová 607979429
|
//tel: zemanová 607979429
|
||||||
|
|
||||||
/*echo json_encode(array (
|
/*echo json_encode(array (
|
||||||
'access_token' => '',
|
'access_token' => '',
|
||||||
'token_type' => 'bearer',
|
'token_type' => 'bearer',
|
||||||
'expires_in' => 3600,
|
'expires_in' => 3600,
|
||||||
'refresh_token' => '',
|
'refresh_token' => '',
|
||||||
'scope' => 'create',
|
'scope' => 'create',
|
||||||
));*/
|
));*/
|
||||||
|
|
||||||
$get = [
|
$get = [
|
||||||
"access_token"=>"23165133",
|
"access_token"=>"23165133",
|
||||||
"token_type"=>"Bearer",
|
"token_type"=>"Bearer",
|
||||||
"expires_in"=>600,
|
"expires_in"=>600,
|
||||||
"state"=>$_GET["state"],
|
"state"=>$_GET["state"],
|
||||||
];
|
];
|
||||||
|
|
||||||
echo $_GET["redirect_uri"] . '#' . http_build_query($get) ;
|
echo $_GET["redirect_uri"] . '#' . http_build_query($get) ;
|
||||||
echo '<a href="'.$_GET["redirect_uri"] . '#' . http_build_query($get) . '">FINISH</a>';
|
echo '<a href="'.$_GET["redirect_uri"] . '#' . http_build_query($get) . '">FINISH</a>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,9 +46,6 @@ class LogManager
|
|||||||
|
|
||||||
function write($value, $type = LogRecordType::ERROR){
|
function write($value, $type = LogRecordType::ERROR){
|
||||||
$record = "[".date("H:m:s")."][".$type."]" . $value . "\n";
|
$record = "[".date("H:m:s")."][".$type."]" . $value . "\n";
|
||||||
if (strlen($record) > 65 ) {
|
|
||||||
$record = Utilities::stringInsert($record,"\n",65);
|
|
||||||
}
|
|
||||||
fwrite($this->logFile, $record);
|
fwrite($this->logFile, $record);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,19 @@
|
|||||||
class RecordManager{
|
class RecordManager{
|
||||||
public static $records;
|
public static $records;
|
||||||
|
|
||||||
|
public static function createWithSubId ($subDeviceId, $value) {
|
||||||
|
$record = array (
|
||||||
|
'subdevice_id' => $subDeviceId,
|
||||||
|
'value' => $value,
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
return Db::add ('records', $record);
|
||||||
|
} catch(PDOException $error) {
|
||||||
|
echo $error->getMessage();
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function create ($deviceId, $type, $value) {
|
public static function create ($deviceId, $type, $value) {
|
||||||
$subDeviceId = Db::loadOne('SELECT * FROM subdevices WHERE device_id = ? AND type = ?;', array($deviceId, $type))['subdevice_id'];
|
$subDeviceId = Db::loadOne('SELECT * FROM subdevices WHERE device_id = ? AND type = ?;', array($deviceId, $type))['subdevice_id'];
|
||||||
if ($subDeviceId == '') {
|
if ($subDeviceId == '') {
|
||||||
@ -19,6 +32,7 @@ class RecordManager{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function setExecuted($recordId) {
|
public static function setExecuted($recordId) {
|
||||||
try {
|
try {
|
||||||
Db::edit ('records', ['execuded' => 1], 'WHERE record_id = ?', array($recordId));
|
Db::edit ('records', ['execuded' => 1], 'WHERE record_id = ?', array($recordId));
|
||||||
|
Loading…
Reference in New Issue
Block a user