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 day = timedatestruct.tm_mday
36 month = timedatestruct.tm_mon
37 year = timedatestruct.tm_year
39 if calendar.isleap(year):
43 monthdays = [0, 31, leap, 31, 30, 31, 30, 31, 31,30, 31,30, 31]
46 # check for sane dates and correct if needed
47 if day > monthdays[month]:
54 newdate = datetime.datetime(year, month, day, timedatestruct.tm_hour, timedatestruct.tm_min, timedatestruct.tm_sec)
55 return newdate.timetuple()
57 # update self.begin and self.end according to the self.repeated-flags
58 def processRepeated(self, findRunningEvent = True):
59 print "ProcessRepeated"
60 if (self.repeated != 0):
63 #to avoid problems with daylight saving, we need to calculate with localtime, in struct_time representation
64 localbegin = localtime(self.begin)
65 localend = localtime(self.end)
66 localnow = localtime(now)
68 print strftime("%c", localbegin)
69 print strftime("%c", localend)
76 print "Day: " + str(x)
81 print strftime("%c", localnow)
83 while ((day[localbegin.tm_wday] != 0) or ((day[localbegin.tm_wday] == 0) and ((findRunningEvent and localend < localnow) or ((not findRunningEvent) and localbegin < localnow)))):
84 print "localbegin:", strftime("%c", localbegin)
85 print "localend:", strftime("%c", localend)
86 localbegin = self.addOneDay(localbegin)
87 localend = self.addOneDay(localend)
89 #we now have a struct_time representation of begin and end in localtime, but we have to calculate back to (gmt) seconds since epoch
90 self.begin = int(mktime(localbegin))
91 self.end = int(mktime(localend)) + 1
93 print "ProcessRepeated result"
94 print strftime("%c", localtime(self.begin))
95 print strftime("%c", localtime(self.end))
100 return self.getNextActivation() < o.getNextActivation()
107 def timeChanged(self):
110 # check if a timer entry must be skipped
111 def shouldSkip(self):
112 return self.end <= time() and self.state == TimerEntry.StateWaiting
117 # in case timer has not yet started, but gets aborted (so it's preparing),
119 if self.begin > self.end:
120 self.begin = self.end
122 self.cancelled = True
124 # must be overridden!
125 def getNextActivation():
132 self.disabled = False
135 # the time between "polls". We do this because
136 # we want to account for time jumps etc.
137 # of course if they occur <100s before starting,
138 # it's not good. thus, you have to repoll when
139 # you change the time.
141 # this is just in case. We don't want the timer
142 # hanging. we use this "edge-triggered-polling-scheme"
143 # anyway, so why don't make it a bit more fool-proof?
147 self.timer_list = [ ]
148 self.processed_timers = [ ]
150 self.timer = eTimer()
151 self.timer.timeout.get().append(self.calcNextActivation)
152 self.lastActivation = time()
154 self.calcNextActivation()
155 self.on_state_change = [ ]
157 def stateChanged(self, entry):
158 for f in self.on_state_change:
162 self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
164 def addTimerEntry(self, entry, noRecalc=0):
165 entry.processRepeated()
167 # when the timer has not yet started, and is already passed,
168 # don't go trough waiting/running/end-states, but sort it
169 # right into the processedTimers.
170 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded or (entry.state == TimerEntry.StateWaiting and entry.disabled):
171 print "already passed, skipping"
172 print "shouldSkip:", entry.shouldSkip()
173 print "state == ended", entry.state == TimerEntry.StateEnded
174 print "waiting && disabled:", (entry.state == TimerEntry.StateWaiting and entry.disabled)
175 insort(self.processed_timers, entry)
176 entry.state = TimerEntry.StateEnded
178 insort(self.timer_list, entry)
180 self.calcNextActivation()
182 def setNextActivation(self, when):
183 delay = int((when - time()) * 1000)
184 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
186 self.timer.start(delay, 1)
189 def calcNextActivation(self):
190 if self.lastActivation > time():
191 print "[timer.py] timewarp - re-evaluating all processed timers."
192 tl = self.processed_timers
193 self.processed_timers = [ ]
195 # simulate a "waiting" state to give them a chance to re-occure
197 self.addTimerEntry(x, noRecalc=1)
199 self.processActivation()
200 self.lastActivation = time()
202 min = int(time()) + self.MaxWaitTime
204 # calculate next activation point
205 if len(self.timer_list):
206 w = self.timer_list[0].getNextActivation()
210 self.setNextActivation(min)
212 def timeChanged(self, timer):
215 if timer.state == TimerEntry.StateEnded:
216 self.processed_timers.remove(timer)
218 self.timer_list.remove(timer)
220 # give the timer a chance to re-enqueue
221 if timer.state == TimerEntry.StateEnded:
222 timer.state = TimerEntry.StateWaiting
223 self.addTimerEntry(timer)
225 def doActivate(self, w):
226 self.timer_list.remove(w)
228 # when activating a timer which has already passed,
229 # simply abort the timer. don't run trough all the stages.
231 w.state = TimerEntry.StateEnded
233 # when active returns true, this means "accepted".
234 # otherwise, the current state is kept.
235 # the timer entry itself will fix up the delay then.
239 # did this timer reached the last state?
240 if w.state < TimerEntry.StateEnded:
241 # no, sort it into active list
242 insort(self.timer_list, w)
244 # yes. Process repeated, and re-add.
247 w.state = TimerEntry.StateWaiting
248 self.addTimerEntry(w)
250 insort(self.processed_timers, w)
254 def processActivation(self):
255 print "It's now ", strftime("%c", localtime(time()))
258 # we keep on processing the first entry until it goes into the future.
259 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
260 self.doActivate(self.timer_list[0])