Module: EISCP::Receiver::Discovery

Included in:
EISCP::Receiver
Defined in:
lib/eiscp/receiver/discovery.rb

Overview

This module discovers receivers on the local LAN.

Constant Summary collapse

ONKYO_MAGIC =

ISCP Magic Packet for Autodiscovery

Message.new(command: 'ECN', value: 'QSTN', terminator: "\r\n", unit_type: 'x').to_eiscp

Instance Method Summary collapse

Instance Method Details

#discover(discovery_port = Receiver::ONKYO_PORT) ⇒ Object

Returns an array of discovered Receiver objects.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eiscp/receiver/discovery.rb', line 30

def discover(discovery_port = Receiver::ONKYO_PORT)
  data = []
  Socket.ip_address_list.each do | addr |
    if addr.ipv4?
      sock = UDPSocket.new
      sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
      sock.bind(addr.ip_address, discovery_port)
      sock.send(ONKYO_MAGIC, 0, '<broadcast>', discovery_port)
      loop do
        msg, addr = sock.recvfrom_nonblock(1024)
        data << Receiver.new(addr[2], ecn_string_to_ecn_array(msg))
      rescue IO::WaitReadable
        io = IO.select([sock], nil, nil, 0.5)
        break if io.nil?
      end
    end
  end
  return data
end

#ecn_string_to_ecn_array(ecn_string) ⇒ Object

Populates Receiver attributes with info from ECNQSTN response.



17
18
19
20
21
22
23
24
25
26
# File 'lib/eiscp/receiver/discovery.rb', line 17

def ecn_string_to_ecn_array(ecn_string)
  hash = {}
  message = Parser.parse(ecn_string)
  array = message.value.split('/')
  hash[:model] = array.shift
  hash[:port] = array.shift.to_i
  hash[:area] = array.shift
  hash[:mac_address] = array.shift
  hash
end