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

Commit

Permalink
YeePHP: discovery: first implementation of network light search
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkEzz committed Apr 4, 2021
1 parent fd042f2 commit 3a69157
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Discovery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace SharkEzz\YeePHP;

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 [];
}
}

0 comments on commit 3a69157

Please sign in to comment.