set wss according to av settings
[enigma2.git] / timer.py
1 import bisect
2 import time
3 from enigma import *
4
5 class TimerEntry:
6         StateWaiting  = 0
7         StatePrepared = 1
8         StateRunning  = 2
9         StateEnded    = 3
10         
11         def __init__(self, begin, end):
12                 self.begin = begin
13                 self.prepare_time = 20
14                 self.end = end
15                 self.state = 0
16                 self.resetRepeated()
17                 self.backoff = 0
18                 
19         def resetRepeated(self):
20                 self.repeated = int(0)
21
22         def setRepeated(self, day):
23                 self.repeated |= (2 ** day)
24                 print "Repeated: " + str(self.repeated)
25                 
26         def isRunning(self):
27                 return self.state == self.StateRunning
28                 
29         # update self.begin and self.end according to the self.repeated-flags
30         def processRepeated(self):
31                 print "ProcessRepeated"
32                 print time.strftime("%c", time.localtime(self.begin))
33                 print time.strftime("%c", time.localtime(self.end))
34                 if (self.repeated != 0):
35                         now = int(time.time()) + 1
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.strftime("%c", time.localtime(now))
48                         print time.strftime("%c", time.localtime(self.begin))
49                         print time.strftime("%c", time.localtime(self.end))
50                         print str(time.localtime(self.begin).tm_wday)
51                         while ((day[time.localtime(self.begin).tm_wday] != 0) or ((day[time.localtime(self.begin).tm_wday] == 0) and self.end < now)):
52                                 print time.strftime("%c", time.localtime(self.begin))
53                                 print time.strftime("%c", time.localtime(self.end))
54                                 self.begin += 86400
55                                 self.end += 86400
56
57         def __lt__(self, o):
58                 return self.getNextActivation() < o.getNextActivation()
59         
60         # must be overridden
61         def activate(self):
62                 pass
63                 
64         # can be overridden
65         def timeChanged(self):
66                 pass
67
68         # check if a timer entry must be skipped
69         def shouldSkip(self):
70                 return self.end <= time.time() and self.state == TimerEntry.StateWaiting
71
72         def abort(self):
73                 self.end = time.time()
74                 
75                 # in case timer has not yet started, but gets aborted (so it's preparing),
76                 # set begin to now.
77                 if self.begin > self.end:
78                         self.begin = self.end
79         
80         # must be overridden!
81         def getNextActivation():
82                 pass
83
84 class Timer:
85         # the time between "polls". We do this because
86         # we want to account for time jumps etc.
87         # of course if they occur <100s before starting,
88         # it's not good. thus, you have to repoll when
89         # you change the time.
90         #
91         # this is just in case. We don't want the timer 
92         # hanging. we use this "edge-triggered-polling-scheme"
93         # anyway, so why don't make it a bit more fool-proof?
94         MaxWaitTime = 100
95
96         def __init__(self):
97                 self.timer_list = [ ]
98                 self.processed_timers = [ ]
99                 
100                 self.timer = eTimer()
101                 self.timer.timeout.get().append(self.calcNextActivation)
102                 self.lastActivation = time.time()
103                 
104                 self.calcNextActivation()
105         
106         def stateChanged(self, entry):
107                 pass
108         
109         def addTimerEntry(self, entry, noRecalc=0):
110                 entry.processRepeated()
111
112                 # when the timer has not yet started, and is already passed,
113                 # don't go trough waiting/running/end-states, but sort it
114                 # right into the processedTimers.
115                 if entry.shouldSkip() or entry.state == TimerEntry.StateEnded:
116                         print "already passed, skipping"
117                         bisect.insort(self.processed_timers, entry)
118                         entry.state = TimerEntry.StateEnded
119                 else:
120                         bisect.insort(self.timer_list, entry)
121                         if not noRecalc:
122                                 self.calcNextActivation()
123         
124         def setNextActivation(self, when):
125                 delay = int((when - time.time()) * 1000)
126                 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
127                 
128                 self.timer.start(delay, 1)
129                 self.next = when
130
131         def calcNextActivation(self):
132                 if self.lastActivation > time.time():
133                         print "[timer.py] timewarp - re-evaluating all processed timers."
134                         tl = self.processed_timers
135                         self.processed_timers = [ ]
136                         for x in tl:
137                                 # simulate a "waiting" state to give them a chance to re-occure
138                                 x.resetState()
139                                 self.addTimerEntry(x, noRecalc=1)
140                 
141                 self.processActivation()
142                 self.lastActivation = time.time()
143         
144                 min = int(time.time()) + self.MaxWaitTime
145                 
146                 # calculate next activation point
147                 if len(self.timer_list):
148                         w = self.timer_list[0].getNextActivation()
149                         if w < min:
150                                 min = w
151                 
152                 self.setNextActivation(min)
153         
154         def timeChanged(self, timer):
155                 timer.timeChanged()
156                 self.timer_list.remove(timer)
157
158                 self.addTimerEntry(timer)
159         
160         def doActivate(self, w):
161                 self.timer_list.remove(w)
162                 
163                 # when activating a timer which has already passed,
164                 # simply abort the timer. don't run trough all the stages.
165                 if w.shouldSkip():
166                         w.abort()
167                         bisect.insort(self.processed_timers, w)
168                 else:
169                         # when active returns true, this means "accepted".
170                         # otherwise, the current state is kept.
171                         # the timer entry itself will fix up the delay then.
172                         if w.activate():
173                                 w.state += 1
174
175                 # did this timer reached the last state?
176                 if w.state < TimerEntry.StateEnded:
177                         # no, sort it into active list
178                         bisect.insort(self.timer_list, w)
179                 else:
180                         # yes. Process repeated, and re-add.
181                         if not w.repeated:
182                                 w.processRepeated()
183                                 w.state = TimerEntry.StateWaiting
184                                 self.addTimerEntry(w)
185                         else:
186                                 bisect.insort(self.processed_timers, w)
187                 
188                 self.stateChanged(w)
189
190         def processActivation(self):
191                 print "It's now ", time.strftime("%c", time.localtime(time.time()))
192                 t = int(time.time()) + 1
193                 
194                 # we keep on processing the first entry until it goes into the future.
195                 while len(self.timer_list) and self.timer_list[0].getNextActivation() < t:
196                         self.doActivate(self.timer_list[0])