Skip to content

Commit

Permalink
Merge branch 'release/0.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
sabinus52 committed Jun 27, 2020
2 parents c144721 + 5eb5aea commit 3ec0f15
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

### v0.7 - 27/06/2020

- Add source example
- Timeout configurable
- Fix


### v0.6.1 - 24/01/2020

- Fix compatibility
Expand Down
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`
2 changes: 1 addition & 1 deletion src/Device/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 22 additions & 4 deletions src/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
class Session
{

/**
* Timeout des requêtes HTTP
*/
const TIMEOUT = 2.0;


/**
* Utilisateur de connection
*
Expand Down Expand Up @@ -59,6 +65,13 @@ class Session
*/
private $token;

/**
* Valeur du timeout en secondes
*
* @var Float
*/
private $timeout;


/**
* Constructeur
Expand All @@ -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();
}

Expand Down Expand Up @@ -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,
));
}

Expand Down Expand Up @@ -177,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);
Expand Down
2 changes: 1 addition & 1 deletion src/TuyaCloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
84 changes: 84 additions & 0 deletions test/exemple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

require 'bootstrap.php';

use Sabinus\TuyaCloudApi\TuyaCloudApi;
use Sabinus\TuyaCloudApi\Session\Session;
use Sabinus\TuyaCloudApi\Session\Platform;

/**
* @param String Identifiant de connexion
* @param String Mot de passe
* @param String Code pays
* @param String Plateforme (tuya ou smartlife)
* @param Float timeout des requêtes http en secondes
*/
$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());

0 comments on commit 3ec0f15

Please sign in to comment.