diff --git a/src/Gateway.php b/src/Gateway.php new file mode 100644 index 0000000..0cb8c73 --- /dev/null +++ b/src/Gateway.php @@ -0,0 +1,106 @@ +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]); + } +} \ No newline at end of file