This commit is contained in:
Haitem 2020-03-16 17:09:07 +01:00
commit 3feeaab086
3 changed files with 48 additions and 15 deletions

43
api.php
View File

@ -31,6 +31,7 @@ if (!$restAcess){
//Log //Log
$logManager = new LogManager(); $logManager = new LogManager();
$apiLogManager = new LogManager('./app/logs/api/'. date("Y-m-d").'.log');
//DB Conector //DB Conector
Db::connect (DBHOST, DBUSER, DBPASS, DBNAME); Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
@ -39,19 +40,23 @@ Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
$json = file_get_contents('php://input'); $json = file_get_contents('php://input');
$obj = json_decode($json, true); $obj = json_decode($json, true);
if (defined(DEBUGMOD) && DEBUGMOD == 1) { //Log RAW api request
$logManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO); if (API_DEBUGMOD == 1) {
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
} }
//zabespecit proti Ddosu //zabespecit proti Ddosu
if (isset($obj['user']) && $obj['user'] != ''){ if (isset($obj['user']) && $obj['user'] != ''){
//user at home //user at home
$user = UserManager::getUser($obj['user']); $user = UserManager::getUser($obj['user']);
$userAtHome = $user['atHome'];
if (!empty($user)) { if (!empty($user)) {
$userId = $user['user_id']; $userId = $user['user_id'];
$atHome = $obj['atHome']; $atHome = $obj['atHome'];
if($userAtHome != $atHome){
UserManager::atHome($userId, $atHome); UserManager::atHome($userId, $atHome);
$logManager->write("[Record] user " . $userId . " changet his home state to " . $atHome . " " . RECORDTIMOUT , LogRecordType::INFO); $logManager->write("[USER] user " . $userId . " changet his home state to " . $atHome , LogRecordType::INFO);
}
echo 'Saved: ' . $atHome; echo 'Saved: ' . $atHome;
header($_SERVER["SERVER_PROTOCOL"]." 200 OK"); header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
die(); die();
@ -92,6 +97,7 @@ try {
$token = $obj['token']; $token = $obj['token'];
$values = null; $values = null;
$settings = null; $settings = null;
$deviceLogs = null;
$command = "null"; $command = "null";
if (isset($obj['values'])) { if (isset($obj['values'])) {
@ -102,6 +108,12 @@ if (isset($obj['settings'])) {
$settings = $obj['settings']; $settings = $obj['settings'];
} }
if (isset($obj['logs'])) {
$deviceLogs = $obj['logs'];
}
//Checks //Checks
if ($token == null || $token == "") { if ($token == null || $token == "") {
echo json_encode(array( echo json_encode(array(
@ -162,7 +174,7 @@ if (!DeviceManager::approved($token)) {
} }
// Diagnostic Data Write to DB // Diagnostic Data Write to DB
if ($settings != null || $settings != ""){ if ($settings != null && $settings != ""){
$data = ['mac' => $settings["network"]["mac"], 'ip_address' => $settings["network"]["ip"]]; $data = ['mac' => $settings["network"]["mac"], 'ip_address' => $settings["network"]["ip"]];
if (array_key_exists("firmware_hash", $settings)) { if (array_key_exists("firmware_hash", $settings)) {
$data['firmware_hash'] = $settings["firmware_hash"]; $data['firmware_hash'] = $settings["firmware_hash"];
@ -175,20 +187,33 @@ if ($command == "null"){
$device = DeviceManager::getDeviceByToken($token); $device = DeviceManager::getDeviceByToken($token);
$deviceId = $device['device_id']; $deviceId = $device['device_id'];
$deviceCommand = $device["command"]; $deviceCommand = $device["command"];
if ($deviceCommand != '' || $deviceCommand != null) if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null")
{ {
$command = $deviceCommand; $command = $deviceCommand;
}
$data = [ $data = [
'command'=>'null' 'command'=>'null'
]; ];
DeviceManager::editByToken($token, $data); DeviceManager::editByToken($token, $data);
$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO); $logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO);
} }
}
// 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,
];
echo json_encode($jsonAnswer, JSON_PRETTY_PRINT);
header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
die();
}
// Subdevices first data! // Subdevices first data!
if ($values != null || $values != "") { if ($values != null && $values != "") {
//ZAPIS //ZAPIS
$device = DeviceManager::getDeviceByToken($token); $device = DeviceManager::getDeviceByToken($token);
@ -268,7 +293,7 @@ if ($values != null || $values != "") {
$subDeviceLastReordValue = $subDeviceLastReord['value']; $subDeviceLastReordValue = $subDeviceLastReord['value'];
if ($subDeviceLastReord['execuded'] == 0){ 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']); RecordManager::setExecuted($subDeviceLastReord['record_id']);
} }

View File

@ -46,7 +46,9 @@ class LogManager
function write($value, $type = LogRecordType::ERROR){ function write($value, $type = LogRecordType::ERROR){
$record = "[".date("H:m:s")."][".$type."]" . $value . "\n"; $record = "[".date("H:m:s")."][".$type."]" . $value . "\n";
if (strlen($record) > 65 ) {
$record = Utilities::stringInsert($record,"\n",65); $record = Utilities::stringInsert($record,"\n",65);
}
fwrite($this->logFile, $record); fwrite($this->logFile, $record);
} }

View File

@ -61,8 +61,14 @@ if (file_exists($localBinary)) {
$logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordType::INFO); $logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordType::INFO);
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) { if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
sendFile($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 //notification
$deviceName = DeviceManager::getDeviceByMac($macAddress)['name'];
$notificationMng = new NotificationManager; $notificationMng = new NotificationManager;
$notificationData = [ $notificationData = [
'title' => 'Info', 'title' => 'Info',