1 from bisect import insort
2 from time import strftime, time, localtime, gmtime, mktime
3 from calendar import timegm
4 from enigma import eTimer
14 def __init__(self, begin, end):
16 self.prepare_time = 20
24 def resetRepeated(self):
25 self.repeated = int(0)
27 def setRepeated(self, day):
28 self.repeated |= (2 ** day)
29 print "Repeated: " + str(self.repeated)
32 return self.state == self.StateRunning
34 def addOneDay(self, timedatestruct):
35 oldHour = timedatestruct.tm_hour
36 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()
37 if localtime(mktime(newdate)).tm_hour != oldHour:
38 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()
41 # update self.begin and self.end according to the self.repeated-flags
42 def processRepeated(self, findRunningEvent = True):
43 print "ProcessRepeated"
44 if (self.repeated != 0):
47 #to avoid problems with daylight saving, we need to calculate with localtime, in struct_time representation
48 localbegin = localtime(self.begin)
49 localend = localtime(self.end)
50 localnow = localtime(now)
52 print "localbegin:", strftime("%c", localbegin)
53 print "localend:", strftime("%c", localend)
60 print "Day: " + str(x)
65 print strftime("%c", localnow)
67 while ((day[localbegin.tm_wday] != 0) or ((day[localbegin.tm_wday] == 0) and ((findRunningEvent and localend < localnow) or ((not findRunningEvent) and localbegin < localnow)))):
68 localbegin = self.addOneDay(localbegin)
69 localend = self.addOneDay(localend)
70 print "localbegin after addOneDay:", strftime("%c", localbegin)
71 print "localend after addOneDay:", strftime("%c", localend)
73 #we now have a struct_time representation of begin and end in localtime, but we have to calculate back to (gmt) seconds since epoch
74 self.begin = int(mktime(localbegin))
75 self.end = int(mktime(localend)) + 1
77 print "ProcessRepeated result"
78 print strftime("%c", localtime(self.begin))
79 print strftime("%c", localtime(self.end))
84 return self.getNextActivation() < o.getNextActivation()
91 def timeChanged(self):
94 # check if a timer entry must be skipped
96 return self.end <= time() and self.state == TimerEntry.StateWaiting
101 # in case timer has not yet started, but gets aborted (so it's preparing),
103 if self.begin > self.end:
104 self.begin = self.end
106 self.cancelled = True
108 # must be overridden!
109 def getNextActivation():
116 self.disabled = False
119 # the time between "polls". We do this because
120 # we want to account for time jumps etc.
121 # of course if they occur <100s before starting,
122 # it's not good. thus, you have to repoll when
123 # you change the time.
125 # this is just in case. We don't want the timer
126 # hanging. we use this "edge-triggered-polling-scheme"
127 # anyway, so why don't make it a bit more fool-proof?
131 self.timer_list = [ ]
132 self.processed_timers = [ ]
134 self.timer = eTimer()
135 self.timer.timeout.get().append(self.calcNextActivation)
136 self.lastActivation = time()
138 self.calcNextActivation()
139 self.on_state_change = [ ]
141 def stateChanged(self, entry):
142 for f in self.on_state_change:
146 self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
148 def addTimerEntry(self, entry, noRecalc=0):
149 entry.processRepeated()
151 # when the timer has not yet started, and is already passed,
152 # don't go trough waiting/running/end-states, but sort it
153 # right into the processedTimers.
154 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded or (entry.state == TimerEntry.StateWaiting and entry.disabled):
155 print "already passed, skipping"
156 print "shouldSkip:", entry.shouldSkip()
157 print "state == ended", entry.state == TimerEntry.StateEnded
158 print "waiting && disabled:", (entry.state == TimerEntry.StateWaiting and entry.disabled)
159 insort(self.processed_timers, entry)
160 entry.state = TimerEntry.StateEnded
162 insort(self.timer_list, entry)
164 self.calcNextActivation()
166 def setNextActivation(self, when):
167 delay = int((when - time()) * 1000)
168 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
170 self.timer.start(delay, 1)
173 def calcNextActivation(self):
174 if self.lastActivation > time():
175 print "[timer.py] timewarp - re-evaluating all processed timers."
176 tl = self.processed_timers
177 self.processed_timers = [ ]
179 # simulate a "waiting" state to give them a chance to re-occure
181 self.addTimerEntry(x, noRecalc=1)
183 self.processActivation()
184 self.lastActivation = time()
186 min = int(time()) + self.MaxWaitTime
188 # calculate next activation point
189 if len(self.timer_list):
190 w = self.timer_list[0].getNextActivation()
194 self.setNextActivation(min)
196 def timeChanged(self, timer):
199 if timer.state == TimerEntry.StateEnded:
200 self.processed_timers.remove(timer)
202 self.timer_list.remove(timer)
204 # give the timer a chance to re-enqueue
205 if timer.state == TimerEntry.StateEnded:
206 timer.state = TimerEntry.StateWaiting
207 self.addTimerEntry(timer)
209 def doActivate(self, w):
210 self.timer_list.remove(w)
212 # when activating a timer which has already passed,
213 # simply abort the timer. don't run trough all the stages.
215 w.state = TimerEntry.StateEnded
217 # when active returns true, this means "accepted".
218 # otherwise, the current state is kept.
219 # the timer entry itself will fix up the delay then.
223 # did this timer reached the last state?
224 if w.state < TimerEntry.StateEnded:
225 # no, sort it into active list
226 insort(self.timer_list, w)
228 # yes. Process repeated, and re-add.
231 w.state = TimerEntry.StateWaiting
232 self.addTimerEntry(w)
234 insort(self.processed_timers, w)
238 def processActivation(self):
239 print "It's now ", strftime("%c", localtime(time()))
242 # we keep on processing the first entry until it goes into the future.
243 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
244 self.doActivate(self.timer_list[0])