-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_reader.py
More file actions
executable file
·332 lines (274 loc) · 9.74 KB
/
queue_reader.py
File metadata and controls
executable file
·332 lines (274 loc) · 9.74 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/python -u
from collections import OrderedDict
from json import dumps, load, loads
import os
import pika
from random import randint
import signal
import threading
import time
import unicornhathd as uhhd
# uhhd.brightness(1)
# uhhd.set_pixel(0, 0, 150, 150, 0)
# uhhd.show()
#
# time.sleep(1)
#
# uhhd.off()
use_animation = True
script_path = os.path.dirname(os.path.abspath(__file__))
with open('{sp}/test_interfaces.config.json'.format(sp=script_path), 'r') as f:
config = load(f)
running = True
def graceful_exit(*args):
global running
global rabbit
print('Exiting gracefully...')
running = False
rabbit.stop_receiving()
class Pixel:
cycle_count = 54
target_rgb = [0, 0, 0]
current_rgb = [0, 0, 0]
increment = [0, 0, 0]
animate = False
def __init__(
self,
is_edge=False,
is_successful=True,
is_blank=False,
source=0,
animate=False,
):
if is_blank:
self.current_rgb = self.target_rgb = [0, 0, 0]
self.animate = False
elif not is_successful:
self.current_rgb = self.target_rgb = [255, 0, 0]
self.animate = False
else:
self.animate = animate
if is_edge:
color = randint(54, 70)
marker_color = 0
else:
color = randint(114, 140)
marker_color = color
red = color
green = color
blue = color
if source == 0:
green = marker_color
elif source == 1:
blue = marker_color
else:
red = marker_color
self.target_rgb = [red, green, blue]
zeros = [0, 0, 0]
if self.animate:
self.current_rgb = zeros
self.increment = [
max(round(red / self.cycle_count), 1),
max(round(green / self.cycle_count), 1),
max(round(blue / self.cycle_count), 1),
]
else:
self.current_rgb = self.target_rgb
self.increment = zeros
@staticmethod
def EMPTY_PIXEL():
return Pixel(is_blank=True)
def increment_animation(self):
# print(
# 'incrementing pixel'
# f'r/g/b ({self.current_rgb[0]}/{self.current_rgb[1]}/{self.current_rgb[2]}), '
# f't r/g/b ({self.target_rgb[0]}/{self.target_rgb[1]}/{self.target_rgb[2]})'
# )
for p in range(3):
self.current_rgb[p] += (
self.increment[p] if self.current_rgb[p] < self.target_rgb[p] else 0
)
def get_rgb(self):
if self.animate:
if sum(self.target_rgb) > sum(self.current_rgb):
self.increment_animation()
else:
self.animate = False
return self.current_rgb
def get_is_animating(self):
return self.animate
class RabbitConn:
consumer_tag = None
def __init__(self, rabbit_config):
credentials = pika.PlainCredentials(rabbit_config['user'], rabbit_config['password'])
parameters = pika.ConnectionParameters(host=rabbit_config['host'], credentials=credentials)
self.queue = rabbit_config['queue']
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
self.channel.queue_declare(queue=self.queue, durable=True, arguments={
'x-max-length': rabbit_config['max-queue-length']
})
def send(self, results):
self.channel.basic_publish(exchange='', routing_key=self.queue, body=dumps(results))
def receive(self, callback):
self.consumer_tag = self.channel.basic_consume(queue=self.queue, auto_ack=True, on_message_callback=callback)
self.channel.start_consuming()
def stop_receiving(self):
self.channel.basic_cancel(self.consumer_tag)
def disconnect(self):
self.connection.close()
class ResultsGrid:
def __init__(self, config):
self.max_length = 16
self.row_length = 4
self.use_markers = False
self.config = config
self.queues = OrderedDict()
self.interface_to_pos = {}
self.target_to_pos = {}
self.clear_grid()
self._init_interface_to_pos()
self._init_target_to_pos()
def clear_grid(self):
for connection in self.config['connection_configs']:
interface = connection['interface']
self.queues[interface] = self._gen_empty_queue()
self.queues['local'] = self._gen_empty_queue()
self.queues['combined'] = self._gen_empty_queue()
def _init_interface_to_pos(self):
count = 0
for interface in self.queues:
self.interface_to_pos[interface] = count
count += 1
def _init_target_to_pos(self):
count = -1
for target in self.config['ips_to_ping']:
count += 1
self.target_to_pos[target] = count
for target in self.config['hosts_to_ping']:
# Intentionally not incrementing count here so the hosts can be last, regardless of host is being pinged.
self.target_to_pos[target] = count
def _gen_empty_queue(self):
queue = []
for x in range(self.max_length):
queue.append(self._gen_empty_row())
return queue
def _gen_empty_row(self):
row = []
for x in range(self.row_length):
row.append({
'pixel': Pixel.EMPTY_PIXEL(),
'result': False
})
return row
def _gen_pixel(self, source, result):
is_edge = result['target'] in self.config['hosts_to_ping']
is_successful = result['result']
return Pixel(
is_edge = is_edge,
is_successful = is_successful,
source = source,
animate = use_animation,
)
# if not is_successful:
# pixel = Pixel(255, 0, 0, False)
# else:
# if is_edge:
# color = randint(54, 70)
# marker_color = 0 if (
# is_marker or not self.use_markers
# ) else color
# else:
# color = randint(114, 140)
# marker_color = 0 if is_marker else color
# if source == 0:
# pixel = Pixel(color, marker_color, color, use_animation)
# elif source == 1:
# pixel = Pixel(color, color, marker_color, use_animation)
# else:
# pixel = Pixel(marker_color, color, color, use_animation)
# return pixel
def add_message(self, message):
if message['interface'] not in self.queues:
print('Found invalid interface (%s)' % (message['interface']))
else:
queue = self.queues[message['interface']]
if message['target'] not in self.target_to_pos:
print('Skipping unexpected target (%s) for interface (%s)'
% (message['target'], message['interface']))
return
target_position = self.target_to_pos[message['target']]
if target_position == 0:
queue.insert(0, self._gen_empty_row())
self.trim_queue(queue)
results = queue[0]
results[target_position] = {
'pixel': self._gen_pixel(self.interface_to_pos[message['interface']], message),
'result': message['result']
}
# if target_position == len(results) - 1:
# queue.insert(0, self._gen_empty_row())
# self.trim_queue(queue)
def trim_queue(self, queue):
self.queues['combined'].insert(0, queue.pop())
self.queues['combined'].pop()
def for_display(self):
_grid = []
for x in range(self.max_length):
row = []
for interface in self.queues:
queue = self.queues[interface]
for y in range(self.row_length):
row.append(queue[x][y]['pixel'])
_grid.append(row)
return _grid
class ThreadedDraw(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global results_grid
animating = True
while animating:
animating = set_uhhd(results_grid.for_display())
def set_uhhd(grid):
animating = False
for x in range(len(grid)):
row = grid[x]
for y in range(len(row)):
# print(f'set_pixel({x}, {y}, [{row[y][0]}, {row[y][1]}, {row[y][2]}])')
rgb = row[y].get_rgb()
animating = animating or row[y].get_is_animating()
uhhd.set_pixel(x, y, rgb[0], rgb[1], rgb[2])
uhhd.show()
return animating
messages_processed = 0
uhhd_thread = None
def msg_received(ch, method, properties, body):
global results_grid
global messages_processed
global uhhd_thread
messages_processed += 1
# print(' [x] Received # %06d: %r' % (messages_processed, body))
results = loads(body)
print(' [x] Received # %06d: %11s/%s' % (messages_processed, results['service_name'], results['target']))
results_grid.add_message(results)
if uhhd_thread is None or not uhhd_thread.is_alive():
uhhd_thread = ThreadedDraw()
uhhd_thread.start()
signal.signal(signal.SIGINT, graceful_exit)
signal.signal(signal.SIGTERM, graceful_exit)
rabbit = RabbitConn(config['rabbit_config'])
results_grid = ResultsGrid(config)
uhhd.brightness(0.6)
uhhd.rotation(180)
set_uhhd(results_grid.for_display())
while running:
try:
rabbit.receive(msg_received)
except KeyboardInterrupt:
graceful_exit()
uhhd.off()
# for row in grid:
# for col in row:
# print('%r' % col)
# print()