16 def __init__(self, begin, end):
18 self.prepare_time = 10
23 def resetRepeated(self):
24 self.repeated = int(0)
26 def setRepeated(self, day):
27 self.repeated |= (2 ** day)
28 print "Repeated: " + str(self.repeated)
31 if self.state == self.StateWait:
32 return self.begin - self.prepare_time
33 elif self.state == self.StatePrepare:
39 return self.getTime() < o.getTime()
41 def activate(self, event):
42 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
46 # the time between "polls". We do this because
47 # we want to account for time jumps etc.
48 # of course if they occur <100s before starting,
49 # it's not good. thus, you have to repoll when
50 # you change the time.
52 # this is just in case. We don't want the timer
53 # hanging. we use this "edge-triggered-polling-scheme"
54 # anyway, so why don't make it a bit more fool-proof?
59 self.processed_timers = [ ]
62 self.timer.timeout.get().append(self.calcNextActivation)
63 self.lastActivation = time.time()
65 self.calcNextActivation()
67 def addTimerEntry(self, entry, noRecalc=0):
68 # we either go trough Prepare/Start/End-state if the timer is still running,
69 # or skip it when it's alrady past the end.
70 if entry.end > time.time():
71 bisect.insort(self.timer_list, entry)
73 self.calcNextActivation()
75 bisect.insort(self.processed_timers, entry)
77 def setNextActivation(self, when):
78 delay = int((when - time.time()) * 1000)
79 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
81 self.timer.start(delay, 1)
84 def calcNextActivation(self):
85 if self.lastActivation > time.time():
86 print "[timer.py] timewarp - re-evaluating all processed timers."
87 tl = self.processed_timers
88 self.processed_timers = [ ]
90 self.addTimerEntry(x, noRecalc=1)
92 self.processActivation()
93 self.lastActivation = time.time()
95 min = int(time.time()) + self.MaxWaitTime
97 # calculate next activation point
98 if len(self.timer_list):
99 w = self.timer_list[0].getTime()
103 self.setNextActivation(min)
105 def timeChanged(self, timer):
106 self.timer_list.remove(timer)
107 self.addTimerEntry(timer)
109 def doActivate(self, w):
111 self.timer_list.remove(w)
113 if w.state < TimerEntry.StateEnded:
114 bisect.insort(self.timer_list, w)
116 bisect.insort(self.processed_timers, w)
118 def processActivation(self):
119 t = int(time.time()) + 1
121 # we keep on processing the first entry until it goes into the future.
122 while len(self.timer_list) and self.timer_list[0].getTime() < t:
123 self.doActivate(self.timer_list[0])