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