Class: MSG_Chumby::LastHourHandler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/msg-chumby-daemon/http-xml-server.rb

Instance Method Summary collapse

Constructor Details

#initialize(reading_cache) ⇒ LastHourHandler

Returns a new instance of LastHourHandler.



87
88
89
# File 'lib/msg-chumby-daemon/http-xml-server.rb', line 87

def initialize(reading_cache)
  @reading_cache=reading_cache;
end

Instance Method Details

#process(request, response) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/msg-chumby-daemon/http-xml-server.rb', line 90

def process(request, response)
  response.start(200) do |head,out|
    head["Content-Type"] = "text/xml"
    readings=(@reading_cache.last_hour())  
    # Even if there are currently no readings we need to provide
    # them.
    if readings==nil
      readings = Array.new();
      (0..59).each {|i|
        timestamp = Time.now.to_i - (i * 60)
        readings << Flukso::UTCReading.new(timestamp, 0.0) 
      }
    end
    flat_data=Array.new;
    readings.each{|reading|
      if not reading.nan? #(reading.value*1.0).nan?
        # Skip NaN values.
      #else
        time=Time.at(reading.utc_timestamp);
        current_reading=
          {'time' => [ time.strftime("%H:%M:%S")], 'value' => [reading.value]};
        flat_data << current_reading
      end
    }
    #pp flat_data
    out.write(XmlSimple.xml_out( { 'reading' => flat_data} ,{'RootName' => "last_hour"}));
  end
end