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 "ProcessRepeated"
33 if (self.repeated != 0):
34 now = int(time.time())
45 while ((day[time.localtime(self.begin).tm_wday] != 0) and (self.end < now)):
46 print str(now) + " " + str(self.end) + " " + str(self.begin)
53 if self.state == self.StateWait:
54 return self.begin - self.prepare_time
55 elif self.state == self.StatePrepare:
61 return self.getTime() < o.getTime()
63 def activate(self, event):
64 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
68 # the time between "polls". We do this because
69 # we want to account for time jumps etc.
70 # of course if they occur <100s before starting,
71 # it's not good. thus, you have to repoll when
72 # you change the time.
74 # this is just in case. We don't want the timer
75 # hanging. we use this "edge-triggered-polling-scheme"
76 # anyway, so why don't make it a bit more fool-proof?
81 self.processed_timers = [ ]
84 self.timer.timeout.get().append(self.calcNextActivation)
85 self.lastActivation = time.time()
87 self.calcNextActivation()
89 def addTimerEntry(self, entry, noRecalc=0):
90 entry.processRepeated()
92 # we either go trough Prepare/Start/End-state if the timer is still running,
93 # or skip it when it's alrady past the end.
95 if entry.end > time.time():
96 bisect.insort(self.timer_list, entry)
98 self.calcNextActivation()
100 bisect.insort(self.processed_timers, entry)
102 def setNextActivation(self, when):
103 delay = int((when - time.time()) * 1000)
104 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
106 self.timer.start(delay, 1)
109 def calcNextActivation(self):
110 if self.lastActivation > time.time():
111 print "[timer.py] timewarp - re-evaluating all processed timers."
112 tl = self.processed_timers
113 self.processed_timers = [ ]
115 self.addTimerEntry(x, noRecalc=1)
117 self.processActivation()
118 self.lastActivation = time.time()
120 min = int(time.time()) + self.MaxWaitTime
122 # calculate next activation point
123 if len(self.timer_list):
124 w = self.timer_list[0].getTime()
128 self.setNextActivation(min)
130 def timeChanged(self, timer):
132 self.timer_list.remove(timer)
135 self.addTimerEntry(timer)
137 def doActivate(self, w):
140 self.timer_list.remove(w)
145 if w.state < TimerEntry.StateEnded:
146 bisect.insort(self.timer_list, w)
148 if (w.repeated == 0):
149 bisect.insort(self.processed_timers, w)
152 w.state = TimerEntry.StateWait
155 def processActivation(self):
156 t = int(time.time()) + 1
158 # we keep on processing the first entry until it goes into the future.
159 while len(self.timer_list) and self.timer_list[0].getTime() < t:
160 self.doActivate(self.timer_list[0])