16 def __init__(self, begin, end):
18 self.prepare_time = 10
21 self.repeated = int(0)
23 def setRepeated(self, day):
24 self.repeated |= (2 ** day)
25 print "Repeated: " + str(self.repeated)
28 if self.state == self.StateWait:
29 return self.begin - self.prepare_time
30 elif self.state == self.StatePrepare:
36 return self.getTime() < o.getTime()
38 def activate(self, event):
39 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
43 # the time between "polls". We do this because
44 # we want to account for time jumps etc.
45 # of course if they occur <100s before starting,
46 # it's not good. thus, you have to repoll when
47 # you change the time.
49 # this is just in case. We don't want the timer
50 # hanging. we use this "edge-triggered-polling-scheme"
51 # anyway, so why don't make it a bit more fool-proof?
56 self.processed_timers = [ ]
59 self.timer.timeout.get().append(self.calcNextActivation)
60 self.lastActivation = time.time()
62 self.calcNextActivation()
64 def addTimerEntry(self, entry, noRecalc=0):
65 # we either go trough Prepare/Start/End-state if the timer is still running,
66 # or skip it when it's alrady past the end.
67 if entry.end > time.time():
68 bisect.insort(self.timer_list, entry)
70 self.calcNextActivation()
72 bisect.insort(self.processed_timers, entry)
74 def setNextActivation(self, when):
75 delay = int((when - time.time()) * 1000)
76 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
78 self.timer.start(delay, 1)
81 def calcNextActivation(self):
82 if self.lastActivation > time.time():
83 print "[timer.py] timewarp - re-evaluating all processed timers."
84 tl = self.processed_timers
85 self.processed_timers = [ ]
87 self.addTimerEntry(x, noRecalc=1)
89 self.processActivation()
90 self.lastActivation = time.time()
92 min = int(time.time()) + self.MaxWaitTime
94 # calculate next activation point
95 if len(self.timer_list):
96 w = self.timer_list[0].getTime()
100 self.setNextActivation(min)
102 def timeChanged(self, timer):
103 self.timer_list.remove(timer)
104 self.addTimerEntry(timer)
106 def doActivate(self, w):
108 self.timer_list.remove(w)
110 if w.state < TimerEntry.StateEnded:
111 bisect.insort(self.timer_list, w)
113 bisect.insort(self.processed_timers, w)
115 def processActivation(self):
116 t = int(time.time()) + 1
118 # we keep on processing the first entry until it goes into the future.
119 while len(self.timer_list) and self.timer_list[0].getTime() < t:
120 self.doActivate(self.timer_list[0])