Class: CurrentCostDaemon::Publishers::AMEEPublisher

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AMEEPublisher

Returns a new instance of AMEEPublisher.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/currentcostd/publishers/amee.rb', line 35

def initialize(config)
  @profile_uid = config['amee']['profile_uid']
  @last_minute = Time.now.min - 1
  # Store AMEE connection details
  @server = config['amee']['server']
  @username = config['amee']['username']
  @password = config['amee']['password']
	# Get electricity UID for later
	req = Net::HTTP::Get.new("/data/home/energy/quantity/drill?type=electricity")
	req.basic_auth @username, @password
	req['Accept'] = "application/xml"
	http = Net::HTTP.new(@server)
	http.start do
		response = http.request(req)
		raise response.body if (response.code != "200" && response.code != "201")
		@uid = response.body.match("<Choices><Name>uid</Name><Choices><Choice><Name>.*?</Name><Value>(.*?)</Value></Choice></Choices></Choices>")[1]
	end
end

Class Method Details

.config_sectionObject



31
32
33
# File 'lib/currentcostd/publishers/amee.rb', line 31

def self.config_section
  'amee'
end

Instance Method Details

#update(reading) ⇒ Object



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
87
88
89
# File 'lib/currentcostd/publishers/amee.rb', line 54

def update(reading)
  return if reading.total_watts == 0
  # Let's put data into AMEE every minute.
  if Time.now.min != @last_minute
    # Store time
    @last_minute = Time.now.min
    # Estimate kwh figure from current power usage
    kwh = (reading.total_watts / 1000.0)
    # Create POST options
    raise "No Data Item UID found!" if @uid.nil?
    options = {
      :dataItemUid => @uid,
    :startDate => Time.now.xmlschema,
    :endDate => (Time.now + 60).xmlschema,
    :energyConsumption => kwh,
    :energyConsumptionUnit => "kWh",
    :energyConsumptionPerUnit => "h",
    :name => "currentcost"
    }
    puts "Storing in AMEE..."
    # Post data to AMEE
    req = Net::HTTP::Post.new("/profiles/#{@profile_uid}/home/energy/quantity")
    req.basic_auth @username, @password
    req['Accept'] = "application/xml"
    req.set_form_data(options)
    http = Net::HTTP.new(@server)
    http.start do
      response = http.request(req)
      raise response.body if (response.code != "200" && response.code != "201")
    end
    puts "done"
  end
rescue
  puts "Something went wrong (AMEE)!"
  puts $!.inspect
end