Class: SmarterMeter::Samples
- Inherits:
-
Array
- Object
- Array
- SmarterMeter::Samples
- Defined in:
- lib/smartermeter/samples.rb
Overview
Represents a collection of samples. In some cases it’s useful to operate on groups of samples and this class provides that functionality.
Class Method Summary collapse
-
.parse_espi(data) ⇒ Samples
Parses the XML returned by PG&E and creates a Samples collection.
Instance Method Summary collapse
-
#on(date_time) ⇒ Samples
Selects all samples starting from the given time until 24 hours into the future.
-
#total_kwh ⇒ Float
Calculates the total number of kilowatt hours for all samples.
-
#total_kwh_on(date_time) ⇒ Float
Calculates the total number of kilowatt hours.
Class Method Details
.parse_espi(data) ⇒ Samples
Parses the XML returned by PG&E and creates a Samples collection.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/smartermeter/samples.rb', line 13 def self.parse_espi(data) samples = Samples.new doc = Nokogiri::HTML(data) doc.xpath("//intervalreading").each do |reading| = reading.xpath("./timeperiod/start").first.content.to_i = Time.at().utc value = reading.xpath("./value").first.content.to_i / 900.0 value = ((value * 100).truncate / 100.0) samples << Sample.new(, value) end samples end |
Instance Method Details
#on(date_time) ⇒ Samples
Selects all samples starting from the given time until 24 hours into the future.
42 43 44 45 46 |
# File 'lib/smartermeter/samples.rb', line 42 def on(date_time) start_time = date_time.to_time end_time = (date_time + 1).to_time Samples.new(select { |s| start_time <= s.time && s.time < end_time }) end |
#total_kwh ⇒ Float
Calculates the total number of kilowatt hours for all samples.
33 34 35 |
# File 'lib/smartermeter/samples.rb', line 33 def total_kwh map { |s| s.kwh || 0 }.reduce(:+) || 0 end |
#total_kwh_on(date_time) ⇒ Float
Calculates the total number of kilowatt hours
53 54 55 |
# File 'lib/smartermeter/samples.rb', line 53 def total_kwh_on(date_time) on(date_time).total_kwh end |