Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Commit

Permalink
Modify getProps to return array values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-Langlais committed Apr 2, 2021
1 parent eb1e4b1 commit de9e052
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/Interfaces/YeePHPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function getPort(): int;
*
* @return int
*/
public function getBrightness(): string;
public function getBrightness(): int;

/**
* Return the current light color
*
* @param string $type The type of color
* @return string
* @return array
*/
public function getColor(string $type): string;
public function getColor(string $type): array;

/**
* Return the current light name
Expand Down
50 changes: 27 additions & 23 deletions src/YeePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function isConnected(): bool
*/
public function isOn(): bool
{
return $this->getProp('power');
return $this->getProp('power')[0] == 'on';
}

/**
Expand All @@ -152,27 +152,29 @@ public function getPort(): int
/**
* @inheritDoc
*/
public function getBrightness(): string
public function getBrightness(): int
{
return dechex($this->getProp('bright'));
return intval($this->getProp('bright')[0]);
}

/**
* @inheritDoc
* @throws Exception
*/
public function getColor(string $type = 'rgb'): string
public function getColor(string $type = 'rgb'): array
{
if (!in_array($type, self::ALLOWED_COLOR_TYPES, true))
throw new Exception('Invalid color type ' . $type . ' available effects : ( ' . implode(" , ", self::ALLOWED_COLOR_TYPES) . ' )');

switch ($type) {
case 'ct':
return $this->getProp('ct');
return ["ct" => $this->getProp('ct')[0]];
case 'rgb':
return dechex($this->getProp('rgb'));
case 'hsv':
return $this->getProps(['hue', 'sat']);
return ["rgb" => dechex($this->getProp('rgb')[0])];
case 'hsv': {
$res = $this->getProps(['hue', 'sat']);
return ['hue' => $res[0], 'sat' => $res[1]];
}
default:
throw new Exception('Invalid color type !');
}
Expand All @@ -183,7 +185,7 @@ public function getColor(string $type = 'rgb'): string
*/
public function getName(): string
{
return $this->getProp('name');
return $this->getProp('name')[0];
}

/**
Expand Down Expand Up @@ -323,19 +325,20 @@ protected function connect(): bool
* Get a certain prop value
*
* @param string $prop The prop name (refer to doc)
* @return string|null
* @return array|null
* @throws Exception
*/
protected function getProp(string $prop): string
protected function getProp(string $prop): ?array
{
if (!in_array($prop, self::ALLOWED_PROPS))
throw new Exception('Invalid prop supplied ' . $prop);

$job = $this->createJobArray('get_prop', [$prop]);

$res = $this->makeRequest($job);

if (!$res)
$res = '';
if ($res[0] == "ok")
throw new Exception('Problem in result'); // TODO

return $res;
}
Expand All @@ -344,19 +347,20 @@ protected function getProp(string $prop): string
* Get a certain prop value
*
* @param string $prop The prop name (refer to doc)
* @return string|null
* @return array|null
* @throws Exception
*/
protected function getProps(array $props): string
protected function getProps(array $props): ?array
{
if (!self::array_every(fn ($value) => in_array($value, self::ALLOWED_PROPS), $props))
throw new Exception('Invalid props supplied ' . $props);

$job = $this->createJobArray('get_prop', $props);

$res = $this->makeRequest($job);

if (!$res)
$res = '';
if ($res[0] == "ok")
throw new Exception('Problem in result'); // TODO

return $res;
}
Expand All @@ -378,10 +382,10 @@ protected function checkIsOnline(): bool
* Make a request to the light
*
* @param array $job The job created by the createJob() method
* @return string|null
* @return array|null
* @throws Exception
*/
protected function makeRequest(array $job): ?string
protected function makeRequest(array $job): ?array
{
$this->checkIsOnline();

Expand All @@ -396,18 +400,18 @@ protected function makeRequest(array $job): ?string
$res = fgets($this->socket);


$resultStr = null;
$result = null;

if (!empty($res)) {
$res = json_decode($res, true);

if (!array_key_exists('error', $res) && array_key_exists('result', $res))
$resultStr = $res['result'][0];
$result = $res['result'];
}

var_dump($requestStr, $res);
var_dump($requestStr, json_encode($res));

return $resultStr;
return $result;
}

/**
Expand Down

0 comments on commit de9e052

Please sign in to comment.