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

23
api.php
View File

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