-
Notifications
You must be signed in to change notification settings - Fork 0
/
statssign_feeder
executable file
·100 lines (93 loc) · 2.94 KB
/
statssign_feeder
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
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
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/ruby
# Statistics Sign Feeder
# Takes data from various stats exchanges and displays them nicely on the
# stats sign.
# Input: Exchanges 'statistics.bandwidth' and 'statistics.power'
# Output: Statistics sign via UDP
require 'rubygems'
require 'amqp'
require 'json'
require 'socket'
require 'inifile'
cfg = IniFile.load(File.expand_path("~/.labamqprc"))
amqp_host = cfg['broker']['BrokerHost']
amqp_user = cfg['statssign']['BrokerUsername']
amqp_pass = cfg['statssign']['BrokerPassword']
@screen_nbr = 0
@cable_up_nice = "0.000";
@cable_down_nice = "0.000";
@power_watts_nice = "0.000";
@power_cost_nice = "0.000";
draw_next_screen = Proc.new {
# 0 = CABL
# 1 = Cable b/w
# 2 = ELEC
# 3 = Power stats
puts "Drawing screen #{@screen_nbr}."
case @screen_nbr
when 0
@udp.send "cAbL\n \n", 0
EM.add_timer(0.75, draw_next_screen)
p Time.now
when 1
@udp.send "#{@cable_down_nice}\n#{@cable_up_nice}\n", 0
EM.add_timer(2, draw_next_screen)
p Time.now
when 2
@udp.send "ELEC\n \n", 0
EM.add_timer(0.75, draw_next_screen)
p Time.now
when 3
@udp.send "#{@power_watts_nice}\n#{@power_cost_nice}\n", 0
EM.add_timer(2, draw_next_screen)
p Time.now
@screen_nbr = -1
end
@screen_nbr += 1
}
@udp = UDPSocket.new
@udp.connect "sign1", 1000
AMQP.logging = true
AMQP.start(:host => amqp_host, :user => amqp_user, :pass => amqp_pass) do
amq = AMQP::Channel.new
bw_exchange = amq.fanout('statistics.bandwidth', :durable => false, :auto_delete => false);
bw_queue = amq.queue('statistics.bandwidth.statssign', :durable => false, :auto_delete => true);
bw_queue.bind(bw_exchange);
bw_queue.subscribe do |header, raw_msg|
msg = JSON.parse(raw_msg)
cable_up = msg["cable"]["up"]
cable_down = msg["cable"]["down"]
if(cable_up == -1) then
@cable_up_nice = @cable_down_nice = "----"
else
@cable_up_nice = '%1.3f' % cable_up
@cable_down_nice = '%1.3f' % cable_down
end
puts "CU: #{@cable_up_nice} CD: #{@cable_down_nice}";
end
pwr_exchange = amq.fanout('statistics.power', :durable => false, :auto_delete => false);
pwr_queue = amq.queue('statistics.power.statssign', :durable => false, :auto_delete => true);
pwr_queue.bind(pwr_exchange);
s6_watts = 0
s6_cost = 0
s12_watts = 0
s12_cost = 0
pwr_queue.subscribe do |header, raw_msg|
msg = JSON.parse(raw_msg)
if(msg["meter_id"] == "6-LC-01") then
s6_watts = msg["watts"]
s6_cost = msg["cost"]
puts "P6W: #{s6_watts} P6C: #{s6_cost}"
elsif(msg["meter_id"] == "12-LC-01") then
s12_watts = msg["watts"]
s12_cost = msg["cost"]
puts "P12W: #{s12_watts} P12C: #{s12_cost}"
end
power_watts = s6_watts + s12_watts
power_cost = s6_cost + s12_cost
@power_watts_nice = power_watts
@power_cost_nice = '%1.3f' % power_cost
puts "PW: #{@power_watts_nice} PC: #{@power_cost_nice}"
end
EM.add_timer(0, draw_next_screen)
end