OTA Tweeks

This commit is contained in:
JonatanRek 2020-07-14 19:59:18 +02:00
parent 8ed3ab2f39
commit 275aa97688
4 changed files with 118 additions and 116 deletions

View File

@ -62,6 +62,7 @@ $obj = json_decode($json, true);
$apiLogManager = new LogManager('../logs/api/'. date("Y-m-d").'.log'); $apiLogManager = new LogManager('../logs/api/'. date("Y-m-d").'.log');
$apiLogManager->write("[API] headers\n" . json_encode($_SERVER, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO); $apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API] POST body\n" . json_encode($_POST, JSON_PRETTY_PRINT), LogRecordType::INFO); $apiLogManager->write("[API] POST body\n" . json_encode($_POST, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordType::INFO); $apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordType::INFO);

View File

@ -33,8 +33,8 @@ $router->any('/api/HA', 'GoogleHomeApi@response');
//Endpoints API //Endpoints API
$router->post('/api/endpoint/', 'EndpointsApi@default'); $router->post('/api/endpoint/', 'EndpointsApi@default');
$router->post('/api/update/', 'EndpointsApi@update'); $router->any('/api/update/', 'EndpointsApi@update');
$router->post('/api/users/status', 'UsersApi@status'); $router->any('/api/users/status', 'UsersApi@status');
// examples // examples
$router->any('/api/example', 'ExampleApi@example'); $router->any('/api/example', 'ExampleApi@example');

View File

@ -1,6 +1,5 @@
<?php <?php
class EndpointsApi extends ApiController{ class EndpointsApi extends ApiController{
public function default(){ public function default(){
// $this->requireAuth(); // $this->requireAuth();
$obj = $this->input; $obj = $this->input;
@ -72,7 +71,7 @@ class EndpointsApi extends ApiController{
$this->response([ $this->response([
'state' => 'succes', 'state' => 'succes',
'command' => $command, 'command' => $command,
]); ], 200);
} }
// Issuing command // Issuing command
@ -177,11 +176,9 @@ class EndpointsApi extends ApiController{
$this->response($jsonAnswer); $this->response($jsonAnswer);
// this method returns response as json // this method returns response as json
} }
private function sendFile($path) private function sendFile($path) {
{
header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK', true, 200); header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK', true, 200);
header('Content-Type: application/octet-stream', true); header('Content-Type: application/octet-stream', true);
header('Content-Disposition: attachment; filename=' . basename($path)); header('Content-Disposition: attachment; filename=' . basename($path));
@ -215,7 +212,7 @@ class EndpointsApi extends ApiController{
if (file_exists($localBinary)) { if (file_exists($localBinary)) {
$logManager->write("[Updater] version PHP: \n" . md5_file($localBinary), LogRecordType::INFO); $logManager->write("[Updater] version PHP: \n" . md5_file($localBinary), LogRecordType::INFO);
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) { if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
$this->sendFile($localBinary); sendFile($localBinary);
//get device data //get device data
$device = DeviceManager::getDeviceByMac($macAddress); $device = DeviceManager::getDeviceByMac($macAddress);
$deviceName = $device['name']; $deviceName = $device['name'];

View File

@ -29,9 +29,13 @@ class ApiController {
} }
} }
protected function response($data = [], $httpCode = '200'){ protected function response($data = [], $httpCode = '200', $contentType = 'application/json', $jsonEncode = true){
header('Content-Type: application/json'); header('Content-Type: ' . $contentType);
http_response_code($httpCode); http_response_code($httpCode);
if ($jsonEncode) {
echo json_encode($data, JSON_UNESCAPED_UNICODE); echo json_encode($data, JSON_UNESCAPED_UNICODE);
} else {
echo $data;
}
} }
} }