fixed timer a bit
[openblackhole/openblackhole-enigma2.git] / timer.py
1 import bisect
2 from time import *
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 %s got activated (%d)!" % (self.description, event)
35
36 class Timer:
37
38         MaxWaitTime = 100
39
40         def __init__(self):
41                 self.timer_list = [ ]
42                 self.processed_timers = [ ]
43                 
44                 self.timer = eTimer()
45                 self.timer.timeout.get().append(self.calcNextActivation)
46                 
47                 self.calcNextActivation()
48         
49         def addTimerEntry(self, entry):
50                 bisect.insort(self.timer_list, entry)
51                 self.calcNextActivation()
52         
53         def setNextActivation(self, when):
54                 delay = int((when - time()) * 1000)
55                 print "next activation: %d (in %d ms)" % (when, delay)
56                 
57                 self.timer.start(delay, 1)
58                 self.next = when
59
60         def calcNextActivation(self):
61                 self.processActivation()
62         
63                 min = int(time()) + self.MaxWaitTime
64                 
65                 # calculate next activation point
66                 if len(self.timer_list):
67                         w = self.timer_list[0].getTime()
68                         if w < min:
69                                 min = w
70                 
71                 self.setNextActivation(min)
72         
73         def timeChanged(self, timer):
74                 self.timer_list.remove(timer)
75                 bisect.insort(self.timer_list, timer)
76         
77         def doActivate(self, w):
78                 w.activate(w.state)
79                 self.timer_list.remove(w)
80                 w.state += 1
81                 if w.state < TimerEntry.StateEnded:
82                         bisect.insort(self.timer_list, w)
83                 else:
84                         bisect.insort(self.processed_timers, w)
85         
86         def processActivation(self):
87                 t = int(time()) + 1
88                 
89                 # we keep on processing the first entry until it goes into the future.
90                 while len(self.timer_list) and self.timer_list[0].getTime() < t:
91                         self.doActivate(self.timer_list[0])
92
93 #t = Timer()
94 #base = time() + 5
95 #t.addTimerEntry(TimerEntry(base+10, base+20, None, None, "test #1: 10 - 20"))
96 #t.addTimerEntry(TimerEntry(base+10, base+30, None, None, "test #2: 10 - 30"))
97 #t.addTimerEntry(TimerEntry(base+15, base+20, None, None, "test #3: 15 - 20"))
98 #t.addTimerEntry(TimerEntry(base+20, base+35, None, None, "test #4: 20 - 35"))