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
17 self.findRunningEvent = True
18 self.findNextEvent = False
20 #begindate = localtime(self.begin)
21 #newdate = datetime.datetime(begindate.tm_year, begindate.tm_mon, begindate.tm_mday 0, 0, 0);
22 self.repeatedbegindate = begin
28 self.state = self.StateWaiting
29 self.cancelled = False
30 self.first_try_prepare = True
31 self.findRunningEvent = True
32 self.findNextEvent = False
35 def resetRepeated(self):
36 self.repeated = int(0)
38 def setRepeated(self, day):
39 self.repeated |= (2 ** day)
42 return self.state == self.StateRunning
44 def addOneDay(self, timedatestruct):
45 oldHour = timedatestruct.tm_hour
46 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()
47 if localtime(mktime(newdate)).tm_hour != oldHour:
48 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()
51 def isFindRunningEvent(self):
52 return self.findRunningEvent
54 def isFindNextEvent(self):
55 return self.findNextEvent
57 # update self.begin and self.end according to the self.repeated-flags
58 def processRepeated(self, findRunningEvent=True, findNextEvent=False):
59 if (self.repeated != 0):
63 self.findRunningEvent = findRunningEvent
64 self.findNextEvent = findNextEvent
65 #to avoid problems with daylight saving, we need to calculate with localtime, in struct_time representation
66 localrepeatedbegindate = localtime(self.repeatedbegindate)
67 localbegin = localtime(self.begin)
68 localend = localtime(self.end)
69 localnow = localtime(now)
73 for x in (0, 1, 2, 3, 4, 5, 6):
80 # if day is NOT in the list of repeated days
81 # 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
82 while ((day[localbegin.tm_wday] != 0) or (mktime(localrepeatedbegindate) > mktime(localbegin)) or
83 ((day[localbegin.tm_wday] == 0) and ((findRunningEvent and localend < localnow) or ((not findRunningEvent) and localbegin < localnow)))):
84 localbegin = self.addOneDay(localbegin)
85 localend = self.addOneDay(localend)
87 #we now have a struct_time representation of begin and end in localtime, but we have to calculate back to (gmt) seconds since epoch
88 self.begin = int(mktime(localbegin))
89 self.end = int(mktime(localend))
90 if self.begin == self.end:
96 return self.getNextActivation() < o.getNextActivation()
103 def timeChanged(self):
106 # check if a timer entry must be skipped
107 def shouldSkip(self):
108 return self.end <= time() and self.state == TimerEntry.StateWaiting
113 # in case timer has not yet started, but gets aborted (so it's preparing),
115 if self.begin > self.end:
116 self.begin = self.end
118 self.cancelled = True
120 # must be overridden!
121 def getNextActivation():
128 self.disabled = False
131 # the time between "polls". We do this because
132 # we want to account for time jumps etc.
133 # of course if they occur <100s before starting,
134 # it's not good. thus, you have to repoll when
135 # you change the time.
137 # this is just in case. We don't want the timer
138 # hanging. we use this "edge-triggered-polling-scheme"
139 # anyway, so why don't make it a bit more fool-proof?
143 self.timer_list = [ ]
144 self.processed_timers = [ ]
146 self.timer = eTimer()
147 self.timer.callback.append(self.calcNextActivation)
148 self.lastActivation = time()
150 self.calcNextActivation()
151 self.on_state_change = [ ]
153 def stateChanged(self, entry):
154 for f in self.on_state_change:
158 self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
160 def cleanupDaily(self, days):
161 limit = time() - (days * 3600 * 24)
162 self.processed_timers = [entry for entry in self.processed_timers if (entry.disabled and entry.repeated) or (entry.end and (entry.end > limit))]
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 insort(self.processed_timers, entry)
172 entry.state = TimerEntry.StateEnded
174 insort(self.timer_list, entry)
176 self.calcNextActivation()
178 # small piece of example code to understand how to use record simulation
179 # if NavigationInstance.instance:
182 # for timer in self.timer_list:
185 # if timer.state == 0: #waiting
186 # lst.append(NavigationInstance.instance.recordService(timer.service_ref))
188 # print "STATE: ", timer.state
191 # if rec.start(True): #simulate
192 # print "FAILED!!!!!!!!!!!!"
194 # print "OK!!!!!!!!!!!!!!"
195 # NavigationInstance.instance.stopRecordService(rec)
199 def setNextActivation(self, now, when):
200 delay = int((when - now) * 1000)
201 self.timer.start(delay, 1)
204 def calcNextActivation(self):
206 if self.lastActivation > now:
207 print "[timer.py] timewarp - re-evaluating all processed timers."
208 tl = self.processed_timers
209 self.processed_timers = [ ]
211 # simulate a "waiting" state to give them a chance to re-occure
213 self.addTimerEntry(x, noRecalc=1)
215 self.processActivation()
216 self.lastActivation = now
218 min = int(now) + self.MaxWaitTime
220 # calculate next activation point
222 w = self.timer_list[0].getNextActivation()
226 if int(now) < 1072224000 and min > now + 5:
227 # system time has not yet been set (before 01.01.2004), keep a short poll interval
230 self.setNextActivation(now, min)
232 def timeChanged(self, timer):
235 if timer.state == TimerEntry.StateEnded:
236 self.processed_timers.remove(timer)
239 self.timer_list.remove(timer)
241 print "[timer] Failed to remove, not in list"
243 # give the timer a chance to re-enqueue
244 if timer.state == TimerEntry.StateEnded:
245 timer.state = TimerEntry.StateWaiting
246 self.addTimerEntry(timer)
248 def doActivate(self, w):
249 self.timer_list.remove(w)
251 # when activating a timer which has already passed,
252 # simply abort the timer. don't run trough all the stages.
254 w.state = TimerEntry.StateEnded
256 # when active returns true, this means "accepted".
257 # otherwise, the current state is kept.
258 # the timer entry itself will fix up the delay then.
262 # did this timer reached the last state?
263 if w.state < TimerEntry.StateEnded:
264 # no, sort it into active list
265 insort(self.timer_list, w)
267 # yes. Process repeated, and re-add.
270 w.state = TimerEntry.StateWaiting
271 self.addTimerEntry(w)
273 insort(self.processed_timers, w)
277 def processActivation(self):
279 # we keep on processing the first entry until it goes into the future.
280 while self.timer_list and self.timer_list[0].getNextActivation() < t:
281 self.doActivate(self.timer_list[0])