Class: CurrentCostDaemon::Publishers::Pachube

Inherits:
Object
  • Object
show all
Defined in:
lib/currentcostd/publishers/pachube.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Pachube

Returns a new instance of Pachube.



36
37
38
39
# File 'lib/currentcostd/publishers/pachube.rb', line 36

def initialize(config)
  @feed = config['pachube']['feed_id']
  @api_key = config['pachube']['api_key']
end

Class Method Details

.config_sectionObject



32
33
34
# File 'lib/currentcostd/publishers/pachube.rb', line 32

def self.config_section
  'pachube'
end

Instance Method Details

#update(reading) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/currentcostd/publishers/pachube.rb', line 41

def update(reading)
  # 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 = reading.total_watts
  eeml.set_updated!
  # Put data
  put = Net::HTTP::Put.new("/feeds/#{@feed}.xml")
  put.body = eeml.to_eeml
  put['X-PachubeApiKey'] = @api_key
  http = Net::HTTP.new('www.pachube.com')
  http.start
  http.request(put)
rescue
  puts "Something went wrong (pachube)!"
  puts $!.inspect
end