PHP_SMART_HOME_V3/app/models/GoogleHome.php

503 lines
14 KiB
PHP
Raw Permalink Normal View History

2020-05-16 15:18:27 +00:00
<?php
2021-04-11 08:40:32 +00:00
https://console.cloud.google.com/logs/query;cursorTimestamp=2021-04-11T07:51:16.567357750Z?project=simple-home-79188
2021-01-24 18:56:53 +00:00
class GoogleHome
{
static function sync($requestId)
{
2020-05-16 15:18:27 +00:00
$devices = [];
$roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomData) {
$devicesData = DeviceManager::getAllDevicesInRoom($roomData['room_id']);
foreach ($devicesData as $deviceKey => $deviceData) {
$traids = [];
2020-06-29 20:00:00 +00:00
$attributes = [];
2020-05-16 15:18:27 +00:00
//Google Compatibile Action Type
$actionType = GoogleHomeDeviceTypes::getAction($deviceData['type']);
if ($actionType == "") continue;
2020-05-16 23:27:06 +00:00
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$deviceTraid = GoogleHomeDeviceTypes::getTraid($subDeviceData['type']);
if ($deviceTraid != "") {
$traids[] = $deviceTraid;
2020-05-16 23:27:06 +00:00
}
$deviceAttributes = GoogleHomeDeviceTypes::getAttribute($subDeviceData['type']);
if ($deviceAttributes != "") {
2020-06-29 20:00:00 +00:00
$attributes += $deviceAttributes;
}
2020-05-16 15:18:27 +00:00
}
2021-02-02 11:47:41 +00:00
2021-01-24 18:56:53 +00:00
if ($traids < 1) {
2020-12-07 19:21:27 +00:00
continue;
}
$tempDevice = [
'id' => (string) $deviceData['device_id'],
'type' => $actionType,
'traits' => $traids,
'attributes' => $attributes,
'name' => [
'name' => $deviceData['name'],
],
'willReportState' => false,
2021-04-11 08:40:32 +00:00
'roomHint' => $roomData['name'],
"otherDeviceIds" => [
["deviceId" => "SH#".(string) $deviceData['device_id']]
]
];
if ($tempDevice['attributes'] == null) unset($tempDevice['attributes']);
//traids & Attributes
$devices[] = $tempDevice;
2020-05-16 15:18:27 +00:00
}
}
2020-05-16 15:18:27 +00:00
$response = [
'requestId' => $requestId,
'payload' => [
2021-01-24 18:56:53 +00:00
'agentUserId' => '651351531531',
'devices' => array_values($devices),
2020-05-16 15:18:27 +00:00
],
];
2021-01-24 18:56:53 +00:00
$apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
2020-07-28 07:02:46 +00:00
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
2020-05-16 15:18:27 +00:00
echo json_encode($response);
}
2021-01-24 18:56:53 +00:00
static function query($requestId, $payload)
{
2020-05-16 15:18:27 +00:00
$devices = [];
2020-07-04 18:06:54 +00:00
$num = 0;
2020-05-16 15:18:27 +00:00
foreach ($payload['devices'] as $deviceId) {
2020-07-04 18:06:54 +00:00
$tempDevice[$deviceId['id']] = [
'online' => false,
'status' => 'OFFLINE',
];
2021-02-02 11:47:41 +00:00
2021-01-24 18:56:53 +00:00
if ($subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id'])) {
foreach ($subDevicesData as $key => $subDeviceData) {
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
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) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed) {
$tempDevice[$deviceId['id']]['online'] = true;
}
2020-05-16 23:27:06 +00:00
}
2021-01-24 18:56:53 +00:00
switch ($subDeviceData['type']) {
case 'temp_cont':
$tempDevice[$deviceId['id']]['thermostatMode'] = 'off';
if ($lastRecord['value'] != 0) {
$tempDevice[$deviceId['id']]['thermostatMode'] = 'heat';
}
$tempDevice[$deviceId['id']]['thermostatTemperatureAmbient'] = $lastRecord['value'];
$tempDevice[$deviceId['id']]['thermostatTemperatureSetpoint'] = $lastRecord['value'];
break;
case 'vol_cont':
$tempDevice[$deviceId['id']]['currentVolume'] = $lastRecord['value'];
break;
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;
2020-05-16 23:27:06 +00:00
}
2020-05-16 15:18:27 +00:00
}
}
2020-05-16 15:18:27 +00:00
2021-01-24 18:56:53 +00:00
// $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) {
$executed++;
} else {
$waiting++;
}
2020-05-16 23:27:06 +00:00
}
2020-07-04 18:06:54 +00:00
2021-01-24 18:56:53 +00:00
if ($waiting < $executed) {
$status = "PENDING";
$online = true;
}
// }
$devices = $tempDevice;
if (count($devices)> 1){
$devices[] = $tempDevice;
2020-05-16 15:18:27 +00:00
}
}
2021-01-24 18:56:53 +00:00
$response = [
'requestId' => $requestId,
'payload' => [
'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), LogRecordTypes::INFO);
$apiLogManager->setLevel(LOGLEVEL);
echo json_encode($response);
}
2021-01-24 18:56:53 +00:00
static function execute($requestId, $payload)
{
$commands = [];
foreach ($payload['commands'] as $key => $command) {
foreach ($command['devices'] as $key2 => $device) {
$executionCommand = $command['execution'][0];
if (isset($command['execution'][$key])) {
$executionCommand = $command['execution'][$key];
}
2020-05-16 15:18:27 +00:00
2021-01-24 18:56:53 +00:00
$deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']);
if ($subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)) {
$subDeviceId = $subDeviceId['subdevice_id'];
switch ($executionCommand['command']) {
case 'action.devices.commands.OnOff':
$commands[] = self::executeSwitch($subDeviceId, $executionCommand);
break;
case 'action.devices.commands.ThermostatTemperatureSetpoint':
$commands[] = self::executeTermostatValue($subDeviceId, $executionCommand);
break;
case 'action.devices.commands.ThermostatSetMode':
$commands[] = self::executeTermostatMode($subDeviceId, $executionCommand);
break;
case 'action.devices.commands.setVolume':
$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;
}
}
2020-05-16 15:18:27 +00:00
2021-01-24 18:56:53 +00:00
}
}
2021-01-24 18:56:53 +00:00
$response = [
'requestId' => $requestId,
'payload' => [
'commands' => $commands,
],
];
2021-01-24 18:56:53 +00:00
$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);
2020-05-16 15:18:27 +00:00
2021-01-24 18:56:53 +00:00
echo json_encode($response);
}
2020-06-26 19:35:40 +00:00
2021-01-24 18:56:53 +00:00
static function executeSwitch($subDeviceId, $executionCommand)
{
$value = 0;
$status = 'OFFLINE';
$online = false;
2020-05-16 15:18:27 +00:00
2021-01-24 18:56:53 +00:00
if ($executionCommand['params']['on']) $value = 1;
2021-02-02 11:47:41 +00:00
RecordManager::createWithSubId($subDeviceId, $value, 'google');
2020-06-26 19:35:40 +00:00
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
2021-01-24 18:56:53 +00:00
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
2021-01-24 18:56:53 +00:00
if ($waiting < $executed) {
$status = "PENDING";
2020-06-26 19:35:40 +00:00
$online = true;
2020-05-16 23:27:06 +00:00
}
2021-01-24 18:56:53 +00:00
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'on' => $executionCommand['params']['on'],
'online' => $online,
],
];
return $commandTemp;
2020-05-16 15:18:27 +00:00
}
2021-01-24 18:56:53 +00:00
static function executeTermostatValue($subDeviceId, $executionCommand)
{
$value = 0;
$status = 'OFFLINE';
$online = false;
2020-07-04 18:06:54 +00:00
2021-01-24 18:56:53 +00:00
if (isset($executionCommand['params']['thermostatTemperatureSetpoint'])) {
$value = $executionCommand['params']['thermostatTemperatureSetpoint'];
}
2020-07-04 18:06:54 +00:00
2021-02-02 11:47:41 +00:00
RecordManager::createWithSubId($subDeviceId, $value, 'google');
2020-07-04 18:06:54 +00:00
$executed = 0;
$waiting = 0;
2021-01-24 18:56:53 +00:00
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $lastValue) {
if ($lastValue['execuded'] == 1) {
2020-07-04 18:06:54 +00:00
$executed++;
} else {
$waiting++;
}
}
2021-01-24 18:56:53 +00:00
if ($waiting < $executed) {
2020-07-04 18:06:54 +00:00
$status = "PENDING";
2021-01-24 18:56:53 +00:00
$online = true;;
2020-07-04 18:06:54 +00:00
}
2021-01-24 18:56:53 +00:00
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'thermostatMode' => 'heat',
'thermostatTemperatureSetpoint' => $value,
'thermostatTemperatureAmbient' => $value,
'online' => $online,
//ambient z dalšího zenzoru v roomu
],
];
return $commandTemp;
2020-07-04 18:06:54 +00:00
}
2021-01-24 18:56:53 +00:00
static function executeTermostatMode($subDeviceId, $executionCommand)
{
$mode = "off";
$value = 0;
$status = 'OFFLINE';
$online = false;
2020-07-04 18:06:54 +00:00
2021-01-24 18:56:53 +00:00
if (isset($executionCommand['params']['thermostatMode']) && $executionCommand['params']['thermostatMode'] != 'off') {
$mode = $executionCommand['params']['thermostatMode'];
$value = RecordManager::getLastRecordNotNull($subDeviceId)['value'];
}
2020-07-04 18:06:54 +00:00
2021-02-02 11:47:41 +00:00
RecordManager::createWithSubId($subDeviceId, $value, 'google');
2020-07-04 18:06:54 +00:00
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
2021-01-24 18:56:53 +00:00
if ($value['execuded'] == 1) {
2020-07-04 18:06:54 +00:00
$executed++;
} else {
$waiting++;
}
}
2021-01-24 18:56:53 +00:00
if ($waiting < $executed) {
2020-07-04 18:06:54 +00:00
$status = "PENDING";
$online = true;
}
2021-01-24 18:56:53 +00:00
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'thermostatMode' => $mode,
'online' => $online,
],
];
return $commandTemp;
2020-07-04 18:06:54 +00:00
}
2021-01-24 18:56:53 +00:00
static function executeVolume($subDeviceId, $executionCommand)
{
//echo $executionCommand['params']['volumeLevel'];
$status = 'OFFLINE';
$online = false;
2020-07-04 18:06:54 +00:00
2021-01-24 18:56:53 +00:00
$currentVolume = RecordManager::getLastRecord($subDeviceId)['value'];
2020-07-04 18:06:54 +00:00
2021-01-24 18:56:53 +00:00
if (isset($executionCommand['params']['volumeLevel'])) {
RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['volumeLevel']);
$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;
$currentVolume = $executionCommand['params']['volumeLevel'];
}
}
2020-07-04 18:06:54 +00:00
2021-01-24 18:56:53 +00:00
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'currentVolume' => $currentVolume,
'online' => $online,
],
];
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;
}
}