diff --git a/app/api/GoogleHomeApi.php b/app/api/GoogleHomeApi.php index b2784d4..81231f7 100644 --- a/app/api/GoogleHomeApi.php +++ b/app/api/GoogleHomeApi.php @@ -21,6 +21,7 @@ class GoogleHomeApi{ break; case 'action.devices.EXECUTE': + GoogleHome::execute($obj['requestId'], $obj['inputs'][0]['payload']); $apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordType::INFO); $apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO); diff --git a/app/models/GoogleHome.php b/app/models/GoogleHome.php index 264ebc7..6b8b6f8 100644 --- a/app/models/GoogleHome.php +++ b/app/models/GoogleHome.php @@ -61,21 +61,19 @@ class GoogleHome { 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', - ] - ]; - + $tempDevice[$deviceId['id']] = [ + 'online' => false, + 'status' => 'OFFLINE', + ]; foreach ($subDevicesData as $key => $subDeviceData) { $lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']); if ($lastRecord['execuded'] == 1){ $tempDevice[$deviceId['id']]['online'] = true; - $tempDevice[$deviceId['id']]['status'] = 'SUCCESS'; + $tempDevice[$deviceId['id']]['status'] = "SUCCESS"; } else { $executed = 0; $waiting = 0; @@ -103,14 +101,22 @@ class GoogleHome { case 'vol_cont': $tempDevice[$deviceId['id']]['currentVolume'] = $lastRecord['value']; break; - default: + case 'media_apps': + $tempDevice[$deviceId['id']]['currentApplication'] = "kodi"; + break; + case 'media_input': + $tempDevice[$deviceId['id']]['currentInput'] = "pc"; + break; + case 'media_status': + $tempDevice[$deviceId['id']]['activityState'] = "ACTIVE"; + $tempDevice[$deviceId['id']]['playbackState'] = "PLAYING"; + break; + case 'on/off': $tempDevice[$deviceId['id']]['on'] = ($lastRecord['value'] == 1 ? true : false); - break; + break; } } - - if ($lastRecord['execuded'] == 1){ $online = true; $status = 'SUCCESS'; @@ -129,11 +135,10 @@ class GoogleHome { $online = true; } } - $devices = $tempDevice; - if (count($devices)> 1){ + /*if (count($devices)> 1){ $devices[] = $tempDevice; - } + }*/ } $response = [ 'requestId' => $requestId, @@ -141,7 +146,6 @@ class GoogleHome { 'devices' => $devices, ], ]; - $apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log'); $apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO); echo json_encode($response); @@ -149,9 +153,8 @@ class GoogleHome { static function execute($requestId, $payload){ $commands = []; - foreach ($payload['commands'] as $key => $command) { - foreach ($command['devices'] as $key => $device) { + foreach ($command['devices'] as $key2 => $device) { $executionCommand = $command['execution'][0]; if (isset($command['execution'][$key])) { $executionCommand = $command['execution'][$key]; @@ -159,7 +162,6 @@ static function execute($requestId, $payload){ $deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']); $subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)['subdevice_id']; - switch ($executionCommand['command']) { case 'action.devices.commands.OnOff': $commands[] = self::executeSwitch($subDeviceId, $executionCommand); @@ -177,10 +179,37 @@ static function execute($requestId, $payload){ $commands[] = self::executeVolume($subDeviceId, $executionCommand); break; + case 'action.devices.commands.appSelect': + $commands[] = self::executeApp($subDeviceId, $executionCommand); + break; + + case 'action.devices.commands.SetInput': + $commands[] = self::executeInput($subDeviceId, $executionCommand); + break; + + case 'action.devices.commands.mediaNext': + $commands[] = self::executeMediaCont($subDeviceId, $executionCommand); + break; + + case 'action.devices.commands.mediaPrevious': + $commands[] = self::executeMediaCont($subDeviceId, $executionCommand); + break; + + case 'action.devices.commands.mediaPause': + $commands[] = self::executeMediaCont($subDeviceId, $executionCommand); + break; + + case 'action.devices.commands.mediaResume': + $commands[] = self::executeMediaCont($subDeviceId, $executionCommand); + break; + + case 'action.devices.commands.mediaStop': + $commands[] = self::executeMediaCont($subDeviceId, $executionCommand); + break; + } } } - $response = [ 'requestId' => $requestId, 'payload' => [ @@ -313,7 +342,7 @@ static function executeTermostatMode($subDeviceId, $executionCommand){ } static function executeVolume($subDeviceId, $executionCommand){ - echo $executionCommand['params']['volumeLevel']; + //echo $executionCommand['params']['volumeLevel']; $status = 'OFFLINE'; $online = false; @@ -349,4 +378,96 @@ static function executeVolume($subDeviceId, $executionCommand){ return $commandTemp; } + +static function executeApp($subDeviceId, $executionCommand){ + //echo $executionCommand['params']['newApplication']; + $status = 'OFFLINE'; + $online = false; + + $currentApplication = RecordManager::getLastRecord($subDeviceId)['value']; + + if (isset($executionCommand['params']['newApplication'])) { + RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['newApplication']); + $executed = 0; + $waiting = 0; + foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) { + if ($value['execuded'] == 1){ + $executed++; + } else { + $waiting++; + } + } + if ($waiting < $executed){ + $status = "PENDING"; + $online = true; + $currentApplication = $executionCommand['params']['newApplication']; + } + } + + $deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id']; + $commandTemp = [ + 'ids' => [(string) $deviceId], + 'status' => $status, + 'states' => [ + 'currentApplication' => $currentApplication, + 'online' => $online, + ], + ]; + + return $commandTemp; +} + +static function executeInput($subDeviceId, $executionCommand){ + //echo $executionCommand['params']['newInput']; + $status = 'OFFLINE'; + $online = false; + + $currentInput = RecordManager::getLastRecord($subDeviceId)['value']; + + if (isset($executionCommand['params']['newInput'])) { + RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['newInput']); + $executed = 0; + $waiting = 0; + foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) { + if ($value['execuded'] == 1){ + $executed++; + } else { + $waiting++; + } + } + if ($waiting < $executed){ + $status = "PENDING"; + $online = true; + $currentInput = $executionCommand['params']['newInput']; + } + } + + $deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id']; + $commandTemp = [ + 'ids' => [(string) $deviceId], + 'status' => $status, + 'states' => [ + 'currentInput' => $currentInput, + 'online' => $online, + ], + ]; + + return $commandTemp; +} + +static function executeMediaCont($subDeviceId, $executionCommand){ + $status = 'SUCCESS'; + $online = true; + + $deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id']; + $commandTemp = [ + 'ids' => [(string) $deviceId], + 'status' => $status, + 'states' => [ + 'online' => $online, + ], + ]; + + return $commandTemp; +} } diff --git a/app/models/managers/SubDeviceManager.php b/app/models/managers/SubDeviceManager.php index 712db1d..d9d11ba 100644 --- a/app/models/managers/SubDeviceManager.php +++ b/app/models/managers/SubDeviceManager.php @@ -25,9 +25,9 @@ class SubDeviceManager } } - public static function getSubDeviceByMasterAndType($deviceId, $subDeviceType = null) + public static function getSubDeviceByMasterAndType($deviceId, $subDeviceType = '') { - if (!empty($subDeviceType)) { + if ($subDeviceType == '') { return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ?;", array($deviceId)); } else { return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ? AND type = ?;", array($deviceId, $subDeviceType)); diff --git a/app/models/types/GoogleHomeDeviceTypes.php b/app/models/types/GoogleHomeDeviceTypes.php index 57dc667..df4a79e 100644 --- a/app/models/types/GoogleHomeDeviceTypes.php +++ b/app/models/types/GoogleHomeDeviceTypes.php @@ -75,13 +75,17 @@ class GoogleHomeDeviceTypes { 'control-light' => 'action.devices.types.LIGHT', 'control-socket' => 'action.devices.types.OUTLET', 'control-temp' => 'action.devices.types.THERMOSTAT', - 'control-media' => 'action.devices.types.REMOTE', + 'control-media' => 'action.devices.types.REMOTECONTROL', ]; private static $traidWordBook = [ 'on/off' => 'action.devices.traits.OnOff', 'temp_cont' => 'action.devices.traits.TemperatureSetting', 'vol_cont' => 'action.devices.traits.Volume', + 'media_cont' => 'action.devices.traits.TransportControl', + 'media_status' => 'action.devices.traits.MediaState', + 'media_apps' => 'action.devices.traits.AppSelector', + 'media_input' => 'action.devices.traits.InputSelector', ]; private static $commandWordBook = [ @@ -89,29 +93,44 @@ class GoogleHomeDeviceTypes { 'action.devices.commands.ThermostatTemperatureSetpoint' => 'temp_cont', 'action.devices.commands.ThermostatSetMode' => 'temp_cont', 'action.devices.commands.setVolume' => 'vol_cont', + 'action.devices.commands.mediaNext' => 'media_status', + 'action.devices.commands.mediaPause' => 'media_status', + 'action.devices.commands.mediaPrevious' => 'media_status', + 'action.devices.commands.mediaResume' => 'media_status', + 'action.devices.commands.mediaStop' => 'media_status', + 'action.devices.commands.appSelect' => 'media_apps', + 'action.devices.commands.SetInput' => 'media_input', ]; private static $attributeWordBook = [ + 'on/off' => [ + 'commandOnlyOnOff' => false, + ], 'temp_cont' => [ 'availableThermostatModes' => 'off,heat', - 'thermostatTemperatureUnit' => 'C' + 'thermostatTemperatureUnit' => 'C', ], 'vol_cont' => [ 'volumeCanMuteAndUnmute' => false, 'volumeDefaultPercentage' => 6, 'volumeMaxLevel' => 100, 'levelStepSize' => 2, - 'commandOnlyVolume' => true, + 'commandOnlyVolume' => false, ], - 'media_status'=> [ + 'media_cont'=> [ 'transportControlSupportedCommands' => [ "NEXT", "PREVIOUS", "PAUSE", "STOP", "RESUME", + "CAPTION_CONTROL" ], ], + 'media_status'=> [ + 'supportActivityState' => true, + 'supportPlaybackState' => true, + ], 'media_apps' => [ "availableApplications" => [ [ @@ -120,22 +139,23 @@ class GoogleHomeDeviceTypes { "name_synonym" => [ "Kodi", ], - "lang" => "en" + "lang" => "en", ], ], ], ], - 'media_inputs' => [ - "availableApplications" => [ - "key" => "pc", - "names" => [ - "name_synonym" => [ - "PC", + 'media_input' => [ + "availableInputs" => [ + [ + "key" => "pc", + "names" => [ + "name_synonym" => [ + "PC", + ], + "lang" => "en", ], - "lang" => "en", ], - - ], + ] ], ];