1 from bisect import insort
2 from time import strftime, time, localtime, gmtime, mktime
3 from calendar import timegm
4 from enigma import eTimer
12 def __init__(self, begin, end):
14 self.prepare_time = 20
22 def resetRepeated(self):
23 self.repeated = int(0)
25 def setRepeated(self, day):
26 self.repeated |= (2 ** day)
27 print "Repeated: " + str(self.repeated)
30 return self.state == self.StateRunning
32 # update self.begin and self.end according to the self.repeated-flags
33 def processRepeated(self, findRunningEvent = True):
34 print "ProcessRepeated"
35 if (self.repeated != 0):
38 #to avoid problems with daylight saving, we need to calculate with localtime, in struct_time representation
39 localbegin = localtime(self.begin)
40 localend = localtime(self.end)
41 localnow = localtime(now)
43 print strftime("%c", localbegin)
44 print strftime("%c", localend)
51 print "Day: " + str(x)
56 print strftime("%c", localnow)
58 while ((day[localbegin.tm_wday] != 0) or ((day[localbegin.tm_wday] == 0) and ((findRunningEvent and localend < localnow) or ((not findRunningEvent) and localbegin < localnow)))):
59 print "localbegin:", strftime("%c", localbegin)
60 print "localend:", strftime("%c", localend)
61 #add one day to the struct_time, we have to convert using gmt functions, because the daylight saving flag might change after we add our 86400 seconds
62 localbegin = gmtime(timegm(localbegin) + 86400)
63 localend = gmtime(timegm(localend) + 86400)
65 #we now have a struct_time representation of begin and end in localtime, but we have to calculate back to (gmt) seconds since epoch
66 self.begin = int(mktime(localbegin))
67 self.end = int(mktime(localend)) + 1
69 print "ProcessRepeated result"
70 print strftime("%c", localtime(self.begin))
71 print strftime("%c", localtime(self.end))
76 return self.getNextActivation() < o.getNextActivation()
83 def timeChanged(self):
86 # check if a timer entry must be skipped
88 return self.end <= time() and self.state == TimerEntry.StateWaiting
93 # in case timer has not yet started, but gets aborted (so it's preparing),
95 if self.begin > self.end:
100 # must be overridden!
101 def getNextActivation():
108 self.disabled = False
111 # the time between "polls". We do this because
112 # we want to account for time jumps etc.
113 # of course if they occur <100s before starting,
114 # it's not good. thus, you have to repoll when
115 # you change the time.
117 # this is just in case. We don't want the timer
118 # hanging. we use this "edge-triggered-polling-scheme"
119 # anyway, so why don't make it a bit more fool-proof?
123 self.timer_list = [ ]
124 self.processed_timers = [ ]
126 self.timer = eTimer()
127 self.timer.timeout.get().append(self.calcNextActivation)
128 self.lastActivation = time()
130 self.calcNextActivation()
131 self.on_state_change = [ ]
133 def stateChanged(self, entry):
134 for f in self.on_state_change:
138 self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
140 def addTimerEntry(self, entry, noRecalc=0):
141 entry.processRepeated()
143 # when the timer has not yet started, and is already passed,
144 # don't go trough waiting/running/end-states, but sort it
145 # right into the processedTimers.
146 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded or (entry.state == TimerEntry.StateWaiting and entry.disabled):
147 print "already passed, skipping"
148 print "shouldSkip:", entry.shouldSkip()
149 print "state == ended", entry.state == TimerEntry.StateEnded
150 print "waiting && disabled:", (entry.state == TimerEntry.StateWaiting and entry.disabled)
151 insort(self.processed_timers, entry)
152 entry.state = TimerEntry.StateEnded
154 insort(self.timer_list, entry)
156 self.calcNextActivation()
158 def setNextActivation(self, when):
159 delay = int((when - time()) * 1000)
160 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
162 self.timer.start(delay, 1)
165 def calcNextActivation(self):
166 if self.lastActivation > time():
167 print "[timer.py] timewarp - re-evaluating all processed timers."
168 tl = self.processed_timers
169 self.processed_timers = [ ]
171 # simulate a "waiting" state to give them a chance to re-occure
173 self.addTimerEntry(x, noRecalc=1)
175 self.processActivation()
176 self.lastActivation = time()
178 min = int(time()) + self.MaxWaitTime
180 # calculate next activation point
181 if len(self.timer_list):
182 w = self.timer_list[0].getNextActivation()
186 self.setNextActivation(min)
188 def timeChanged(self, timer):
191 if timer.state == TimerEntry.StateEnded:
192 self.processed_timers.remove(timer)
194 self.timer_list.remove(timer)
196 # give the timer a chance to re-enqueue
197 if timer.state == TimerEntry.StateEnded:
198 timer.state = TimerEntry.StateWaiting
199 self.addTimerEntry(timer)
201 def doActivate(self, w):
202 self.timer_list.remove(w)
204 # when activating a timer which has already passed,
205 # simply abort the timer. don't run trough all the stages.
207 w.state = TimerEntry.StateEnded
209 # when active returns true, this means "accepted".
210 # otherwise, the current state is kept.
211 # the timer entry itself will fix up the delay then.
215 # did this timer reached the last state?
216 if w.state < TimerEntry.StateEnded:
217 # no, sort it into active list
218 insort(self.timer_list, w)
220 # yes. Process repeated, and re-add.
223 w.state = TimerEntry.StateWaiting
224 self.addTimerEntry(w)
226 insort(self.processed_timers, w)
230 def processActivation(self):
231 print "It's now ", strftime("%c", localtime(time()))
234 # we keep on processing the first entry until it goes into the future.
235 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
236 self.doActivate(self.timer_list[0])