Floppy-currentcost 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +3 -3
- data/examples/simple.rb +19 -8
- data/lib/currentcost/exceptions.rb +2 -1
- data/lib/currentcost/meter.rb +47 -2
- data/lib/currentcost/reading.rb +19 -5
- metadata +3 -3
data/README
CHANGED
@@ -23,6 +23,6 @@ Floppy-rb232 >= 0.1.0
|
|
23
23
|
|
24
24
|
== USAGE
|
25
25
|
|
26
|
-
You can read data from the meter by creating an instance of the Meter
|
27
|
-
and
|
28
|
-
example of how to do this.
|
26
|
+
You can read data from the meter by creating an instance of the CurrentCost::Meter
|
27
|
+
class, and registering an observer which will receive readings. See examples/simple.rb
|
28
|
+
or the CurrentCost::Meter documentation for a simple example of how to do this.
|
data/examples/simple.rb
CHANGED
@@ -16,13 +16,24 @@ OptionParser.new do |opts|
|
|
16
16
|
end
|
17
17
|
end.parse!
|
18
18
|
|
19
|
+
# A simple observer class which will receive updates from the meter
|
20
|
+
class SimpleObserver
|
21
|
+
def update(reading)
|
22
|
+
# Add all channels to get real figure
|
23
|
+
watts = 0
|
24
|
+
reading.channels.each { |c| watts += c[:watts] }
|
25
|
+
# Print out measurement
|
26
|
+
puts "New reading received: #{watts} W"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
19
30
|
# Create meter
|
20
31
|
meter = CurrentCost::Meter.new(options[:port])
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
32
|
+
# Create observer
|
33
|
+
observer = SimpleObserver.new
|
34
|
+
# Register observer with meter
|
35
|
+
meter.add_observer(observer)
|
36
|
+
# Wait a while, let some readings come in
|
37
|
+
sleep(30)
|
38
|
+
# Close the meter object to stop it receiving data
|
39
|
+
meter.close
|
data/lib/currentcost/meter.rb
CHANGED
@@ -1,11 +1,42 @@
|
|
1
1
|
require 'rb232'
|
2
2
|
require 'rb232/text_protocol'
|
3
|
+
require 'currentcost/meter'
|
3
4
|
require 'currentcost/reading'
|
5
|
+
require 'observer'
|
4
6
|
|
5
7
|
module CurrentCost
|
8
|
+
|
9
|
+
# A class to represent a physical CurrentCost meter attached to a serial port.
|
10
|
+
# This class is Observable (see Ruby documentation for details). Client code
|
11
|
+
# should create an observer class which defines an update(CurrentCost::Reading)
|
12
|
+
# function, and call Meter.add_observer(my_observer) to receive updates when
|
13
|
+
# new readings are received from the physical meter.
|
14
|
+
#
|
15
|
+
# For example:
|
16
|
+
#
|
17
|
+
# require 'currentcost/meter'
|
18
|
+
#
|
19
|
+
# class SimpleObserver
|
20
|
+
# def update(reading)
|
21
|
+
# puts "New reading received: #{reading.channels[0][:watts]} W"
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# meter = CurrentCost::Meter.new
|
26
|
+
# observer = SimpleObserver.new
|
27
|
+
# meter.add_observer(observer)
|
28
|
+
# sleep(30) # wait a while, let some readings come in
|
29
|
+
# meter.close
|
30
|
+
|
6
31
|
|
7
32
|
class Meter
|
8
|
-
|
33
|
+
|
34
|
+
include Observable
|
35
|
+
|
36
|
+
# Constructor. 'port' is the name of the serial port that the physical
|
37
|
+
# meter is connected to.
|
38
|
+
# This function will automatically start processing serial data on the
|
39
|
+
# specified port. To stop this processing, call close.
|
9
40
|
def initialize(port = '/dev/ttyS0')
|
10
41
|
@port = RB232::Port.new(port, :baud_rate => 9600)
|
11
42
|
@protocol = RB232::TextProtocol.new(@port, "\n")
|
@@ -13,16 +44,30 @@ module CurrentCost
|
|
13
44
|
@protocol.start
|
14
45
|
end
|
15
46
|
|
47
|
+
# Internal use only, client code does not need to use this function. Informs
|
48
|
+
# the Meter object that a new message has been received by the serial port.
|
16
49
|
def update(message)
|
17
|
-
|
50
|
+
unless message.nil?
|
51
|
+
# Parse reading from message
|
52
|
+
@latest_reading = Reading.from_xml(message)
|
53
|
+
# Inform observers
|
54
|
+
changed
|
55
|
+
notify_observers(@latest_reading)
|
56
|
+
end
|
18
57
|
rescue CurrentCost::ParseError
|
19
58
|
nil
|
20
59
|
end
|
21
60
|
|
61
|
+
# Get the last Reading received. If no reading has been received yet,
|
62
|
+
# returns nil. If you have registered an observer with add_observer(),
|
63
|
+
# you will most likely not need this function as the reading will be
|
64
|
+
# delivered automatically to your observer's update() function.
|
22
65
|
def latest_reading
|
23
66
|
@latest_reading
|
24
67
|
end
|
25
68
|
|
69
|
+
# Stops serial data processing. Call this once you're done with the Meter
|
70
|
+
# object.
|
26
71
|
def close
|
27
72
|
@protocol.stop
|
28
73
|
end
|
data/lib/currentcost/reading.rb
CHANGED
@@ -4,7 +4,10 @@ require 'rexml/document'
|
|
4
4
|
module CurrentCost
|
5
5
|
|
6
6
|
class Reading
|
7
|
-
|
7
|
+
|
8
|
+
# Creates a reading object from an XML string.
|
9
|
+
# Raises CurrentCost::ParseError if the XML is malformed or missing
|
10
|
+
# expected content.
|
8
11
|
def self.from_xml(xml)
|
9
12
|
# Parse XML
|
10
13
|
doc = REXML::Document.new(xml)
|
@@ -49,19 +52,30 @@ module CurrentCost
|
|
49
52
|
rescue
|
50
53
|
raise CurrentCost::ParseError.new("Couldn't parse XML data.")
|
51
54
|
end
|
52
|
-
|
55
|
+
|
56
|
+
# Number of days since the meter was turned on
|
53
57
|
attr_accessor :days_since_birth
|
58
|
+
# Current time - hour
|
54
59
|
attr_accessor :hour
|
60
|
+
# Current time - minute
|
55
61
|
attr_accessor :minute
|
62
|
+
# Current time - second
|
56
63
|
attr_accessor :second
|
64
|
+
# Name of the device - always "CC02".
|
57
65
|
attr_accessor :name
|
66
|
+
# ID number of the device
|
58
67
|
attr_accessor :id
|
68
|
+
# Type id of the device - "1" for a standard CurrentCost meter.
|
59
69
|
attr_accessor :type
|
70
|
+
# Version of the meter software
|
60
71
|
attr_accessor :software_version
|
72
|
+
# An array of channels. channels[x][:watts] contains the current power for that channel in watts. The figure shown on the meter is the sum of the wattage for all channels.
|
61
73
|
attr_accessor :channels
|
74
|
+
# Current temperature
|
62
75
|
attr_accessor :temperature
|
76
|
+
# Historical data, represented as a hash. There is a hash entry for days, weeks, months, and years. Each of these is an array of historical kWh data.
|
63
77
|
attr_accessor :history
|
64
|
-
|
78
|
+
|
65
79
|
end
|
66
|
-
|
67
|
-
end
|
80
|
+
|
81
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Floppy-currentcost
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Smith
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-08-
|
12
|
+
date: 2008-08-20 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -38,7 +38,7 @@ files:
|
|
38
38
|
- lib/currentcost/version.rb
|
39
39
|
- lib/currentcost/exceptions.rb
|
40
40
|
- examples/simple.rb
|
41
|
-
has_rdoc:
|
41
|
+
has_rdoc: true
|
42
42
|
homepage: http://github.com/Floppy/currentcost-ruby
|
43
43
|
post_install_message:
|
44
44
|
rdoc_options: []
|