PHP_SMART_HOME_V3/app/models/GoogleHome.php

264 lines
7.2 KiB
PHP
Raw Normal View History

2020-05-16 15:18:27 +00:00
<?php
class GoogleHome {
static function sync($requestId){
$devices = [];
$roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomData) {
$devicesData = DeviceManager::getAllDevicesInRoom($roomData['room_id']);
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
if ($subDeviceData['type'] != "on/off" && $subDeviceData['type'] != "temp_cont") continue;
//Google Compatibile Action Type
$actionType = GoogleHomeDeviceTypes::getAction($subDeviceData['type']);
2020-05-16 23:27:06 +00:00
if (strpos($deviceData['name'], 'Světlo') !== false || strpos($deviceData['name'], 'světlo') !== false) {
$actionType = 'action.devices.types.LIGHT';
}
2020-05-16 15:18:27 +00:00
$tempDevice = [
'id' => (string) $subDeviceData['subdevice_id'],
'type' => $actionType,
'name' => [
'name' => $deviceData['name'],
],
'willReportState' => false,
'roomHint' => $roomData['name']
];
//traids & Attributes
$devices[] = GoogleHomeDeviceTypes::getSyncObj($tempDevice, $actionType);
}
}
}
$response = [
'requestId' => $requestId,
'payload' => [
'agentUserId'=>'651351531531',
'devices' => $devices,
],
];
2020-05-20 07:07:27 +00:00
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
2020-05-16 15:18:27 +00:00
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
echo json_encode($response);
}
static function query($requestId, $payload){
$devices = [];
foreach ($payload['devices'] as $deviceId) {
$subDeviceData = SubDeviceManager::getSubDevice($deviceId['id']);
if ($subDeviceData['type'] != "on/off" && $subDeviceData['type'] != "temp_cont") continue;
$state = false;
if (RecordManager::getLastRecord($deviceId['id'])['value'] == 1){
$state = true;
}
$online = false;
$status = 'OFFLINE';
if (RecordManager::getLastRecord($deviceId['id'])['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++;
}
}
if ($waiting < $executed){
$status = "PENDING";
2020-05-16 20:05:11 +00:00
2020-05-16 15:18:27 +00:00
$online = true;
}
}
$tempDevice = [
$deviceId['id'] => [
'online' => $online,
'status'=> $status,
2020-05-16 23:27:06 +00:00
]
];
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
if ($subDeviceData['type'] == "temp_cont"){
$tempDevice[$deviceId['id']]['thermostatMode'] = 'off';
if (RecordManager::getLastRecord($deviceId['id'])['value'] != 0) {
$tempDevice[$deviceId['id']]['thermostatMode'] = 'heat';
}
2020-05-16 15:18:27 +00:00
$tempDevice[$deviceId['id']]['thermostatTemperatureAmbient'] = RecordManager::getLastRecord($deviceId['id'])['value'];
$tempDevice[$deviceId['id']]['thermostatTemperatureSetpoint'] = RecordManager::getLastRecord($deviceId['id'])['value'];
2020-05-16 23:27:06 +00:00
} else {
$tempDevice[$deviceId['id']]['on'] = $state;
}
$devices = $tempDevice;
if (count($devices)> 1){
$devices[] = $tempDevice;
}
2020-05-16 15:18:27 +00:00
}
2020-05-16 23:27:06 +00:00
$response = [
'requestId' => $requestId,
'payload' => [
'devices' => $devices,
],
];
2020-05-16 15:18:27 +00:00
2020-05-20 07:07:27 +00:00
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
2020-05-16 23:27:06 +00:00
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
echo json_encode($response);
}
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
static function execute($requestId, $payload){
$commands = [];
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
foreach ($payload['commands'] as $key => $command) {
foreach ($command['devices'] as $key => $device) {
$executionCommand = $command['execution'][0];
if (isset($command['execution'][$key])) {
$executionCommand = $command['execution'][$key];
}
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
$subDeviceId = $device['id'];
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
switch ($executionCommand['command']) {
case 'action.devices.commands.OnOff':
$commands[] = self::executeSwitch($subDeviceId, $executionCommand);
2020-05-16 15:18:27 +00:00
break;
2020-05-16 23:27:06 +00:00
case 'action.devices.commands.ThermostatTemperatureSetpoint':
$commands[] = self::executeTermostatValue($subDeviceId, $executionCommand);
2020-05-16 15:18:27 +00:00
break;
2020-05-16 23:27:06 +00:00
case 'action.devices.commands.ThermostatSetMode':
$commands[] = self::executeTermostatMode($subDeviceId, $executionCommand);
2020-05-16 15:18:27 +00:00
break;
2020-05-16 23:27:06 +00:00
}
2020-05-16 15:18:27 +00:00
}
}
2020-05-16 23:27:06 +00:00
$response = [
'requestId' => $requestId,
'payload' => [
'commands' => $commands,
],
];
2020-05-20 07:07:27 +00:00
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
2020-05-16 23:27:06 +00:00
$apiLogManager->write("[API][EXECUTE][$requestId]\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
echo json_encode($response);
}
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
static function executeSwitch($subDeviceId, $executionCommand){
$value = 0;
$status = 'SUCCESS';
if ($executionCommand['params']['on']) $value = 1;
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
RecordManager::createWithSubId($subDeviceId, $value);
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1){
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
$status = "PENDING";
2020-05-16 15:18:27 +00:00
} else {
2020-05-16 23:27:06 +00:00
$status = "OFFLINE";
2020-05-16 15:18:27 +00:00
}
2020-05-16 23:27:06 +00:00
$commandTemp = [
'ids' => [$subDeviceId],
'status' => $status,
'states' => [
'on' => $executionCommand['params']['on'],
],
];
return $commandTemp;
}
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
static function executeTermostatValue($subDeviceId, $executionCommand){
$value = 0;
$status = 'SUCCESS';
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
if (isset($executionCommand['params']['thermostatTemperatureSetpoint'])) {
$value = $executionCommand['params']['thermostatTemperatureSetpoint'];
}
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
RecordManager::createWithSubId($subDeviceId, $value);
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $lastValue) {
if ($lastValue['execuded'] == 1){
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
$status = "PENDING";
$status = "SUCCESS";
2020-05-16 15:18:27 +00:00
} else {
2020-05-16 23:27:06 +00:00
$status = "OFFLINE";
2020-05-16 15:18:27 +00:00
}
2020-05-16 23:27:06 +00:00
$commandTemp = [
'ids' => [$subDeviceId],
'status' => $status,
'states' => [
'thermostatMode' => 'heat',
'thermostatTemperatureSetpoint' => $value,
'thermostatTemperatureAmbient' => $value,
//ambient z dalšího zenzoru v roomu
],
];
return $commandTemp;
}
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
static function executeTermostatMode($subDeviceId, $executionCommand){
$mode = "off";
$value = 0;
$status = "SUCCESS";
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
if (isset($executionCommand['params']['thermostatMode']) && $executionCommand['params']['thermostatMode'] != 'off') {
$mode = $executionCommand['params']['thermostatMode'];
$value = RecordManager::getLastRecordNotNull($subDeviceId)['value'];
}
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
RecordManager::createWithSubId($subDeviceId, $value);
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1){
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
$status = "PENDING";
2020-05-16 15:18:27 +00:00
}
2020-05-16 23:27:06 +00:00
$commandTemp = [
'ids' => [$subDeviceId],
'status' => $status,
'states' => [
'thermostatMode' => $mode
],
];
2020-05-16 15:18:27 +00:00
2020-05-16 23:27:06 +00:00
return $commandTemp;
}
2020-05-16 15:18:27 +00:00
}