-
Notifications
You must be signed in to change notification settings - Fork 4
/
crosstag_viewer.py
127 lines (112 loc) · 4.43 KB
/
crosstag_viewer.py
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
# -*- coding: utf-8 -*-
import os
from pyfiglet import Figlet
from optparse import OptionParser
import requests
import json
import time
#Test commit
class CrosstagViewer(object):
server = None
port = None
online = None
last_event = None
counter = 0
display_user = False
user_data = None
user_tagins = None
display_time = 20
sleep_time = 2
def main(self, server, port):
self.server = server
self.port = port
current = self.poll_server()
try:
self.last_event = current['index']
except:
print("unable to fetch data =(")
self.last_event = 0
while True:
# current could be NONE you need to deal with that. Make it robust!
# you need to supress the output from the server.
# you need to supress the output from the reader.
# use a logger?
current = self.poll_server()
if self.online:
pass
#self.print_clear_screen("online")
else:
self.print_clear_screen("offline")
if current is None:
self.display_user = False
self.print_clear_screen("offline")
elif current['index'] != self.last_event: # THIS FAILS WHEN THE DB IS EMPTY
self.print_clear_screen("online")
self.last_event = current['index']
self.display_user = True
self.counter = 0
if self.display_user and self.counter >= 0:
# todo johan: imptimize this so we only pull the user data once
self.user_data = self.get_user_data(current['tag_id'])
if not self.user_data and self.display_user:
print("read tag: %s" % current['tag_id'])
self.display_user = False
if self.display_user and self.user_data and self.counter == 0:
self.print_user(self.user_data, self.user_tagins)
self.counter += 1
if self.display_user and self.user_data and self.counter != 0:
self.counter += 1
#print self.display_time - self.counter * self.sleep_time
if self.counter == self.display_time / self.sleep_time:
self.counter = 0
self.display_user = False
self.user_data = None
self.print_clear_screen("online")
time.sleep(self.sleep_time)
def ascii_print(self, text, font='slant'):
f = Figlet(font=font)
print(f.renderText(text))
def get_user_data(self, tag_nbr):
try:
res = requests.get("http://localhost:80/crosstag/v1.0/get_user_data_tag/%s" % tag_nbr, timeout=3)
return json.loads(res.text)
except:
return None
def get_user_stats(self, user_id):
res = requests.get("http://localhost:80/crosstag/v1.0/get_tagevents_user/%s" % user_id, timeout=3)
return json.loads(res.text)
def print_user(self, user_data, user_tagins):
to_print = {}
keys = ['name', 'expiry_date', 'create_date', 'gender']
for k, v in user_data.items():
if k in keys:
to_print[k] = v
for k, v in to_print.items():
print(k.rjust(15), str(v).ljust(10))
def print_clear_screen(self, msg=None):
os.system('clear')
self.ascii_print("Crossfit Kalmar")
if msg:
print (msg)
def poll_server(self):
try:
res = requests.get("http://%s:%d/crosstag/v1.0/last_tagin" % (
self.server, self.port), timeout=3)
res = json.loads(res.text)
self.online = True
return res
except:
self.online = False
return None
if __name__ == '__main__':
parser = OptionParser(usage="usage: %prog [options] arg \nTry this: \
python crosstag_viewer.py", version="%prog 1.0")
parser.add_option("-s", "--server", action="store", type="string",
dest="server", default="localhost",
help="What is the URL to the crosstag server?")
parser.add_option("-p", "--port", action="store", type="int",
dest="port", default=80,
help="What is the PORT of the crosstag server?")
(options, args) = parser.parse_args()
r = CrosstagViewer()
r.main(options.server, options.port)