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