From 32013759113a8762fe5a2cc914d4634d2c984fce Mon Sep 17 00:00:00 2001 From: Pawel Janisio Date: Wed, 10 Jul 2024 14:47:03 +0200 Subject: [PATCH] 1.35.31 - removed forceGetDataMethod and forceUpdateDevice as they have been successfully replaced by getDataWebsocket and setDataWebsocket - added forceWakeUp method that is using websocket connection to refresh some of parameters that http can not (energy, temperature etc). - updated index.php to have a live example of new method --- index.php | 29 ++++++++++--------- src/Devices.php | 77 ++++++------------------------------------------- 2 files changed, 24 insertions(+), 82 deletions(-) diff --git a/index.php b/index.php index b27128a..08f709c 100644 --- a/index.php +++ b/index.php @@ -8,7 +8,7 @@ * Description: API connector for Sonoff / ewelink devices */ -ini_set('max_execution_time', '300'); //300 seconds = 5 minutes +ini_set('max_execution_time', 0); require_once __DIR__ . '/autoloader.php'; // User Inputs @@ -16,7 +16,7 @@ $devId = '100xxxxxxx'; // Single Device ID $multiDevId = '100xxxxxxx'; // Multi-Channel Device ID $singleDevId = '100xxxxxxx'; // Another Single Device ID -$devIdent = 'Name of device'; // Device Identifier for online check +$devIdent = 'Switch'; // Device Identifier for online check $singleParams = ['switch' => 'off']; // Parameters for single-channel device $multiParams = [ @@ -31,7 +31,6 @@ ['colorB' => 0] ]; // Multiple parameters for single-channel device $liveParam = ['voltage', 'current', 'power']; // Parameters to get live data -$forceParams = ['current', 'power', 'voltage']; // Parameters for force get data $updateParams = ['switch' => 'on']; // Parameters for force update device // ----------------------------------------------- @@ -71,6 +70,7 @@ $liveRes = $devs->getDeviceParamLive($devId, $liveParam); echo '

Live Device Parameter

'; echo '
' . print_r($liveRes, true) . '
'; + $allLiveParams = $devs->getAllDeviceParamLive($devId); echo '

Get All Device Parameters Live

'; @@ -84,9 +84,9 @@ echo '

Set Single-Channel Device Status Result

'; echo '
' . print_r($setSingleRes, true) . '
'; - $setSingleMultiRes = $devs->setDeviceStatus($singleDevId, $singleParamsMulti); - echo '

Set Single-Channel Device Status Result (Multiple Parameters)

'; - echo '
' . print_r($setSingleMultiRes, true) . '
'; + //$setSingleMultiRes = $devs->setDeviceStatus($singleDevId, $singleParamsMulti); + //echo '

Set Single-Channel Device Status Result (Multiple Parameters)

'; + //echo '
' . print_r($setSingleMultiRes, true) . '
'; $onlineRes = $devs->isOnline($devIdent); echo '

Is Device Online?

'; @@ -98,13 +98,15 @@ echo '
' . print_r($familyData, true) . '
'; echo '

Current Family ID: ' . htmlspecialchars($home->getCurrentFamilyId()) . '

'; - $forceRes = $devs->forceGetData('Gniazdko biuro', $forceParams); - echo '

Force Get Data Result

'; - echo '
' . print_r($forceRes, true) . '
'; + $forceWakeUpRes = $devs->forceWakeUp($devIdent); + echo '

Force Wake Up Result

'; + echo '
' . print_r($forceWakeUpRes, true) . '
'; - $updateRes = $devs->forceUpdateDevice($devIdent, $updateParams, 3); - echo '

Force Update Device Result

'; - echo '
' . print_r($updateRes, true) . '
'; + if ($forceWakeUpRes) { + $allLiveParams = $devs->getAllDeviceParamLive($devIdent); + echo '

Get All Device Parameters Live After Force Wake Up

