diff --git a/app/api/EndpointsApi.php b/app/api/EndpointsApi.php index 05a4558..6648f40 100644 --- a/app/api/EndpointsApi.php +++ b/app/api/EndpointsApi.php @@ -31,10 +31,10 @@ class EndpointsApi extends ApiController{ ]; //Subdevice Registration - $deviceId = DeviceManager::create($obj['token'], $obj['token']); + $device = DeviceManager::create($obj['token'], $obj['token']); foreach ($obj['values'] as $key => $value) { - if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) { - SubDeviceManager::create($deviceId, $key, UNITS[$key]); + if (!SubDeviceManager::getSubDeviceByMaster($device['device_id'], $key)) { + SubDeviceManager::create($device['device_id'], $key, UNITS[$key]); } } @@ -61,6 +61,8 @@ class EndpointsApi extends ApiController{ ], 401); } + $device = DeviceManager::getDeviceByToken($obj['token']); + //Diagnostic if (isset($obj['settings'])){ $data = ['mac' => $obj['settings']["network"]["mac"], 'ip_address' => $obj['settings']["network"]["ip"]]; @@ -77,17 +79,15 @@ class EndpointsApi extends ApiController{ //Log Data Save if (isset($obj['logs'])){ foreach ($obj['logs'] as $log) { - $logManager = new LogManager('../logs/devices/'. date("Y-m-d").'.log'); - $logManager->setLevel(LOGLEVEL); - $logManager->write("[Device Log Msg] Device_ID " . $deviceId . "->" . $log, LogRecordTypes::ERROR); - unset($logManager); + $deviceLogManager = new LogManager('../logs/devices/'. date("Y-m-d").'.log'); + $deviceLogManager->setLevel(LOGLEVEL); + $deviceLogManager->write("[Device Log Msg] Device_ID " . $device['device_id'] . "->" . $log, LogRecordTypes::ERROR); + unset($deviceLogManager); } } // Issuing command if ($command == "null"){ - $device = DeviceManager::getDeviceByToken($obj['token']); - $deviceId = $device['device_id']; $deviceCommand = $device["command"]; if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null") { @@ -96,24 +96,22 @@ class EndpointsApi extends ApiController{ 'command'=>'null' ]; DeviceManager::editByToken($obj['token'], $data); - $logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordTypes::INFO); + $logManager->write("[API] Device_ID " . $device['device_id'] . " executing command " . $command, LogRecordTypes::INFO); } } $jsonAnswer = []; $subDeviceLastReordValue = []; - $device = DeviceManager::getDeviceByToken($obj['token']); - $deviceId = $device['device_id']; if (isset($obj['values'])) { //ZAPIS foreach ($obj['values'] as $key => $value) { - if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) { - SubDeviceManager::create($deviceId, $key, UNITS[$key]); + if (!SubDeviceManager::getSubDeviceByMaster($device['device_id'], $key)) { + SubDeviceManager::create($device['device_id'], $key, UNITS[$key]); } $subDeviceLastReordValue[$key] = $value['value']; - RecordManager::create($deviceId, $key, round($value['value'],3)); - $logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO); + RecordManager::create($device['device_id'], $key, round($value['value'],3)); + $logManager->write("[API] Device_ID " . $device['device_id'] . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO); //notification if ($key == 'door' || $key == 'water') { @@ -151,17 +149,17 @@ class EndpointsApi extends ApiController{ //upravit format na setings-> netvork etc - $subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($deviceId); + $subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($device['device_id']); if (!in_array($subDevicesTypeList, ['on/off', 'door', 'water'])) { $jsonAnswer['device']['sleepTime'] = $device['sleep_time']; } } else { - if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) { - //SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]); - //RecordManager::create($deviceId, 'on/off', 0); + if (count(SubDeviceManager::getAllSubDevices($device['device_id'])) == 0) { + //SubDeviceManager::create($device['device_id'], 'on/off', UNITS[$key]); + //RecordManager::create($device['device_id'], 'on/off', 0); } - $subDevicesData = SubDeviceManager::getAllSubDevices($deviceId); + $subDevicesData = SubDeviceManager::getAllSubDevices($device['device_id']); foreach ($subDevicesData as $key => $subDeviceData) { $subDeviceId = $subDeviceData['subdevice_id']; @@ -186,6 +184,7 @@ class EndpointsApi extends ApiController{ $this->response($jsonAnswer); // this method returns response as json - unset($logManager); + //unset($logManager); //TODO: Opravit + die(); } } diff --git a/app/api/UsersApi.php b/app/api/UsersApi.php index bbaeb53..0f15e09 100644 --- a/app/api/UsersApi.php +++ b/app/api/UsersApi.php @@ -16,4 +16,22 @@ class UsersApi extends ApiController{ $this->response($response); } + + public function status(){ + //$this->requireAuth(); + $response = null; + $obj = $this->input; + $atHome = $obj['atHome']; + + $user = UserManager::getUser($obj['user']); + $userAtHome = $user['at_home']; + $userId = $user['user_id']; + + if (!empty($user)) { + if($userAtHome != $atHome){ + UserManager::atHome($userId, $atHome); + } + } + $this->response(['value'=>'OK']); + } } diff --git a/app/models/managers/RoomManager.php b/app/models/managers/RoomManager.php index b747393..b279cde 100644 --- a/app/models/managers/RoomManager.php +++ b/app/models/managers/RoomManager.php @@ -3,7 +3,7 @@ class RoomManager{ public static $rooms; static function getDefaultRoomId() { - $defaultRoom = Db::loadOne("SELECT room_id FROM rooms WHERE 'default' = 1"); + $defaultRoom = Db::loadOne("SELECT `room_id` FROM `rooms` WHERE `default` = 1"); return $defaultRoom['room_id']; } diff --git a/app/models/managers/UserManager.php b/app/models/managers/UserManager.php index a65b56e..312dc7a 100644 --- a/app/models/managers/UserManager.php +++ b/app/models/managers/UserManager.php @@ -3,7 +3,7 @@ class UserManager { public static function getUsers ($filtr = ['*']) { try { - $allUsers = Db::loadAll ("SELECT " . implode($filtr, ",") . " FROM users"); + $allUsers = Db::loadAll ("SELECT " . implode(",", $filtr) . " FROM users"); return $allUsers; } catch(PDOException $error) { echo $error->getMessage(); diff --git a/app/plugins/Spotify.php b/app/plugins/Spotify.php index bfef6ab..1c27b55 100644 --- a/app/plugins/Spotify.php +++ b/app/plugins/Spotify.php @@ -16,9 +16,7 @@ class Spotify extends VirtualDeviceManager { } public function callback(){ - $token = $_GET; - $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; - var_dump($actual_link); + var_dump($_REQUEST); (new SettingsManager)->create('spotify_token', $token); } diff --git a/app/views/Setting.php b/app/views/Setting.php index 4c8836f..4982903 100644 --- a/app/views/Setting.php +++ b/app/views/Setting.php @@ -3,7 +3,6 @@ class Setting extends Template { function __construct() { - $userManager = new UserManager(); $langMng = new LanguageManager('en'); @@ -57,6 +56,7 @@ class Setting extends Template $template->prepare('rooms', $rooms); + $template->render(); } }