-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.py
135 lines (92 loc) · 3.66 KB
/
timer.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
import time, threading, re, wx
from player import Timer
from wx.lib.masked import TimeCtrl
class TimePicker(wx.Dialog):
def __init__(self, parent, pos = (0,0), size = (200,110)):
wx.Dialog.__init__(self, parent, wx.ID_ANY, "Korekta czasu", pos, size)
#self.SetBackgroundColour((230,255,230))
self.Bind(wx.EVT_CLOSE, self.OnClose)
Sizer1 = wx.BoxSizer(wx.VERTICAL)
self.c = wx.StaticText(self, -1, u"Podaj aktualną godzinę:")
Sizer1.Add(self.c, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 3)
self.tc = TimeCtrl(self, -1, fmt24hr = True)
Sizer1.Add(self.tc, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 3)
self.tc.SetValue(wx.DateTime.Now())
self.ok = wx.Button(self, -1, "OK")
self.ok.Bind(wx.EVT_BUTTON, self.OnConfirm)
Sizer1.Add(self.ok, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 3)
self.t = Timer(self.OnUpdate, 1000)
self.SetSizer(Sizer1)
self.Layout()
def OnClose(self, e):
self.t.stop()
self.EndModal(1)
e.Skip()
def OnConfirm(self, e):
self.t.stop()
self.EndModal(1)
e.Skip()
def OnUpdate(self):
value = self.tc.GetValue(as_wxTimeSpan = 1)#as_wxDateTime, as_mxDateTime, as_wxTimeSpan, as_mxDateTimeDelta
value += wx.TimeSpan(0, 0, 1, 0)
sel = self.tc.GetSelection()
self.tc.ChangeValue(value)
self.tc.SetSelection(sel[0], sel[1])
def GetLag(self):
now = wx.DateTime.Now()
t = now - now.GetDateOnly()
return self.tc.GetValue(as_wxTimeSpan = 1) - t
class TimeKeeper(threading.Thread):
def __init__(self, path, lag, OnStart, OnEnd, Update):
threading.Thread.__init__(self)
self.running = True
self.path = path
self.lag = lag
self.OnStart = OnStart
self.OnEnd = OnEnd
self.Update = Update
self.loadlib()
self.last = []
for x in self.table:
self.last.append(False)
self.start()
def loadlib(self):
self.table = []
p = re.compile(r"(\d{1,2}):(\d{2})-(\d{1,2}):(\d{2})")
f = open(self.path, "r")
for l in f:
m = p.search(l)
self.table.append(((int(m.group(1)), int(m.group(2))), (int(m.group(3)), int(m.group(4)))))
# ^ Adds to self.table touple of 2 touples: ((hs, ms), (he, me)) where h?, m? = huors, minutes; ?s = start, ?e = end
#print self.table
def isin(self, t, tt):
return tt[0][0] <= t[0] <= tt[1][0] and tt[0][1] <= t[1] < tt[1][1]
def nowin(self, tt):
t = wx.DateTime.Now()
now = (t - t.GetDateOnly()) + self.lag
h = now.GetHours()
m = now.GetMinutes() - h*60
return self.isin((h, m), tt)
def run(self):
time.sleep(3)
while self.running:
for x in self.table:
i = self.table.index(x)
s = self.nowin(x)
if s and (not self.last[i]):
self.OnStart()
if (not s) and self.last[i]:
self.OnEnd()
self.last[i] = s
time.sleep(1)
t = wx.DateTime.Now()
now = (t - t.GetDateOnly()) + self.lag
try:
self.Update(str(now))
except:
pass
def stop(self):
self.running = False
#return 1
#raise Exception("Force exit")
#self.join(0)