2019-08-23 11:39:42 +00:00
< ? php
/** Includes **/
include_once ( './config.php' );
//Autoloader
2019-09-19 12:48:31 +00:00
$files = scandir ( './app/class/' );
$files = array_diff ( $files , array (
'.' ,
'..' ,
'app' ,
'ChartJS.php' ,
'ChartJS_Line.php' ,
'ChartManager.php' ,
'DashboardManager.php' ,
'Partial.php' ,
'Form.php' ,
'Route.php' ,
'Template.php' ,
'Ajax.php' ,
));
foreach ( $files as $file ) {
include './app/class/' . $file ;
}
//Allow acces only wia Curl, Ajax ETC
$restAcess = 'XMLHttpRequest' == ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ] ? ? '' );
if ( ! $restAcess ){
header ( 'Location: ./' );
2019-08-23 11:39:42 +00:00
}
2019-08-30 15:29:34 +00:00
//Log
$logManager = new LogManager ();
2019-08-23 11:39:42 +00:00
//DB Conector
Db :: connect ( DBHOST , DBUSER , DBPASS , DBNAME );
//Read API data
$json = file_get_contents ( 'php://input' );
$obj = json_decode ( $json , true );
2019-08-25 12:09:08 +00:00
//zabespecit proti Ddosu
2019-08-23 11:39:42 +00:00
if ( isset ( $obj [ 'user' ]) && $obj [ 'user' ] != '' ){
//user at home
$user = UserManager :: getUser ( $obj [ 'user' ]);
2019-09-01 15:36:46 +00:00
if ( ! empty ( $user )) {
$userId = $user [ 'user_id' ];
2019-09-19 12:48:31 +00:00
$atHome = $obj [ 'atHome' ];
UserManager :: atHome ( $userId , $atHome );
$logManager -> write ( " [Record] user " . $userId . " changet his home state to " . $atHome . RECORDTIMOUT , LogRecordType :: WARNING );
echo 'Saved: ' . $atHome ;
2019-09-04 17:09:36 +00:00
header ( " HTTP/1.1 200 OK " );
die ();
2019-09-01 15:36:46 +00:00
}
2019-08-23 11:39:42 +00:00
}
2019-08-25 12:07:01 +00:00
//Filtrování IP adress
2019-08-25 12:09:08 +00:00
if ( DEBUGMOD != 1 ) {
2019-08-25 12:07:01 +00:00
if ( ! in_array ( $_SERVER [ 'REMOTE_ADDR' ], HOMEIP )) {
echo json_encode ( array (
'state' => 'unsuccess' ,
2019-09-01 13:01:57 +00:00
'errorMSG' => " Using API from your IP insnt alowed! " ,
2019-08-25 12:07:01 +00:00
));
header ( " HTTP/1.1 401 Unauthorized " );
2019-08-30 15:44:22 +00:00
$logManager -> write ( " [API] acces denied from " . $_SERVER [ 'REMOTE_ADDR' ], LogRecordType :: WARNING );
2019-08-25 12:07:01 +00:00
exit ();
}
2019-08-25 12:09:08 +00:00
}
2019-08-25 12:07:01 +00:00
2019-08-23 11:39:42 +00:00
//automationExecution
2019-09-01 13:01:57 +00:00
try {
AutomationManager :: executeAll ();
} catch ( \Exception $e ) {
$logManager -> write ( " [Automation] Something happen during automation execution " , LogRecordType :: ERROR );
}
2019-08-23 11:39:42 +00:00
//Record Cleaning
2019-09-01 13:01:57 +00:00
try {
RecordManager :: clean ( RECORDTIMOUT );
} catch ( \Exception $e ) {
2019-09-19 12:48:31 +00:00
$logManager -> write ( " [Record] cleaning record older that " . RECORDTIMOUT , LogRecordType :: ERROR );
2019-08-23 11:39:42 +00:00
2019-09-01 13:01:57 +00:00
}
2019-08-23 11:39:42 +00:00
//Variables
$token = $obj [ 'token' ];
$values = null ;
if ( isset ( $obj [ 'values' ])) {
$values = $obj [ 'values' ];
}
//Checks
if ( $token == null || $token == " " ) {
echo json_encode ( array (
'state' => 'unsuccess' ,
'errorMSG' => " Missing Value Token in JSON payload " ,
));
header ( " HTTP/1.1 401 Unauthorized " );
die ();
}
//Vstupní Checky
if ( ! DeviceManager :: registeret ( $token )) {
2019-10-09 08:53:52 +00:00
$deviceId = DeviceManager :: create ( $token , $token );
foreach ( $values as $key => $value ) {
if ( ! SubDeviceManager :: getSubDeviceByMaster ( $deviceId , $key )) {
SubDeviceManager :: create ( $deviceId , $key , UNITS [ $key ]);
}
}
2019-08-23 11:39:42 +00:00
header ( " HTTP/1.1 401 Unauthorized " );
echo json_encode ( array (
'state' => 'unsuccess' ,
'errorMSG' => " Device not registeret " ,
));
2019-08-30 15:44:22 +00:00
$logManager -> write ( " [API] Registering Device " , LogRecordType :: INFO );
2019-08-23 11:39:42 +00:00
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 != " " ) {
//ZAPIS
$device = DeviceManager :: getDeviceByToken ( $token );
$deviceId = $device [ 'device_id' ];
foreach ( $values as $key => $value ) {
if ( ! SubDeviceManager :: getSubDeviceByMaster ( $deviceId , $key )) {
SubDeviceManager :: create ( $deviceId , $key , UNITS [ $key ]);
}
RecordManager :: create ( $deviceId , $key , round ( $value [ 'value' ], 2 ));
2019-09-19 12:48:31 +00:00
$logManager -> write ( " [API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value [ 'value' ], LogRecordType :: INFO );
2019-08-23 11:39:42 +00:00
}
$hostname = strtolower ( $device [ 'name' ]);
$hostname = str_replace ( ' ' , '_' , $hostname );
echo json_encode ( array (
'device' => [
'hostname' => $hostname ,
'sleepTime' => $device [ 'sleep_time' ],
],
'state' => 'succes' ,
));
header ( " HTTP/1.1 200 OK " );
} else {
//Vypis
//TODO: doděla uložení výpisu jinými slovy zda li byl comman vykonán
$device = DeviceManager :: getDeviceByToken ( $token );
$deviceId = $device [ 'device_id' ];
if ( count ( SubDeviceManager :: getAllSubDevices ( $deviceId )) == 0 ) {
SubDeviceManager :: create ( $deviceId , 'on/off' , UNITS [ $key ]);
2019-08-30 15:29:34 +00:00
//RecordManager::create($deviceId, 'on/off', 0);
2019-08-23 11:39:42 +00:00
}
$subDeviceId = SubDeviceManager :: getAllSubDevices ( $deviceId )[ 0 ][ 'subdevice_id' ];
$subDeviceLastReord = RecordManager :: getLastRecord ( $subDeviceId );
$subDeviceLastReordValue = $subDeviceLastReord [ 'value' ];
2019-08-30 15:29:34 +00:00
if ( $subDeviceLastReord [ 'execuded' ] == 0 ){
2019-08-30 15:44:22 +00:00
$logManager -> write ( " [API] subDevice id " . $subDeviceId . " executed comand with value " . $subDeviceLastReordValue . " record id " . $subDeviceLastReord [ 'record_id' ] . " executed " . $subDeviceLastReord [ 'execuded' ]);
2019-08-30 15:29:34 +00:00
RecordManager :: setExecuted ( $subDeviceLastReord [ 'record_id' ]);
}
2019-08-23 11:39:42 +00:00
echo json_encode ( array (
'device' => [
'hostname' => $device [ 'name' ],
'sleepTime' => $device [ 'sleep_time' ],
],
'state' => 'succes' ,
'value' => $subDeviceLastReordValue
));
header ( " HTTP/1.1 200 OK " );
}
2019-08-25 12:07:01 +00:00
2019-08-30 15:29:34 +00:00
unset ( $logManager );
2019-08-25 12:07:01 +00:00
Db :: disconect ();
die ();