From 32013759113a8762fe5a2cc914d4634d2c984fce Mon Sep 17 00:00:00 2001 From: Pawel Janisio
' . print_r($liveRes, true) . ''; + $allLiveParams = $devs->getAllDeviceParamLive($devId); echo '
' . print_r($setSingleRes, true) . ''; - $setSingleMultiRes = $devs->setDeviceStatus($singleDevId, $singleParamsMulti); - echo '
' . print_r($setSingleMultiRes, true) . ''; + //$setSingleMultiRes = $devs->setDeviceStatus($singleDevId, $singleParamsMulti); + //echo '
' . print_r($setSingleMultiRes, true) . ''; $onlineRes = $devs->isOnline($devIdent); echo '
' . print_r($familyData, true) . ''; echo '
Current Family ID: ' . htmlspecialchars($home->getCurrentFamilyId()) . '
'; - $forceRes = $devs->forceGetData('Gniazdko biuro', $forceParams); - echo '' . print_r($forceRes, true) . ''; + $forceWakeUpRes = $devs->forceWakeUp($devIdent); + echo '
' . print_r($forceWakeUpRes, true) . ''; - $updateRes = $devs->forceUpdateDevice($devIdent, $updateParams, 3); - echo '
' . print_r($updateRes, true) . ''; + if ($forceWakeUpRes) { + $allLiveParams = $devs->getAllDeviceParamLive($devIdent); + 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; } } -?>