Module: Serialbar::Listener

Defined in:
lib/serialbar/listener.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object

Using method missing to check for the inclusion of the subclass implementation of parse



98
99
100
101
102
103
104
# File 'lib/serialbar/listener.rb', line 98

def method_missing(id, *args) #:nodoc:
if id.to_s.eql?("parse")        
  raise Serialbar::Exceptions::NoParseMethodError, "Parse method not implemented"     
else
  raise NoMethodError
end
end

Instance Method Details

#listener?Boolean

:nodoc: Testing method for class inclusion

Returns:

  • (Boolean)


106
107
108
# File 'lib/serialbar/listener.rb', line 106

def listener? #:nodoc: Testing method for class inclusion
  return true  #testing method
end

#parse_missing?Boolean

:nodoc: Testing method for presence of parse

Returns:

  • (Boolean)


110
111
112
# File 'lib/serialbar/listener.rb', line 110

def parse_missing? #:nodoc: Testing method for presence of parse
  parse("test string")
end

#poll(send, lines = 1) ⇒ Object

Poll device by sending string

Attributes

  • send - string to send to the serial port

  • lines - number of lines to read back

Examples

data = listener.poll(“#001\n”)

read lines not implemented yet



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/serialbar/listener.rb', line 50

def poll(send,lines=1)
  if port_initialized?
    begin
      @sp.write(send)
      data = @sp.readline
    rescue Interrupt
          puts "Exiting" 
        end
        parse(data)
      end
end

#poll_every_n_minutes(send, lines = 1, n = 1) ⇒ Object

Poll device with a timer could use clever missing_method stuff here

data is passed as an argument to parse

Attributes

  • send - string to send to the serial port

  • lines - number of lines to read back

  • n - number of minutes between each poll of the device



90
91
92
93
94
# File 'lib/serialbar/listener.rb', line 90

def poll_every_n_minutes(send,lines=1,n=1)
  timer = Timers::Group.new
  every_seconds = timer.every(60*n) { parse(poll(send,lines)) }
  loop { timers.wait } 
end

#poll_every_n_seconds(send, lines = 1, n = 1) ⇒ Object

Poll device with a timer could use clever missing_method stuff here

data is passed as an argument to parse

Attributes

  • send - string to send to the serial port

  • lines - number of lines to read back

  • n - number of seconds between each poll of the device



73
74
75
76
77
# File 'lib/serialbar/listener.rb', line 73

def poll_every_n_seconds(send,lines=1,n=1)
  timer = Timers::Group.new
  every_seconds = timer.every(n) { parse(poll(send,lines)) }
  loop { timers.wait } 
end

#port_initialized?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
# File 'lib/serialbar/listener.rb', line 114

def port_initialized?
  if @sp.nil?
    raise Serialbar::Exceptions::PortNotInitialized, "Call setup on listener class to initialize serial port"     
  else
    return true
  end

end

#runObject

Trigger listening on setup serial port

simply reads each line from the port and passes it as string to implemented parse method



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/serialbar/listener.rb', line 23

def run
  #is the serial port setup?
  puts "Listening on serial port #{@portname}"
  if port_initialized?
    @sp.flush_input
        begin 
      while data = @sp.readline
        parse(data)
      end
        rescue Interrupt
          puts "Exiting" 
        end
      end
end

#serial_portObject

Direct access to the serialport serial port object



16
17
18
# File 'lib/serialbar/listener.rb', line 16

def serial_port
  return @sp if port_initialized?
end

#setup(port, baud = 9600, data_bits = 8, stop_bits = 1, parity = 1) ⇒ Object

Setup the serial port



9
10
11
12
13
# File 'lib/serialbar/listener.rb', line 9

def setup(port, baud=9600, data_bits=8, stop_bits=1, parity=1)
  @portname = port
  @sp = SerialPort.new(@portname,baud,data_bits,stop_bits,parity)
  @setup = true
end