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