3 Commits

Author SHA1 Message Date
2410441b88 Keep record of record origin 2021-02-02 12:47:41 +01:00
GamerClassN7
1f49391b36 Google Home api Fix 2021-01-24 19:56:53 +01:00
haitem
c17e3090a8 repairt bug 2021-01-15 13:38:47 +01:00
4 changed files with 413 additions and 386 deletions

View File

@@ -113,7 +113,7 @@ class EndpointsApi extends ApiController{
} }
$subDeviceLastReordValue[$key] = $value['value']; $subDeviceLastReordValue[$key] = $value['value'];
RecordManager::create($device['device_id'], $key, round($value['value'],3)); RecordManager::create($device['device_id'], $key, round($value['value'],3), 'device');
$logManager->write("[API] Device_ID " . $device['device_id'] . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO); $logManager->write("[API] Device_ID " . $device['device_id'] . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO);
//notification //notification

View File

@@ -14,7 +14,7 @@ class WidgetApi extends ApiController
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId); $subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
if ($subDeviceData['type'] == 'on/off') { if ($subDeviceData['type'] == 'on/off') {
$lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value']; $lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'];
RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue); RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue, "vue-app");
$response = !$lastValue; $response = !$lastValue;
} else { } else {
throw new Exception("Bad Request", 403); throw new Exception("Bad Request", 403);

View File

@@ -1,6 +1,8 @@
<?php <?php
class GoogleHome { class GoogleHome
static function sync($requestId){ {
static function sync($requestId)
{
$devices = []; $devices = [];
$roomsData = RoomManager::getAllRooms(); $roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomData) { foreach ($roomsData as $roomKey => $roomData) {
@@ -26,7 +28,7 @@ class GoogleHome {
} }
} }
if ($traids < 1){ if ($traids < 1) {
continue; continue;
} }
@@ -53,428 +55,444 @@ class GoogleHome {
$response = [ $response = [
'requestId' => $requestId, 'requestId' => $requestId,
'payload' => [ 'payload' => [
'agentUserId'=>'651351531531', 'agentUserId' => '651351531531',
'devices' => array_values( $devices ), 'devices' => array_values($devices),
], ],
]; ];
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log'); $apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
$apiLogManager->setLevel(LOGLEVEL); $apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO); $apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
echo json_encode($response); echo json_encode($response);
} }
static function query($requestId, $payload){ static function query($requestId, $payload)
{
$devices = []; $devices = [];
$num = 0; $num = 0;
foreach ($payload['devices'] as $deviceId) { foreach ($payload['devices'] as $deviceId) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id']);
$tempDevice[$deviceId['id']] = [ $tempDevice[$deviceId['id']] = [
'online' => false, 'online' => false,
'status' => 'OFFLINE', 'status' => 'OFFLINE',
]; ];
foreach ($subDevicesData as $key => $subDeviceData) {
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']); if ($subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id'])) {
if ($lastRecord['execuded'] == 1){ foreach ($subDevicesData as $key => $subDeviceData) {
$tempDevice[$deviceId['id']]['online'] = true; $lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
$tempDevice[$deviceId['id']]['status'] = "SUCCESS"; if ($lastRecord != false && $lastRecord['execuded'] == 1) {
} else { $tempDevice[$deviceId['id']]['online'] = true;
$executed = 0; $tempDevice[$deviceId['id']]['status'] = "SUCCESS";
$waiting = 0; } else {
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) { $executed = 0;
if ($value['execuded'] == 1){ $waiting = 0;
$executed++; foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) {
} else { if ($value['execuded'] == 1) {
$waiting++; $executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed) {
$tempDevice[$deviceId['id']]['online'] = true;
} }
} }
if ($waiting < $executed){
$tempDevice[$deviceId['id']]['online'] = true; 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;
}
}
}
// $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++;
} }
} }
switch ($subDeviceData['type']) { if ($waiting < $executed) {
case 'temp_cont': $status = "PENDING";
$tempDevice[$deviceId['id']]['thermostatMode'] = 'off'; $online = true;
if ($lastRecord['value'] != 0) { }
$tempDevice[$deviceId['id']]['thermostatMode'] = 'heat'; // }
} $devices = $tempDevice;
$tempDevice[$deviceId['id']]['thermostatTemperatureAmbient'] = $lastRecord['value']; if (count($devices)> 1){
$tempDevice[$deviceId['id']]['thermostatTemperatureSetpoint'] = $lastRecord['value']; $devices[] = $tempDevice;
break; }
case 'vol_cont': }
$tempDevice[$deviceId['id']]['currentVolume'] = $lastRecord['value']; $response = [
break; 'requestId' => $requestId,
case 'media_apps': 'payload' => [
$tempDevice[$deviceId['id']]['currentApplication'] = "kodi"; 'devices' => $devices,
break; ],
case 'media_input': ];
$tempDevice[$deviceId['id']]['currentInput'] = "pc"; $apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
break; $apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
case 'media_status': $apiLogManager->setLevel(LOGLEVEL);
$tempDevice[$deviceId['id']]['activityState'] = "ACTIVE"; echo json_encode($response);
$tempDevice[$deviceId['id']]['playbackState'] = "PLAYING"; }
break;
case 'on/off': static function execute($requestId, $payload)
$tempDevice[$deviceId['id']]['on'] = ($lastRecord['value'] == 1 ? true : false); {
break; $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];
}
$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;
}
}
}
}
$response = [
'requestId' => $requestId,
'payload' => [
'commands' => $commands,
],
];
$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);
echo json_encode($response);
}
static function executeSwitch($subDeviceId, $executionCommand)
{
$value = 0;
$status = 'OFFLINE';
$online = false;
if ($executionCommand['params']['on']) $value = 1;
RecordManager::createWithSubId($subDeviceId, $value, 'google');
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1) {
$executed++;
} else {
$waiting++;
} }
} }
if ($lastRecord['execuded'] == 1){ if ($waiting < $executed) {
$status = "PENDING";
$online = true; $online = true;
$status = 'SUCCESS'; }
} else {
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'on' => $executionCommand['params']['on'],
'online' => $online,
],
];
return $commandTemp;
}
static function executeTermostatValue($subDeviceId, $executionCommand)
{
$value = 0;
$status = 'OFFLINE';
$online = false;
if (isset($executionCommand['params']['thermostatTemperatureSetpoint'])) {
$value = $executionCommand['params']['thermostatTemperatureSetpoint'];
}
RecordManager::createWithSubId($subDeviceId, $value, 'google');
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $lastValue) {
if ($lastValue['execuded'] == 1) {
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed) {
$status = "PENDING";
$online = true;;
}
$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;
}
static function executeTermostatMode($subDeviceId, $executionCommand)
{
$mode = "off";
$value = 0;
$status = 'OFFLINE';
$online = false;
if (isset($executionCommand['params']['thermostatMode']) && $executionCommand['params']['thermostatMode'] != 'off') {
$mode = $executionCommand['params']['thermostatMode'];
$value = RecordManager::getLastRecordNotNull($subDeviceId)['value'];
}
RecordManager::createWithSubId($subDeviceId, $value, 'google');
$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;
}
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'thermostatMode' => $mode,
'online' => $online,
],
];
return $commandTemp;
}
static function executeVolume($subDeviceId, $executionCommand)
{
//echo $executionCommand['params']['volumeLevel'];
$status = 'OFFLINE';
$online = false;
$currentVolume = RecordManager::getLastRecord($subDeviceId)['value'];
if (isset($executionCommand['params']['volumeLevel'])) {
RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['volumeLevel']);
$executed = 0; $executed = 0;
$waiting = 0; $waiting = 0;
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) { foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($value['execuded'] == 1){ if ($value['execuded'] == 1) {
$executed++; $executed++;
} else { } else {
$waiting++; $waiting++;
} }
} }
if ($waiting < $executed){ if ($waiting < $executed) {
$status = "PENDING"; $status = "PENDING";
$online = true; $online = true;
$currentVolume = $executionCommand['params']['volumeLevel'];
} }
} }
$devices = $tempDevice;
/*if (count($devices)> 1){ $deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$devices[] = $tempDevice; $commandTemp = [
}*/ 'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'currentVolume' => $currentVolume,
'online' => $online,
],
];
return $commandTemp;
} }
$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);
}
static function execute($requestId, $payload){ static function executeApp($subDeviceId, $executionCommand)
$commands = []; {
foreach ($payload['commands'] as $key => $command) { //echo $executionCommand['params']['newApplication'];
foreach ($command['devices'] as $key2 => $device) { $status = 'OFFLINE';
$executionCommand = $command['execution'][0]; $online = false;
if (isset($command['execution'][$key])) {
$executionCommand = $command['execution'][$key]; $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) {
$deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']); $status = "PENDING";
$subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)['subdevice_id']; $online = true;
switch ($executionCommand['command']) { $currentApplication = $executionCommand['params']['newApplication'];
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;
} }
} }
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'currentApplication' => $currentApplication,
'online' => $online,
],
];
return $commandTemp;
} }
$response = [
'requestId' => $requestId,
'payload' => [
'commands' => $commands,
],
];
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log'); static function executeInput($subDeviceId, $executionCommand)
$apiLogManager->setLevel(LOGLEVEL); {
$apiLogManager->write("[API][EXECUTE][$requestId]\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO); //echo $executionCommand['params']['newInput'];
$status = 'OFFLINE';
$online = false;
echo json_encode($response); $currentInput = RecordManager::getLastRecord($subDeviceId)['value'];
}
static function executeSwitch($subDeviceId, $executionCommand){ if (isset($executionCommand['params']['newInput'])) {
$value = 0; RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['newInput']);
$status = 'OFFLINE'; $executed = 0;
$online = false; $waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
if ($executionCommand['params']['on']) $value = 1; if ($value['execuded'] == 1) {
$executed++;
RecordManager::createWithSubId($subDeviceId, $value); } else {
$waiting++;
$executed = 0; }
$waiting = 0; }
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) { if ($waiting < $executed) {
if ($value['execuded'] == 1){ $status = "PENDING";
$executed++; $online = true;
} else { $currentInput = $executionCommand['params']['newInput'];
$waiting++; }
} }
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'currentInput' => $currentInput,
'online' => $online,
],
];
return $commandTemp;
} }
if ($waiting < $executed){ static function executeMediaCont($subDeviceId, $executionCommand)
$status = "PENDING"; {
$status = 'SUCCESS';
$online = true; $online = true;
}
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id']; $deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [ $commandTemp = [
'ids' => [(string) $deviceId], 'ids' => [(string) $deviceId],
'status' => $status, 'status' => $status,
'states' => [ 'states' => [
'on' => $executionCommand['params']['on'], 'online' => $online,
'online' => $online, ],
], ];
];
return $commandTemp; return $commandTemp;
} }
static function executeTermostatValue($subDeviceId, $executionCommand){
$value = 0;
$status = 'OFFLINE';
$online = false;
if (isset($executionCommand['params']['thermostatTemperatureSetpoint'])) {
$value = $executionCommand['params']['thermostatTemperatureSetpoint'];
}
RecordManager::createWithSubId($subDeviceId, $value);
$executed = 0;
$waiting = 0;
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $lastValue) {
if ($lastValue['execuded'] == 1){
$executed++;
} else {
$waiting++;
}
}
if ($waiting < $executed){
$status = "PENDING";
$online = true;;
}
$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;
}
static function executeTermostatMode($subDeviceId, $executionCommand){
$mode = "off";
$value = 0;
$status = 'OFFLINE';
$online = false;
if (isset($executionCommand['params']['thermostatMode']) && $executionCommand['params']['thermostatMode'] != 'off') {
$mode = $executionCommand['params']['thermostatMode'];
$value = RecordManager::getLastRecordNotNull($subDeviceId)['value'];
}
RecordManager::createWithSubId($subDeviceId, $value);
$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;
}
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
$commandTemp = [
'ids' => [(string) $deviceId],
'status' => $status,
'states' => [
'thermostatMode' => $mode,
'online' => $online,
],
];
return $commandTemp;
}
static function executeVolume($subDeviceId, $executionCommand){
//echo $executionCommand['params']['volumeLevel'];
$status = 'OFFLINE';
$online = false;
$currentVolume = RecordManager::getLastRecord($subDeviceId)['value'];
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'];
}
}
$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;
}
} }

