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