Class: SmarterMeter::Samples

Inherits:
Array
  • Object
show all
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)

Instance Method Summary (collapse)

Class Method Details

+ (Samples) parse_espi(data)

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|
    timestamp = reading.xpath("./timeperiod/start").first.content.to_i
    timestamp = Time.at(timestamp).utc

    value = reading.xpath("./value").first.content.to_i / 900.0
    value = ((value * 100).truncate / 100.0)

    samples << Sample.new(timestamp, value)
  end

  samples
end

Instance Method Details

- (Samples) on(date_time)

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

- (Float) total_kwh

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

- (Float) total_kwh_on(date_time)

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