-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpower_hungry.rb
More file actions
48 lines (41 loc) · 1.37 KB
/
power_hungry.rb
File metadata and controls
48 lines (41 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'rubygems'
require 'lib/power_hungry/config'
require 'lib/power_hungry/history'
require 'lib/power_hungry/reading'
require 'lib/power_hungry/database'
require 'lib/xbee/packet'
class PowerHungry
def initialize
@serial = IO.popen("python serial_proxy.py #{Config.serial_port}")
end
def debug
@debug = true
run
end
def next_reading
packet = XBee::Packet.new(@serial, @debug)
PowerHungry::Reading.new(packet, @debug)
end
def run
puts "Power Hungry network up and running on #{DateTime.now.strftime("%Y-%m-%d %H:%M")}"
while true
reading = next_reading
puts reading.to_s if @debug
PowerHungry::History.add(reading.sensor, reading.watt_hours, reading.averages[:watts])
if PowerHungry::History.interval_passed?
PowerHungry::History.sensors.each do |sensor|
puts "#{sensor.to_s}: Watt-hours used in the past #{PowerHungry::History::INTERVAL_LENGTH} seconds: #{PowerHungry::History.interval_watt_hours(sensor)}"
Interval.create!(
:watts => PowerHungry::History.interval_watts_avg(sensor),
:watt_hours => PowerHungry::History.interval_watt_hours(sensor),
:interval_length => PowerHungry::History::INTERVAL_LENGTH,
:sensor => sensor
)
end
PowerHungry::History.restart
else
end
end
end
end
PowerHungry::Config.init