Skip to content

Commit

Permalink
control gateway light
Browse files Browse the repository at this point in the history
  • Loading branch information
ggottwald committed Feb 18, 2018
1 parent f56f271 commit 6133468
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace MiIO;

use MiIO\Models\Device;

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) == 8) {
return substr($hexValue, 2);
}

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);

$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) == 8) {
return substr($hexValue, 0, 2);
}

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);

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

/**
* @param Device $device
* @return string|null
*/
public function getRgbAndBrightness(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 setRgbAndBrightness(Device $device, $value)
{
if (preg_match('/^[0-9a-f]{8}$/i', $value) !== false) {
$value = hexdec($value);
}

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

0 comments on commit 6133468

Please sign in to comment.