CleanUp After Docker Try
This commit is contained in:
		
							
								
								
									
										25
									
								
								app/api/AuthApi.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								app/api/AuthApi.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
<?php
 | 
			
		||||
class AuthApi {
 | 
			
		||||
	public function login(){
 | 
			
		||||
		$token = (new AuthManager)->getToken($this->input->username,$this->input->password);
 | 
			
		||||
		if (!$token) {
 | 
			
		||||
			throw new Exception("Auth failed", 401);
 | 
			
		||||
		}
 | 
			
		||||
		$this->response(['token' => $token]);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function logout(){
 | 
			
		||||
		$authenticationBearrer = $_SERVER['HTTP_AUTHORIZATION'];
 | 
			
		||||
		if (!(new AuthManager)->deleteToken($authenticationBearrer)) {
 | 
			
		||||
			throw new Exception("logout Failed", 401);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function registration(){
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function restartPassword(){
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								app/api/DevicesApi.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								app/api/DevicesApi.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
class DevicesApi extends ApiController{
 | 
			
		||||
 | 
			
		||||
	public function default(){
 | 
			
		||||
		$this->requireAuth();
 | 
			
		||||
		$response = [];
 | 
			
		||||
 | 
			
		||||
		// TODO: process the request
 | 
			
		||||
 | 
			
		||||
		$this->response($response);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function getDevicesByRoom($roomId){
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								app/api/ExampleApi.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								app/api/ExampleApi.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
class ExampleApi extends ApiController{
 | 
			
		||||
 | 
			
		||||
	public function example(){
 | 
			
		||||
		// if this function should be accessible only for logged users uncomment next line
 | 
			
		||||
		// $this->requireAuth();
 | 
			
		||||
		// if user is logged in, next lines will be processed
 | 
			
		||||
		// otherwise script get terminated with 401 UNAUTHORIZED
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		// input data are stored in $this->input
 | 
			
		||||
		// in this example we just copy input to response
 | 
			
		||||
		$response = $this->input;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		// this method returns response as json
 | 
			
		||||
		$this->response($response);
 | 
			
		||||
		// you can specify returned http code by second optional parameter
 | 
			
		||||
		// default value is 200
 | 
			
		||||
		// $this->response($response, $httpCode);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										46
									
								
								app/api/GoogleHomeApi.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								app/api/GoogleHomeApi.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
<?php
 | 
			
		||||
class GoogleHomeApi {
 | 
			
		||||
	static function response(){
 | 
			
		||||
 | 
			
		||||
		$json = file_get_contents('php://input');
 | 
			
		||||
		$obj = json_decode($json, true);
 | 
			
		||||
 | 
			
		||||
		$apiLogManager = new LogManager('../logs/api/HA/'. date("Y-m-d").'.log');
 | 
			
		||||
		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);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
			case 'action.devices.QUERY':
 | 
			
		||||
			GoogleHome::query($obj['requestId'], $obj['inputs'][0]['payload']);
 | 
			
		||||
			//$apiLogManager->write("[Google Home] action.devices.QUERY", LogRecordType::INFO);
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
			case 'action.devices.EXECUTE':
 | 
			
		||||
			GoogleHome::execute($obj['requestId'], $obj['inputs'][0]['payload']);
 | 
			
		||||
			$apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordType::INFO);
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	static function autorize(){
 | 
			
		||||
		$json = file_get_contents('php://input');
 | 
			
		||||
		$obj = json_decode($json, true);
 | 
			
		||||
 | 
			
		||||
		$apiLogManager = new LogManager('../logs/api/HA/'. 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);
 | 
			
		||||
 | 
			
		||||
		$get = [
 | 
			
		||||
			"access_token"=>"2222255888",
 | 
			
		||||
			"token_type"=>"Bearer",
 | 
			
		||||
			"state"=>$_GET["state"],
 | 
			
		||||
		];
 | 
			
		||||
 | 
			
		||||
		echo $_GET["redirect_uri"] . '#' . http_build_query($get) ;
 | 
			
		||||
		echo '<a href="'.$_GET["redirect_uri"] . '#' . http_build_query($get) . '">FINISH</a>';
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								app/api/RoomsApi.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								app/api/RoomsApi.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
class RoomsApi extends ApiController{
 | 
			
		||||
 | 
			
		||||
	public function default(){
 | 
			
		||||
		//$this->requireAuth();
 | 
			
		||||
		$rooms = [];
 | 
			
		||||
		$roomsData = RoomManager::getAllRooms();
 | 
			
		||||
		foreach ($roomsData as $roomKey => $roomData) {
 | 
			
		||||
 | 
			
		||||
			$widgets = [];
 | 
			
		||||
			$devicesData = DeviceManager::getAllDevicesInRoom($roomData['room_id']);
 | 
			
		||||
			foreach ($devicesData as $deviceKey => $deviceData) {
 | 
			
		||||
 | 
			
		||||
				$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
 | 
			
		||||
				foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
 | 
			
		||||
 | 
			
		||||
					$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
 | 
			
		||||
					$widgets[] = [
 | 
			
		||||
						'subdevice_id' => $subDeviceData['subdevice_id'],
 | 
			
		||||
						'device_id' =>  $deviceData['device_id'],
 | 
			
		||||
						'name' => $deviceData['name'],
 | 
			
		||||
						'type' => $subDeviceData['type'],
 | 
			
		||||
						'icon' => $deviceData['icon'],
 | 
			
		||||
						'value' => $lastRecord['value'],
 | 
			
		||||
						'unit' => $subDeviceData['unit'],
 | 
			
		||||
					];
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			$rooms[] = [
 | 
			
		||||
				'room_id' => $roomData['room_id'],
 | 
			
		||||
				'name' => $roomData['name'],
 | 
			
		||||
				'widgets' => $widgets,
 | 
			
		||||
			];
 | 
			
		||||
		}
 | 
			
		||||
		$this->response($rooms);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user