Bether Loggig Class
This commit is contained in:
		@@ -3,5 +3,6 @@ class CronApi extends ApiController {
 | 
			
		||||
    public function clean(){
 | 
			
		||||
        $logKeeper = new LogMaintainer();
 | 
			
		||||
        $logKeeper->purge(LOGTIMOUT);
 | 
			
		||||
        $this->response(['Value' => 'OK']);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -9,6 +9,7 @@ class EndpointsApi extends ApiController{
 | 
			
		||||
 | 
			
		||||
		//Log
 | 
			
		||||
		$logManager = new LogManager();
 | 
			
		||||
		$apiLogManager->setLevel(LOGLEVEL);
 | 
			
		||||
		$apiLogManager = new LogManager('../logs//api/'. date("Y-m-d").'.log');
 | 
			
		||||
 | 
			
		||||
		//Token Checks
 | 
			
		||||
@@ -42,12 +43,12 @@ class EndpointsApi extends ApiController{
 | 
			
		||||
			if ($notificationData != []) {
 | 
			
		||||
				$subscribers = $notificationMng::getSubscription();
 | 
			
		||||
				foreach ($subscribers as $key => $subscriber) {
 | 
			
		||||
					$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
 | 
			
		||||
					$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordTypes::INFO);
 | 
			
		||||
					$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			$logManager->write("[API] Registering Device", LogRecordType::INFO);
 | 
			
		||||
			$logManager->write("[API] Registering Device", LogRecordTypes::INFO);
 | 
			
		||||
			$this->response([
 | 
			
		||||
				'state' => 'unsuccess',
 | 
			
		||||
				'errorMSG' => "Device not registeret",
 | 
			
		||||
@@ -86,7 +87,7 @@ class EndpointsApi extends ApiController{
 | 
			
		||||
					'command'=>'null'
 | 
			
		||||
				];
 | 
			
		||||
				DeviceManager::editByToken($obj['token'], $data);
 | 
			
		||||
				$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO);
 | 
			
		||||
				$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordTypes::INFO);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -103,7 +104,7 @@ class EndpointsApi extends ApiController{
 | 
			
		||||
				}
 | 
			
		||||
				$subDeviceLastReordValue[$key] = $value['value'];
 | 
			
		||||
				RecordManager::create($deviceId, $key, round($value['value'],3));
 | 
			
		||||
				$logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordType::INFO);
 | 
			
		||||
				$logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO);
 | 
			
		||||
 | 
			
		||||
				//notification
 | 
			
		||||
				if ($key == 'door' || $key == 'water') {
 | 
			
		||||
@@ -131,7 +132,7 @@ class EndpointsApi extends ApiController{
 | 
			
		||||
					if ($notificationData != []) {
 | 
			
		||||
						$subscribers = $notificationMng::getSubscription();
 | 
			
		||||
						foreach ($subscribers as $key => $subscriber) {
 | 
			
		||||
							$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
 | 
			
		||||
							$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordTypes::INFO);
 | 
			
		||||
							$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
@@ -159,7 +160,7 @@ class EndpointsApi extends ApiController{
 | 
			
		||||
				$subDeviceLastReordValue[$subDeviceData['type']] = $subDeviceLastReord['value'];
 | 
			
		||||
 | 
			
		||||
				if ($subDeviceLastReord['execuded'] == 0){
 | 
			
		||||
					$logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " . json_encode($subDeviceLastReordValue) ." executed " . $subDeviceLastReord['execuded'], LogRecordType::INFO);
 | 
			
		||||
					$logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " . json_encode($subDeviceLastReordValue) ." executed " . $subDeviceLastReord['execuded'], LogRecordTypes::INFO);
 | 
			
		||||
					RecordManager::setExecuted($subDeviceLastReord['record_id']);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,25 +6,27 @@ class GoogleHomeApi{
 | 
			
		||||
		$obj = json_decode($json, true);
 | 
			
		||||
 | 
			
		||||
		$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
 | 
			
		||||
		$apiLogManager->setLevel(LOGLEVEL);
 | 
			
		||||
 | 
			
		||||
		header('Content-Type: application/json');
 | 
			
		||||
 | 
			
		||||
		switch ($obj['inputs'][0]['intent']) {
 | 
			
		||||
			case 'action.devices.SYNC':
 | 
			
		||||
			GoogleHome::sync($obj['requestId']);
 | 
			
		||||
			$apiLogManager->write("[Google Home] action.devices.SYNC", LogRecordType::INFO);
 | 
			
		||||
			$apiLogManager->write("[Google Home] action.devices.SYNC", LogRecordTypes::INFO);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
			case 'action.devices.QUERY':
 | 
			
		||||
			GoogleHome::query($obj['requestId'], $obj['inputs'][0]['payload']);
 | 
			
		||||
			$apiLogManager->write("[Google Home] action.devices.QUERY", LogRecordType::INFO);
 | 
			
		||||
			$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
 | 
			
		||||
			$apiLogManager->write("[Google Home] action.devices.QUERY", LogRecordTypes::INFO);
 | 
			
		||||
			$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
			case 'action.devices.EXECUTE':
 | 
			
		||||
 | 
			
		||||
			GoogleHome::execute($obj['requestId'], $obj['inputs'][0]['payload']);
 | 
			
		||||
			$apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordType::INFO);
 | 
			
		||||
			$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
 | 
			
		||||
			$apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordTypes::INFO);
 | 
			
		||||
			$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
 | 
			
		||||
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
@@ -35,8 +37,10 @@ class GoogleHomeApi{
 | 
			
		||||
		$obj = json_decode($json, true);
 | 
			
		||||
 | 
			
		||||
		$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
 | 
			
		||||
		$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
 | 
			
		||||
		$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordType::INFO);
 | 
			
		||||
		$apiLogManager->setLevel(LOGLEVEL);
 | 
			
		||||
		
 | 
			
		||||
		$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
 | 
			
		||||
		$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
 | 
			
		||||
 | 
			
		||||
		$get = [
 | 
			
		||||
			"access_token"=>"2222255888",
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@ class UpdatesApi {
 | 
			
		||||
    public function default(){
 | 
			
		||||
		 header('Content-type: text/plain; charset=utf8', true);
 | 
			
		||||
        $logManager = new LogManager('../logs/ota/'. date("Y-m-d").'.log');
 | 
			
		||||
        $logManager->setLevel(LOGLEVEL);
 | 
			
		||||
 | 
			
		||||
        //Filtrování IP adress
 | 
			
		||||
       	/* if (DEBUGMOD != 1) {
 | 
			
		||||
@@ -21,7 +22,7 @@ class UpdatesApi {
 | 
			
		||||
                    'errorMSG' => "Using API from your IP insnt alowed!",
 | 
			
		||||
                ));
 | 
			
		||||
                header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
 | 
			
		||||
                $logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::WARNING);
 | 
			
		||||
                $logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordTypes::WARNING);
 | 
			
		||||
                exit();
 | 
			
		||||
            }
 | 
			
		||||
        }*/
 | 
			
		||||
@@ -29,10 +30,10 @@ class UpdatesApi {
 | 
			
		||||
        $macAddress = $_SERVER['HTTP_X_ESP8266_STA_MAC'];
 | 
			
		||||
		  $localBinary = "../updater/" . str_replace(':', '', $macAddress) . ".bin";
 | 
			
		||||
 | 
			
		||||
        $logManager->write("[Updater] url: " . $localBinary, LogRecordType::INFO);
 | 
			
		||||
        $logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
 | 
			
		||||
        $logManager->write("[Updater] url: " . $localBinary, LogRecordTypes::INFO);
 | 
			
		||||
        $logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordTypes::INFO);
 | 
			
		||||
        if (file_exists($localBinary)) {
 | 
			
		||||
            $logManager->write("[Updater] version PHP: \n" . md5_file($localBinary), LogRecordType::INFO);
 | 
			
		||||
            $logManager->write("[Updater] version PHP: \n" . md5_file($localBinary), LogRecordTypes::INFO);
 | 
			
		||||
            if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
 | 
			
		||||
                $this->sendFile($localBinary);
 | 
			
		||||
                //get device data
 | 
			
		||||
@@ -40,8 +41,8 @@ class UpdatesApi {
 | 
			
		||||
                $deviceName = $device['name'];
 | 
			
		||||
                $deviceId = $device['device_id'];
 | 
			
		||||
                //logfile write
 | 
			
		||||
                $logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordType::WARNING);
 | 
			
		||||
                $logManager->write("[Device] version hash: \n" . md5_file($localBinary), LogRecordType::INFO);
 | 
			
		||||
                $logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordTypes::WARNING);
 | 
			
		||||
                $logManager->write("[Device] version hash: \n" . md5_file($localBinary), LogRecordTypes::INFO);
 | 
			
		||||
                //notification
 | 
			
		||||
                $notificationMng = new NotificationManager;
 | 
			
		||||
                $notificationData = [
 | 
			
		||||
@@ -52,7 +53,7 @@ class UpdatesApi {
 | 
			
		||||
                if ($notificationData != []) {
 | 
			
		||||
                    $subscribers = $notificationMng->getSubscription();
 | 
			
		||||
                    foreach ($subscribers as $key => $subscriber) {
 | 
			
		||||
                        $logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordType::INFO);
 | 
			
		||||
                        $logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordTypes::INFO);
 | 
			
		||||
                        $answer = $notificationMng->sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
@@ -10,9 +10,10 @@ class UpdatesApi {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function default(){
 | 
			
		||||
		 header('Content-type: text/plain; charset=utf8', true);
 | 
			
		||||
        header('Content-type: text/plain; charset=utf8', true);
 | 
			
		||||
        $logManager = new LogManager('../logs/ota/'. date("Y-m-d").'.log');
 | 
			
		||||
		  $logManager->write("[Updater] Client Connected", LogRecordType::WARNING);
 | 
			
		||||
        $logManager->setLevel(LOGLEVEL);
 | 
			
		||||
        $logManager->write("[Updater] Client Connected", LogRecordTypes::WARNING);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        //Filtrování IP adress
 | 
			
		||||
@@ -23,17 +24,17 @@ class UpdatesApi {
 | 
			
		||||
                    'errorMSG' => "Using API from your IP insnt alowed!",
 | 
			
		||||
                ));
 | 
			
		||||
                header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
 | 
			
		||||
                $logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::INFO);
 | 
			
		||||
                $logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordTypes::INFO);
 | 
			
		||||
                exit();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $macAddress = $_SERVER['HTTP_X_ESP8266_STA_MAC'];
 | 
			
		||||
        $localBinary = "../updater/" . str_replace(':', '', $macAddress) . ".bin";
 | 
			
		||||
        $logManager->write("[Updater] url: " . $localBinary, LogRecordType::INFO);
 | 
			
		||||
        $logManager->write("[Updater] version:      " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
 | 
			
		||||
        $logManager->write("[Updater] url: " . $localBinary, LogRecordTypes::INFO);
 | 
			
		||||
        $logManager->write("[Updater] version:      " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordTypes::INFO);
 | 
			
		||||
        if (file_exists($localBinary)) {
 | 
			
		||||
            $logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordType::INFO);
 | 
			
		||||
            $logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordTypes::INFO);
 | 
			
		||||
            if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
 | 
			
		||||
                $this->sendFile($localBinary);
 | 
			
		||||
                //get device data
 | 
			
		||||
@@ -41,8 +42,8 @@ class UpdatesApi {
 | 
			
		||||
                $deviceName = $device['name'];
 | 
			
		||||
                $deviceId = $device['device_id'];
 | 
			
		||||
                //logfile write
 | 
			
		||||
                $logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordType::WARNING);
 | 
			
		||||
                $logManager->write("[Device] version hash: " . md5_file($localBinary), LogRecordType::INFO);
 | 
			
		||||
                $logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordTypes::WARNING);
 | 
			
		||||
                $logManager->write("[Device] version hash: " . md5_file($localBinary), LogRecordTypes::INFO);
 | 
			
		||||
                //notification
 | 
			
		||||
                $notificationMng = new NotificationManager;
 | 
			
		||||
                $notificationData = [
 | 
			
		||||
@@ -53,7 +54,7 @@ class UpdatesApi {
 | 
			
		||||
                if ($notificationData != []) {
 | 
			
		||||
                    $subscribers = $notificationMng->getSubscription();
 | 
			
		||||
                    foreach ($subscribers as $key => $subscriber) {
 | 
			
		||||
                        $logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordType::INFO);
 | 
			
		||||
                        $logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordTypes::INFO);
 | 
			
		||||
                        $answer = $notificationMng->sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user