Skip to content

Commit

Permalink
control light
Browse files Browse the repository at this point in the history
  • Loading branch information
ggottwald committed Feb 19, 2018
1 parent 051560b commit dd8c288
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 98 deletions.
100 changes: 2 additions & 98 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,9 @@

namespace MiIO;

use MiIO\Models\Device;
use MiIO\Traits\Light;

class Gateway
{
/**
* @var MiIO
*/
private $miIO;

public function __construct()
{
$this->miIO = new MiIO();
}

/**
* @param Device $device
* @return string|null
*/
public function getRgb(Device $device)
{
$hexValue = $this->getRgbAndBrightness($device);

if (strlen($hexValue) >= 7) {
return strrev(substr(strrev($hexValue), 0, 6));
}

return null;
}

/**
* @param Device $device
* @param int|string $value
*/
public function setRgb(Device $device, $value)
{
if (preg_match('/^[0-9a-f]{6}$/i', $value) === false) {
$value = dechex($value);
}

$bright = $this->getBrightness($device) ?? '01';

$this->miIO->send($device, 'set_rgb', [hexdec($bright . $value)]);
}

/**
* @param Device $device
* @return string|null
*/
public function getBrightness(Device $device)
{
$hexValue = $this->getRgbAndBrightness($device);

if (strlen($hexValue) >= 7) {
return strrev(substr(strrev($hexValue), 6));
}

return null;
}

/**
* @param Device $device
* @param int|string $value
*/
public function setBrightness(Device $device, $value)
{
if (preg_match('/^[0-9a-f]{2}$/i', $value) === false) {
$value = dechex($value);
}

$rgb = $this->getRgb($device) ?? 'ffffff';

$this->miIO->send($device, 'set_rgb', [hexdec($value . $rgb)]);
}

/**
* @param Device $device
* @return string|null
*/
public function getBrightnessAndRgb(Device $device)
{
$response = $this->miIO->send($device, 'get_prop', ['rgb']);

if (isset($response['result'][0])) {
return dechex($response['result'][0]);
}

return null;
}

/**
* @param Device $device
* @param int|string $value
*/
public function setBrightnessAndRgb(Device $device, $value)
{
if (preg_match('/^[0-9a-f]{8}$/i', $value) !== false) {
$value = hexdec($value);
}

$this->miIO->send($device, 'set_rgb', [$value]);
}
use Light;
}
123 changes: 123 additions & 0 deletions src/Traits/Light.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace MiIO\Traits;

use MiIO\MiIO;
use MiIO\Models\Device;

trait Light
{
/**
* @var MiIO
*/
private $miIO;

public function __construct()
{
$this->miIO = new MiIO();
}

/**
* Get color in hex format
*
* @param Device $device
* @return string|null
*/
public function getRgb(Device $device)
{
$hexValue = $this->getBrightnessAndRgb($device);

if (strlen($hexValue) >= 7) {
return strrev(substr(strrev($hexValue), 0, 6));
}

return null;
}

/**
* @param Device $device
* @param int|string $value
*/
public function setRgb(Device $device, $value)
{
if (preg_match('/^[0-9a-f]{6}$/i', $value) === false) {
$value = dechex($value);
}

$bright = $this->getBrightness($device) ?? '01';

$this->miIO->send($device, 'set_rgb', [hexdec($bright . $value)]);
}

/**
* Get current brightness in hex format
*
* @param Device $device
* @return string|null
*/
public function getBrightness(Device $device)
{
$hexValue = $this->getBrightnessAndRgb($device);

if (strlen($hexValue) >= 7) {
return strrev(substr(strrev($hexValue), 6));
}

return null;
}

/**
* Set brightness in percent
*
* @param Device $device
* @param int|string $value
*/
public function setBrightness(Device $device, $value)
{
$bright = dechex(round($value * 2.55));
$rgb = $this->getRgb($device) ?? 'ffffff';

$this->miIO->send($device, 'set_rgb', [hexdec($bright . $rgb)]);
}

/**
* Get brightness an color in hex format
*
* @param Device $device
* @return string|null
*/
public function getBrightnessAndRgb(Device $device)
{
$response = $this->miIO->send($device, 'get_prop', ['rgb']);

if (isset($response['result'][0])) {
return dechex($response['result'][0]);
}

return null;
}

/**
* Set brightness and color in hex format
*
* @param Device $device
* @param int|string $value
*/
public function setBrightnessAndRgb(Device $device, $value)
{
if (preg_match('/^[0-9a-f]{7,8}$/i', $value) !== false) {
$value = hexdec($value);
}

$this->miIO->send($device, 'set_rgb', [$value]);
}

/**
* @param Device $device
* @param bool $on
*/
public function switchPower(Device $device, $on = true)
{
$this->setBrightness($device, $on ? 5 : 0);
}
}

0 comments on commit dd8c288

Please sign in to comment.