From c1384e1aaaa3aecf810251890d98299513fb7344 Mon Sep 17 00:00:00 2001 From: Pawel Janisio Date: Fri, 14 Jun 2024 22:45:59 +0200 Subject: [PATCH] 1.15.14 (pre release) - updated index.php to single channel update params - updated README --- README.md | 101 ++++++++++++++++++++++++++++++++++++++++++++---- index.php | 10 +++-- src/Devices.php | 3 +- 3 files changed, 101 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index bf765ee..5f36243 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ API connector for Sonoff/ewelink devices using simple webapi based on OAuth2. PHP 7.4+, no other dependiencies required. -Current version from branch **main** is operative, but not yet tested in 100% and in active development, so feedback is appreciated **create issues / PR** if needed. +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 @@ -12,8 +13,9 @@ Current version from branch **main** is operative, but not yet tested in 100% an - get devices list with all parameters - saving devices and other outputs from API to .json - get any value of parameter for each device (f.e switch status, productName, MAC etc.) -- update parameter of device (switch on, off) +- set parameter of device (switch on, off) - check if device has MultiChannel support +- set parameter for Multichannel devices ## Public key and secret @@ -21,6 +23,93 @@ Generate here: [dev.ewelink](https://dev.ewelink.cc/) And put your keys and redirect Url in **Constants.php** +## Example of main features + +This is a long content example of almost all main features. If you want to get all of them, run **index.php** in your root directory. + +```php +redirectToUrl(Constants::REDIRECT_URL); + //if not we either needs token, or we are still not logged +} else { + + //time to get token and start requests to API + if ($token->checkAndRefreshToken()) { + $tokenData = $token->getTokenData(); + //at this place you should have token.json in you directory + //its neccesary to get familyID before we contiunue with devices + $home = $httpClient->getHome(); + $familyData = $home->fetchFamilyData(); + + //lets initialize devices class and get all data displayed, because why not + $devices = new Devices($httpClient); + $devicesData = $devices->fetchDevicesData(); + echo '

Devices Data

'; + echo '
' . print_r($devicesData, true) . '
'; + + //that was a complete output, now for better readibility lets focus on devicesId + //which we will use in next operations: + + $devicesList = $devices->getDevicesList(); + echo '

Devices List

'; + echo '
' . print_r($devicesList, true) . '
'; + + //if you want to search through the device parameters and get the value of search, use this functio + $searchKey = 'productModel'; // example key to search for + $deviceId = '100xxxxxx'; // example device ID + + //search and output + $searchResult = $devices->searchDeviceParam($searchKey, $deviceId); + echo '

Search Result

'; + echo '
' . print_r($searchResult, true) . '
'; + + + //Set the device status (f.e turn off the device) + $singleChannelDeviceId = '100xxxxxxxx'; + $singleChannelParams = ['switch' => 'off']; + + //get output + $setStatusResultSingle = $devices->setDeviceStatus($singleChannelDeviceId, $singleChannelParams); + echo '

Set Single-Channel Device Status Result

'; + echo '
' . print_r($setStatusResultSingle, true) . '
'; + + + // Example usage of setDeviceStatus for multi-channel device + $multiChannelDeviceId = '100xxxxxxxx'; + $multiChannelParams = [ + ['switch' => 'off', 'outlet' => 0], + ['switch' => 'off', 'outlet' => 1], + ['switch' => 'off', 'outlet' => 2], + ['switch' => 'off', 'outlet' => 3] + ]; + $setStatusResult = $devices->setDeviceStatus($multiChannelDeviceId, $multiChannelParams); + echo '

Set Multi-Channel Device Status Result

'; + echo '
' . print_r($setStatusResult, true) . '
'; + + + } else { + $loginUrl = $httpClient->getLoginUrl(); + echo 'Login with OAuth'; + } +} + +``` + ## Structure ``` rust @@ -44,15 +133,11 @@ Index.php works as a gateway to API and also as a debug for all availablemethods .json files outputs will be saved in project_root (ensure writeability) -## Example - -Examples will follow with stable release, for now - see **index.php** - ## 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. **Means:** better to get last commit data than tags before stable release +- 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 -- there could be some incosistency in the code still, like f.e handling error messages +- there could be some incosistency in the code still, like f.e handling error messages or orphan methods - index.php is a quasi-test file which helps me to check new methods and it is also a great example diff --git a/index.php b/index.php index 02544cd..a7b39e7 100644 --- a/index.php +++ b/index.php @@ -6,9 +6,6 @@ $token = new Token($httpClient); if (isset($_GET['code']) && isset($_GET['region'])) { - $code = $_GET['code']; - $region = $_GET['region']; - try { $tokenData = $token->getToken(); echo '

Token Data

'; @@ -67,6 +64,13 @@ echo '

Set Multi-Channel Device Status Result

'; echo '
' . print_r($setStatusResult, true) . '
'; + // Example usage of setDeviceStatus for single-channel device + $singleChannelDeviceId = '10011015b6'; + $singleChannelParams = ['switch' => 'off']; + $setStatusResultSingle = $devices->setDeviceStatus($singleChannelDeviceId, $singleChannelParams); + echo '

Set Single-Channel Device Status Result

'; + echo '
' . print_r($setStatusResultSingle, true) . '
'; + // Example usage of isOnline $deviceIdentifier = 'Ledy salon'; // example device name or ID $isOnlineResult = $devices->isOnline($deviceIdentifier); diff --git a/src/Devices.php b/src/Devices.php index 93edb97..b757182 100644 --- a/src/Devices.php +++ b/src/Devices.php @@ -147,13 +147,12 @@ public function searchDeviceParam($searchKey, $deviceId) { } /** - * Gets a live device parameter using the API. + * Gets live device parameter using the API. * * @param string $deviceId The ID of the device. * @param string $param The parameter to get. * @param int $type The type (default is 1). * @return mixed The specific parameter value from the API response or null if not found. - * @throws Exception If there is an error in the request. */ public function getDeviceParamLive($deviceId, $param, $type = 1) { $endpoint = '/v2/device/thing/status';