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