fix typo to make current timercode work again
[vuplus_dvbapp] / 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                 
56                 self.calcNextActivation()
57         
58         def addTimerEntry(self, entry):
59                 # we either go trough Prepare/Start/End-state if the timer is still running,
60                 # or skip it when it's alrady past the end.
61                 if entry.end > time.time():
62                         bisect.insort(self.timer_list, entry)
63                         self.calcNextActivation()
64                 else:
65                         bisect.insort(self.processed_timers, entry)
66         
67         def setNextActivation(self, when):
68                 delay = int((when - time.time()) * 1000)
69                 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
70                 
71                 self.timer.start(delay, 1)
72                 self.next = when
73
74         def calcNextActivation(self):
75                 self.processActivation()
76         
77                 min = int(time.time()) + self.MaxWaitTime
78                 
79                 # calculate next activation point
80                 if len(self.timer_list):
81                         w = self.timer_list[0].getTime()
82                         if w < min:
83                                 min = w
84                 
85                 self.setNextActivation(min)
86         
87         def timeChanged(self, timer):
88                 self.timer_list.remove(timer)
89                 bisect.insort(self.timer_list, timer)
90         
91         def doActivate(self, w):
92                 w.activate(w.state)
93                 self.timer_list.remove(w)
94                 w.state += 1
95                 if w.state < TimerEntry.StateEnded:
96                         bisect.insort(self.timer_list, w)
97                 else:
98                         bisect.insort(self.processed_timers, w)
99         
100         def processActivation(self):
101                 t = int(time.time()) + 1
102                 
103                 # we keep on processing the first entry until it goes into the future.
104                 while len(self.timer_list) and self.timer_list[0].getTime() < t:
105                         self.doActivate(self.timer_list[0])