-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimed.py
94 lines (85 loc) · 3.12 KB
/
timed.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
import redis
import time
import threading
r = redis.Redis(host='localhost', port=6379, db=0)
started = False
start_time = None
pause_time = 0
paused = False
quit = False
def interaction_thread():
global quit
global started
global start_time
global paused
global r
quit = False
while not quit:
try:
x = raw_input("timed> ");
if x.lower() == "start":
if started: print "already started the competition at:", start_time
else:
started = True
start_competition()
elif x.lower() == "pause":
print "paused"
paused = True
r.set("org.srobo.time.paused", True)
elif x.lower() == "resume":
paused = False
r.set("org.srobo.time.paused", False)
elif x.lower() == "quit":
quit = True
elif x.lower() == "get":
print "real time", r.get("org.srobo.time.real")
print "competition time", r.get("org.srobo.time.competition")
print "competition start time", r.get("org.srobo.time.start")
print "competition offset", r.get("org.srobo.time.offset")
elif x.lower()[0:len("setstart")] == "setstart":
#plus one because of the space
diff = int(x.lower()[len("setstart") + 1:].strip())
now = time.time()
local_start_time = now + diff * 60
r.set("org.srobo.time.start", local_start_time)
elif x.lower() == "help":
print "this is timed"
print "the commands are"
print "- start: start the timed clock, requires org.srobo.time.start to exist"
print "- pause: pause the timed clock"
print "- resume: resume the timed clock"
print "- quit: quit timed"
print "- setstart: sets the start time of the competition, offset in minutes"
except:
break
time_thread = None
def start_competition():
global time_thread
global start_time
time_thread = threading.Thread(target=clock_thread)
start_time = float(r.get("org.srobo.time.start"))
if start_time == None:
print "the start time for the competiton has not been set"
print "to set it, create a key in redis \"org.srobo.time.start\""
print "with a value equal to the unix time of the start of the competition"
exit()
time_thread.start()
def clock_thread():
global pause_time
global start_time
global quit
last = time.time()
while not quit:
r.set("org.srobo.time.real", time.time())
paused = r.get("org.srobo.time.paused") == "True"
if paused:
pause_time += time.time() - last
r.set("org.srobo.time.competition", time.time() - pause_time - start_time)
r.set("org.srobo.time.offset", pause_time + start_time)
last = time.time()
time.sleep(1)
def main():
interaction_thread()
if __name__ == "__main__":
main()
quit = True