auto tuner selection is now working (this breaks allocateRawChannel..)
[enigma2.git] / timer.py
1 import bisect
2 import time
3 from enigma import *
4
5 class TimerEntry:
6         EventPrepare = 0
7         EventStart   = 1
8         EventEnd     = 2
9         EventAbort   = 3
10         
11         StateWait    = 0
12         StatePrepare = 1
13         StateRunning = 2
14         StateEnded   = 3
15         
16         def __init__(self, begin, end):
17                 self.begin = begin
18                 self.prepare_time = 10
19                 self.end = end
20                 self.state = 0
21         
22         def getTime(self):
23                 if self.state == self.StateWait:
24                         return self.begin - self.prepare_time
25                 elif self.state == self.StatePrepare:
26                         return self.begin
27                 else:
28                         return self.end 
29         
30         def __lt__(self, o):
31                 return self.getTime() < o.getTime()
32         
33         def activate(self, event):
34                 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
35
36 class Timer:
37
38         # the time between "polls". We do this because
39         # we want to account for time jumps etc.
40         # of course if they occur <100s before starting,
41         # it's not good. thus, you have to repoll when
42         # you change the time.
43         #
44         # this is just in case. We don't want the timer 
45         # hanging. we use this "edge-triggered-polling-scheme"
46         # anyway, so why don't make it a bit more fool-proof?
47         MaxWaitTime = 100
48
49         def __init__(self):
50                 self.timer_list = [ ]
51                 self.processed_timers = [ ]
52                 
53                 self.timer = eTimer()
54                 self.timer.timeout.get().append(self.calcNextActivation)
55                 self.lastActivation = time.time()
56                 
57                 self.calcNextActivation()
58         
59         def addTimerEntry(self, entry, noRecalc=0):
60                 # we either go trough Prepare/Start/End-state if the timer is still running,
61                 # or skip it when it's alrady past the end.
62                 if entry.end > time.time():
63                         bisect.insort(self.timer_list, entry)
64                         if not noRecalc:
65                                 self.calcNextActivation()
66                 else:
67                         bisect.insort(self.processed_timers, entry)
68         
69         def setNextActivation(self, when):
70                 delay = int((when - time.time()) * 1000)
71                 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
72                 
73                 self.timer.start(delay, 1)
74                 self.next = when
75
76         def calcNextActivation(self):
77                 if self.lastActivation > time.time():
78                         print "[timer.py] timewarp - re-evaluating all processed timers."
79                         tl = self.processed_timers
80                         self.processed_timers = [ ]
81                         for x in tl:
82                                 self.addTimerEntry(x, noRecalc=1)
83                 
84                 self.processActivation()
85                 self.lastActivation = time.time()
86         
87                 min = int(time.time()) + self.MaxWaitTime
88                 
89                 # calculate next activation point
90                 if len(self.timer_list):
91                         w = self.timer_list[0].getTime()
92                         if w < min:
93                                 min = w
94                 
95                 self.setNextActivation(min)
96         
97         def timeChanged(self, timer):
98                 self.timer_list.remove(timer)
99                 self.addTimerEntry(timer)
100         
101         def doActivate(self, w):
102                 w.activate(w.state)
103                 self.timer_list.remove(w)
104                 w.state += 1
105                 if w.state < TimerEntry.StateEnded:
106                         bisect.insort(self.timer_list, w)
107                 else:
108                         bisect.insort(self.processed_timers, w)
109         
110         def processActivation(self):
111                 t = int(time.time()) + 1
112                 
113                 # we keep on processing the first entry until it goes into the future.
114                 while len(self.timer_list) and self.timer_list[0].getTime() < t:
115                         self.doActivate(self.timer_list[0])