Class: ITACH::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/itach/remote.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Remote

Returns a new instance of Remote.



5
6
7
# File 'lib/itach/remote.rb', line 5

def initialize options={}
  @config = Config.load
end

Instance Method Details

#addressObject



9
10
11
# File 'lib/itach/remote.rb', line 9

def address
  @config['RECEIVER']['ip']
end

#combo_exists?(combo) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/itach/remote.rb', line 29

def combo_exists? combo
  @config['COMBOS'].keys.include?(combo.split("_").last)
end

#command_exists?(command) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (RuntimeError)


33
34
35
36
37
38
39
# File 'lib/itach/remote.rb', line 33

def command_exists? command
  remote, command, *action = command.split("_")
  command = [command, action].join('_') unless action.empty?
  raise RuntimeError, "No remote defined for #{remote}" unless remote_exists? remote
  return true if @config['REMOTES'][remote].keys.include? command
  raise RuntimeError, "No command #{command} defined for #{remote}" 
end

#connected?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/itach/remote.rb', line 17

def connected?
  !!socket
end

#get_learning_responses(&block) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/itach/remote.rb', line 68

def get_learning_responses(&block)
  while connected?
    str = ""
    until str[-2..-1] == "\r\n"
      str << read_block(1)
    end
    yield str
  end
end

#learn(state) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/itach/remote.rb', line 57

def learn(state)
  load_config
  if state
    socket.puts ("get_IRL\r")
    puts "IR Learner Enabled\r"
  else
    socket.puts("stop_IRL\r")
    puts "IR Learner Disabled\r"
  end
end

#portObject



13
14
15
# File 'lib/itach/remote.rb', line 13

def port
  @config['RECEIVER']['port']
end

#remote_exists?(remote) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/itach/remote.rb', line 25

def remote_exists? remote
  @config['REMOTES'].keys.include?(remote)
end

#send_combo(combo) ⇒ Object

Raises:

  • (RuntimeError)


52
53
54
55
# File 'lib/itach/remote.rb', line 52

def send_combo combo
  raise RuntimeError, "No combo #{combo} defined." unless combo_exists? combo
  @config['COMBOS'][combo.split("_").last].map { |c| send_command c }
end

#send_command(command, repeat = 1) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/itach/remote.rb', line 41

def send_command command, repeat = 1
  if command_exists? command
    remote, command, *action = command.split("_")
    command = [command, action].join('_') unless action.empty?
    repeat.times do
      socket.puts "sendir,#{@config['REMOTES'][remote][command]}\r"
      puts read_from_unblock_to_block
    end
  end
end

#socketObject



21
22
23
# File 'lib/itach/remote.rb', line 21

def socket
  @socket ||= TCPSocket.new address, port
end