diff --git a/README.md b/README.md index 40009ad..60ff847 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,6 @@ API connector for Sonoff/ewelink devices using simple webapi based on OAuth2. PHP 7.4+, no other dependiencies required. -Current version from branch **main** should be operative, but not yet tested in 100% and in active development, so please keep this in mind. -For pre released versions look at recent released [Tagged versions](https://github.com/PJanisio/ewelinkApiPhp/tags) - ## Current features - login and authorization to ewelink (with refresh token) @@ -50,15 +47,14 @@ try { $deviceId = '100xxxxxx'; // Replace with your actual device ID // Turn on the device - $setStatusResult = $devices->setDeviceStatus($deviceId, ['switch' => 'on']); - echo '

Turn Device On Result

'; - echo '
' . print_r($setStatusResult, true) . '
'; + $param = ['switch' => 'on']; + $setStatusResult = $devices->setDeviceStatus($deviceId, $param); } else { //if we have no token, or we are not authorized paste link to authorization $loginUrl = $httpClient->getLoginUrl(); - echo 'Login with OAuth'; + echo 'Authorize ewelinkApiPhp'; } } catch (Exception $e) { echo 'Error: ' . $e->getMessage(); @@ -92,7 +88,6 @@ Index.php works as a gateway to API and also as a debug for all availablemethods ## More inside info about current development -- released version is expected to be ready on **01.07.2024** latest - main branch when commited used to be operative. - with next stable release methods and invoking functions structure can be changed (keep this in mind) - branch **websockets** will probably be not maintened anymore diff --git a/autoloader.php b/autoloader.php index 4abe2e4..4ddd309 100644 --- a/autoloader.php +++ b/autoloader.php @@ -1,5 +1,13 @@ Live Device Parameter'; echo '
' . print_r($liveResult, true) . '
'; - // Example usage of refreshDeviceStatusLive - $refreshResult = $devices->refreshDeviceStatusLive($deviceId); - echo '

Refresh Device Status Live

'; + // Example usage of getAllDeviceParamLive + $refreshResult = $devices->getAllDeviceParamLive($deviceId); + echo '

Get All Device Parameters Live

'; echo '
' . print_r($refreshResult, true) . '
'; // Example usage of setDeviceStatus for multi-channel device @@ -63,7 +71,7 @@ // Example usage of setDeviceStatus for single-channel device $singleChannelDeviceId = '10011015b6'; - $singleChannelParams = ['bright' => 80]; + $singleChannelParams = ['switch' => 'off']; $setStatusResultSingle = $devices->setDeviceStatus($singleChannelDeviceId, $singleChannelParams); echo '

Set Single-Channel Device Status Result

'; echo '
' . print_r($setStatusResultSingle, true) . '
'; @@ -104,6 +112,6 @@ } } else { $loginUrl = $httpClient->getLoginUrl(); - echo 'Login with OAuth'; + echo 'Authorize ewelinkApiPhp'; } } diff --git a/src/Constants.php b/src/Constants.php index 176a3e1..d18547f 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -1,5 +1,13 @@ $deviceId, @@ -219,7 +227,7 @@ public function refreshDeviceStatusLive($deviceId, $type = 1) { */ public function setDeviceStatus($deviceId, $params) { $device = $this->getDeviceById($deviceId); - $currentParams = $device['params'] ?? null; + $currentParams = $this->getAllDeviceParamLive($deviceId) ?? null; // Use getAllDeviceParamLive to get current state if ($currentParams === null) { return "Device $deviceId does not have any parameters to update."; diff --git a/src/Home.php b/src/Home.php index a41d4ed..3216f9e 100644 --- a/src/Home.php +++ b/src/Home.php @@ -1,5 +1,13 @@ validateConstants(); + $errors = []; + foreach ($validationResults as $key => $result) { + if (!$result['is_valid']) { + $errors[] = "Invalid {$key}: " . $result['value']; + } + } + if (!empty($errors)) { + foreach ($errors as $error) { + echo "

$error

"; + } + exit; + } + $this->region = Constants::REGION; // Assign region from Constants $this->loginUrl = $this->createLoginUrl('ewelinkapiphp'); // Default state diff --git a/src/Token.php b/src/Token.php index a2b0432..66d7ca6 100644 --- a/src/Token.php +++ b/src/Token.php @@ -1,5 +1,13 @@ $file, + 'validation' => $isValidJson, + 'creation_time' => $creationTime + ]; + } + + return $results; + } + + /** + * Validate the constants in the Constants class. + * + * @return array The validation results for REDIRECT_URL, EMAIL, and REGION. + */ + public function validateConstants() { + $results = []; + + // Validate REDIRECT_URL + $url = Constants::REDIRECT_URL; + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_NOBODY, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); // Set timeout to 5 seconds + curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + $results['REDIRECT_URL'] = [ + 'value' => $url, + 'is_valid' => $httpCode >= 200 && $httpCode < 400 // Consider 2xx and 3xx responses as valid + ]; + + // Validate EMAIL + $email = Constants::EMAIL; + $results['EMAIL'] = [ + 'value' => $email, + 'is_valid' => filter_var($email, FILTER_VALIDATE_EMAIL) !== false + ]; + + // Validate REGION + $region = Constants::REGION; + $validRegions = ['cn', 'us', 'eu', 'as']; + $results['REGION'] = [ + 'value' => $region, + 'is_valid' => in_array($region, $validRegions) + ]; + + return $results; + } }