16 def __init__(self, begin, end):
18 self.prepare_time = 10
23 def resetRepeated(self):
24 self.repeated = int(0)
26 def setRepeated(self, day):
27 self.repeated |= (2 ** day)
28 print "Repeated: " + str(self.repeated)
31 return self.state == self.StateRunning
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
46 print "Day: " + str(x)
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))
62 if self.state == self.StateWait:
63 return self.begin - self.prepare_time
64 elif self.state == self.StatePrepare:
70 return self.getTime() < o.getTime()
72 def activate(self, event):
73 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
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.
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?
90 self.processed_timers = [ ]
93 self.timer.timeout.get().append(self.calcNextActivation)
94 self.lastActivation = time.time()
96 self.calcNextActivation()
98 def addTimerEntry(self, entry, noRecalc=0):
99 entry.processRepeated()
101 # we either go trough Prepare/Start/End-state if the timer is still running,
102 # or skip it when it's alrady past the end.
104 if entry.end > time.time():
105 bisect.insort(self.timer_list, entry)
107 self.calcNextActivation()
109 bisect.insort(self.processed_timers, entry)
111 def setNextActivation(self, when):
112 delay = int((when - time.time()) * 1000)
113 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
115 self.timer.start(delay, 1)
118 def calcNextActivation(self):
119 if self.lastActivation > time.time():
120 print "[timer.py] timewarp - re-evaluating all processed timers."
121 tl = self.processed_timers
122 self.processed_timers = [ ]
124 self.addTimerEntry(x, noRecalc=1)
126 self.processActivation()
127 self.lastActivation = time.time()
129 min = int(time.time()) + self.MaxWaitTime
131 # calculate next activation point
132 if len(self.timer_list):
133 w = self.timer_list[0].getTime()
137 self.setNextActivation(min)
139 def timeChanged(self, timer):
140 self.timer_list.remove(timer)
142 self.addTimerEntry(timer)
144 def doActivate(self, w):
146 self.timer_list.remove(w)
149 if w.state < TimerEntry.StateEnded:
150 bisect.insort(self.timer_list, w)
152 if (w.repeated != 0):
154 w.state = TimerEntry.StateWait
155 self.addTimerEntry(w)
157 bisect.insort(self.processed_timers, w)
160 def processActivation(self):
161 t = int(time.time()) + 1
163 # we keep on processing the first entry until it goes into the future.
164 while len(self.timer_list) and self.timer_list[0].getTime() < t:
165 self.doActivate(self.timer_list[0])