Google Home api Fix

This commit is contained in:
GamerClassN7 2021-01-24 19:56:53 +01:00
parent c17e3090a8
commit 1f49391b36

View File

@ -1,6 +1,8 @@
<?php
class GoogleHome {
static function sync($requestId){
class GoogleHome
{
static function sync($requestId)
{
$devices = [];
$roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomData) {
@ -26,7 +28,7 @@ class GoogleHome {
}
}
if ($traids < 1){
if ($traids < 1) {
continue;
}
@ -53,43 +55,45 @@ class GoogleHome {
$response = [
'requestId' => $requestId,
'payload' => [
'agentUserId'=>'651351531531',
'devices' => array_values( $devices ),
'agentUserId' => '651351531531',
'devices' => array_values($devices),
],
];
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
$apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
echo json_encode($response);
}
static function query($requestId, $payload){
static function query($requestId, $payload)
{
$devices = [];
$num = 0;
foreach ($payload['devices'] as $deviceId) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id']);
$tempDevice[$deviceId['id']] = [
'online' => false,
'status' => 'OFFLINE',
];
if ($subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id'])) {
foreach ($subDevicesData as $key => $subDeviceData) {
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
if ($lastRecord['execuded'] == 1){
if ($lastRecord != false && $lastRecord['execuded'] == 1) {
$tempDevice[$deviceId['id']]['online'] = true;
$tempDevice[$deviceId['id']]['status'] = "SUCCESS";
} else {
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) {
if ($value['execuded'] == 1){
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
if ($waiting < $executed) {
$tempDevice[$deviceId['id']]['online'] = true;
}
}
@ -121,29 +125,33 @@ class GoogleHome {
break;
}
}
}
if ($lastRecord['execuded'] == 1){
$online = true;
$status = 'SUCCESS';
} else {
// $lastRecord = RecordManager::getLastRecord($deviceId['id']);
// //var_dump($lastRecord);
// if ($lastRecord['execuded'] == 1) {
// $online = true;
// $status = 'SUCCESS';
// } else {
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) {
if ($value['execuded'] == 1){
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
if ($waiting < $executed) {
$status = "PENDING";
$online = true;
}
}
// }
$devices = $tempDevice;
/*if (count($devices)> 1){
if (count($devices)> 1){
$devices[] = $tempDevice;
}*/
}
}
$response = [
'requestId' => $requestId,
@ -151,13 +159,14 @@ class GoogleHome {
'devices' => $devices,
],
];
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
$apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->setLevel(LOGLEVEL);
echo json_encode($response);
}
}
static function execute($requestId, $payload){
static function execute($requestId, $payload)
{
$commands = [];
foreach ($payload['commands'] as $key => $command) {
foreach ($command['devices'] as $key2 => $device) {
@ -167,7 +176,8 @@ static function execute($requestId, $payload){
}
$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']) {
case 'action.devices.commands.OnOff':
$commands[] = self::executeSwitch($subDeviceId, $executionCommand);
@ -212,9 +222,10 @@ static function execute($requestId, $payload){
case 'action.devices.commands.mediaStop':
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
break;
}
}
}
}
$response = [
'requestId' => $requestId,
@ -223,14 +234,15 @@ static function execute($requestId, $payload){
],
];
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
$apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[API][EXECUTE][$requestId]\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
echo json_encode($response);
}
}
static function executeSwitch($subDeviceId, $executionCommand){
static function executeSwitch($subDeviceId, $executionCommand)
{
$value = 0;
$status = 'OFFLINE';
$online = false;
@ -242,14 +254,14 @@ static function executeSwitch($subDeviceId, $executionCommand){
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1){
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
if ($waiting < $executed) {
$status = "PENDING";
$online = true;
}
@ -264,9 +276,10 @@ static function executeSwitch($subDeviceId, $executionCommand){
],
];
return $commandTemp;
}
}
static function executeTermostatValue($subDeviceId, $executionCommand){
static function executeTermostatValue($subDeviceId, $executionCommand)
{
$value = 0;
$status = 'OFFLINE';
$online = false;
@ -280,14 +293,14 @@ static function executeTermostatValue($subDeviceId, $executionCommand){
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $lastValue) {
if ($lastValue['execuded'] == 1){
if ($lastValue['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
if ($waiting < $executed) {
$status = "PENDING";
$online = true;;
}
@ -305,9 +318,10 @@ static function executeTermostatValue($subDeviceId, $executionCommand){
],
];
return $commandTemp;
}
}
static function executeTermostatMode($subDeviceId, $executionCommand){
static function executeTermostatMode($subDeviceId, $executionCommand)
{
$mode = "off";
$value = 0;
$status = 'OFFLINE';
@ -323,14 +337,14 @@ static function executeTermostatMode($subDeviceId, $executionCommand){
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1){
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
if ($waiting < $executed) {
$status = "PENDING";
$online = true;
}
@ -346,9 +360,10 @@ static function executeTermostatMode($subDeviceId, $executionCommand){
];
return $commandTemp;
}
}
static function executeVolume($subDeviceId, $executionCommand){
static function executeVolume($subDeviceId, $executionCommand)
{
//echo $executionCommand['params']['volumeLevel'];
$status = 'OFFLINE';
$online = false;
@ -360,13 +375,13 @@ static function executeVolume($subDeviceId, $executionCommand){
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1){
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
if ($waiting < $executed) {
$status = "PENDING";
$online = true;
$currentVolume = $executionCommand['params']['volumeLevel'];
@ -384,9 +399,10 @@ static function executeVolume($subDeviceId, $executionCommand){
];
return $commandTemp;
}
}
static function executeApp($subDeviceId, $executionCommand){
static function executeApp($subDeviceId, $executionCommand)
{
//echo $executionCommand['params']['newApplication'];
$status = 'OFFLINE';
$online = false;
@ -398,13 +414,13 @@ static function executeApp($subDeviceId, $executionCommand){
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1){
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
if ($waiting < $executed) {
$status = "PENDING";
$online = true;
$currentApplication = $executionCommand['params']['newApplication'];
@ -422,9 +438,10 @@ static function executeApp($subDeviceId, $executionCommand){
];
return $commandTemp;
}
}
static function executeInput($subDeviceId, $executionCommand){
static function executeInput($subDeviceId, $executionCommand)
{
//echo $executionCommand['params']['newInput'];
$status = 'OFFLINE';
$online = false;
@ -436,13 +453,13 @@ static function executeInput($subDeviceId, $executionCommand){
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1){
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
if ($waiting < $executed) {
$status = "PENDING";
$online = true;
$currentInput = $executionCommand['params']['newInput'];
@ -460,9 +477,10 @@ static function executeInput($subDeviceId, $executionCommand){
];
return $commandTemp;
}
}
static function executeMediaCont($subDeviceId, $executionCommand){
static function executeMediaCont($subDeviceId, $executionCommand)
{
$status = 'SUCCESS';
$online = true;
@ -476,5 +494,5 @@ static function executeMediaCont($subDeviceId, $executionCommand){
];
return $commandTemp;
}
}
}