class Milight::V6::Socket
Send and receive UDP packets.
Constants
- READ_TIMEOUT
Attributes
host[R]
port[R]
Public Class Methods
new(host, port)
click to toggle source
# File lib/milight/v6/socket.rb, line 14 def initialize(host, port) @host = host @port = port end
Public Instance Methods
close()
click to toggle source
# File lib/milight/v6/socket.rb, line 39 def close socket.close end
receive_bytes()
click to toggle source
# File lib/milight/v6/socket.rb, line 25 def receive_bytes response, address = socket.recvfrom_nonblock(128) bytes = response.unpack('C*') logger.debug("Received: #{format_bytes_as_hex(bytes)}") [bytes, address.last] rescue IO::WaitReadable ready = IO.select([socket], nil, nil, READ_TIMEOUT) retry if ready return nil end
send_bytes(bytes)
click to toggle source
# File lib/milight/v6/socket.rb, line 19 def send_bytes(bytes) logger.debug("Sending: #{format_bytes_as_hex(bytes)}") socket.send(bytes.pack('C*'), 0, @host, @port) end
Private Instance Methods
format_bytes_as_hex(bytes)
click to toggle source
# File lib/milight/v6/socket.rb, line 65 def format_bytes_as_hex(bytes) bytes.map { |s| format("0x%02X", s) }.join(", ") end
logger()
click to toggle source
# File lib/milight/v6/socket.rb, line 57 def logger @logger ||= begin logger = Logger.new(STDOUT) logger.level = Logger::INFO if ENV["MILIGHT_DEBUG"] != "1" logger end end
socket()
click to toggle source
# File lib/milight/v6/socket.rb, line 45 def socket @socket ||= begin socket = UDPSocket.new if @host == "<broadcast>" || @host == "255.255.255.255" socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, true) end socket end end