Last active
July 8, 2016 20:52
-
-
Save metadaddy/56c330cfca66722f5fab8e40770939cf to your computer and use it in GitHub Desktop.
Get metrics from StreamSets Data Collector and show them on the Raspberry Pi PiTFT screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
With thanks to jeremyblythe for showing how to use pygame with | |
the PiTFT screen. | |
''' | |
import argparse | |
import pygame | |
import os | |
from time import sleep | |
import RPi.GPIO as GPIO | |
import locale | |
import json | |
import requests | |
import urllib | |
parser = argparse.ArgumentParser(description='Display SDC pipeline metrics.') | |
parser.add_argument('pipelineName', metavar='pipelineName', nargs=1, | |
help='The name of a pipeline in SDC') | |
args = parser.parse_args() | |
pipeline = urllib.quote(args.pipelineName[0]) | |
locale.setlocale(locale.LC_ALL, '') | |
button_map = {23:(255,0,0), 22:(0,255,0), 27:(0,0,255), 17:(0,0,0)} | |
#Setup the GPIOs as inputs with Pull Ups since the buttons are connected to GND | |
GPIO.setmode(GPIO.BCM) | |
for k in button_map.keys(): | |
GPIO.setup(k, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
#Buttons | |
START_BUTTON = 17 | |
STOP_BUTTON = 22 | |
#Colours | |
BLACK = (0,0,0) | |
WHITE = (255,255,255) | |
BLUE = (31,119,180) | |
GREEN = (92,184,92) | |
RED = (255,51,51) | |
#Bars | |
BAR_WIDTH = 80 | |
BAR_HEIGHT = 190 | |
BAR_Y = 25 | |
INPUT_X = 20 | |
OUTPUT_X = 120 | |
ERROR_X = 220 | |
#UI | |
X_RES = 320 | |
Y_RES = 240 | |
TEXT_SIZE = 24 | |
#PiTFT setup | |
os.putenv('SDL_FBDEV', '/dev/fb1') | |
pygame.init() | |
pygame.mouse.set_visible(False) | |
lcd = pygame.display.set_mode((X_RES, Y_RES)) | |
lcd.fill(WHITE) | |
pygame.display.update() | |
font = pygame.font.Font(None, TEXT_SIZE) | |
#Draw a histogram bar | |
def drawBar(lcd, n, label, x, y, width, height, color): | |
pygame.draw.rect(lcd, color, [x, Y_RES - y - height, width, height]) | |
text_surface = font.render(label, True, BLACK) | |
rect = text_surface.get_rect(center=((x + (width/2),Y_RES - (y - (font.get_linesize() / 2))))) | |
lcd.blit(text_surface, rect) | |
text_surface = font.render(locale.format("%d", n, grouping=True), True, BLACK) | |
rect = text_surface.get_rect(center=((x + (width/2),Y_RES - (y + height +(font.get_linesize() / 2))))) | |
lcd.blit(text_surface, rect) | |
counter = 0 | |
debounce = False | |
while True: | |
if (not debounce): | |
pressed = None; | |
if (GPIO.input(START_BUTTON) == False): | |
pressed = 'start' | |
elif (GPIO.input(STOP_BUTTON) == False): | |
pressed = 'stop' | |
if (pressed is not None): | |
print 'Sending {} request to {}'.format(pressed, pipeline) | |
r = requests.post('http://localhost:18630/rest/v1/pipeline/{}/{}?rev=0'.format(pipeline, pressed), | |
auth=('admin', 'admin'), headers={'X-Requested-By': 'sdc'}) | |
print 'Status code {}'.format(r.status_code) | |
debounce = True; | |
continue | |
counter += 1 | |
# Only poll SDC every second | |
if (counter % 10 != 1): | |
continue | |
debounce = False | |
lcd.fill(WHITE) | |
r = requests.get('http://localhost:18630/rest/v1/pipeline/{}/metrics?rev=0'.format(pipeline), | |
auth=('admin', 'admin'), headers={'X-Requested-By': 'sdc'}) | |
input = 0 | |
output = 0 | |
error = 0 | |
if (r.status_code == 200): | |
try: | |
data = r.json() | |
input = data["meters"]["pipeline.batchInputRecords.meter"]["count"] | |
output = data["meters"]["pipeline.batchOutputRecords.meter"]["count"] | |
error = data["meters"]["pipeline.batchErrorRecords.meter"]["count"] | |
except ValueError: | |
print 'Decoding JSON failed: {}'.format(r.text) | |
max_val = max(input, output, error) | |
if (max_val > 0): | |
scale = BAR_HEIGHT / float(max_val) | |
else: | |
scale = 0 | |
drawBar(lcd, input, 'Input', INPUT_X, BAR_Y, BAR_WIDTH, input * scale, BLUE) | |
drawBar(lcd, output, 'Output', OUTPUT_X, BAR_Y, BAR_WIDTH, output * scale, GREEN) | |
drawBar(lcd, error, 'Error', ERROR_X, BAR_Y, BAR_WIDTH, error * scale, RED) | |
pygame.display.update() | |
sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment