1 from bisect import insort
2 from time import strftime, time, localtime, mktime
3 from enigma import eTimer
12 def __init__(self, begin, end):
14 self.prepare_time = 20
18 #begindate = localtime(self.begin)
19 #newdate = datetime.datetime(begindate.tm_year, begindate.tm_mon, begindate.tm_mday 0, 0, 0);
20 self.repeatedbegindate = begin
26 self.state = self.StateWaiting
27 self.cancelled = False
28 self.first_try_prepare = True
31 def resetRepeated(self):
32 self.repeated = int(0)
34 def setRepeated(self, day):
35 self.repeated |= (2 ** day)
36 print "Repeated: " + str(self.repeated)
39 return self.state == self.StateRunning
41 def addOneDay(self, timedatestruct):
42 oldHour = timedatestruct.tm_hour
43 newdate = (datetime.datetime(timedatestruct.tm_year, timedatestruct.tm_mon, timedatestruct.tm_mday, timedatestruct.tm_hour, timedatestruct.tm_min, timedatestruct.tm_sec) + datetime.timedelta(days=1)).timetuple()
44 if localtime(mktime(newdate)).tm_hour != oldHour:
45 return (datetime.datetime(timedatestruct.tm_year, timedatestruct.tm_mon, timedatestruct.tm_mday, timedatestruct.tm_hour, timedatestruct.tm_min, timedatestruct.tm_sec) + datetime.timedelta(days=2)).timetuple()
48 # update self.begin and self.end according to the self.repeated-flags
49 def processRepeated(self, findRunningEvent = True):
50 print "ProcessRepeated"
51 if (self.repeated != 0):
54 #to avoid problems with daylight saving, we need to calculate with localtime, in struct_time representation
55 localrepeatedbegindate = localtime(self.repeatedbegindate)
56 localbegin = localtime(self.begin)
57 localend = localtime(self.end)
58 localnow = localtime(now)
60 print "localrepeatedbegindate:", strftime("%c", localrepeatedbegindate)
61 print "localbegin:", strftime("%c", localbegin)
62 print "localend:", strftime("%c", localend)
63 print "localnow:", strftime("%c", localnow)
70 print "Day: " + str(x)
75 # if day is NOT in the list of repeated days
76 # OR if the day IS in the list of the repeated days, check, if event is currently running... then if findRunningEvent is false, go to the next event
77 while ((day[localbegin.tm_wday] != 0) or (mktime(localrepeatedbegindate) > mktime(localbegin)) or
78 ((day[localbegin.tm_wday] == 0) and ((findRunningEvent and localend < localnow) or ((not findRunningEvent) and localbegin < localnow)))):
79 localbegin = self.addOneDay(localbegin)
80 localend = self.addOneDay(localend)
81 print "localbegin after addOneDay:", strftime("%c", localbegin)
82 print "localend after addOneDay:", strftime("%c", localend)
84 #we now have a struct_time representation of begin and end in localtime, but we have to calculate back to (gmt) seconds since epoch
85 self.begin = int(mktime(localbegin))
86 self.end = int(mktime(localend))
87 if self.begin == self.end:
90 print "ProcessRepeated result"
91 print strftime("%c", localtime(self.begin))
92 print strftime("%c", localtime(self.end))
97 return self.getNextActivation() < o.getNextActivation()
104 def timeChanged(self):
107 # check if a timer entry must be skipped
108 def shouldSkip(self):
109 return self.end <= time() and self.state == TimerEntry.StateWaiting
114 # in case timer has not yet started, but gets aborted (so it's preparing),
116 if self.begin > self.end:
117 self.begin = self.end
119 self.cancelled = True
121 # must be overridden!
122 def getNextActivation():
129 self.disabled = False
132 # the time between "polls". We do this because
133 # we want to account for time jumps etc.
134 # of course if they occur <100s before starting,
135 # it's not good. thus, you have to repoll when
136 # you change the time.
138 # this is just in case. We don't want the timer
139 # hanging. we use this "edge-triggered-polling-scheme"
140 # anyway, so why don't make it a bit more fool-proof?
144 self.timer_list = [ ]
145 self.processed_timers = [ ]
147 self.timer = eTimer()
148 self.timer.callback.append(self.calcNextActivation)
149 self.lastActivation = time()
151 self.calcNextActivation()
152 self.on_state_change = [ ]
154 def stateChanged(self, entry):
155 for f in self.on_state_change:
159 self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
161 def addTimerEntry(self, entry, noRecalc=0):
162 entry.processRepeated()
164 # when the timer has not yet started, and is already passed,
165 # don't go trough waiting/running/end-states, but sort it
166 # right into the processedTimers.
167 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded or (entry.state == TimerEntry.StateWaiting and entry.disabled):
168 print "already passed, skipping"
169 print "shouldSkip:", entry.shouldSkip()
170 print "state == ended", entry.state == TimerEntry.StateEnded
171 print "waiting && disabled:", (entry.state == TimerEntry.StateWaiting and entry.disabled)
172 insort(self.processed_timers, entry)
173 entry.state = TimerEntry.StateEnded
175 insort(self.timer_list, entry)
177 self.calcNextActivation()
179 def setNextActivation(self, when):
180 delay = int((when - time()) * 1000)
181 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
183 self.timer.start(delay, 1)
186 def calcNextActivation(self):
187 if self.lastActivation > time():
188 print "[timer.py] timewarp - re-evaluating all processed timers."
189 tl = self.processed_timers
190 self.processed_timers = [ ]
192 # simulate a "waiting" state to give them a chance to re-occure
194 self.addTimerEntry(x, noRecalc=1)
196 self.processActivation()
197 self.lastActivation = time()
199 min = int(time()) + self.MaxWaitTime
201 # calculate next activation point
202 if len(self.timer_list):
203 w = self.timer_list[0].getNextActivation()
207 print "next real activation is", strftime("%c", localtime(w))
209 self.setNextActivation(min)
211 def timeChanged(self, timer):
214 if timer.state == TimerEntry.StateEnded:
215 self.processed_timers.remove(timer)
217 self.timer_list.remove(timer)
219 # give the timer a chance to re-enqueue
220 if timer.state == TimerEntry.StateEnded:
221 timer.state = TimerEntry.StateWaiting
222 self.addTimerEntry(timer)
224 def doActivate(self, w):
225 self.timer_list.remove(w)
227 # when activating a timer which has already passed,
228 # simply abort the timer. don't run trough all the stages.
230 w.state = TimerEntry.StateEnded
232 # when active returns true, this means "accepted".
233 # otherwise, the current state is kept.
234 # the timer entry itself will fix up the delay then.
238 # did this timer reached the last state?
239 if w.state < TimerEntry.StateEnded:
240 # no, sort it into active list
241 insort(self.timer_list, w)
243 # yes. Process repeated, and re-add.
246 w.state = TimerEntry.StateWaiting
247 self.addTimerEntry(w)
249 insort(self.processed_timers, w)
253 def processActivation(self):
254 print "It's now ", strftime("%c", localtime(time()))
257 # we keep on processing the first entry until it goes into the future.
258 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
259 self.doActivate(self.timer_list[0])