Some edit

This commit is contained in:
Haitem 2020-07-04 20:06:54 +02:00
parent cdcddffc69
commit 61a1b5057c
4 changed files with 179 additions and 37 deletions

View File

@ -21,6 +21,7 @@ class GoogleHomeApi{
break; break;
case 'action.devices.EXECUTE': case 'action.devices.EXECUTE':
GoogleHome::execute($obj['requestId'], $obj['inputs'][0]['payload']); GoogleHome::execute($obj['requestId'], $obj['inputs'][0]['payload']);
$apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordType::INFO); $apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordType::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO); $apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);

View File

@ -61,21 +61,19 @@ class GoogleHome {
static function query($requestId, $payload){ static function query($requestId, $payload){
$devices = []; $devices = [];
$num = 0;
foreach ($payload['devices'] as $deviceId) { foreach ($payload['devices'] as $deviceId) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id']); $subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id']);
$tempDevice = [ $tempDevice[$deviceId['id']] = [
$deviceId['id'] => [ 'online' => false,
'online' => false, 'status' => 'OFFLINE',
'status'=> 'OFFLINE', ];
]
];
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['execuded'] == 1){
$tempDevice[$deviceId['id']]['online'] = true; $tempDevice[$deviceId['id']]['online'] = true;
$tempDevice[$deviceId['id']]['status'] = 'SUCCESS'; $tempDevice[$deviceId['id']]['status'] = "SUCCESS";
} else { } else {
$executed = 0; $executed = 0;
$waiting = 0; $waiting = 0;
@ -103,14 +101,22 @@ class GoogleHome {
case 'vol_cont': case 'vol_cont':
$tempDevice[$deviceId['id']]['currentVolume'] = $lastRecord['value']; $tempDevice[$deviceId['id']]['currentVolume'] = $lastRecord['value'];
break; 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); $tempDevice[$deviceId['id']]['on'] = ($lastRecord['value'] == 1 ? true : false);
break; break;
} }
} }
if ($lastRecord['execuded'] == 1){ if ($lastRecord['execuded'] == 1){
$online = true; $online = true;
$status = 'SUCCESS'; $status = 'SUCCESS';
@ -129,11 +135,10 @@ class GoogleHome {
$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,
@ -141,7 +146,6 @@ class GoogleHome {
'devices' => $devices, '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), LogRecordType::INFO); $apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
echo json_encode($response); echo json_encode($response);
@ -149,9 +153,8 @@ class GoogleHome {
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 $key => $device) { foreach ($command['devices'] as $key2 => $device) {
$executionCommand = $command['execution'][0]; $executionCommand = $command['execution'][0];
if (isset($command['execution'][$key])) { if (isset($command['execution'][$key])) {
$executionCommand = $command['execution'][$key]; $executionCommand = $command['execution'][$key];
@ -159,7 +162,6 @@ static function execute($requestId, $payload){
$deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']); $deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']);
$subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)['subdevice_id']; $subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)['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);
@ -177,10 +179,37 @@ static function execute($requestId, $payload){
$commands[] = self::executeVolume($subDeviceId, $executionCommand); $commands[] = self::executeVolume($subDeviceId, $executionCommand);
break; 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 = [ $response = [
'requestId' => $requestId, 'requestId' => $requestId,
'payload' => [ 'payload' => [
@ -313,7 +342,7 @@ static function executeTermostatMode($subDeviceId, $executionCommand){
} }
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;
@ -349,4 +378,96 @@ static function executeVolume($subDeviceId, $executionCommand){
return $commandTemp; 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;
}
} }

View File

@ -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)); return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ?;", array($deviceId));
} else { } else {
return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ? AND type = ?;", array($deviceId, $subDeviceType)); return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ? AND type = ?;", array($deviceId, $subDeviceType));

View File

@ -75,13 +75,17 @@ class GoogleHomeDeviceTypes {
'control-light' => 'action.devices.types.LIGHT', 'control-light' => 'action.devices.types.LIGHT',
'control-socket' => 'action.devices.types.OUTLET', 'control-socket' => 'action.devices.types.OUTLET',
'control-temp' => 'action.devices.types.THERMOSTAT', 'control-temp' => 'action.devices.types.THERMOSTAT',
'control-media' => 'action.devices.types.REMOTE', 'control-media' => 'action.devices.types.REMOTECONTROL',
]; ];
private static $traidWordBook = [ private static $traidWordBook = [
'on/off' => 'action.devices.traits.OnOff', 'on/off' => 'action.devices.traits.OnOff',
'temp_cont' => 'action.devices.traits.TemperatureSetting', 'temp_cont' => 'action.devices.traits.TemperatureSetting',
'vol_cont' => 'action.devices.traits.Volume', '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 = [ private static $commandWordBook = [
@ -89,29 +93,44 @@ class GoogleHomeDeviceTypes {
'action.devices.commands.ThermostatTemperatureSetpoint' => 'temp_cont', 'action.devices.commands.ThermostatTemperatureSetpoint' => 'temp_cont',
'action.devices.commands.ThermostatSetMode' => 'temp_cont', 'action.devices.commands.ThermostatSetMode' => 'temp_cont',
'action.devices.commands.setVolume' => 'vol_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 = [ private static $attributeWordBook = [
'on/off' => [
'commandOnlyOnOff' => false,
],
'temp_cont' => [ 'temp_cont' => [
'availableThermostatModes' => 'off,heat', 'availableThermostatModes' => 'off,heat',
'thermostatTemperatureUnit' => 'C' 'thermostatTemperatureUnit' => 'C',
], ],
'vol_cont' => [ 'vol_cont' => [
'volumeCanMuteAndUnmute' => false, 'volumeCanMuteAndUnmute' => false,
'volumeDefaultPercentage' => 6, 'volumeDefaultPercentage' => 6,
'volumeMaxLevel' => 100, 'volumeMaxLevel' => 100,
'levelStepSize' => 2, 'levelStepSize' => 2,
'commandOnlyVolume' => true, 'commandOnlyVolume' => false,
], ],
'media_status'=> [ 'media_cont'=> [
'transportControlSupportedCommands' => [ 'transportControlSupportedCommands' => [
"NEXT", "NEXT",
"PREVIOUS", "PREVIOUS",
"PAUSE", "PAUSE",
"STOP", "STOP",
"RESUME", "RESUME",
"CAPTION_CONTROL"
], ],
], ],
'media_status'=> [
'supportActivityState' => true,
'supportPlaybackState' => true,
],
'media_apps' => [ 'media_apps' => [
"availableApplications" => [ "availableApplications" => [
[ [
@ -120,22 +139,23 @@ class GoogleHomeDeviceTypes {
"name_synonym" => [ "name_synonym" => [
"Kodi", "Kodi",
], ],
"lang" => "en" "lang" => "en",
], ],
], ],
], ],
], ],
'media_inputs' => [ 'media_input' => [
"availableApplications" => [ "availableInputs" => [
"key" => "pc", [
"names" => [ "key" => "pc",
"name_synonym" => [ "names" => [
"PC", "name_synonym" => [
"PC",
],
"lang" => "en",
], ],
"lang" => "en",
], ],
]
],
], ],
]; ];