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)
30 # update self.begin and self.end according to the self.repeated-flags
31 def processRepeated(self):
32 print "Processing repeated"
33 if (self.repeated != 0):
34 now = int(time.time())
35 print "Now: " + str(now)
47 print time.localtime(self.begin).tm_wday
49 print str(now) + " " + str(self.end) + " " + str(self.begin)
50 while ((day[time.localtime(self.begin).tm_wday] != 0) and (self.end < now)):
51 print str(now) + " " + str(self.end) + " " + str(self.begin)
58 if self.state == self.StateWait:
59 return self.begin - self.prepare_time
60 elif self.state == self.StatePrepare:
66 return self.getTime() < o.getTime()
68 def activate(self, event):
69 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
73 # the time between "polls". We do this because
74 # we want to account for time jumps etc.
75 # of course if they occur <100s before starting,
76 # it's not good. thus, you have to repoll when
77 # you change the time.
79 # this is just in case. We don't want the timer
80 # hanging. we use this "edge-triggered-polling-scheme"
81 # anyway, so why don't make it a bit more fool-proof?
86 self.processed_timers = [ ]
89 self.timer.timeout.get().append(self.calcNextActivation)
90 self.lastActivation = time.time()
92 self.calcNextActivation()
94 def addTimerEntry(self, entry, noRecalc=0):
95 entry.processRepeated()
97 # we either go trough Prepare/Start/End-state if the timer is still running,
98 # or skip it when it's alrady past the end.
100 if entry.end > time.time():
101 bisect.insort(self.timer_list, entry)
103 self.calcNextActivation()
105 bisect.insort(self.processed_timers, entry)
107 def setNextActivation(self, when):
108 delay = int((when - time.time()) * 1000)
109 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
111 self.timer.start(delay, 1)
114 def calcNextActivation(self):
115 if self.lastActivation > time.time():
116 print "[timer.py] timewarp - re-evaluating all processed timers."
117 tl = self.processed_timers
118 self.processed_timers = [ ]
120 self.addTimerEntry(x, noRecalc=1)
122 self.processActivation()
123 self.lastActivation = time.time()
125 min = int(time.time()) + self.MaxWaitTime
127 # calculate next activation point
128 if len(self.timer_list):
129 w = self.timer_list[0].getTime()
133 self.setNextActivation(min)
135 def timeChanged(self, timer):
136 self.timer_list.remove(timer)
137 self.addTimerEntry(timer)
139 def doActivate(self, w):
141 self.timer_list.remove(w)
143 if w.state < TimerEntry.StateEnded:
144 bisect.insort(self.timer_list, w)
146 bisect.insort(self.processed_timers, w)
148 def processActivation(self):
149 t = int(time.time()) + 1
151 # we keep on processing the first entry until it goes into the future.
152 while len(self.timer_list) and self.timer_list[0].getTime() < t:
153 self.doActivate(self.timer_list[0])