Class: GreenButtonClasses::IntervalBlock

Inherits:
GreenButtonEntry show all
Defined in:
lib/greenbutton/gb_classes.rb

Instance Attribute Summary collapse

Attributes inherited from GreenButtonEntry

#href_from_parent, #other_related, #related_hrefs

Instance Method Summary collapse

Methods inherited from GreenButtonEntry

#add_related_entry, #add_related_link, #all_rules, #assign_rule, #create_attributes, #entry_node, #find_by_href, #get_related_links, #initialize

Constructor Details

This class inherits a constructor from GreenButtonClasses::GreenButtonEntry

Instance Attribute Details

#meter_readingObject

Returns the value of attribute meter_reading


301
302
303
# File 'lib/greenbutton/gb_classes.rb', line 301

def meter_reading
  @meter_reading
end

Instance Method Details

#additional_rulesObject


307
308
309
310
311
312
# File 'lib/greenbutton/gb_classes.rb', line 307

def additional_rules
  {
    start_time: Rule.new(:start_time, './/interval/start', :unix_time),
    duration: Rule.new(:duration, './/interval/duration', :integer)
  }
end

#average_interval_valueObject


348
349
350
# File 'lib/greenbutton/gb_classes.rb', line 348

def average_interval_value
  total/n_readings
end

#docObject


314
315
316
# File 'lib/greenbutton/gb_classes.rb', line 314

def doc
  self.meter_reading.doc
end

#end_timeObject


318
319
320
# File 'lib/greenbutton/gb_classes.rb', line 318

def end_time
  self.start_time + self.duration
end

#n_readingsObject


352
353
354
# File 'lib/greenbutton/gb_classes.rb', line 352

def n_readings
  entry_node.xpath('.//IntervalReading').length
end

#power_of_ten_multiplierObject


322
323
324
# File 'lib/greenbutton/gb_classes.rb', line 322

def power_of_ten_multiplier
  self.meter_reading.reading_type.power_of_ten_multiplier
end

#pre_rule_assignment(parent) ⇒ Object


303
304
305
# File 'lib/greenbutton/gb_classes.rb', line 303

def pre_rule_assignment(parent)
  self.meter_reading = parent
end

#reading_at_time(time) ⇒ Object


326
327
328
329
330
331
332
333
334
335
# File 'lib/greenbutton/gb_classes.rb', line 326

def reading_at_time(time)
  if (time >= self.start_time) && (time < end_time)
    entry_node.xpath('.//IntervalReading').each do |interval_reading|
       intervalReading = IntervalReading.new(interval_reading)
       if (intervalReading.start_time <= time) && (intervalReading.end_time > time)
         return intervalReading
       end
    end   
  end
end

#sum(starttime = nil, endtime = nil) ⇒ Object


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/greenbutton/gb_classes.rb', line 356

def sum(starttime=nil, endtime=nil)
  starttime = starttime.nil? ? self.start_time : starttime.utc
  endtime = endtime.nil? ? end_time : endtime.utc
  sum = 0
  entry_node.xpath('.//IntervalReading').each do |interval_reading|
    intervalReading = IntervalReading.new(interval_reading)
    if intervalReading.start_time >= starttime && intervalReading.start_time < endtime
      if intervalReading.end_time <= endtime
        sum += intervalReading.value
      else
        ratio = (intervalReading.end_time.to_i - endtime.to_i)/intervalReading.duration
        sum += ratio*intervalReading.value
        break
      end
    end
  end
  sum*10**power_of_ten_multiplier  
end

#totalObject


341
342
343
344
345
346
# File 'lib/greenbutton/gb_classes.rb', line 341

def total
  if @total.nil?
    @total = sum
  end
  @total
end

#value_at_time(time) ⇒ Object


337
338
339
# File 'lib/greenbutton/gb_classes.rb', line 337

def value_at_time(time)
  reading_at_time(time).value*10**power_of_ten_multiplier
end