Skip to content

Commit

Permalink
1.16.15
Browse files Browse the repository at this point in the history
- added new method - clearToken
- added handling of 401 error in HttpClient class using clearToken
- added switch or switches status to function getDevicesList
  • Loading branch information
PJanisio committed Jun 15, 2024
1 parent f0ecd44 commit b6ecb12
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/Devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,6 +12,7 @@ class HttpClient {
private $authorization;
private $tokenData;
private $home;
private $token;

public function __construct() {
$utils = new Utils();
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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.
Expand Down
36 changes: 28 additions & 8 deletions src/Token.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
<?php

require_once __DIR__ . '/HttpClient.php';
require_once __DIR__ . '/Utils.php';
require_once __DIR__ . '/Constants.php';

class Token {
private $httpClient;
private $tokenData;
private $state;

public function __construct(HttpClient $httpClient) {
public function __construct(HttpClient $httpClient, $state = 'ewelinkapiphp') {
$this->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.
*
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit b6ecb12

Please sign in to comment.