'; + echo '
' . print_r($allLiveParams, true) . '
'; + } // Initialize WebSocket connection and get data $wsClient = $devs->initializeWebSocketConnection($devId); @@ -120,5 +122,4 @@ $loginUrl = $http->getLoginUrl(); echo 'Authorize ewelinkApiPhp'; } -} -?> +} \ No newline at end of file diff --git a/src/Devices.php b/src/Devices.php index 8a2d1df..a84b233 100644 --- a/src/Devices.php +++ b/src/Devices.php @@ -527,15 +527,15 @@ public function setDataWebSocket($identifier, $params) { return $response['params']; } + /** - * Force get data of a device using WebSocket. + * Force wake up the device by fetching all parameters and setting them back to their current values. * * @param string $identifier The device name or ID. - * @param array|string $params The parameters to query. - * @return array The response data. + * @return bool True if the operation was successful, false otherwise. * @throws Exception If there is an error during the process. */ - public function forceGetData($identifier, $params) { + public function forceWakeUp($identifier) { $deviceId = $this->getDeviceIdByIdentifier($identifier); if (!$deviceId) { throw new Exception("Device not found."); @@ -547,15 +547,9 @@ public function forceGetData($identifier, $params) { throw new Exception($errorMsg); } - // Check if the device has energy parameters - $energyParams = ['power', 'current', 'voltage']; - $deviceParams = array_keys($this->getAllDeviceParamLive($identifier) ?? []); - $hasEnergyParams = !empty(array_intersect($energyParams, $deviceParams)); - - // If the device has energy parameters, update 'current' to 0.50 before fetching data - if ($hasEnergyParams) { - $updateParams = ['current' => 0.50]; - $this->forceUpdateDevice($identifier, $updateParams, 5); + $currentParams = $this->getAllDeviceParamLive($deviceId); + if ($currentParams === null) { + return false; } $wsClient = new WebSocketClient($this->httpClient); @@ -567,7 +561,7 @@ public function forceGetData($identifier, $params) { throw new Exception("Handshake Error: $errorMsg"); } - $data = $wsClient->createQueryData($device, $params); + $data = $wsClient->createUpdateData($device, $currentParams, $device['apikey']); $wsClient->send(json_encode($data)); $response = json_decode($wsClient->receive(), true); @@ -579,59 +573,6 @@ public function forceGetData($identifier, $params) { throw new Exception("Error: $errorMsg"); } - return $response['params']; - } - - /** - * Force update the status of a device using WebSocket. - * - * @param string $identifier The device name or ID. - * @param array $params The parameters to update. - * @param int $sleepSec The number of seconds to wait before verifying the update (default is 10 seconds). - * @return array The response data. - * @throws Exception If there is an error during the process. - */ - public function forceUpdateDevice($identifier, $params, $sleepSec = 5) { - $deviceId = $this->getDeviceIdByIdentifier($identifier); - if (!$deviceId) { - throw new Exception("Device not found."); - } - $device = $this->getDeviceById($deviceId); - if (!$device) { - $errorCode = 'DEVICE_NOT_FOUND'; // Example error code - $errorMsg = Constants::ERROR_CODES[$errorCode] ?? 'Unknown error'; - throw new Exception($errorMsg); - } - - $wsClient = new WebSocketClient($this->httpClient); - $handshakeResponse = $wsClient->handshake($device); - - if (isset($handshakeResponse['error']) && $handshakeResponse['error'] != 0) { - $errorCode = $handshakeResponse['error']; - $errorMsg = Constants::ERROR_CODES[$errorCode] ?? 'Unknown error'; - throw new Exception("Handshake Error: $errorMsg"); - } - - $data = $wsClient->createUpdateData($device, $params, $device['apikey']); - $wsClient->send(json_encode($data)); - - // Keep heartbeat running and verify changes - $response = json_decode($wsClient->receive(), true); - - if (isset($response['error']) && $response['error'] != 0) { - $errorCode = $response['error']; - $errorMsg = Constants::ERROR_CODES[$errorCode] ?? 'Unknown error'; - throw new Exception("Error: $errorMsg"); - } - - sleep($sleepSec); // Wait for a while to let the changes take effect - - // Check if the parameters have been updated - $updatedParams = $this->forceGetData($deviceId, array_keys($params)); - - $wsClient->close(); - - return $updatedParams; + return true; } } -?>