2020-06-28 14:45:05 +00:00
< ? php
2021-04-05 08:43:30 +00:00
class EndpointsApi extends ApiController
{
public function default ()
{
2020-06-28 14:45:05 +00:00
// $this->requireAuth();
$obj = $this -> input ;
//variables Definition
$command = " null " ;
//Log
2021-04-05 08:43:30 +00:00
$logManager = new LogManager ( '../logs/api/' . date ( " Y-m-d " ) . '.log' );
2020-07-28 13:40:48 +00:00
$logManager -> setLevel ( LOGLEVEL );
2020-06-28 14:45:05 +00:00
//Token Checks
if ( $obj [ 'token' ] == null || ! isset ( $obj [ 'token' ])) {
$this -> response ([
'state' => 'unsuccess' ,
'errorMSG' => " Missing Value Token in JSON payload " ,
], 401 );
2020-07-14 16:24:21 +00:00
die ();
2020-06-28 14:45:05 +00:00
}
//Vstupní Checky
if ( ! DeviceManager :: registeret ( $obj [ 'token' ])) {
//Notification data setup
$notificationMng = new NotificationManager ;
$notificationData = [
'title' => 'Info' ,
'body' => 'New device Detected Found' ,
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png' ,
];
//Subdevice Registration
2020-10-05 19:12:06 +00:00
$device = DeviceManager :: create ( $obj [ 'token' ], $obj [ 'token' ]);
2020-06-28 14:45:05 +00:00
foreach ( $obj [ 'values' ] as $key => $value ) {
2020-10-05 19:12:06 +00:00
if ( ! SubDeviceManager :: getSubDeviceByMaster ( $device [ 'device_id' ], $key )) {
SubDeviceManager :: create ( $device [ 'device_id' ], $key , UNITS [ $key ]);
2020-06-28 14:45:05 +00:00
}
}
//Notification for newly added Device
if ( $notificationData != []) {
$subscribers = $notificationMng :: getSubscription ();
foreach ( $subscribers as $key => $subscriber ) {
2020-07-28 07:02:46 +00:00
$logManager -> write ( " [NOTIFICATION] SENDING TO " . $subscriber [ 'id' ] . " " , LogRecordTypes :: INFO );
2020-06-28 14:45:05 +00:00
$notificationMng :: sendSimpleNotification ( SERVERKEY , $subscriber [ 'token' ], $notificationData );
}
}
2020-07-28 07:02:46 +00:00
$logManager -> write ( " [API] Registering Device " , LogRecordTypes :: INFO );
2020-06-28 14:45:05 +00:00
$this -> response ([
'state' => 'unsuccess' ,
'errorMSG' => " Device not registeret " ,
], 401 );
}
if ( ! DeviceManager :: approved ( $obj [ 'token' ])) {
$this -> response ([
'state' => 'unsuccess' ,
'errorMSG' => " Unaproved Device " ,
], 401 );
}
2020-10-05 19:12:06 +00:00
$device = DeviceManager :: getDeviceByToken ( $obj [ 'token' ]);
2021-01-05 12:24:49 +00:00
DeviceManager :: setHeartbeat ( $device [ 'device_id' ]);
2020-10-05 19:12:06 +00:00
2020-07-28 13:40:48 +00:00
//Diagnostic
2021-04-05 08:43:30 +00:00
if ( isset ( $obj [ 'settings' ])) {
2020-06-28 14:45:05 +00:00
$data = [ 'mac' => $obj [ 'settings' ][ " network " ][ " mac " ], 'ip_address' => $obj [ 'settings' ][ " network " ][ " ip " ]];
if ( array_key_exists ( " firmware_hash " , $obj [ 'settings' ])) {
$data [ 'firmware_hash' ] = $obj [ 'settings' ][ " firmware_hash " ];
}
DeviceManager :: editByToken ( $obj [ 'token' ], $data );
2020-07-14 17:59:18 +00:00
}
2020-06-28 14:45:05 +00:00
2020-07-28 13:40:48 +00:00
//Log Data Save
2021-04-05 08:43:30 +00:00
if ( isset ( $obj [ 'logs' ])) {
2020-09-03 20:15:59 +00:00
foreach ( $obj [ 'logs' ] as $log ) {
2021-04-05 08:43:30 +00:00
$deviceLogManager = new LogManager ( '../logs/devices/' . date ( " Y-m-d " ) . '.log' );
2020-10-05 19:12:06 +00:00
$deviceLogManager -> setLevel ( LOGLEVEL );
2021-04-05 08:43:30 +00:00
if ( $log != 'HTTP_UPDATE_FAILD code-102 messageFile Not Found (404)' ) {
2021-02-15 13:03:15 +00:00
$deviceLogManager -> write ( " [Device Log Msg] Device_ID " . $device [ 'device_id' ] . " -> " . $log , LogRecordTypes :: ERROR );
}
2020-10-05 19:12:06 +00:00
unset ( $deviceLogManager );
2020-07-28 13:40:48 +00:00
}
2020-10-14 12:35:04 +00:00
$this -> response ([
'state' => 'succes' ,
'command' => $command ,
], 200 );
die ();
2020-07-28 13:40:48 +00:00
}
2020-07-14 17:59:18 +00:00
// Issuing command
2021-04-05 08:43:30 +00:00
if ( $command == " null " ) {
2020-07-14 17:59:18 +00:00
$deviceCommand = $device [ " command " ];
2021-04-05 08:43:30 +00:00
if ( $deviceCommand != '' && $deviceCommand != null && $deviceCommand != " null " ) {
2020-07-14 17:59:18 +00:00
$command = $deviceCommand ;
$data = [
2021-04-05 08:43:30 +00:00
'command' => 'null'
2020-07-14 17:59:18 +00:00
];
DeviceManager :: editByToken ( $obj [ 'token' ], $data );
2020-10-05 19:12:06 +00:00
$logManager -> write ( " [API] Device_ID " . $device [ 'device_id' ] . " executing command " . $command , LogRecordTypes :: INFO );
2020-07-14 17:59:18 +00:00
}
}
$jsonAnswer = [];
$subDeviceLastReordValue = [];
if ( isset ( $obj [ 'values' ])) {
//ZAPIS
foreach ( $obj [ 'values' ] as $key => $value ) {
2020-10-05 19:12:06 +00:00
if ( ! SubDeviceManager :: getSubDeviceByMaster ( $device [ 'device_id' ], $key )) {
SubDeviceManager :: create ( $device [ 'device_id' ], $key , UNITS [ $key ]);
2020-07-14 17:59:18 +00:00
}
2021-01-05 12:24:49 +00:00
2020-07-14 17:59:18 +00:00
$subDeviceLastReordValue [ $key ] = $value [ 'value' ];
2021-04-05 08:43:30 +00:00
RecordManager :: create ( $device [ 'device_id' ], $key , round ( $value [ 'value' ], 3 ), 'device' );
2020-10-05 19:12:06 +00:00
$logManager -> write ( " [API] Device_ID " . $device [ 'device_id' ] . " writed value " . $key . ' ' . $value [ 'value' ], LogRecordTypes :: INFO );
2020-07-04 18:07:40 +00:00
2020-07-14 17:59:18 +00:00
//notification
if ( $key == 'door' || $key == 'water' ) {
$notificationMng = new NotificationManager ;
$notificationData = [];
switch ( $key ) {
case 'door' :
$notificationData = [
'title' => 'Info' ,
2021-04-05 08:43:30 +00:00
'body' => 'Someone just open up ' . $device [ 'name' ],
2020-07-14 17:59:18 +00:00
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png' ,
];
2021-04-05 08:43:30 +00:00
break ;
2020-07-14 17:59:18 +00:00
case 'water' :
$notificationData = [
'title' => 'Alert' ,
2021-04-05 08:43:30 +00:00
'body' => 'Wather leak detected by ' . $device [ 'name' ],
2020-07-14 17:59:18 +00:00
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png' ,
];
2021-04-05 08:43:30 +00:00
break ;
2020-07-04 18:07:40 +00:00
}
2021-04-05 08:43:30 +00:00
if ( DEBUGMOD ) $notificationData [ 'body' ] .= ' value=' . $value [ 'value' ];
2020-07-14 17:59:18 +00:00
if ( $notificationData != []) {
$subscribers = $notificationMng :: getSubscription ();
foreach ( $subscribers as $key => $subscriber ) {
2020-07-28 07:02:46 +00:00
$logManager -> write ( " [NOTIFICATION] SENDING TO " . $subscriber [ 'id' ] . " " , LogRecordTypes :: INFO );
2020-07-14 17:59:18 +00:00
$notificationMng :: sendSimpleNotification ( SERVERKEY , $subscriber [ 'token' ], $notificationData );
2020-07-04 18:07:40 +00:00
}
}
}
2020-07-14 17:59:18 +00:00
}
2020-07-04 18:07:40 +00:00
2020-07-14 16:24:21 +00:00
2020-07-14 17:59:18 +00:00
//upravit format na setings-> netvork etc
2020-07-04 18:07:40 +00:00
2020-10-05 19:12:06 +00:00
$subDevicesTypeList = SubDeviceManager :: getSubDeviceSTypeForMater ( $device [ 'device_id' ]);
2020-07-14 17:59:18 +00:00
if ( ! in_array ( $subDevicesTypeList , [ 'on/off' , 'door' , 'water' ])) {
$jsonAnswer [ 'device' ][ 'sleepTime' ] = $device [ 'sleep_time' ];
}
} else {
2020-10-05 19:12:06 +00:00
if ( count ( SubDeviceManager :: getAllSubDevices ( $device [ 'device_id' ])) == 0 ) {
//SubDeviceManager::create($device['device_id'], 'on/off', UNITS[$key]);
//RecordManager::create($device['device_id'], 'on/off', 0);
2020-07-14 17:59:18 +00:00
}
2020-06-28 14:45:05 +00:00
2020-10-05 19:12:06 +00:00
$subDevicesData = SubDeviceManager :: getAllSubDevices ( $device [ 'device_id' ]);
2020-06-28 14:45:05 +00:00
2020-07-14 17:59:18 +00:00
foreach ( $subDevicesData as $key => $subDeviceData ) {
$subDeviceId = $subDeviceData [ 'subdevice_id' ];
$subDeviceLastReord = RecordManager :: getLastRecord ( $subDeviceId );
2021-04-05 08:43:30 +00:00
if ( ! empty ( $subDeviceLastReord )) {
2020-11-18 20:42:48 +00:00
$subDeviceLastReordValue [ $subDeviceData [ 'type' ]] = $subDeviceLastReord [ 'value' ];
2021-04-05 08:43:30 +00:00
if ( $subDeviceLastReord [ 'execuded' ] == 0 ) {
2020-11-18 20:42:48 +00:00
$logManager -> write ( " [API] subDevice_ID " . $subDeviceId . " executed comand with value " . json_encode ( $subDeviceLastReordValue ) . " executed " . $subDeviceLastReord [ 'execuded' ], LogRecordTypes :: INFO );
RecordManager :: setExecuted ( $subDeviceLastReord [ 'record_id' ]);
}
2020-06-28 14:45:05 +00:00
}
2020-07-14 16:24:21 +00:00
}
2020-07-14 17:59:18 +00:00
}
2020-06-28 14:45:05 +00:00
2020-07-14 17:59:18 +00:00
$hostname = " " ;
$hostname = strtolower ( $device [ 'name' ]);
$hostname = str_replace ( ' ' , '_' , $hostname );
2020-07-14 16:24:21 +00:00
2020-07-14 17:59:18 +00:00
$jsonAnswer [ 'device' ][ 'hostname' ] = $hostname ;
$jsonAnswer [ 'state' ] = 'succes' ;
$jsonAnswer [ 'values' ] = $subDeviceLastReordValue ;
$jsonAnswer [ 'command' ] = $command ;
2020-06-28 14:45:05 +00:00
2020-07-14 17:59:18 +00:00
$this -> response ( $jsonAnswer );
// this method returns response as json
2020-10-05 19:12:06 +00:00
//unset($logManager); //TODO: Opravit
die ();
2020-07-14 17:59:18 +00:00
}
2021-04-05 08:43:30 +00:00
protected function requireAuth ()
{
if ( isset ( $_SERVER [ 'HTTP_AUTHORIZATION' ])) {
// TODO: call appropriate class/method
$deviceManager = new DeviceManager ();
list ( $type , $hash ) = explode ( ' ' , $_SERVER [ 'HTTP_AUTHORIZATION' ]);
$this -> authenticated = $deviceManager -> approved ( $hash );
return $hash ;
if ( ! $this -> authenticated ) {
throw new Exception ( " Authorization required " , 401 );
}
} else {
throw new Exception ( " Authorization required " , 401 );
}
}
public function default_v2 ()
{
$token = $this -> requireAuth ();
$obj = $this -> input ;
$response = [];
$device = DeviceManager :: getDeviceByToken ( $token );
DeviceManager :: setHeartbeat ( $device [ 'device_id' ]);
// Issue command
$deviceCommand = $device [ " command " ];
if ( $deviceCommand != '' && $deviceCommand != null && $deviceCommand != " null " ) {
$response [ 'command' ] = $deviceCommand ;
DeviceManager :: editByToken ( $obj [ 'token' ], [ 'command' => 'null' ]);
}
$subDevicesData = SubDeviceManager :: getAllSubDevices ( $device [ 'device_id' ]);
if ( count ( $subDevicesData ) > 0 ) {
foreach ( $subDevicesData as $key => $subDeviceData ) {
$subDeviceId = $subDeviceData [ 'subdevice_id' ];
$subDeviceLastReord = RecordManager :: getLastRecord ( $subDeviceId );
if ( ! empty ( $subDeviceLastReord )) {
$response [ 'values' ][ $subDeviceData [ 'type' ]] = $subDeviceLastReord [ 'value' ];
if ( $subDeviceLastReord [ 'execuded' ] == 0 ) {
RecordManager :: setExecuted ( $subDeviceLastReord [ 'record_id' ]);
}
}
}
}
$this -> response ( $response , 200 );
}
public function cofiguration_v2 ()
{
$token = $this -> requireAuth ();
$response = [];
$device = DeviceManager :: getDeviceByToken ( $token );
DeviceManager :: setHeartbeat ( $device [ 'device_id' ]);
if ( ! empty ( $device [ " name " ]) && isset ( $device [ " name " ])) $response [ " nettwork " ][ 'hostname' ] = $this -> nameToHostname ( $device [ " name " ]);
if ( ! empty ( $device [ " ip_address " ]) && isset ( $device [ " ip_address " ])) $response [ " nettwork " ][ 'ip' ] = $device [ " ip_address " ];
if ( ! empty ( $device [ " gateway " ]) && isset ( $device [ " gateway " ])) $response [ " nettwork " ][ 'gateway' ] = $device [ " gateway " ];
if ( ! empty ( $device [ " dns " ]) && isset ( $device [ " dns " ])) $response [ " nettwork " ][ 'dns' ] = $device [ " dns " ];
if ( ! empty ( $device [ " sleep_time " ]) && isset ( $device [ " sleep_time " ])) $response [ " sleep " ] = $device [ " sleep_time " ];
$this -> response ( $response , 200 );
}
private function nameToHostname ( string $name = null )
{
$hostname = " " ;
$hostname = strtolower ( $name );
return str_replace ( ' ' , '_' , $hostname );
}
2020-07-14 16:24:21 +00:00
}