Class: CurrentCost::Meter

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

Instance Method Summary collapse

Constructor Details

#initialize(port = '/dev/ttyS0') ⇒ Meter

Returns a new instance of Meter.



9
10
11
12
13
14
# File 'lib/currentcost/meter.rb', line 9

def initialize(port = '/dev/ttyS0')
  @port = RB232::Port.new(port, :baud_rate => 9600)
  @protocol = RB232::TextProtocol.new(@port, "\n")
  @protocol.add_observer(self)
  @protocol.start
end

Instance Method Details

#closeObject



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

def close
  @protocol.stop
end

#latest_readingObject



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

def latest_reading
  @latest_reading
end

#update(message) ⇒ Object



16
17
18
19
20
# File 'lib/currentcost/meter.rb', line 16

def update(message)
  @latest_reading = Reading.from_xml(message) unless message.nil?
rescue CurrentCost::ParseError
  nil
end