From: Sam Moore Date: Fri, 18 Oct 2013 13:37:18 +0000 (+0800) Subject: Add python based GUI as proof of concept X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=e33cd834a9f4a9d48b63270c22a8ee96a14814d5;p=matches%2FMCTX3420.git Add python based GUI as proof of concept Uses matplotlib.pyplot, numpy and requests Interfaces with existing server API. Could potentially be expanded using Tkinter or something. TKinter won't look as pretty as a jQuery GUI though. Also I have no idea how cookies work with requests. At the moment you have to disable the login check in fastcgi.c to test this. --- diff --git a/testing/pygui/pygui.py b/testing/pygui/pygui.py new file mode 100755 index 0000000..a27e003 --- /dev/null +++ b/testing/pygui/pygui.py @@ -0,0 +1,89 @@ +#!/usr/bin/python + +""" + Python has a different style of documentation which will break doxygen... +""" + +import sys +import os +import matplotlib.pyplot as plt +import numpy +import requests +import datetime +import time + +#TODO: Replace with URL of testing server +api_url = "https://daedalus/api" + +def log(message): + sys.stderr.write("%s: %s : %s\n" % (sys.argv[0], str(datetime.datetime.now()), message)) + +def update_plot(plot, axes, data_x, data_y): + """ + Update data to plot, allegedly this is faster than just replotting everything + """ + plot.set_xdata(numpy.append(plot.get_xdata(), data_x)) + plot.set_ydata(numpy.append(plot.get_ydata(), data_y)) + axes.relim() + axes.autoscale_view() + plt.draw() + +def main(argv): + if (len(argv) < 2): + sys.stderr.write("Usage: %s sensor_id\n" % argv[0]) + sys.stderr.write("Identifying sensors...\n\n") + r = requests.get(api_url + "?sensors", verify=False) + print r.text + return 1 + + plt.ion() + + fig = plt.figure() + axes = fig.add_subplot(111) + #NOTE: Comma here is *not* a typo and is extremely important and some kind of mysterious python magical tuple thing + # Do not remove the comma or things will break. Horribly. + plot, = axes.plot([],[]) + + start_time = 0 + + while True: + params = { "id" : argv[1], "start_time" : "-1", "format" : "tsv"} + try: + r = requests.get(api_url + "/sensors", params=params, verify=False) + except: + log("Failed to make request for data"); + return 1 + + if r.status_code != 200: + log("Bad status code %d" % r.status_code) + print r.text + return 1 + + log("Got data") + + data_x = [] + data_y = [] + + count = 0 + for line in r.text.split("\n"): + count += 1 + + + point = map(float, line.split("\t")) + + if point[0] > start_time: + data_x.append(point[0]) + data_y.append(point[1]) + start_time = point[0] + + if count > 0: + update_plot(plot, axes, data_x, data_y) + time.sleep(0.5) + + return 0 + +# ... This is how you make main work in python. With string comparisons. +if __name__ == "__main__": + exit(main(sys.argv)) + +