11 def __init__(self, begin, end):
13 self.prepare_time = 20
19 def resetRepeated(self):
20 self.repeated = int(0)
22 def setRepeated(self, day):
23 self.repeated |= (2 ** day)
24 print "Repeated: " + str(self.repeated)
27 return self.state == self.StateRunning
29 # update self.begin and self.end according to the self.repeated-flags
30 def processRepeated(self):
31 print "ProcessRepeated"
32 print time.strftime("%c", time.localtime(self.begin))
33 print time.strftime("%c", time.localtime(self.end))
34 if (self.repeated != 0):
35 now = int(time.time()) + 1
42 print "Day: " + str(x)
47 print time.strftime("%c", time.localtime(now))
48 print time.strftime("%c", time.localtime(self.begin))
49 print time.strftime("%c", time.localtime(self.end))
50 print str(time.localtime(self.begin).tm_wday)
51 while ((day[time.localtime(self.begin).tm_wday] != 0) or ((day[time.localtime(self.begin).tm_wday] == 0) and self.end < now)):
52 print time.strftime("%c", time.localtime(self.begin))
53 print time.strftime("%c", time.localtime(self.end))
61 return self.getNextActivation() < o.getNextActivation()
68 def timeChanged(self):
71 # check if a timer entry must be skipped
73 return self.end <= time.time() and self.state == TimerEntry.StateWaiting
76 self.end = time.time()
78 # in case timer has not yet started, but gets aborted (so it's preparing),
80 if self.begin > self.end:
84 def getNextActivation():
88 # the time between "polls". We do this because
89 # we want to account for time jumps etc.
90 # of course if they occur <100s before starting,
91 # it's not good. thus, you have to repoll when
92 # you change the time.
94 # this is just in case. We don't want the timer
95 # hanging. we use this "edge-triggered-polling-scheme"
96 # anyway, so why don't make it a bit more fool-proof?
100 self.timer_list = [ ]
101 self.processed_timers = [ ]
103 self.timer = eTimer()
104 self.timer.timeout.get().append(self.calcNextActivation)
105 self.lastActivation = time.time()
107 self.calcNextActivation()
108 self.on_state_change = [ ]
110 def stateChanged(self, entry):
111 for f in self.on_state_change:
114 def addTimerEntry(self, entry, noRecalc=0):
115 entry.processRepeated()
117 # when the timer has not yet started, and is already passed,
118 # don't go trough waiting/running/end-states, but sort it
119 # right into the processedTimers.
120 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded:
121 print "already passed, skipping"
122 bisect.insort(self.processed_timers, entry)
123 entry.state = TimerEntry.StateEnded
125 bisect.insort(self.timer_list, entry)
127 self.calcNextActivation()
129 def setNextActivation(self, when):
130 delay = int((when - time.time()) * 1000)
131 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
133 self.timer.start(delay, 1)
136 def calcNextActivation(self):
137 if self.lastActivation > time.time():
138 print "[timer.py] timewarp - re-evaluating all processed timers."
139 tl = self.processed_timers
140 self.processed_timers = [ ]
142 # simulate a "waiting" state to give them a chance to re-occure
144 self.addTimerEntry(x, noRecalc=1)
146 self.processActivation()
147 self.lastActivation = time.time()
149 min = int(time.time()) + self.MaxWaitTime
151 # calculate next activation point
152 if len(self.timer_list):
153 w = self.timer_list[0].getNextActivation()
157 self.setNextActivation(min)
159 def timeChanged(self, timer):
161 self.timer_list.remove(timer)
163 self.addTimerEntry(timer)
165 def doActivate(self, w):
166 self.timer_list.remove(w)
168 # when activating a timer which has already passed,
169 # simply abort the timer. don't run trough all the stages.
172 bisect.insort(self.processed_timers, w)
174 # when active returns true, this means "accepted".
175 # otherwise, the current state is kept.
176 # the timer entry itself will fix up the delay then.
180 # did this timer reached the last state?
181 if w.state < TimerEntry.StateEnded:
182 # no, sort it into active list
183 bisect.insort(self.timer_list, w)
185 # yes. Process repeated, and re-add.
188 w.state = TimerEntry.StateWaiting
189 self.addTimerEntry(w)
191 bisect.insort(self.processed_timers, w)
195 def processActivation(self):
196 print "It's now ", time.strftime("%c", time.localtime(time.time()))
197 t = int(time.time()) + 1
199 # we keep on processing the first entry until it goes into the future.
200 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
201 self.doActivate(self.timer_list[0])