module LIRC::Protocol

EventMachine Protocol for LIRC.

Internally relies on Messages for parsing. Can send Commands to LIRC with send_command.

Attributes

logger[R]

Public Class Methods

connect!(server:, port: DEFAULT_PORT, &block) click to toggle source
# File lib/lirc/protocol.rb, line 14
def self.connect!(server:, port: DEFAULT_PORT, &block)
  EventMachine.connect(server, port, self, &block)
end
new(*args, logger: Logger.new(STDERR), **kwargs) click to toggle source
Calls superclass method
# File lib/lirc/protocol.rb, line 18
def initialize(*args, logger: Logger.new(STDERR), **kwargs)
  @logger = logger

  super(*args, **kwargs)
end

Public Instance Methods

receive_line(line) click to toggle source
# File lib/lirc/protocol.rb, line 29
def receive_line(line)
  line = line.chomp
  if @response_parser
    parse_message_line(line)
  elsif line.eql?("BEGIN")
    parse_message_line(line)
  elsif line =~ /^[0-9a-f]/
    receive_message(Messages::ButtonPress.parse(line))
  else
    logger.warn "Received unknown line from lirc: #{line}"
  end
end
send_command(command) click to toggle source
# File lib/lirc/protocol.rb, line 24
def send_command(command)
  send_data "#{command.serialize}\n"
  response_deferrables[command.serialize] ||= EM::DefaultDeferrable.new
end

Private Instance Methods

parse_message_line(line) click to toggle source
# File lib/lirc/protocol.rb, line 44
def parse_message_line(line)
  @response_parser ||= Messages::ResponseParser.new
  @response_parser.parse_line(line)
  if @response_parser.valid?
    msg = @response_parser.message
    resolve_response(msg)
    receive_message(msg) if respond_to?(:receive_message)
    @response_parser = nil
  end
end
resolve_response(response) click to toggle source

 resolve here means like Promises - Deferrables are pretty much promises

# File lib/lirc/protocol.rb, line 56
def resolve_response(response)
  deferrable = response_deferrables[response.command]
  return if deferrable.nil?

  if response.success
    deferrable.succeed(response)
  else
    deferrable.fail(response)
  end
  response_deferrables.delete(response.command)
end
response_deferrables() click to toggle source
# File lib/lirc/protocol.rb, line 68
def response_deferrables
  @response_deferrables ||= {}
end