From b6ecb12bfe0b611643eb921b804a41cb0b0fa981 Mon Sep 17 00:00:00 2001
From: Pawel Janisio
Date: Sat, 15 Jun 2024 20:10:43 +0200
Subject: [PATCH] 1.16.15
- added new method - clearToken
- added handling of 401 error in HttpClient class using clearToken
- added switch or switches status to function getDevicesList
---
src/Devices.php | 11 +++++++++--
src/HttpClient.php | 13 ++++++++++++-
src/Token.php | 36 ++++++++++++++++++++++++++++--------
3 files changed, 49 insertions(+), 11 deletions(-)
diff --git a/src/Devices.php b/src/Devices.php
index e751f6a..4efcc36 100644
--- a/src/Devices.php
+++ b/src/Devices.php
@@ -80,7 +80,7 @@ public function getDeviceById($deviceId) {
}
/**
- * Create a list of devices containing name, deviceid, productModel, online status, and channel support.
+ * Create a list of devices containing name, deviceid, productModel, online status, channel support, and switch status.
* The list is an associative array with device names as keys.
*
* @return array The list of devices.
@@ -90,12 +90,19 @@ public function getDevicesList() {
if ($this->devicesData && isset($this->devicesData['thingList'])) {
foreach ($this->devicesData['thingList'] as $device) {
$itemData = $device['itemData'];
- $devicesList[$itemData['name']] = [
+ $deviceStatus = [
'deviceid' => $itemData['deviceid'],
'productModel' => $itemData['productModel'],
'online' => $itemData['online'] == 1,
'isSupportChannelSplit' => $this->isMultiChannel($itemData['deviceid'])
];
+
+ // Get current switch status
+ $statusParam = $this->isMultiChannel($itemData['deviceid']) ? 'switches' : 'switch';
+ $switchStatus = $this->getDeviceParamLive($itemData['deviceid'], $statusParam);
+ $deviceStatus[$statusParam] = $switchStatus;
+
+ $devicesList[$itemData['name']] = $deviceStatus;
}
}
return $devicesList;
diff --git a/src/HttpClient.php b/src/HttpClient.php
index 4d9b14a..da966c8 100644
--- a/src/HttpClient.php
+++ b/src/HttpClient.php
@@ -3,6 +3,7 @@
require_once __DIR__ . '/Utils.php';
require_once __DIR__ . '/Constants.php';
require_once __DIR__ . '/Home.php';
+require_once __DIR__ . '/Token.php';
class HttpClient {
private $loginUrl;
@@ -11,6 +12,7 @@ class HttpClient {
private $authorization;
private $tokenData;
private $home;
+ private $token;
public function __construct() {
$utils = new Utils();
@@ -23,6 +25,7 @@ public function __construct() {
}
$this->loadTokenData();
$this->home = new Home($this); // Initialize the Home class
+ $this->token = new Token($this); // Initialize the Token class
}
/**
@@ -150,6 +153,10 @@ public function postRequest($endpoint, $data, $useTokenAuthorization = false) {
$result = json_decode($response, true);
if ($result['error'] !== 0) {
+ if ($result['error'] === 401) {
+ $this->token->clearToken();
+ $this->token->redirectToUrl($this->getLoginUrl(), 1);
+ }
$errorCode = $result['error'];
$errorMsg = Constants::ERROR_CODES[$errorCode] ?? 'Unknown error';
throw new Exception("Error $errorCode: $errorMsg");
@@ -187,6 +194,10 @@ public function getRequest($endpoint, $params = []) {
$response = json_decode($result, true);
if ($response['error'] !== 0) {
+ if ($response['error'] === 401) {
+ $this->token->clearToken();
+ $this->token->redirectToUrl($this->getLoginUrl(), 1);
+ }
$errorCode = $response['error'];
$errorMsg = Constants::ERROR_CODES[$errorCode] ?? 'Unknown error';
throw new Exception("Error $errorCode: $errorMsg");
@@ -195,7 +206,7 @@ public function getRequest($endpoint, $params = []) {
return $response['data'];
}
- /**
+ /**
* Get the stored token data.
*
* @return array|null The token data or null if not set.
diff --git a/src/Token.php b/src/Token.php
index 35bad1e..be6319c 100644
--- a/src/Token.php
+++ b/src/Token.php
@@ -1,18 +1,28 @@
httpClient = $httpClient;
- $this->state = 'ewelinkapiphp';
+ $this->state = $state;
$this->loadTokenData();
}
+ /**
+ * Load token data from token.json file.
+ */
+ private function loadTokenData() {
+ if (file_exists('token.json')) {
+ $this->tokenData = json_decode(file_get_contents('token.json'), true);
+ }
+ }
+
/**
* Create a login URL for OAuth.
*
@@ -41,12 +51,12 @@ public function createLoginUrl($state = null) {
}
/**
- * Load token data from token.json file.
+ * Get the login URL.
+ *
+ * @return string The login URL.
*/
- private function loadTokenData() {
- if (file_exists('token.json')) {
- $this->tokenData = json_decode(file_get_contents('token.json'), true);
- }
+ public function getLoginUrl() {
+ return $this->createLoginUrl();
}
/**
@@ -120,6 +130,16 @@ public function getTokenData() {
return $this->tokenData;
}
+ /**
+ * Clear the content of token.json file.
+ */
+ public function clearToken() {
+ if (file_exists('token.json')) {
+ file_put_contents('token.json', '');
+ $this->tokenData = null;
+ }
+ }
+
/**
* Redirect to a given URL after a delay.
*