Some Fixes "hey, Google fix yourself :D"

This commit is contained in:
JonatanRek 2020-05-02 17:50:18 +02:00
parent 043cac1cba
commit c74f93890d
1 changed files with 16 additions and 9 deletions

View File

@ -2,22 +2,29 @@
class GoogleHomeApi { class GoogleHomeApi {
public static function response() public static function response()
{ {
set_time_limit (5); set_time_limit (20);
$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');
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']);
$apiLogManager->write("[Google Home] action.devices.SYNC", LogRecordType::INFO);
break; break;
case 'action.devices.QUERY': case 'action.devices.QUERY':
self::query($obj['requestId'], $obj['inputs'][0]['payload']); self::query($obj['requestId'], $obj['inputs'][0]['payload']);
$apiLogManager->write("[Google Home] action.devices.QUERY", LogRecordType::INFO);
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']);
$apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordType::INFO);
break; break;
} }
} }
@ -34,10 +41,10 @@ class GoogleHomeApi {
} }
$online = false; $online = false;
$status = 'SUCCESS'; $status = 'OFFLINE';
if (RecordManager::getLastRecord($deviceId['id'])['execuded'] == 1){ if (RecordManager::getLastRecord($deviceId['id'])['execuded'] == 1){
$online = true; $online = true;
$status = 'ERROR'; $status = 'SUCCESS';
} }
$devices[] = [ $devices[] = [
@ -65,20 +72,21 @@ class GoogleHomeApi {
$devices = []; $devices = [];
$roomsData = RoomManager::getAllRooms(); $roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomsData) { foreach ($roomsData as $roomKey => $roomData) {
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']); $devicesData = DeviceManager::getAllDevicesInRoom($roomData['room_id']);
foreach ($devicesData as $deviceKey => $deviceData) { foreach ($devicesData as $deviceKey => $deviceData) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']); $subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) { foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
if ($subDeviceData['type'] != "on/off") continue; if ($subDeviceData['type'] != "on/off") continue;
$devices[] = [ $devices[] = [
'id' => $subDeviceData['subdevice_id'], 'id' => (string) $subDeviceData['subdevice_id'],
'type' => 'action.devices.types.OUTLET', 'type' => 'action.devices.types.OUTLET',
'traits' => [ 'action.devices.traits.OnOff' ], 'traits' => [ 'action.devices.traits.OnOff' ],
'name' => [ 'name' => [
'name' => [$deviceData['name']], 'name' => $deviceData['name'],
], ],
'willReportState' => false, 'willReportState' => false,
'roomHint' => $roomData['name']
]; ];
} }
} }
@ -130,7 +138,7 @@ class GoogleHomeApi {
]; ];
if ($timeout >= 5){ if ($timeout >= 5){
$commandTemp['status'] = "ERROR"; $commandTemp['status'] = "OFFLINE";
} }
$commands[] = $commandTemp; $commands[] = $commandTemp;
@ -173,7 +181,6 @@ class GoogleHomeApi {
$get = [ $get = [
"access_token"=>"23165133", "access_token"=>"23165133",
"token_type"=>"Bearer", "token_type"=>"Bearer",
"expires_in"=>600,
"state"=>$_GET["state"], "state"=>$_GET["state"],
]; ];