Class: Flukso::QueryReadings

Inherits:
Object
  • Object
show all
Defined in:
lib/flukso/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(sensor_id, timerange = :hour, unit = :watt) ⇒ QueryReadings

Returns a new instance of QueryReadings.

Raises:



25
26
27
28
29
30
31
32
33
34
# File 'lib/flukso/api.rb', line 25

def initialize(sensor_id, timerange=:hour, unit=:watt)
  @sensor_id=sensor_id;
  # sanity checks.
  valid_timeranges=[:hour, :day, :month, :year, :night];
  valid_units=[:watt, :kwh, :eur, :aud];
  raise Flukso::General, "Invalid timerange #{timerange}" unless valid_timeranges.include?(timerange);
  raise Flukso::General, "Invalid unit #{unit}" unless valid_units.include?(unit);
  @timerange=timerange;
  @unit=unit;
end

Instance Method Details

#execute(api) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/flukso/api.rb', line 35

def execute(api)
  if api.class != Flukso::API
    raise  Flukso::General, "Cannot execute query: API object invalid"
  end
  query_url="/#{@sensor_id}?interval=#{@timerange}&unit=#{@unit}";
  #query_url="#{Flukso::BASE_SENSOR_URL}/#{@sensor_id}?interval=#{@timerange}&unit=#{@unit}";
  puts "Using query url #{query_url}" if $verbose
  response = api.perform_get(query_url) 
  return wrap_response(response);
end

#wrap_response(response) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/flukso/api.rb', line 45

def wrap_response(response)
  # The response is an array of arrays. Convert it to UTCReadings.
  retval=Array.new();
  response.each{|reading|
    current=UTCReading.new(reading[0], reading[1]);
    retval << current;
  }
  return retval
end