Floppy-currentcost 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,7 @@ module CurrentCost
22
22
  # end
23
23
  # end
24
24
  #
25
- # meter = CurrentCost::Meter.new
25
+ # meter = CurrentCost::Meter.new(:cc128 => true)
26
26
  # observer = SimpleObserver.new
27
27
  # meter.add_observer(observer)
28
28
  # sleep(30) # wait a while, let some readings come in
@@ -35,10 +35,12 @@ module CurrentCost
35
35
 
36
36
  # Constructor. 'port' is the name of the serial port that the physical
37
37
  # meter is connected to.
38
+ # Connection is to a classic meter by default. To connect to a new-stlye
39
+ # CC128, pass :cc128 => true at the end
38
40
  # This function will automatically start processing serial data on the
39
41
  # specified port. To stop this processing, call close.
40
- def initialize(port = '/dev/ttyS0')
41
- @port = RB232::Port.new(port, :baud_rate => 9600)
42
+ def initialize(port = '/dev/ttyS0', options = {})
43
+ @port = RB232::Port.new(port, :baud_rate => options[:cc128] == true ? 57600 : 9600)
42
44
  @protocol = RB232::TextProtocol.new(@port, "\n")
43
45
  @protocol.add_observer(self)
44
46
  @protocol.start
@@ -22,9 +22,33 @@ module CurrentCost
22
22
  if r.software_version.include? "CC128"
23
23
  r.days_since_birth = REXML::XPath.first(doc, "/msg/dsb").text.to_i
24
24
  r.hour, r.minute, r.second = REXML::XPath.first(doc, "/msg/time").text.split(':').map{|x| x.to_i}
25
- r.id = REXML::XPath.first(doc, "/msg/id").text
26
- r.type = REXML::XPath.first(doc, "/msg/type").text
27
- r.temperature = REXML::XPath.first(doc, "/msg/tmpr").text.to_f
25
+ r.id = REXML::XPath.first(doc, "/msg/id").text rescue nil
26
+ r.type = REXML::XPath.first(doc, "/msg/type").text rescue nil
27
+ r.temperature = REXML::XPath.first(doc, "/msg/tmpr").text.to_f rescue nil
28
+ r.sensor = REXML::XPath.first(doc, "/msg/sensor").text rescue nil
29
+ # Extract history data
30
+ if REXML::XPath.first(doc, "/msg/hist")
31
+ r.history = {}
32
+ REXML::XPath.each(doc, "/msg/hist/data") do |sensor|
33
+ sensor_num = sensor.elements['sensor'].text.to_i
34
+ sensor.elements.each do |item|
35
+ match = item.name.match "([hdm])([0-9][0-9][0-9])"
36
+ if match
37
+ case match[1]
38
+ when 'h'
39
+ type = :hours
40
+ when 'd'
41
+ type = :days
42
+ when 'm'
43
+ type = :months
44
+ end
45
+ r.history[type] ||= []
46
+ r.history[type][match[2].to_i] ||= []
47
+ r.history[type][match[2].to_i][sensor_num] = item.text.to_f
48
+ end
49
+ end
50
+ end
51
+ end
28
52
  else
29
53
  r.days_since_birth = REXML::XPath.first(doc, "/msg/date/dsb").text.to_i
30
54
  r.hour = REXML::XPath.first(doc, "/msg/date/hr").text.to_i
@@ -39,19 +63,19 @@ module CurrentCost
39
63
  r.history = {}
40
64
  r.history[:hours] = []
41
65
  REXML::XPath.each(doc, "/msg/hist/hrs/*") do |node|
42
- r.history[:hours][node.name.slice(1,2).to_i] = node.text.to_f
66
+ r.history[:hours][node.name.slice(1,2).to_i] = [node.text.to_f]
43
67
  end
44
68
  r.history[:days] = []
45
69
  REXML::XPath.each(doc, "/msg/hist/days/*") do |node|
46
- r.history[:days][node.name.slice(1,2).to_i] = node.text.to_i
70
+ r.history[:days][node.name.slice(1,2).to_i] = [node.text.to_i]
47
71
  end
48
72
  r.history[:months] = []
49
73
  REXML::XPath.each(doc, "/msg/hist/mths/*") do |node|
50
- r.history[:months][node.name.slice(1,2).to_i] = node.text.to_i
74
+ r.history[:months][node.name.slice(1,2).to_i] = [node.text.to_i]
51
75
  end
52
76
  r.history[:years] = []
53
77
  REXML::XPath.each(doc, "/msg/hist/yrs/*") do |node|
54
- r.history[:years][node.name.slice(1,2).to_i] = node.text.to_i
78
+ r.history[:years][node.name.slice(1,2).to_i] = [node.text.to_i]
55
79
  end
56
80
  end
57
81
  end
@@ -86,7 +110,9 @@ module CurrentCost
86
110
  attr_accessor :channels
87
111
  # Current temperature
88
112
  attr_accessor :temperature
89
- # 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.
113
+ # Sensor number
114
+ attr_accessor :sensor
115
+ # Historical data, represented as a hash. There is a hash entry for days, weeks, months, and years. Each of these is an array of sensors, each of which contains an array of historical kWh data.
90
116
  attr_accessor :history
91
117
 
92
118
  # The sum of the current wattage for all channels, as shown on the meter
@@ -3,7 +3,7 @@ module CurrentCost
3
3
  module VERSION #:nodoc:
4
4
  MAJOR = 0
5
5
  MINOR = 3
6
- TINY = 0
6
+ TINY = 1
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
9
9
 
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.3.0
4
+ version: 0.3.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: 2009-02-17 00:00:00 -08:00
12
+ date: 2009-02-19 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency