From 2c96a66a6b01dde22878cf7b0c0a6a8b77edc036 Mon Sep 17 00:00:00 2001 From: Sabinus52 Date: Sat, 6 Jun 2020 14:24:15 +0200 Subject: [PATCH 1/5] Readme + example --- README.md | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ test/exemple.php | 84 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 test/exemple.php diff --git a/README.md b/README.md index 06d235f..b939593 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,98 @@ # TuyaCloudApi Library to control the Tuya device + +## Support devices + +- Switch +- Scene +- Light +- Cover + + +## Installation + +~~~ bash +composer require sabinus52/tuyacloudapi +~~~ + + +## Example + +~~~ php +require __DIR__ . '../vendor/autoload.php'; + +use Sabinus\TuyaCloudApi\TuyaCloudApi; +use Sabinus\TuyaCloudApi\Session\Session; +use Sabinus\TuyaCloudApi\Session\Platform; + +$session = new Session($argv[1], $argv[2], '33', Platform::SMART_LIFE, 5.0); + +// Initialize object API +$api = new TuyaCloudApi($session); + +// Retourne la liste des objets +$return = $api->discoverDevices(); +var_dump($return); + + +/** + * Scene + */ +$device = $api->getDeviceById('1654566545484'); +// Active la scene +$device->activate($api); + + +/** + * Prise : Méthodfe 1 + */ +$device = $api->getDeviceById('012345678901234598'); +// Allume la prise +$rep = $api->sendEvent($device->getTurnOnEvent()); +// Mets à jour l'objet pour récupér le dernier état +$device->update($api); +print 'Etat : ' . $device->getState(); + + +/** + * Prise : Méthode 2 + */ +$device = $api->getDeviceById('012345678901234598'); +// Allume la prise +$device->turnOn($api); +sleep(3); +// Eteins la prise +$device->turnOff($api); + + +/** + * Lampe + */ +$device = $api->getDeviceById('012345678901234598'); +// Allume la lampe +$api->sendEvent($device->getTurnOnEvent()); +sleep(3); +// Change la couleur +$api->sendEvent($device->getSetColorEvent(100, 80)); +sleep(3); +// Luminosité à 50% +$api->sendEvent($device->getSetBrightnessEvent(50)); +sleep(3); +// Eteins la lampe +$api->sendEvent($device->getTurnOffEvent()); + + +/** + * Volet + */ +$device = $api->getDeviceById('012345678901234598'); +// Ferme le volet +$rep = $api->sendEvent($device->getCloseEvent()); +sleep(5); +// Stoppe le volet +$rep = $api->sendEvent($device->getStopEvent()); +// Ouvre le volet +$rep = $api->sendEvent($device->getOpenEvent()); +~~~ + +See the file `./test/exemple.php` diff --git a/test/exemple.php b/test/exemple.php new file mode 100644 index 0000000..1adee36 --- /dev/null +++ b/test/exemple.php @@ -0,0 +1,84 @@ +discoverDevices(); +var_dump($return); + + +/** + * Scene + */ +$device = $api->getDeviceById('1654566545484'); +// Active la scene +$device->activate($api); + + +/** + * Prise : Méthodfe 1 + */ +$device = $api->getDeviceById('012345678901234598'); +// Allume la prise +$rep = $api->sendEvent($device->getTurnOnEvent()); +// Mets à jour l'objet pour récupér le dernier état +$device->update($api); +print 'Etat : ' . $device->getState(); + + +/** + * Prise : Méthode 2 + */ +$device = $api->getDeviceById('012345678901234598'); +// Allume la prise +$device->turnOn($api); +sleep(3); +// Eteins la prise +$device->turnOff($api); + + +/** + * Lampe + */ +$device = $api->getDeviceById('012345678901234598'); +// Allume la lampe +$api->sendEvent($device->getTurnOnEvent()); +sleep(3); +// Change la couleur +$api->sendEvent($device->getSetColorEvent(100, 80)); +sleep(3); +// Luminosité à 50% +$api->sendEvent($device->getSetBrightnessEvent(50)); +sleep(3); +// Eteins la lampe +$api->sendEvent($device->getTurnOffEvent()); + + +/** + * Volet + */ +$device = $api->getDeviceById('012345678901234598'); +// Ferme le volet +$rep = $api->sendEvent($device->getCloseEvent()); +sleep(5); +// Stoppe le volet +$rep = $api->sendEvent($device->getStopEvent()); +// Ouvre le volet +$rep = $api->sendEvent($device->getOpenEvent()); + From cd90f0eff80ee5bf36e8f7589e680e07cdaf2912 Mon Sep 17 00:00:00 2001 From: Sabinus52 Date: Sat, 6 Jun 2020 14:33:11 +0200 Subject: [PATCH 2/5] Timeout #3 --- src/Session/Session.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Session/Session.php b/src/Session/Session.php index 3d01804..219a3f9 100644 --- a/src/Session/Session.php +++ b/src/Session/Session.php @@ -17,6 +17,12 @@ class Session { + /** + * Timeout des requêtes HTTP + */ + const TIMEOUT = 2.0; + + /** * Utilisateur de connection * @@ -59,6 +65,13 @@ class Session */ private $token; + /** + * Valeur du timeout en secondes + * + * @var Float + */ + private $timeout; + /** * Constructeur @@ -67,14 +80,16 @@ class Session * @param String $password : Mot de passe * @param Integer $country : Code du pays * @param String $biztype : Type de la plateforme + * @param Float $timeout : Timeout des requêtes http en secondes */ - public function __construct($username, $password, $country, $biztype = null) + public function __construct($username, $password, $country, $biztype = null, $timeout = self::TIMEOUT) { $this->username = $username; $this->password = $password; $this->countryCode = $country; $this->platform = new Platform($biztype); $this->token = new Token(); + $this->timeout = $timeout; $this->client = $this->_createClient(); } @@ -116,8 +131,8 @@ private function _createClient() { return new Client(array( 'base_uri' => $this->platform->getBaseUrl(), - 'connect_timeout' => 2.0, - 'timeout' => 2.0, + 'connect_timeout' => $this->timeout, + 'timeout' => $this->timeout, )); } From 0d2b0effc2a38a9d91007ff1f4fb12326017435c Mon Sep 17 00:00:00 2001 From: Sabinus52 Date: Sat, 20 Jun 2020 14:51:25 +0200 Subject: [PATCH 3/5] Timeout #3 --- src/Session/Session.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Session/Session.php b/src/Session/Session.php index 3d01804..219a3f9 100644 --- a/src/Session/Session.php +++ b/src/Session/Session.php @@ -17,6 +17,12 @@ class Session { + /** + * Timeout des requêtes HTTP + */ + const TIMEOUT = 2.0; + + /** * Utilisateur de connection * @@ -59,6 +65,13 @@ class Session */ private $token; + /** + * Valeur du timeout en secondes + * + * @var Float + */ + private $timeout; + /** * Constructeur @@ -67,14 +80,16 @@ class Session * @param String $password : Mot de passe * @param Integer $country : Code du pays * @param String $biztype : Type de la plateforme + * @param Float $timeout : Timeout des requêtes http en secondes */ - public function __construct($username, $password, $country, $biztype = null) + public function __construct($username, $password, $country, $biztype = null, $timeout = self::TIMEOUT) { $this->username = $username; $this->password = $password; $this->countryCode = $country; $this->platform = new Platform($biztype); $this->token = new Token(); + $this->timeout = $timeout; $this->client = $this->_createClient(); } @@ -116,8 +131,8 @@ private function _createClient() { return new Client(array( 'base_uri' => $this->platform->getBaseUrl(), - 'connect_timeout' => 2.0, - 'timeout' => 2.0, + 'connect_timeout' => $this->timeout, + 'timeout' => $this->timeout, )); } From 2887e975d3d6ee76f0871abd103260739011d8bb Mon Sep 17 00:00:00 2001 From: Sabinus52 Date: Sat, 27 Jun 2020 17:29:48 +0200 Subject: [PATCH 4/5] Fix setData --- src/Device/Device.php | 2 +- src/Session/Session.php | 5 ++++- src/TuyaCloudApi.php | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Device/Device.php b/src/Device/Device.php index a56826a..a0e0bfb 100644 --- a/src/Device/Device.php +++ b/src/Device/Device.php @@ -71,7 +71,7 @@ public function __construct($id, $name = '', $icon = '') * * @param Array $data */ - public function setData(array $data) + public function setData($data) { $this->data = $data; } diff --git a/src/Session/Session.php b/src/Session/Session.php index 219a3f9..5028f76 100644 --- a/src/Session/Session.php +++ b/src/Session/Session.php @@ -192,8 +192,11 @@ private function _refreshToken() * @param String $message : Message par défaut * @throws Exception */ - public function checkResponse(array $response, $message = null) + public function checkResponse($response, $message = null) { + if ( empty($response) ) { + throw new \Exception($message.' : Datas return null'); + } if ( isset($response['responseStatus']) && $response['responseStatus'] === 'error' ) { $message = isset($response['errorMsg']) ? $response['errorMsg'] : $message; throw new \Exception($message); diff --git a/src/TuyaCloudApi.php b/src/TuyaCloudApi.php index f6398ea..ce30c60 100644 --- a/src/TuyaCloudApi.php +++ b/src/TuyaCloudApi.php @@ -132,7 +132,7 @@ private function _request($name, $namespace, array $payload = []) ), )); $response = json_decode((string) $response->getBody(), true); - $this->session->checkResponse($response, sprintf('Failed to get response from %s', $name)); + $this->session->checkResponse($response, sprintf('Failed to get "%s" response from Cloud Tuya', $name)); return $response; } From 5eb5aeaa294c399d8605d901916a3cb64d19a4ff Mon Sep 17 00:00:00 2001 From: Sabinus52 Date: Sat, 27 Jun 2020 17:34:04 +0200 Subject: [PATCH 5/5] Bump version 0.7 --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8381111..ded3f93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +### v0.7 - 27/06/2020 + +- Add source example +- Timeout configurable +- Fix + + ### v0.6.1 - 24/01/2020 - Fix compatibility