-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.rb
54 lines (42 loc) · 1.01 KB
/
run.rb
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
49
50
51
52
53
54
#!/usr/bin/env ruby
require "bunny"
require "json"
load 'nut_adapter.rb'
settings = JSON.parse File.read("settings.json")
bunny = Bunny.new(
settings['amqp_url'],
{
client_properties: {
connection_name: settings['device_id']
}
}
)
bunny.start
channel = bunny.create_channel
exchange = channel.topic("sensors", auto_delete: false, durable: true)
adapter = NutAdapter.new(settings['ups_name'])
lastSend = Time.now
readings = []
while true do
# check if it's been a minute since we last sent anything
if(lastSend < (Time.now - 60) && readings.any?)
# collect average
average = readings.sum / readings.count
# publish average
exchange.publish(
{
device_id: settings['device_id'],
ups_name: settings['ups_name'],
watts: average,
time: Time.now.to_i
}.to_json,
routing_key: "power"
)
# reset variables
readings = []
lastSend = Time.now
end
# collect current_load value
readings << adapter.current_load
sleep 5
end