This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
YeePHP: move Discovery into own namespace, typo fix
- Loading branch information
Showing
2 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters