Class: CurrentCostDaemon::Publishers::Http::Servlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/currentcostd/publishers/http.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.update(reading) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/currentcostd/publishers/http.rb', line 87

def self.update(reading)
  @@reading = reading
  @@updated_at = Time.now
  # Add all channels to get real total
  @@total = @@reading.total_watts
  # Store history if available
  @@history = @@reading.history if @@reading.history
end

Instance Method Details

#do_GET(request, response) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
84
85
86
# File 'lib/currentcostd/publishers/http.rb', line 36

def do_GET(request, response)
  if request.query['format'].nil? || request.query['format'] == "html"
    response.status = 200
    response['Content-Type'] = "text/html"
    response.body = "
<html>
  <head>
    <meta http-equiv='refresh' content='6' />
    <meta name='viewport' content='width=420'/> 
  </head>
  <body>
    <div style='float:right'>
      <a title='EEML' href='/?format=eeml'><span style='border:1px solid;border-color:#9C9 #030 #030 #696;padding:0 3px;font:bold 10px verdana,sans-serif;color:#FFF;background:#090;text-decoration:none;margin:0;'>EEML</span></a>
      <a title='XML' href='/?format=xml'><span style='border:1px solid;border-color:#FC9 #630 #330 #F96;padding:0 3px;font:bold 10px verdana,sans-serif;color:#FFF;background:#F60;text-decoration:none;margin:0;'>XML</span></a>
    </div>
    <h1>#{@@total} Watts</h2>
    <p>updated at #{@@updated_at}</p>
    <h2>History</h2>
    <h3>Hourly</h3>
    <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:hours].reverse.delete_if{|x|x.nil?}.join(',')}&chds=0,#{@@history[:hours].delete_if{|x|x.nil?}.max}'/>
    <h3>Daily</h3>
    <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:days].reverse.map{|x| x.nil? ? 0 : x[0]}.join(',')}&chds=0,#{@@history[:days].delete_if{|x|x.nil?}.max}'/>
    <h3>Monthly</h3>
    <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:months].reverse.map{|x| x.nil? ? 0 : x[0]}.join(',')}&chds=0,#{@@history[:months].delete_if{|x|x.nil?}.max}'/>
    <h3>Yearly</h3>
    <img src='http://chart.apis.google.com/chart?cht=lc&chs=400x125&chd=t:#{@@history[:years].reverse.map{|x| x.nil? ? 0 : x[0]}.join(',')}&chds=0,#{@@history[:years].delete_if{|x|x.nil?}.max}'/>
  </body>
</html>"
  elsif request.query['format'] == "eeml"
    # Create EEML document
    eeml = EEML::Environment.new
    # Create data object
    data = EEML::Data.new(0)
    data.unit = EEML::Unit.new("Watts", :symbol => 'W', :type => :derivedSI)
    eeml << data
    eeml[0].value = @@total
    eeml.updated_at = @@updated_at
    response.status = 200
    response['Content-Type'] = "text/xml"
    response.body = eeml.to_eeml
  elsif request.query['format'] == "xml"
    response.status = 200
    response['Content-Type'] = "text/xml"
    # Create XML
    xml = Builder::XmlMarkup.new
    xml.instruct!
    response.body = xml.currentcost { xml.watts @@total }
  else
    response.status = 400
  end
end