From f570556d85cc0bc3e156eba7fe8c072deae2b3b1 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Mon, 16 Mar 2020 14:52:36 +0100 Subject: [PATCH 1/4] Bether Log + Log Wia API --- api.php | 38 +++++++++++++++++++++++++++++--------- app/class/LogManager.php | 4 +++- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/api.php b/api.php index 904395b..4416c9e 100644 --- a/api.php +++ b/api.php @@ -92,6 +92,7 @@ try { $token = $obj['token']; $values = null; $settings = null; +$deviceLogs = null; $command = "null"; if (isset($obj['values'])) { @@ -102,6 +103,12 @@ if (isset($obj['settings'])) { $settings = $obj['settings']; } +if (isset($obj['logs'])) { + $deviceLogs = $obj['logs']; +} + + + //Checks if ($token == null || $token == "") { echo json_encode(array( @@ -162,7 +169,7 @@ if (!DeviceManager::approved($token)) { } // Diagnostic Data Write to DB -if ($settings != null || $settings != ""){ +if ($settings != null && $settings != ""){ $data = ['mac' => $settings["network"]["mac"], 'ip_address' => $settings["network"]["ip"]]; if (array_key_exists("firmware_hash", $settings)) { $data['firmware_hash'] = $settings["firmware_hash"]; @@ -175,20 +182,33 @@ if ($command == "null"){ $device = DeviceManager::getDeviceByToken($token); $deviceId = $device['device_id']; $deviceCommand = $device["command"]; - if ($deviceCommand != '' || $deviceCommand != null) + if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null") { - $command = $deviceCommand; + $command = $deviceCommand; + $data = [ + 'command'=>'null' + ]; + DeviceManager::editByToken($token, $data); + $logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO); } +} - $data = [ - 'command'=>'null' +// Diagnostic Logs Write To log File +if ($deviceLogs != null && $deviceLogs != ""){ + foreach ($deviceLogs as $log) { + $logManager->write("[Device Log Msg] Device_ID " . $deviceId . "->" . $log, LogRecordType::ERROR); + } + $jsonAnswer = [ + 'state' => 'succes', + 'command' => $command, ]; - DeviceManager::editByToken($token, $data); - $logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO); + echo json_encode($jsonAnswer, JSON_PRETTY_PRINT); + header($_SERVER["SERVER_PROTOCOL"]." 200 OK"); + die(); } // Subdevices first data! -if ($values != null || $values != "") { +if ($values != null && $values != "") { //ZAPIS $device = DeviceManager::getDeviceByToken($token); @@ -268,7 +288,7 @@ if ($values != null || $values != "") { $subDeviceLastReordValue = $subDeviceLastReord['value']; if ($subDeviceLastReord['execuded'] == 0){ - $logManager->write("[API] subDevice id ".$subDeviceId . " executed comand with value " .$subDeviceLastReordValue . " record id " . $subDeviceLastReord['record_id'] . " executed " . $subDeviceLastReord['execuded']); + $logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " .$subDeviceLastReordValue . " record id " . $subDeviceLastReord['record_id'] . " executed " . $subDeviceLastReord['execuded']); RecordManager::setExecuted($subDeviceLastReord['record_id']); } diff --git a/app/class/LogManager.php b/app/class/LogManager.php index 95f82f4..8376315 100644 --- a/app/class/LogManager.php +++ b/app/class/LogManager.php @@ -46,7 +46,9 @@ class LogManager function write($value, $type = LogRecordType::ERROR){ $record = "[".date("H:m:s")."][".$type."]" . $value . "\n"; - $record = Utilities::stringInsert($record,"\n",65); + if (strlen($record) > 65 ) { + $record = Utilities::stringInsert($record,"\n",65); + } fwrite($this->logFile, $record); } From 801e26dda98fa0a65dc8b684409793756cf9f4e7 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Mon, 16 Mar 2020 15:03:43 +0100 Subject: [PATCH 2/4] Updater Log Message + api tweeks --- api.php | 2 +- update.php | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/api.php b/api.php index 4416c9e..438ec6e 100644 --- a/api.php +++ b/api.php @@ -288,7 +288,7 @@ if ($values != null && $values != "") { $subDeviceLastReordValue = $subDeviceLastReord['value']; if ($subDeviceLastReord['execuded'] == 0){ - $logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " .$subDeviceLastReordValue . " record id " . $subDeviceLastReord['record_id'] . " executed " . $subDeviceLastReord['execuded']); + $logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " .$subDeviceLastReordValue . " record id " . $subDeviceLastReord['record_id'] . " executed " . $subDeviceLastReord['execuded'], LogRecordType::INFO); RecordManager::setExecuted($subDeviceLastReord['record_id']); } diff --git a/update.php b/update.php index cecc76a..0fb3acb 100644 --- a/update.php +++ b/update.php @@ -61,8 +61,14 @@ if (file_exists($localBinary)) { $logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordType::INFO); if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) { sendFile($localBinary); + //get device data + $device = DeviceManager::getDeviceByMac($macAddress); + $deviceName = $device['name']; + $deviceId = $device['device_id']; + //logfile write + $logManager->write("[Device] device_ID " . $deviceId . "was just updated to new version", LogRecordType::WARNING); + $logManager->write("[Device] version hash:" . md5_file($localBinary), LogRecordType::INFO); //notification - $deviceName = DeviceManager::getDeviceByMac($macAddress)['name']; $notificationMng = new NotificationManager; $notificationData = [ 'title' => 'Info', From 24072cb5820a2cf218f4878f8c8e7ecb8f51a35d Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Mon, 16 Mar 2020 15:07:44 +0100 Subject: [PATCH 3/4] Some tweeks --- api.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/api.php b/api.php index 438ec6e..510b3cf 100644 --- a/api.php +++ b/api.php @@ -47,11 +47,14 @@ if (defined(DEBUGMOD) && DEBUGMOD == 1) { if (isset($obj['user']) && $obj['user'] != ''){ //user at home $user = UserManager::getUser($obj['user']); + $userAtHome = $user['atHome']; if (!empty($user)) { $userId = $user['user_id']; $atHome = $obj['atHome']; - UserManager::atHome($userId, $atHome); - $logManager->write("[Record] user " . $userId . " changet his home state to " . $atHome . " " . RECORDTIMOUT , LogRecordType::INFO); + if($userAtHome != $atHome){ + UserManager::atHome($userId, $atHome); + $logManager->write("[USER] user " . $userId . " changet his home state to " . $atHome , LogRecordType::INFO); + } echo 'Saved: ' . $atHome; header($_SERVER["SERVER_PROTOCOL"]." 200 OK"); die(); From 8ab0b043f9feefef404faeec9f0220f7da1cf2e3 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Mon, 16 Mar 2020 16:34:37 +0100 Subject: [PATCH 4/4] Api Impruvement --- api.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/api.php b/api.php index 510b3cf..c9fb12f 100644 --- a/api.php +++ b/api.php @@ -31,6 +31,7 @@ if (!$restAcess){ //Log $logManager = new LogManager(); +$apiLogManager = new LogManager('./app/logs/api/'. date("Y-m-d").'.log'); //DB Conector Db::connect (DBHOST, DBUSER, DBPASS, DBNAME); @@ -39,8 +40,9 @@ Db::connect (DBHOST, DBUSER, DBPASS, DBNAME); $json = file_get_contents('php://input'); $obj = json_decode($json, true); -if (defined(DEBUGMOD) && DEBUGMOD == 1) { - $logManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO); +//Log RAW api request +if (API_DEBUGMOD == 1) { + $apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO); } //zabespecit proti Ddosu