Skip to content

Commit

Permalink
1.29.28
Browse files Browse the repository at this point in the history
- added method: initializeWebSocketConnection for future  constant connection
- added websocket property
- updated README
  • Loading branch information
PJanisio committed Jun 25, 2024
1 parent ddc257c commit e14f8e6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ Please see example app written based on this class that checks chosen parameters

![ewe_websocket](https://github.com/PJanisio/ewelinkApiPhp/assets/9625885/daa54a81-b0b3-4a43-b376-4ec412108675)


## Tech info

Visit wiki page for devs: [devs-wiki](https://github.com/PJanisio/ewelinkApiPhp/wiki/Developers)
72 changes: 71 additions & 1 deletion src/Devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Devices {
private $devicesData;
private $httpClient;
private $home;
private $wsClient;

/**
* Constructor for the Devices class.
Expand Down Expand Up @@ -419,6 +420,75 @@ public function getDeviceHistory($identifier) {

return $response;
}


/**
* Initialize WebSocket connection and perform handshake.
*
* @param string $identifier The device name or ID.
* @return WebSocketClient The initialized WebSocket client.
* @throws Exception If the device is not found or handshake fails.
*/
public function initializeWebSocketConnection($identifier) {
$deviceId = $this->getDeviceIdByIdentifier($identifier);
if (!$deviceId) {
throw new Exception("Device not found.");
}

$device = $this->getDeviceById($deviceId);
if (!$device) {
throw new Exception("Device data not available.");
}

$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");
}

return $wsClient;
}

/**
* Get data of a device using WebSocket.
*
* @param string $identifier The device name or ID.
* @param array|string $params The parameters to query.
* @return array The response data.
* @throws Exception If there is an error during the process.
*/
public function getDataWebSocket($identifier, $params) {
$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);
}

// Ensure WebSocket connection is initialized
if (!isset($this->wsClient)) {
$this->wsClient = $this->initializeWebSocketConnection($identifier);
}

$data = $this->wsClient->createQueryData($device, $params);
$this->wsClient->send(json_encode($data));
$response = json_decode($this->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");
}

return $response['params'];
}

/**
* Force get data of a device using WebSocket.
Expand Down Expand Up @@ -515,4 +585,4 @@ public function forceUpdateDevice($identifier, $params, $sleepSec = 3) {

return $updatedParams;
}
}
}
4 changes: 2 additions & 2 deletions src/WebSocketsClient.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Class: WebSocketClient
* Class: ewelinkApiPhp
* Author: Paweł 'Pavlus' Janisio
* Website: https://github.com/AceExpert/ewelink-api-python
* Dependencies: PHP 7.4+
Expand Down Expand Up @@ -423,4 +423,4 @@ public function getWebSocketUrl() {
public function getSocket() {
return $this->socket;
}
}
}

0 comments on commit e14f8e6

Please sign in to comment.