Class: MSG_Chumby::LastMinuteHandler

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) ⇒ LastMinuteHandler

Returns a new instance of LastMinuteHandler.



53
54
55
# File 'lib/msg-chumby-daemon/http-xml-server.rb', line 53

def initialize(reading_cache)
  @reading_cache=reading_cache;
end

Instance Method Details

#process(request, response) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/msg-chumby-daemon/http-xml-server.rb', line 56

def process(request, response)
  response.start(200) do |head,out|
    head["Content-Type"] = "text/xml"
    readings=(@reading_cache.last_minute())  
    # 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
        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_minute"}));
  end
end