diff --git a/.gitignore b/.gitignore
index f90b54e..d8b4f59 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ _nemazat/index.html
_nemazat/css/main.css.map
_nemazat/css/main.css
_nemazat/css/font-awesome.min.css
+logs/*.log
\ No newline at end of file
diff --git a/api.php b/api.php
index 7960ed7..9a4063b 100644
--- a/api.php
+++ b/api.php
@@ -11,41 +11,55 @@ foreach (["class", "views"] as $dir) {
}
}
+//Log
+$logManager = new LogManager();
+
//DB Conector
Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
-//Filtrování IP adress
-/*if (DEBUGMOD != 1) {
-if (!in_array($_SERVER['REMOTE_ADDR'], HOMEIP)) {
-echo json_encode(array(
-'state' => 'unsuccess',
-'errorMSG' => "Using API from your IP insn´t alowed!",
-));
-header("HTTP/1.1 401 Unauthorized");
-exit();
-}
-}*/
-
-
-
//Read API data
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
+
+//zabespecit proti Ddosu
if (isset($obj['user']) && $obj['user'] != ''){
//user at home
$user = UserManager::getUser($obj['user']);
$userId = $user['user_id'];
- UserManager::atHome($userId, $obj['location']);
+ $keyWords = ['entered', 'connected', 'connected to'];
+ UserManager::atHome($userId, $obj['atHome']);
+ echo 'saved';
die();
}
+//Filtrování IP adress
+if (DEBUGMOD != 1) {
+ if (!in_array($_SERVER['REMOTE_ADDR'], HOMEIP)) {
+ echo json_encode(array(
+ 'state' => 'unsuccess',
+ 'errorMSG' => "Using API from your IP insnt alowed!",
+ ));
+ header("HTTP/1.1 401 Unauthorized");
+ $logManager->write("[API] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::WARNING);
+ exit();
+ }
+}
+
//automationExecution
-AutomationManager::executeAll();
+try {
+ AutomationManager::executeAll();
+} catch (\Exception $e) {
+ $logManager->write("[Automation] Something happen during automation execution", LogRecordType::ERROR);
+}
//Record Cleaning
-RecordManager::clean(RECORDTIMOUT);
+try {
+ RecordManager::clean(RECORDTIMOUT);
+} catch (\Exception $e) {
+ $logManager->write("[Record] cleaning record older that" . RECORDTIMOUT , LogRecordType::ERROR);
+}
//Variables
$token = $obj['token'];
$values = null;
@@ -72,6 +86,7 @@ if (!DeviceManager::registeret($token)) {
'state' => 'unsuccess',
'errorMSG' => "Device not registeret",
));
+ $logManager->write("[API] Registering Device", LogRecordType::INFO);
exit();
}
@@ -84,16 +99,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 != "") {
@@ -105,6 +110,7 @@ if ($values != null || $values != "") {
SubDeviceManager::create($deviceId, $key, UNITS[$key]);
}
RecordManager::create($deviceId, $key, round($value['value'],2));
+ $logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . $value['value'], LogRecordType::INFO);
}
$hostname = strtolower($device['name']);
@@ -117,7 +123,6 @@ if ($values != null || $values != "") {
'state' => 'succes',
));
header("HTTP/1.1 200 OK");
- die();
} else {
//Vypis
//TODO: doděla uložení výpisu jinými slovy zda li byl comman vykonán
@@ -126,15 +131,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'];
- RecordManager::setExecuted($subDeviceLastReord['record_id']);
+ if ($subDeviceLastReord['execuded'] == 0){
+ $logManager->write("[API] 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' => [
@@ -145,5 +152,8 @@ if ($values != null || $values != "") {
'value' => $subDeviceLastReordValue
));
header("HTTP/1.1 200 OK");
- die();
}
+
+ unset($logManager);
+ Db::disconect();
+ die();
diff --git a/class/AutomationManager.php b/class/AutomationManager.php
index d71ec63..65706bc 100644
--- a/class/AutomationManager.php
+++ b/class/AutomationManager.php
@@ -73,13 +73,40 @@ class AutomationManager{
$restart = true;
}
}
- }
+ } else if ($onValue['type'] == 'outHome') {
- if ($onValue == 'outHome') {
+ } else if ($onValue['type'] == 'inHome') {
- }
-
- if ($onValue == 'inHome') {
+ } else if ($onValue['type'] == 'noOneHome') {
+ $users = UserManager::getUsers();
+ $membersHome = 0;
+ foreach ($users as $key => $user) {
+ if ($user['at_home'] == 'true'){
+ $membersHome++;
+ }
+ }
+ if ($membersHome == 0 && $automation['executed'] == 0) {
+ $run = true;
+ } else if ($membersHome > 0 && $automation['executed'] == 1){
+ $restart = true;
+ }
+ } else if ($onValue['type'] == 'someOneHome') {
+ $users = UserManager::getUsers();
+ $membersHome = 0;
+ foreach ($users as $key => $user) {
+ if ($user['at_home'] == 'true'){
+ $membersHome++;
+ }
+ }
+ if ($membersHome == 0 && $automation['executed'] == 1) {
+ $restart = true;
+ } else if ($membersHome > 0 && $automation['executed'] == 0){
+ $run = true;
+ }
+ /*echo "Someone Home". '
';
+ echo "at home" . $membersHome. '
';
+ echo "run" . $run. '
';
+ echo "restart" . $restart. '
';*/
}
@@ -89,8 +116,10 @@ class AutomationManager{
foreach ($sceneDoArray as $deviceId => $deviceState) {
RecordManager::create($deviceId, 'on/off', $deviceState);
}
+ $logManager->write("[AUTOMATIONS] automation id ". $automation['automation_id'] . " was executed");
Db::edit('automation', array('executed' => 1), 'WHERE automation_id = ?', array($automation['automation_id']));
} else if ($restart) {
+ $logManager->write("[AUTOMATIONS] automation id ". $automation['automation_id'] . " was restarted");
Db::edit('automation', array('executed' => 0), 'WHERE automation_id = ?', array($automation['automation_id']));
}
}
diff --git a/class/DB.php b/class/DB.php
index 8134907..4308385 100644
--- a/class/DB.php
+++ b/class/DB.php
@@ -19,6 +19,10 @@ class Db{
}
}
+ public static function disconect(){
+ self::$join = null;
+ }
+
public static function loadOne ($sql, $values = array (), $numberKey = false) {
$answer = self::$join->prepare ($sql);
$answer->execute ($values);
diff --git a/class/LogManager.php b/class/LogManager.php
new file mode 100644
index 0000000..678524d
--- /dev/null
+++ b/class/LogManager.php
@@ -0,0 +1,38 @@
+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);
+ }
+ }
+}
diff --git a/lang/cs.php b/lang/cs.php
index c574793..b7e3af3 100644
--- a/lang/cs.php
+++ b/lang/cs.php
@@ -63,8 +63,12 @@ $lang = [
'w_icon' => 'Ikona',
'w_no' => 'žádná',
'w_noOne' => 'Nikdo',
+ 'w_someOne' => 'Někdo',
'w_room' => 'Místnost',
'w_moduls' => 'Moduly',
+ 'w_home' => 'Doma',
+ 'w_neni' => 'Není',
+ 'w_is' => 'je',
//example
'' => '',
diff --git a/lang/en.php b/lang/en.php
index 77251f8..cfb2369 100644
--- a/lang/en.php
+++ b/lang/en.php
@@ -63,8 +63,12 @@ $lang = [
'w_icon' => 'Icon',
'w_no' => 'no',
'w_noOne' => 'noone',
+ 'w_someOne' => 'Some',
'w_room' => 'Room',
'w_moduls' => 'Moduls',
+ 'w_home' => 'Home',
+ 'w_neni' => 'At',
+ 'w_is' => 'is',
//example
'' => '',
diff --git a/templates/dashboard.phtml b/templates/dashboard.phtml
index 0ab0229..747e310 100644
--- a/templates/dashboard.phtml
+++ b/templates/dashboard.phtml
@@ -48,7 +48,7 @@