2020-04-29 17:28:33 +00:00
|
|
|
<?php
|
|
|
|
class GoogleHomeApi {
|
2020-05-02 11:18:15 +00:00
|
|
|
public static function response()
|
2020-04-30 17:40:34 +00:00
|
|
|
{
|
2020-05-02 15:50:18 +00:00
|
|
|
set_time_limit (20);
|
2020-04-30 17:40:34 +00:00
|
|
|
$json = file_get_contents('php://input');
|
|
|
|
$obj = json_decode($json, true);
|
2020-05-02 15:50:18 +00:00
|
|
|
|
|
|
|
$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
|
|
|
|
|
2020-04-30 17:40:34 +00:00
|
|
|
header('Content-Type: application/json');
|
2020-05-02 11:18:15 +00:00
|
|
|
|
2020-04-30 17:40:34 +00:00
|
|
|
switch ($obj['inputs'][0]['intent']) {
|
|
|
|
case 'action.devices.SYNC':
|
2020-05-02 11:18:15 +00:00
|
|
|
self::sync($obj['requestId']);
|
2020-05-02 15:50:18 +00:00
|
|
|
$apiLogManager->write("[Google Home] action.devices.SYNC", LogRecordType::INFO);
|
2020-04-30 17:40:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'action.devices.QUERY':
|
2020-05-02 11:18:15 +00:00
|
|
|
self::query($obj['requestId'], $obj['inputs'][0]['payload']);
|
2020-05-02 15:50:18 +00:00
|
|
|
$apiLogManager->write("[Google Home] action.devices.QUERY", LogRecordType::INFO);
|
2020-04-30 17:40:34 +00:00
|
|
|
break;
|
|
|
|
|
2020-05-02 15:50:18 +00:00
|
|
|
|
2020-04-30 17:40:34 +00:00
|
|
|
case 'action.devices.EXECUTE':
|
2020-05-02 11:18:15 +00:00
|
|
|
self::execute($obj['requestId'], $obj['inputs'][0]['payload']);
|
2020-05-02 15:50:18 +00:00
|
|
|
$apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordType::INFO);
|
2020-04-30 17:40:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 11:18:15 +00:00
|
|
|
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;
|
2020-05-02 15:50:18 +00:00
|
|
|
$status = 'OFFLINE';
|
2020-05-02 11:18:15 +00:00
|
|
|
if (RecordManager::getLastRecord($deviceId['id'])['execuded'] == 1){
|
|
|
|
$online = true;
|
2020-05-02 15:50:18 +00:00
|
|
|
$status = 'SUCCESS';
|
2020-05-02 11:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$devices[] = [
|
|
|
|
$deviceId['id'] => [
|
|
|
|
'on' => $state,
|
|
|
|
'online' => $online,
|
2020-05-02 11:26:18 +00:00
|
|
|
'status'=> $status,
|
2020-05-02 11:18:15 +00:00
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
2020-04-30 17:40:34 +00:00
|
|
|
static function sync($requestId){
|
|
|
|
$devices = [];
|
|
|
|
|
|
|
|
$roomsData = RoomManager::getAllRooms();
|
2020-05-02 15:50:18 +00:00
|
|
|
foreach ($roomsData as $roomKey => $roomData) {
|
|
|
|
$devicesData = DeviceManager::getAllDevicesInRoom($roomData['room_id']);
|
2020-04-30 17:40:34 +00:00
|
|
|
foreach ($devicesData as $deviceKey => $deviceData) {
|
|
|
|
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
|
|
|
|
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
|
|
|
|
if ($subDeviceData['type'] != "on/off") continue;
|
|
|
|
$devices[] = [
|
2020-05-02 15:50:18 +00:00
|
|
|
'id' => (string) $subDeviceData['subdevice_id'],
|
2020-04-30 17:40:34 +00:00
|
|
|
'type' => 'action.devices.types.OUTLET',
|
|
|
|
'traits' => [ 'action.devices.traits.OnOff' ],
|
|
|
|
'name' => [
|
2020-05-02 15:50:18 +00:00
|
|
|
'name' => $deviceData['name'],
|
2020-04-30 17:40:34 +00:00
|
|
|
],
|
|
|
|
'willReportState' => false,
|
2020-05-02 15:50:18 +00:00
|
|
|
'roomHint' => $roomData['name']
|
2020-04-30 17:40:34 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = [
|
|
|
|
'requestId' => $requestId,
|
|
|
|
'payload' => [
|
2020-05-02 11:18:15 +00:00
|
|
|
'agentUserId'=>'651351531531',
|
|
|
|
'devices' => $devices,
|
|
|
|
],
|
2020-04-30 17:40:34 +00:00
|
|
|
];
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2020-05-02 11:18:15 +00:00
|
|
|
static function execute($requestId, $payload){
|
|
|
|
$commands = [];
|
|
|
|
|
2020-04-30 17:40:34 +00:00
|
|
|
foreach ($payload['commands'] as $key => $command) {
|
|
|
|
foreach ($command['devices'] as $key => $device) {
|
2020-05-02 11:18:15 +00:00
|
|
|
$executionCommand = $command['execution'][0];
|
|
|
|
if (isset($command['execution'][$key])) {
|
|
|
|
$executionCommand = $command['execution'][$key];
|
|
|
|
}
|
|
|
|
|
2020-04-30 17:40:34 +00:00
|
|
|
$subDeviceId = $device['id'];
|
|
|
|
|
2020-05-02 11:18:15 +00:00
|
|
|
switch ($executionCommand['command']) {
|
2020-04-30 17:40:34 +00:00
|
|
|
case 'action.devices.commands.OnOff':
|
2020-05-02 11:18:15 +00:00
|
|
|
$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){
|
2020-05-02 15:50:18 +00:00
|
|
|
$commandTemp['status'] = "OFFLINE";
|
2020-05-02 11:18:15 +00:00
|
|
|
}
|
|
|
|
$commands[] = $commandTemp;
|
2020-04-30 17:40:34 +00:00
|
|
|
|
2020-05-02 11:18:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-04-30 17:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = [
|
|
|
|
'requestId' => $requestId,
|
|
|
|
'payload' => [
|
2020-05-02 11:18:15 +00:00
|
|
|
'commands' => $commands,
|
|
|
|
],
|
2020-04-30 17:40:34 +00:00
|
|
|
];
|
|
|
|
$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);
|
2020-05-02 11:18:15 +00:00
|
|
|
|
2020-04-30 17:40:34 +00:00
|
|
|
echo json_encode($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
function autorize(){
|
|
|
|
$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] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
|
|
|
|
|
|
|
//tel: zemanová 607979429
|
|
|
|
|
|
|
|
/*echo json_encode(array (
|
2020-05-02 11:18:15 +00:00
|
|
|
'access_token' => '',
|
|
|
|
'token_type' => 'bearer',
|
|
|
|
'expires_in' => 3600,
|
|
|
|
'refresh_token' => '',
|
|
|
|
'scope' => 'create',
|
|
|
|
));*/
|
|
|
|
|
|
|
|
$get = [
|
|
|
|
"access_token"=>"23165133",
|
|
|
|
"token_type"=>"Bearer",
|
|
|
|
"state"=>$_GET["state"],
|
|
|
|
];
|
|
|
|
|
|
|
|
echo $_GET["redirect_uri"] . '#' . http_build_query($get) ;
|
|
|
|
echo '<a href="'.$_GET["redirect_uri"] . '#' . http_build_query($get) . '">FINISH</a>';
|
|
|
|
}
|
2020-04-29 17:28:33 +00:00
|
|
|
}
|