-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarker_timing.py
152 lines (120 loc) · 4.38 KB
/
marker_timing.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 11 21:21:42 2022
@author: thlu
"""
from dataclasses import dataclass, field
from typing import List
import time, datetime
@dataclass
class Marker_data:
id: int
in_box: bool
last_change: float = 0
last_seen: float = field(default_factory=time.time)
racing: int = -1
time_start: float = 0
time_end: float = 0
# def __repr__(self):
# return str(self.id)
def __eq__(self, other) -> bool:
return self.id == other
@dataclass
class Active_markers:
markers: List[Marker_data]
def __iter__(self):
yield from (self.markers)
def update(self, id, in_box):
index = self.find(id)
if index >= 0:
# print(id, "in", index)
if self.markers[index].racing == -1:
# outside box, not in box yet
if in_box:
# now in box, ready to start
self.markers[index].racing = 0
self.markers[index].in_box = True
self.markers[index].last_change = time.time()
else:
# Still outside box, no change
pass
elif self.markers[index].racing == 0:
# In box, ready to start
if in_box:
# Still in box, bo change
pass
else:
# Now out of box, start racing
self.markers[index].racing = 1
self.markers[index].in_box = False
self.markers[index].last_change = time.time()
self.markers[index].time_start = time.time()
elif self.markers[index].racing == 1:
# Racing, time is running...
if in_box:
# Done racing, stop timer
self.markers[index].racing = 2
self.markers[index].in_box = True
self.markers[index].last_change = time.time()
self.markers[index].time_end = time.time()
else:
# Still racing
pass
elif self.markers[index].racing == 2:
# Race done.
# TODO: How to reset?
pass
else:
print("Error var racing wrong value")
# update hardbead
self.markers[index].last_seen = time.time()
else:
self.markers.append(Marker_data(id, in_box))
# print(id, "Added!")
def find(self, id):
if id not in self.markers:
return -1
for i,marker in enumerate(self.markers):
if marker == id:
break
return i
def status_text(self):
t = ""
for marker in self.markers:
t += "Bil {:02d}".format(marker.id)
if marker.racing == 1:
seconds = round(time.time()-marker.time_start,2)
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
t += " Racing\t{:.0f}:{:02.0f}:{:02.02f}".format(h, m, s)
elif marker.racing == 2:
seconds = round(marker.time_end-marker.time_start,2)
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
t += " Done\t{:.0f}:{:02.0f}:{:02.02f}".format(h, m, s)
elif marker.racing == 0 and marker.in_box:
t += " Ready to start! "
t += "\n"
return t
if __name__ == '__main__':
markers_seen = Active_markers([])
markers_seen.update(1, False)
print(markers_seen.status_text())
# print(markers_seen)
markers_seen.update(1, True)
print(markers_seen.status_text())
# print(markers_seen)
markers_seen.update(1, False)
print(markers_seen.status_text())
# print(markers_seen)
time.sleep(.4)
markers_seen.update(1, False)
print(markers_seen.status_text())
# print(markers_seen)
time.sleep(.3)
markers_seen.update(1, True)
print(markers_seen.status_text())
# print(markers_seen)
markers_seen.update(1, False)
print(markers_seen.status_text())
# print(markers_seen)