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

Commit

Permalink
YeePHP: move Discovery into own namespace, typo fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkEzz committed Apr 4, 2021
1 parent ac18e10 commit c827c8f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
60 changes: 60 additions & 0 deletions src/Net/Discovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace SharkEzz\Yeelight\Net;

/**
* Network light discovery
*/
class Discovery
{
private const DISCOVERY_REQUEST = "M-SEARCH * HTTP/1.1\r\n
HOST: 239.255.255.250:1982\r\n
MAN: \"ssdp:discover\"\r\n
ST: wifi_bulb\r\n";
private const MULTICAST_ADDRESS = "239.255.255.250";
private const MULTICAST_PORT = 1982;

/**
* @var resource The socket
*/
private $socket;

/**
* @var array Bulbs array
*/
private $bulbs;

public function __construct()
{
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_block($this->socket);
}

public function search(): array
{
$result = socket_sendto(
$this->socket,
self::DISCOVERY_REQUEST,
strlen(self::DISCOVERY_REQUEST),
0,
self::MULTICAST_ADDRESS,
self::MULTICAST_PORT
);

if(!$result)
throw new \Exception('Socket exception');

$res = socket_read($this->socket, 4096);

if(!$res)
throw new \Exception('Socket exception');

return [];
}

private function processResult(string $res): array
{
// TODO
return [];
}
}
16 changes: 9 additions & 7 deletions src/YeePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Exception;
use SharkEzz\Yeelight\Interfaces\YeePHPInterface;

/**
* Main YeePHP class
*/
class YeePHP implements YeePHPInterface
{
/**
Expand Down Expand Up @@ -87,12 +90,12 @@ class YeePHP implements YeePHPInterface
/**
* The default fade effect
*/
public const DEDAULT_FADE_EFFECT = 'smooth';
public const DEFAULT_FADE_EFFECT = 'smooth';

/**
* The default fade delay
*/
public const DEDAULT_FADE_DELAY = 300;
public const DEFAULT_FADE_DELAY = 300;

/**
* The default color flow action
Expand Down Expand Up @@ -319,8 +322,8 @@ public function setPower(string $power): self

$this->createJob('set_power', [
$power,
self::DEDAULT_FADE_EFFECT,
self::DEDAULT_FADE_DELAY
self::DEFAULT_FADE_EFFECT,
self::DEFAULT_FADE_DELAY
]);

return $this;
Expand Down Expand Up @@ -470,7 +473,6 @@ protected function makeRequest(array $job): ?array
while ($out = stream_get_line($this->socket, 4096, "\r\n"))
$res = $out;


$result = null;

if (!empty($res)) {
Expand Down Expand Up @@ -563,7 +565,7 @@ private function checkColorValue(int $value, array $params)
* @return array
* @throws Exception
*/
private function checkFadeParams(array $params, string $defaultFadeEffect = self::DEDAULT_FADE_EFFECT)
private function checkFadeParams(array $params, string $defaultFadeEffect = self::DEFAULT_FADE_EFFECT)
{
if (array_key_exists('effect', $params)) {
if (!in_array($params['effect'], self::ALLOWED_FADE_EFFECTS))
Expand All @@ -581,7 +583,7 @@ private function checkFadeParams(array $params, string $defaultFadeEffect = self
if ($params['delay'] < 30 || $params['delay'] > 3000)
throw new Exception('Invalid delay value ' . $params['delay'] . '! value must be range from 30 to 3000');
} else {
$params['delay'] = self::DEDAULT_FADE_DELAY;
$params['delay'] = self::DEFAULT_FADE_DELAY;
}
}

Expand Down

0 comments on commit c827c8f

Please sign in to comment.