finished the rotor config plugin (untested and lacking some features)
[enigma2.git] / timer.py
1 import bisect
2 import time
3 from enigma import *
4
5 class TimerEntry:
6         StateWaiting  = 0
7         StatePrepared = 1
8         StateRunning  = 2
9         StateEnded    = 3
10         
11         def __init__(self, begin, end):
12                 self.begin = begin
13                 self.prepare_time = 20
14                 self.end = end
15                 self.state = 0
16                 self.resetRepeated()
17                 self.backoff = 0
18                 
19                 self.disabled = False
20                 
21         def resetRepeated(self):
22                 self.repeated = int(0)
23
24         def setRepeated(self, day):
25                 self.repeated |= (2 ** day)
26                 print "Repeated: " + str(self.repeated)
27                 
28         def isRunning(self):
29                 return self.state == self.StateRunning
30                 
31         # update self.begin and self.end according to the self.repeated-flags
32         def processRepeated(self):
33                 print "ProcessRepeated"
34                 print time.strftime("%c", time.localtime(self.begin))
35                 print time.strftime("%c", time.localtime(self.end))
36                 if (self.repeated != 0):
37                         now = int(time.time()) + 1
38                         
39                         day = []
40                         flags = self.repeated
41                         for x in range(0, 7):
42                                 if (flags & 1 == 1):
43                                         day.append(0)
44                                         print "Day: " + str(x)
45                                 else:
46                                         day.append(1)
47                                 flags = flags >> 1
48
49                         print time.strftime("%c", time.localtime(now))
50                         print time.strftime("%c", time.localtime(self.begin))
51                         print time.strftime("%c", time.localtime(self.end))
52                         print str(time.localtime(self.begin).tm_wday)
53                         while ((day[time.localtime(self.begin).tm_wday] != 0) or ((day[time.localtime(self.begin).tm_wday] == 0) and self.end < now)):
54                                 print time.strftime("%c", time.localtime(self.begin))
55                                 print time.strftime("%c", time.localtime(self.end))
56                                 self.begin += 86400
57                                 self.end += 86400
58                         
59                         self.timeChanged()
60                         
61
62         def __lt__(self, o):
63                 return self.getNextActivation() < o.getNextActivation()
64         
65         # must be overridden
66         def activate(self):
67                 pass
68                 
69         # can be overridden
70         def timeChanged(self):
71                 pass
72
73         # check if a timer entry must be skipped
74         def shouldSkip(self):
75                 return self.end <= time.time() and self.state == TimerEntry.StateWaiting
76
77         def abort(self):
78                 self.end = time.time()
79                 
80                 # in case timer has not yet started, but gets aborted (so it's preparing),
81                 # set begin to now.
82                 if self.begin > self.end:
83                         self.begin = self.end
84
85                 self.cancelled = True
86         
87         # must be overridden!
88         def getNextActivation():
89                 pass
90
91 class Timer:
92         # the time between "polls". We do this because
93         # we want to account for time jumps etc.
94         # of course if they occur <100s before starting,
95         # it's not good. thus, you have to repoll when
96         # you change the time.
97         #
98         # this is just in case. We don't want the timer 
99         # hanging. we use this "edge-triggered-polling-scheme"
100         # anyway, so why don't make it a bit more fool-proof?
101         MaxWaitTime = 100
102
103         def __init__(self):
104                 self.timer_list = [ ]
105                 self.processed_timers = [ ]
106                 
107                 self.timer = eTimer()
108                 self.timer.timeout.get().append(self.calcNextActivation)
109                 self.lastActivation = time.time()
110                 
111                 self.calcNextActivation()
112                 self.on_state_change = [ ]
113         
114         def stateChanged(self, entry):
115                 for f in self.on_state_change:
116                         f(entry)
117                         
118         def getNextRecordingTime(self):
119                 if len(self.timer_list) > 0:
120                         return self.timer_list[0].begin
121                 return -1
122                         
123         def cleanup(self):
124                 self.processed_timers = [entry for entry in self.processed_timers if entry.disabled]
125         
126         def addTimerEntry(self, entry, noRecalc=0):
127                 entry.processRepeated()
128
129                 # when the timer has not yet started, and is already passed,
130                 # don't go trough waiting/running/end-states, but sort it
131                 # right into the processedTimers.
132                 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded or (entry.state == TimerEntry.StateWaiting and entry.disabled):
133                         print "already passed, skipping"
134                         bisect.insort(self.processed_timers, entry)
135                         entry.state = TimerEntry.StateEnded
136                 else:
137                         bisect.insort(self.timer_list, entry)
138                         if not noRecalc:
139                                 self.calcNextActivation()
140         
141         def setNextActivation(self, when):
142                 delay = int((when - time.time()) * 1000)
143                 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
144                 
145                 self.timer.start(delay, 1)
146                 self.next = when
147
148         def calcNextActivation(self):
149                 if self.lastActivation > time.time():
150                         print "[timer.py] timewarp - re-evaluating all processed timers."
151                         tl = self.processed_timers
152                         self.processed_timers = [ ]
153                         for x in tl:
154                                 # simulate a "waiting" state to give them a chance to re-occure
155                                 x.resetState()
156                                 self.addTimerEntry(x, noRecalc=1)
157                 
158                 self.processActivation()
159                 self.lastActivation = time.time()
160         
161                 min = int(time.time()) + self.MaxWaitTime
162                 
163                 # calculate next activation point
164                 if len(self.timer_list):
165                         w = self.timer_list[0].getNextActivation()
166                         if w < min:
167                                 min = w
168                 
169                 self.setNextActivation(min)
170         
171         def timeChanged(self, timer):
172                 timer.timeChanged()
173                 if timer.state == TimerEntry.StateEnded:
174                         self.processed_timers.remove(timer)
175                 else:
176                         self.timer_list.remove(timer)
177
178                 self.addTimerEntry(timer)
179         
180         def doActivate(self, w):
181                 self.timer_list.remove(w)
182                 
183                 # when activating a timer which has already passed,
184                 # simply abort the timer. don't run trough all the stages.
185                 if w.shouldSkip():
186                         w.state = TimerEntry.StateEnded
187                 else:
188                         # when active returns true, this means "accepted".
189                         # otherwise, the current state is kept.
190                         # the timer entry itself will fix up the delay then.
191                         if w.activate():
192                                 w.state += 1
193
194                 # did this timer reached the last state?
195                 if w.state < TimerEntry.StateEnded:
196                         # no, sort it into active list
197                         bisect.insort(self.timer_list, w)
198                 else:
199                         # yes. Process repeated, and re-add.
200                         if w.repeated:
201                                 w.processRepeated()
202                                 w.state = TimerEntry.StateWaiting
203                                 self.addTimerEntry(w)
204                         else:
205                                 bisect.insort(self.processed_timers, w)
206                 
207                 self.stateChanged(w)
208
209         def processActivation(self):
210                 print "It's now ", time.strftime("%c", time.localtime(time.time()))
211                 t = int(time.time()) + 1
212                 
213                 # we keep on processing the first entry until it goes into the future.
214                 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
215                         self.doActivate(self.timer_list[0])