View File

@@ -2,16 +2,21 @@
class RecordManager{ class RecordManager{
public static $records; public static $records;
public static function createWithSubId ($subDeviceId, $value) { public static function createWithSubId ($subDeviceId, $value, $origin = false) {
try { try {
$record = [ $record = [
'execuded' => 1, 'execuded' => 1,
]; ];
Db::edit ('records', $record, 'WHERE subdevice_id = ?', array ($subDeviceId)); Db::edit ('records', $record, 'WHERE subdevice_id = ?', array ($subDeviceId));
$record = array ( $record = array (
'subdevice_id' => $subDeviceId, 'subdevice_id' => $subDeviceId,
'value' => $value, 'value' => $value,
); );
if ($origin != false)
$record['Origin'] = $origin;
return Db::add ('records', $record); return Db::add ('records', $record);
} catch(PDOException $error) { } catch(PDOException $error) {
echo $error->getMessage(); echo $error->getMessage();
@@ -19,7 +24,7 @@ class RecordManager{
} }
} }
public static function create ($deviceId, $type, $value) { public static function create ($deviceId, $type, $value, $origin = false) {
$subDeviceId = Db::loadOne('SELECT * FROM subdevices WHERE device_id = ? AND type = ?;', array($deviceId, $type))['subdevice_id']; $subDeviceId = Db::loadOne('SELECT * FROM subdevices WHERE device_id = ? AND type = ?;', array($deviceId, $type))['subdevice_id'];
if ($subDeviceId == '') { if ($subDeviceId == '') {
return false; return false;
@@ -28,7 +33,7 @@ class RecordManager{
//Ochrana proti duplicitním hodnotám zapisují se jen změny //Ochrana proti duplicitním hodnotám zapisují se jen změny
$lastRecord = self::getLastRecord($subDeviceId, 1); $lastRecord = self::getLastRecord($subDeviceId, 1);
if ($lastRecord['value'] == $value){ if (isset($lastRecord['value']) && $lastRecord['value'] == $value){
return false; return false;
} }
@@ -42,6 +47,10 @@ class RecordManager{
'subdevice_id' => $subDeviceId, 'subdevice_id' => $subDeviceId,
'value' => $value, 'value' => $value,
); );
if ($origin != false)
$record['Origin'] = $origin;
return Db::add ('records', $record); return Db::add ('records', $record);
} catch(PDOException $error) { } catch(PDOException $error) {
echo $error->getMessage(); echo $error->getMessage();