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