Merge pull request #85 from Callum-/dilatometer
[matches/MCTX3420.git] / testing / pygui / pygui.py
1 #!/usr/bin/python
2
3 """
4         Python has a different style of documentation which will break doxygen...
5 """
6
7 import sys
8 import os
9 import matplotlib.pyplot as plt
10 import numpy
11 import requests
12 import datetime
13 import time
14
15 #TODO: Replace with URL of testing server
16 api_url = "https://192.168.1.10/api"
17
18 def log(message):
19         sys.stderr.write("%s: %s : %s\n" % (sys.argv[0], str(datetime.datetime.now()), message))
20
21 def update_plot(plot, axes, data_x, data_y):
22         """
23                 Update data to plot, allegedly this is faster than just replotting everything
24         """
25         plot.set_xdata(numpy.append(plot.get_xdata(), data_x))
26         plot.set_ydata(numpy.append(plot.get_ydata(), data_y))
27         axes.relim()
28         axes.autoscale_view()
29         plt.draw()      
30
31 def main(argv):
32         if (len(argv) < 2):
33                 sys.stderr.write("Usage: %s sensor_id\n" % argv[0])
34                 sys.stderr.write("Identifying sensors...\n\n")
35                 r = requests.get(api_url + "?sensors", verify=False)            
36                 print r.text
37                 return 1        
38         
39         plt.ion()
40
41         fig = plt.figure()
42         axes = fig.add_subplot(111)
43         #NOTE: Comma here is *not* a typo and is extremely important and some kind of mysterious python magical tuple thing
44         #               Do not remove the comma or things will break. Horribly.
45         plot, = axes.plot([],[])
46
47         start_time = 0
48         
49         while True:
50                 params = { "id" : argv[1], "start_time" : "-1", "format" : "tsv"}
51                 try:
52                         r = requests.get(api_url + "/sensors", params=params, verify=False)
53                 except:
54                         log("Failed to make request for data");
55                         return 1
56
57                 if r.status_code != 200:
58                         log("Bad status code %d" % r.status_code)
59                         print r.text
60                         return 1
61                 
62                 log("Got data")
63
64                 data_x = []
65                 data_y = []             
66
67                 count = 0
68                 for line in r.text.split("\n"):
69                         count += 1
70
71                         
72                         point = map(float, line.split("\t"))
73
74                         if point[0] > start_time:
75                                 data_x.append(point[0])
76                                 data_y.append(point[1])
77                                 start_time = point[0]
78
79                 if count > 0:
80                         update_plot(plot, axes, data_x, data_y)
81                         time.sleep(0.5)
82
83         return 0
84
85 # ... This is how you make main work in python. With string comparisons.
86 if __name__ == "__main__":
87         exit(main(sys.argv))
88
89

UCC git Repository :: git.ucc.asn.au