duncan3dc\Sonos\Network::getDevices PHP Метод

getDevices() защищенный Метод

Get all the devices on the current network.
protected getDevices ( ) : string[]
Результат string[] An array of ip addresses
    protected function getDevices()
    {
        $this->logger->info("discovering devices...");
        $port = 1900;
        $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
        $level = getprotobyname("ip");
        socket_set_option($sock, $level, IP_MULTICAST_TTL, 2);
        if ($this->networkInterface !== null) {
            socket_set_option($sock, $level, IP_MULTICAST_IF, $this->networkInterface);
        }
        $data = "M-SEARCH * HTTP/1.1\r\n";
        $data .= "HOST: {$this->multicastAddress}:reservedSSDPport\r\n";
        $data .= "MAN: ssdp:discover\r\n";
        $data .= "MX: 1\r\n";
        $data .= "ST: urn:schemas-upnp-org:device:ZonePlayer:1\r\n";
        $this->logger->debug($data);
        socket_sendto($sock, $data, strlen($data), null, $this->multicastAddress, $port);
        $read = [$sock];
        $write = [];
        $except = [];
        $name = null;
        $port = null;
        $tmp = "";
        $response = "";
        while (socket_select($read, $write, $except, 1)) {
            socket_recvfrom($sock, $tmp, 2048, null, $name, $port);
            $response .= $tmp;
        }
        $this->logger->debug($response);
        $devices = [];
        foreach (explode("\r\n\r\n", $response) as $reply) {
            if (!$reply) {
                continue;
            }
            $data = [];
            foreach (explode("\r\n", $reply) as $line) {
                if (!($pos = strpos($line, ":"))) {
                    continue;
                }
                $key = strtolower(substr($line, 0, $pos));
                $val = trim(substr($line, $pos + 1));
                $data[$key] = $val;
            }
            $devices[] = $data;
        }
        $return = [];
        $unique = [];
        foreach ($devices as $device) {
            if ($device["st"] !== "urn:schemas-upnp-org:device:ZonePlayer:1") {
                continue;
            }
            if (in_array($device["usn"], $unique)) {
                continue;
            }
            $this->logger->info("found device: {usn}", $device);
            $url = parse_url($device["location"]);
            $ip = $url["host"];
            $return[] = $ip;
            $unique[] = $device["usn"];
        }
        return $return;
    }