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
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
27 def resetRepeated(self):
28 self.repeated = int(0)
30 def setRepeated(self, day):
31 self.repeated |= (2 ** day)
32 print "Repeated: " + str(self.repeated)
35 return self.state == self.StateRunning
37 def addOneDay(self, timedatestruct):
38 oldHour = timedatestruct.tm_hour
39 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()
40 if localtime(mktime(newdate)).tm_hour != oldHour:
41 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()
44 # update self.begin and self.end according to the self.repeated-flags
45 def processRepeated(self, findRunningEvent = True):
46 print "ProcessRepeated"
47 if (self.repeated != 0):
50 #to avoid problems with daylight saving, we need to calculate with localtime, in struct_time representation
51 localrepeatedbegindate = localtime(self.repeatedbegindate)
52 localbegin = localtime(self.begin)
53 localend = localtime(self.end)
54 localnow = localtime(now)
56 print "localbegin:", strftime("%c", localbegin)
57 print "localend:", strftime("%c", localend)
64 print "Day: " + str(x)
69 print strftime("%c", localnow)
71 # if day is NOT in the list of repeated days
72 # 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
73 while ((day[localbegin.tm_wday] != 0) or (mktime(localrepeatedbegindate) > mktime(localbegin)) or ((day[localbegin.tm_wday] == 0) and ((findRunningEvent and localend < localnow) or ((not findRunningEvent) and localbegin < localnow)))):
74 localbegin = self.addOneDay(localbegin)
75 localend = self.addOneDay(localend)
76 print "localbegin after addOneDay:", strftime("%c", localbegin)
77 print "localend after addOneDay:", strftime("%c", localend)
79 #we now have a struct_time representation of begin and end in localtime, but we have to calculate back to (gmt) seconds since epoch
80 self.begin = int(mktime(localbegin))
81 self.end = int(mktime(localend))
82 if self.begin == self.end:
85 print "ProcessRepeated result"
86 print strftime("%c", localtime(self.begin))
87 print strftime("%c", localtime(self.end))
92 return self.getNextActivation() < o.getNextActivation()
99 def timeChanged(self):
102 # check if a timer entry must be skipped
103 def shouldSkip(self):
104 return self.end <= time() and self.state == TimerEntry.StateWaiting
109 # in case timer has not yet started, but gets aborted (so it's preparing),
111 if self.begin > self.end:
112 self.begin = self.end
114 self.cancelled = True
116 # must be overridden!
117 def getNextActivation():
124 self.disabled = False
127 # the time between "polls". We do this because
128 # we want to account for time jumps etc.
129 # of course if they occur <100s before starting,
130 # it's not good. thus, you have to repoll when
131 # you change the time.
133 # this is just in case. We don't want the timer
134 # hanging. we use this "edge-triggered-polling-scheme"
135 # anyway, so why don't make it a bit more fool-proof?
139 self.timer_list = [ ]
140 self.processed_timers = [ ]
142 self.timer = eTimer()
143 self.timer.timeout.get().append(self.calcNextActivation)
144 self.lastActivation = time()
146 self.calcNextActivation()
147 self.on_state_change = [ ]
149 def stateChanged(self, entry):
150 for f in self.on_state_change:
154 self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
156 def addTimerEntry(self, entry, noRecalc=0):
157 entry.processRepeated()
159 # when the timer has not yet started, and is already passed,
160 # don't go trough waiting/running/end-states, but sort it
161 # right into the processedTimers.
162 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded or (entry.state == TimerEntry.StateWaiting and entry.disabled):
163 print "already passed, skipping"
164 print "shouldSkip:", entry.shouldSkip()
165 print "state == ended", entry.state == TimerEntry.StateEnded
166 print "waiting && disabled:", (entry.state == TimerEntry.StateWaiting and entry.disabled)
167 insort(self.processed_timers, entry)
168 entry.state = TimerEntry.StateEnded
170 insort(self.timer_list, entry)
172 self.calcNextActivation()
174 def setNextActivation(self, when):
175 delay = int((when - time()) * 1000)
176 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
178 self.timer.start(delay, 1)
181 def calcNextActivation(self):
182 if self.lastActivation > time():
183 print "[timer.py] timewarp - re-evaluating all processed timers."
184 tl = self.processed_timers
185 self.processed_timers = [ ]
187 # simulate a "waiting" state to give them a chance to re-occure
189 self.addTimerEntry(x, noRecalc=1)
191 self.processActivation()
192 self.lastActivation = time()
194 min = int(time()) + self.MaxWaitTime
196 # calculate next activation point
197 if len(self.timer_list):
198 w = self.timer_list[0].getNextActivation()
202 self.setNextActivation(min)
204 def timeChanged(self, timer):
207 if timer.state == TimerEntry.StateEnded:
208 self.processed_timers.remove(timer)
210 self.timer_list.remove(timer)
212 # give the timer a chance to re-enqueue
213 if timer.state == TimerEntry.StateEnded:
214 timer.state = TimerEntry.StateWaiting
215 self.addTimerEntry(timer)
217 def doActivate(self, w):
218 self.timer_list.remove(w)
220 # when activating a timer which has already passed,
221 # simply abort the timer. don't run trough all the stages.
223 w.state = TimerEntry.StateEnded
225 # when active returns true, this means "accepted".
226 # otherwise, the current state is kept.
227 # the timer entry itself will fix up the delay then.
231 # did this timer reached the last state?
232 if w.state < TimerEntry.StateEnded:
233 # no, sort it into active list
234 insort(self.timer_list, w)
236 # yes. Process repeated, and re-add.
239 w.state = TimerEntry.StateWaiting
240 self.addTimerEntry(w)
242 insort(self.processed_timers, w)
246 def processActivation(self):
247 print "It's now ", strftime("%c", localtime(time()))
250 # we keep on processing the first entry until it goes into the future.
251 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
252 self.doActivate(self.timer_list[0])