11 def __init__(self, begin, end):
13 self.prepare_time = 20
21 def resetRepeated(self):
22 self.repeated = int(0)
24 def setRepeated(self, day):
25 self.repeated |= (2 ** day)
26 print "Repeated: " + str(self.repeated)
29 return self.state == self.StateRunning
31 # update self.begin and self.end according to the self.repeated-flags
32 def processRepeated(self):
33 print "ProcessRepeated"
34 print time.strftime("%c", time.localtime(self.begin))
35 print time.strftime("%c", time.localtime(self.end))
36 if (self.repeated != 0):
37 now = int(time.time()) + 1
44 print "Day: " + str(x)
49 print time.strftime("%c", time.localtime(now))
50 print time.strftime("%c", time.localtime(self.begin))
51 print time.strftime("%c", time.localtime(self.end))
52 print str(time.localtime(self.begin).tm_wday)
53 while ((day[time.localtime(self.begin).tm_wday] != 0) or ((day[time.localtime(self.begin).tm_wday] == 0) and self.end < now)):
54 print time.strftime("%c", time.localtime(self.begin))
55 print time.strftime("%c", time.localtime(self.end))
63 return self.getNextActivation() < o.getNextActivation()
70 def timeChanged(self):
73 # check if a timer entry must be skipped
75 return self.end <= time.time() and self.state == TimerEntry.StateWaiting
78 self.end = time.time()
80 # in case timer has not yet started, but gets aborted (so it's preparing),
82 if self.begin > self.end:
88 def getNextActivation():
92 # the time between "polls". We do this because
93 # we want to account for time jumps etc.
94 # of course if they occur <100s before starting,
95 # it's not good. thus, you have to repoll when
96 # you change the time.
98 # this is just in case. We don't want the timer
99 # hanging. we use this "edge-triggered-polling-scheme"
100 # anyway, so why don't make it a bit more fool-proof?
104 self.timer_list = [ ]
105 self.processed_timers = [ ]
107 self.timer = eTimer()
108 self.timer.timeout.get().append(self.calcNextActivation)
109 self.lastActivation = time.time()
111 self.calcNextActivation()
112 self.on_state_change = [ ]
114 def stateChanged(self, entry):
115 for f in self.on_state_change:
118 def getNextRecordingTime(self):
119 if len(self.timer_list) > 0:
120 return self.timer_list[0].begin
124 self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
126 def addTimerEntry(self, entry, noRecalc=0):
127 entry.processRepeated()
129 # when the timer has not yet started, and is already passed,
130 # don't go trough waiting/running/end-states, but sort it
131 # right into the processedTimers.
132 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded or (entry.state == TimerEntry.StateWaiting and entry.disabled):
133 print "already passed, skipping"
134 bisect.insort(self.processed_timers, entry)
135 entry.state = TimerEntry.StateEnded
137 bisect.insort(self.timer_list, entry)
139 self.calcNextActivation()
141 def setNextActivation(self, when):
142 delay = int((when - time.time()) * 1000)
143 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
145 self.timer.start(delay, 1)
148 def calcNextActivation(self):
149 if self.lastActivation > time.time():
150 print "[timer.py] timewarp - re-evaluating all processed timers."
151 tl = self.processed_timers
152 self.processed_timers = [ ]
154 # simulate a "waiting" state to give them a chance to re-occure
156 self.addTimerEntry(x, noRecalc=1)
158 self.processActivation()
159 self.lastActivation = time.time()
161 min = int(time.time()) + self.MaxWaitTime
163 # calculate next activation point
164 if len(self.timer_list):
165 w = self.timer_list[0].getNextActivation()
169 self.setNextActivation(min)
171 def timeChanged(self, timer):
173 if timer.state == TimerEntry.StateEnded:
174 self.processed_timers.remove(timer)
176 self.timer_list.remove(timer)
178 self.addTimerEntry(timer)
180 def doActivate(self, w):
181 self.timer_list.remove(w)
183 # when activating a timer which has already passed,
184 # simply abort the timer. don't run trough all the stages.
186 w.state = TimerEntry.StateEnded
188 # when active returns true, this means "accepted".
189 # otherwise, the current state is kept.
190 # the timer entry itself will fix up the delay then.
194 # did this timer reached the last state?
195 if w.state < TimerEntry.StateEnded:
196 # no, sort it into active list
197 bisect.insort(self.timer_list, w)
199 # yes. Process repeated, and re-add.
202 w.state = TimerEntry.StateWaiting
203 self.addTimerEntry(w)
205 bisect.insort(self.processed_timers, w)
209 def processActivation(self):
210 print "It's now ", time.strftime("%c", time.localtime(time.time()))
211 t = int(time.time()) + 1
213 # we keep on processing the first entry until it goes into the future.
214 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
215 self.doActivate(self.timer_list[0])