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))
58 return self.getNextActivation() < o.getNextActivation()
65 def timeChanged(self):
68 # check if a timer entry must be skipped
70 return self.end <= time.time() and self.state == TimerEntry.StateWaiting
73 self.end = time.time()
75 # in case timer has not yet started, but gets aborted (so it's preparing),
77 if self.begin > self.end:
81 def getNextActivation():
85 # the time between "polls". We do this because
86 # we want to account for time jumps etc.
87 # of course if they occur <100s before starting,
88 # it's not good. thus, you have to repoll when
89 # you change the time.
91 # this is just in case. We don't want the timer
92 # hanging. we use this "edge-triggered-polling-scheme"
93 # anyway, so why don't make it a bit more fool-proof?
98 self.processed_timers = [ ]
100 self.timer = eTimer()
101 self.timer.timeout.get().append(self.calcNextActivation)
102 self.lastActivation = time.time()
104 self.calcNextActivation()
106 def stateChanged(self, entry):
109 def addTimerEntry(self, entry, noRecalc=0):
110 entry.processRepeated()
112 # when the timer has not yet started, and is already passed,
113 # don't go trough waiting/running/end-states, but sort it
114 # right into the processedTimers.
115 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded:
116 print "already passed, skipping"
117 bisect.insort(self.processed_timers, entry)
118 entry.state = TimerEntry.StateEnded
120 bisect.insort(self.timer_list, entry)
122 self.calcNextActivation()
124 def setNextActivation(self, when):
125 delay = int((when - time.time()) * 1000)
126 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
128 self.timer.start(delay, 1)
131 def calcNextActivation(self):
132 if self.lastActivation > time.time():
133 print "[timer.py] timewarp - re-evaluating all processed timers."
134 tl = self.processed_timers
135 self.processed_timers = [ ]
137 # simulate a "waiting" state to give them a chance to re-occure
139 self.addTimerEntry(x, noRecalc=1)
141 self.processActivation()
142 self.lastActivation = time.time()
144 min = int(time.time()) + self.MaxWaitTime
146 # calculate next activation point
147 if len(self.timer_list):
148 w = self.timer_list[0].getNextActivation()
152 self.setNextActivation(min)
154 def timeChanged(self, timer):
156 self.timer_list.remove(timer)
158 self.addTimerEntry(timer)
160 def doActivate(self, w):
161 self.timer_list.remove(w)
163 # when activating a timer which has already passed,
164 # simply abort the timer. don't run trough all the stages.
167 bisect.insort(self.processed_timers, w)
169 # when active returns true, this means "accepted".
170 # otherwise, the current state is kept.
171 # the timer entry itself will fix up the delay then.
175 # did this timer reached the last state?
176 if w.state < TimerEntry.StateEnded:
177 # no, sort it into active list
178 bisect.insort(self.timer_list, w)
180 # yes. Process repeated, and re-add.
183 w.state = TimerEntry.StateWaiting
184 self.addTimerEntry(w)
186 bisect.insort(self.processed_timers, w)
190 def processActivation(self):
191 print "It's now ", time.strftime("%c", time.localtime(time.time()))
192 t = int(time.time()) + 1
194 # we keep on processing the first entry until it goes into the future.
195 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
196 self.doActivate(self.timer_list[0])