Log Function added

This commit is contained in:
JonatanRek 2019-08-30 17:29:34 +02:00
parent 568fd6c59a
commit 031b35937d
3 changed files with 52 additions and 13 deletions

25
api.php
View File

@ -11,6 +11,9 @@ foreach (["class", "views"] as $dir) {
} }
} }
//Log
$logManager = new LogManager();
//DB Conector //DB Conector
Db::connect (DBHOST, DBUSER, DBPASS, DBNAME); Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
@ -38,6 +41,7 @@ if (DEBUGMOD != 1) {
'errorMSG' => "Using API from your IP insn´t alowed!", 'errorMSG' => "Using API from your IP insn´t alowed!",
)); ));
header("HTTP/1.1 401 Unauthorized"); header("HTTP/1.1 401 Unauthorized");
$logManager->write("acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::WARNING);
exit(); exit();
} }
} }
@ -74,6 +78,7 @@ if (!DeviceManager::registeret($token)) {
'state' => 'unsuccess', 'state' => 'unsuccess',
'errorMSG' => "Device not registeret", 'errorMSG' => "Device not registeret",
)); ));
$logManager->write("Registering Device", LogRecordType::INFO);
exit(); exit();
} }
@ -86,16 +91,6 @@ if (!DeviceManager::approved($token)) {
exit(); exit();
} }
if (!DeviceManager::approved($token)) {
header("HTTP/1.1 401 Unauthorized");
echo json_encode(array(
'state' => 'unsuccess',
'errorMSG' => "Unaproved Device",
));
exit();
}
// Subdevices first data! // Subdevices first data!
if ($values != null || $values != "") { if ($values != null || $values != "") {
@ -107,6 +102,7 @@ if ($values != null || $values != "") {
SubDeviceManager::create($deviceId, $key, UNITS[$key]); SubDeviceManager::create($deviceId, $key, UNITS[$key]);
} }
RecordManager::create($deviceId, $key, round($value['value'],2)); RecordManager::create($deviceId, $key, round($value['value'],2));
$logManager->write("Device_ID " . $deviceId . " writed value " . $key . $value['value'], LogRecordType::INFO);
} }
$hostname = strtolower($device['name']); $hostname = strtolower($device['name']);
@ -127,15 +123,17 @@ if ($values != null || $values != "") {
if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) { if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) {
SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]); SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]);
RecordManager::create($deviceId, 'on/off', 0); //RecordManager::create($deviceId, 'on/off', 0);
} }
$subDeviceId = SubDeviceManager::getAllSubDevices($deviceId)[0]['subdevice_id']; $subDeviceId = SubDeviceManager::getAllSubDevices($deviceId)[0]['subdevice_id'];
$subDeviceLastReord = RecordManager::getLastRecord($subDeviceId); $subDeviceLastReord = RecordManager::getLastRecord($subDeviceId);
$subDeviceLastReordValue = $subDeviceLastReord['value']; $subDeviceLastReordValue = $subDeviceLastReord['value'];
RecordManager::setExecuted($subDeviceLastReord['record_id']); if ($subDeviceLastReord['execuded'] == 0){
$logManager->write("subDevice id ".$subDeviceId . " executed comand with value " .$subDeviceLastReordValue . " record id " . $subDeviceLastReord['record_id'] . " executed " . $subDeviceLastReord['execuded']);
RecordManager::setExecuted($subDeviceLastReord['record_id']);
}
echo json_encode(array( echo json_encode(array(
'device' => [ 'device' => [
@ -148,5 +146,6 @@ if ($values != null || $values != "") {
header("HTTP/1.1 200 OK"); header("HTTP/1.1 200 OK");
} }
unset($logManager);
Db::disconect(); Db::disconect();
die(); die();

38
class/LogManager.php Normal file
View File

@ -0,0 +1,38 @@
<?php
/**
*
*/
class LogRecordType{
const WARNING = 'warning';
const ERROR = 'error';
const INFO = 'info';
}
class LogManager
{
private $logFile;
function __construct($fileName = "")
{
if ($fileName == ""){
$fileName = './logs/'. date("Y-m-d").'.log';
}
if(!is_dir("./logs/"))
{
mkdir("./logs/");
}
$this->logFile = fopen($fileName, "a") or die("Unable to open file!");
}
function write($value, $type = LogRecordType::ERROR){
$record = "[".date("H:m:s")."][".$type."]" .$value . "\n";
fwrite($this->logFile, $record);
}
function __destruct(){
if (isset($this->logFile)) {
fclose($this->logFile);
}
}
}

View File

@ -43,6 +43,8 @@ $userManager = new UserManager();
if (isset($_POST['username']) && isset($_POST['password']) ) { if (isset($_POST['username']) && isset($_POST['password']) ) {
$userManager->login($_POST['username'], $_POST['password'], $_POST['remember']); $userManager->login($_POST['username'], $_POST['password'], $_POST['remember']);
} }
$logManager = new LogManager();
/* /*
$form = new Form('name','1','POST',''); $form = new Form('name','1','POST','');
$form->addInput(InputTypes::TEXT,'nadpis','','Label',''); $form->addInput(InputTypes::TEXT,'nadpis','','Label','');