Class: Sonos::Discovery

Inherits:
Object
  • Object
show all
Defined in:
lib/sonos/discovery.rb

Constant Summary collapse

MULTICAST_ADDR =
'239.255.255.250'
MULTICAST_PORT =
1900
DEFAULT_TIMEOUT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout = DEFAULT_TIMEOUT, default_ip = nil) ⇒ Discovery

Returns a new instance of Discovery.



25
26
27
28
29
# File 'lib/sonos/discovery.rb', line 25

def initialize(timeout = DEFAULT_TIMEOUT, default_ip = nil)
  @timeout = timeout
  @default_ip = default_ip
  initialize_socket
end

Instance Attribute Details

#default_ipObject (readonly)

Returns the value of attribute default_ip.



23
24
25
# File 'lib/sonos/discovery.rb', line 23

def default_ip
  @default_ip
end

#first_device_ipObject (readonly)

Returns the value of attribute first_device_ip.



22
23
24
# File 'lib/sonos/discovery.rb', line 22

def first_device_ip
  @first_device_ip
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



21
22
23
# File 'lib/sonos/discovery.rb', line 21

def timeout
  @timeout
end

Instance Method Details

#discoverString

Look for Sonos devices on the network and return the first IP address found

Returns:

  • (String)

    the IP address of the first Sonos device found



33
34
35
36
# File 'lib/sonos/discovery.rb', line 33

def discover
  send_discovery_message
  @first_device_ip = listen_for_responses
end

#topologyArray

Find all of the Sonos devices on the network

Returns:

  • (Array)

    an array of TopologyNode objects



40
41
42
43
44
45
46
47
48
# File 'lib/sonos/discovery.rb', line 40

def topology
  self.discover unless @first_device_ip
  return [] unless @first_device_ip

  doc = Nokogiri::XML(open("http://#{@first_device_ip}:#{Sonos::PORT}/status/topology"))
  doc.xpath('//ZonePlayers/ZonePlayer').map do |node|
    TopologyNode.new(node)
  end
end