class Egauge::Client

Constants

EGAUGE_PATH

Attributes

client[R]

Public Class Methods

new(base_url) click to toggle source

The base_url should look something like this: 'egauge12345.egaug.es'

# File lib/egauge/client.rb, line 10
def initialize(base_url)
  @client = HTTPClient.new(base_url: base_url)
end

Public Instance Methods

fetch(options) click to toggle source

The options hash should have 3 keys defined:

  1. :timestamp [Timestamp] - This is the point in time from which to fetch past readings. The “latest” reading will be the beginning of the current period breakdown. For instance if the timestamp is 2018-10-20 13:06 and the breakdown is hourly, the latest reading will be from 2018-10-20 13:00

  2. :breakdown [Symbol] - This defines the time period between the readings. This can be :hour, :day or :month

  3. :count [Integer] - Number of past readings to fetch

This method returns an array of hashes with these values

- "Date & Time" (An integer representing time since epoch)
- "Usage [kWh]"
- "Generation [kWh]"
- "Solar [kWh]"
- "Solar+ [kWh]"
# File lib/egauge/client.rb, line 31
def fetch(options)
  timestamps = build_timestamps(options)
  params = { T: timestamps.join(","), c: nil }
  query(params)
end

Private Instance Methods

build_timestamps(options) click to toggle source
# File lib/egauge/client.rb, line 40
def build_timestamps(options)
  timestamp = options[:timestamp]
  breakdown = options[:breakdown]
  count     = options[:count]

  end_time = timestamp.send("beginning_of_#{breakdown}".to_sym)
  time_method = "#{breakdown}s"
  timestamps = []
  count.times do |i|
    timestamps << (end_time - i.send(time_method)).to_i
  end
  timestamps
end
query(params) click to toggle source
# File lib/egauge/client.rb, line 54
def query(params)
  csv_str = client.get(EGAUGE_PATH + query_string(params)).body
  records = []
  CSV.parse(csv_str, headers: true){|l| records << l.to_h }
  records
end
query_string(params) click to toggle source
# File lib/egauge/client.rb, line 61
def query_string(params)
  params.stringify_keys.map do |key, value|
    query_parameter = key.dup
    query_parameter << "=#{value.to_s}" unless value.nil?
    query_parameter
  end.join('&')
end