Google Home api Fix

This commit is contained in:
GamerClassN7 2021-01-24 19:56:53 +01:00
parent c17e3090a8
commit 1f49391b36
1 changed files with 396 additions and 378 deletions

View File

@ -1,6 +1,8 @@
<?php <?php
class GoogleHome { class GoogleHome
static function sync($requestId){ {
static function sync($requestId)
{
$devices = []; $devices = [];
$roomsData = RoomManager::getAllRooms(); $roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomData) { foreach ($roomsData as $roomKey => $roomData) {
@ -64,19 +66,21 @@ class GoogleHome {
echo json_encode($response); echo json_encode($response);
} }
static function query($requestId, $payload){ static function query($requestId, $payload)
{
$devices = []; $devices = [];
$num = 0; $num = 0;
foreach ($payload['devices'] as $deviceId) { foreach ($payload['devices'] as $deviceId) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id']);
$tempDevice[$deviceId['id']] = [ $tempDevice[$deviceId['id']] = [
'online' => false, 'online' => false,
'status' => 'OFFLINE', 'status' => 'OFFLINE',
]; ];
if ($subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id'])) {
foreach ($subDevicesData as $key => $subDeviceData) { foreach ($subDevicesData as $key => $subDeviceData) {
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']); $lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
if ($lastRecord['execuded'] == 1){ if ($lastRecord != false && $lastRecord['execuded'] == 1) {
$tempDevice[$deviceId['id']]['online'] = true; $tempDevice[$deviceId['id']]['online'] = true;
$tempDevice[$deviceId['id']]['status'] = "SUCCESS"; $tempDevice[$deviceId['id']]['status'] = "SUCCESS";
} else { } else {
@ -121,11 +125,14 @@ class GoogleHome {
break; break;
} }
} }
}
if ($lastRecord['execuded'] == 1){ // $lastRecord = RecordManager::getLastRecord($deviceId['id']);
$online = true; // //var_dump($lastRecord);
$status = 'SUCCESS'; // if ($lastRecord['execuded'] == 1) {
} else { // $online = true;
// $status = 'SUCCESS';
// } else {
$executed = 0; $executed = 0;
$waiting = 0; $waiting = 0;
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) { foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) {
@ -135,15 +142,16 @@ class GoogleHome {
$waiting++; $waiting++;
} }
} }
if ($waiting < $executed) { if ($waiting < $executed) {
$status = "PENDING"; $status = "PENDING";
$online = true; $online = true;
} }
} // }
$devices = $tempDevice; $devices = $tempDevice;
/*if (count($devices)> 1){ if (count($devices)> 1){
$devices[] = $tempDevice; $devices[] = $tempDevice;
}*/ }
} }
$response = [ $response = [
'requestId' => $requestId, 'requestId' => $requestId,
@ -157,7 +165,8 @@ class GoogleHome {
echo json_encode($response); echo json_encode($response);
} }
static function execute($requestId, $payload){ static function execute($requestId, $payload)
{
$commands = []; $commands = [];
foreach ($payload['commands'] as $key => $command) { foreach ($payload['commands'] as $key => $command) {
foreach ($command['devices'] as $key2 => $device) { foreach ($command['devices'] as $key2 => $device) {
@ -167,7 +176,8 @@ static function execute($requestId, $payload){
} }
$deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']); $deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']);
$subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)['subdevice_id']; if ($subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)) {
$subDeviceId = $subDeviceId['subdevice_id'];
switch ($executionCommand['command']) { switch ($executionCommand['command']) {
case 'action.devices.commands.OnOff': case 'action.devices.commands.OnOff':
$commands[] = self::executeSwitch($subDeviceId, $executionCommand); $commands[] = self::executeSwitch($subDeviceId, $executionCommand);
@ -212,9 +222,10 @@ static function execute($requestId, $payload){
case 'action.devices.commands.mediaStop': case 'action.devices.commands.mediaStop':
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand); $commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
break; break;
} }
} }
}
} }
$response = [ $response = [
'requestId' => $requestId, 'requestId' => $requestId,
@ -230,7 +241,8 @@ static function execute($requestId, $payload){
echo json_encode($response); echo json_encode($response);
} }
static function executeSwitch($subDeviceId, $executionCommand){ static function executeSwitch($subDeviceId, $executionCommand)
{
$value = 0; $value = 0;
$status = 'OFFLINE'; $status = 'OFFLINE';
$online = false; $online = false;
@ -266,7 +278,8 @@ static function executeSwitch($subDeviceId, $executionCommand){
return $commandTemp; return $commandTemp;
} }
static function executeTermostatValue($subDeviceId, $executionCommand){ static function executeTermostatValue($subDeviceId, $executionCommand)
{
$value = 0; $value = 0;
$status = 'OFFLINE'; $status = 'OFFLINE';
$online = false; $online = false;
@ -307,7 +320,8 @@ static function executeTermostatValue($subDeviceId, $executionCommand){
return $commandTemp; return $commandTemp;
} }
static function executeTermostatMode($subDeviceId, $executionCommand){ static function executeTermostatMode($subDeviceId, $executionCommand)
{
$mode = "off"; $mode = "off";
$value = 0; $value = 0;
$status = 'OFFLINE'; $status = 'OFFLINE';
@ -348,7 +362,8 @@ static function executeTermostatMode($subDeviceId, $executionCommand){
return $commandTemp; return $commandTemp;
} }
static function executeVolume($subDeviceId, $executionCommand){ static function executeVolume($subDeviceId, $executionCommand)
{
//echo $executionCommand['params']['volumeLevel']; //echo $executionCommand['params']['volumeLevel'];
$status = 'OFFLINE'; $status = 'OFFLINE';
$online = false; $online = false;
@ -386,7 +401,8 @@ static function executeVolume($subDeviceId, $executionCommand){
return $commandTemp; return $commandTemp;
} }
static function executeApp($subDeviceId, $executionCommand){ static function executeApp($subDeviceId, $executionCommand)
{
//echo $executionCommand['params']['newApplication']; //echo $executionCommand['params']['newApplication'];
$status = 'OFFLINE'; $status = 'OFFLINE';
$online = false; $online = false;
@ -424,7 +440,8 @@ static function executeApp($subDeviceId, $executionCommand){
return $commandTemp; return $commandTemp;
} }
static function executeInput($subDeviceId, $executionCommand){ static function executeInput($subDeviceId, $executionCommand)
{
//echo $executionCommand['params']['newInput']; //echo $executionCommand['params']['newInput'];
$status = 'OFFLINE'; $status = 'OFFLINE';
$online = false; $online = false;
@ -462,7 +479,8 @@ static function executeInput($subDeviceId, $executionCommand){
return $commandTemp; return $commandTemp;
} }
static function executeMediaCont($subDeviceId, $executionCommand){ static function executeMediaCont($subDeviceId, $executionCommand)
{
$status = 'SUCCESS'; $status = 'SUCCESS';
$online = true; $online = true;