Class: RelayrAce

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

Instance Method Summary collapse

Constructor Details

#initialize(port, logger = nil) ⇒ RelayrAce

Returns a new instance of RelayrAce.



5
6
7
8
# File 'lib/relayrace.rb', line 5

def initialize(port, logger = nil)
  @port = port
  @logger = logger
end

Instance Method Details

#get_channel(channel) ⇒ Object



26
27
28
# File 'lib/relayrace.rb', line 26

def get_channel(channel)
  raise "Reads aren't working yet"
end

#get_relay(relay) ⇒ Object



10
11
12
# File 'lib/relayrace.rb', line 10

def get_relay(relay)
  raise "Reads aren't working yet"
end

#send_cmd(cmd) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/relayrace.rb', line 38

def send_cmd(cmd)
  begin
    SystemTimer.timeout_after(10) do
      SerialPort.open(@port, 115200, 8, 1, SerialPort::NONE) do |tty|
        tty.write("\r\n")
        2.times { tty.getc == ':' }
        tty.write("#{cmd}\r\n")
      end
    end
  rescue Timeout::Error
    @logger.warn "Failed to execute operation #{cmd}" if @logger    
  end
end

#set_all_relays(value) ⇒ Object



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

def set_all_relays(value)
  send_cmd("RELS.#{(value)? 'ON' : 'OFF'}")
end

#set_channel(channel, value) ⇒ Object



30
31
32
# File 'lib/relayrace.rb', line 30

def set_channel(channel, value)
  send_cmd("CH#{channel}.#{(value)? 'ON' : 'OFF'}")
end

#set_relay(relay, value) ⇒ Object



14
15
16
# File 'lib/relayrace.rb', line 14

def set_relay(relay, value)
  send_cmd("REL#{relay}.#{(value)? 'ON' : 'OFF'}")
end

#toggle_channel(channel) ⇒ Object



34
35
36
# File 'lib/relayrace.rb', line 34

def toggle_channel(channel)
  send_cmd("CH#{channel}.TOGGLE")
end

#toggle_relay(relay) ⇒ Object



18
19
20
# File 'lib/relayrace.rb', line 18

def toggle_relay(relay)
  send_cmd("REL#{relay}.TOGGLE")
end