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)
30 # update self.begin and self.end according to the self.repeated-flags
31 def processRepeated(self):
32 print "ProcessRepeated"
33 print time.strftime("%c", time.localtime(self.begin))
34 print time.strftime("%c", time.localtime(self.end))
35 if (self.repeated != 0):
36 now = int(time.time()) + 1
43 print "Day: " + str(x)
48 print time.strftime("%c", time.localtime(now))
49 print time.strftime("%c", time.localtime(self.begin))
50 print time.strftime("%c", time.localtime(self.end))
51 print str(time.localtime(self.begin).tm_wday)
52 while ((day[time.localtime(self.begin).tm_wday] != 0) or ((day[time.localtime(self.begin).tm_wday] == 0) and self.end < now)):
53 print time.strftime("%c", time.localtime(self.begin))
54 print time.strftime("%c", time.localtime(self.end))
59 if self.state == self.StateWait:
60 return self.begin - self.prepare_time
61 elif self.state == self.StatePrepare:
67 return self.getTime() < o.getTime()
69 def activate(self, event):
70 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
74 # the time between "polls". We do this because
75 # we want to account for time jumps etc.
76 # of course if they occur <100s before starting,
77 # it's not good. thus, you have to repoll when
78 # you change the time.
80 # this is just in case. We don't want the timer
81 # hanging. we use this "edge-triggered-polling-scheme"
82 # anyway, so why don't make it a bit more fool-proof?
87 self.processed_timers = [ ]
90 self.timer.timeout.get().append(self.calcNextActivation)
91 self.lastActivation = time.time()
93 self.calcNextActivation()
95 def addTimerEntry(self, entry, noRecalc=0):
96 entry.processRepeated()
98 # we either go trough Prepare/Start/End-state if the timer is still running,
99 # or skip it when it's alrady past the end.
101 if entry.end > time.time():
102 bisect.insort(self.timer_list, entry)
104 self.calcNextActivation()
106 bisect.insort(self.processed_timers, entry)
108 def setNextActivation(self, when):
109 delay = int((when - time.time()) * 1000)
110 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
112 self.timer.start(delay, 1)
115 def calcNextActivation(self):
116 if self.lastActivation > time.time():
117 print "[timer.py] timewarp - re-evaluating all processed timers."
118 tl = self.processed_timers
119 self.processed_timers = [ ]
121 self.addTimerEntry(x, noRecalc=1)
123 self.processActivation()
124 self.lastActivation = time.time()
126 min = int(time.time()) + self.MaxWaitTime
128 # calculate next activation point
129 if len(self.timer_list):
130 w = self.timer_list[0].getTime()
134 self.setNextActivation(min)
136 def timeChanged(self, timer):
137 self.timer_list.remove(timer)
139 self.addTimerEntry(timer)
141 def doActivate(self, w):
143 self.timer_list.remove(w)
146 if w.state < TimerEntry.StateEnded:
147 bisect.insort(self.timer_list, w)
149 if (w.repeated != 0):
151 w.state = TimerEntry.StateWait
152 self.addTimerEntry(w)
154 bisect.insort(self.processed_timers, w)
157 def processActivation(self):
158 t = int(time.time()) + 1
160 # we keep on processing the first entry until it goes into the future.
161 while len(self.timer_list) and self.timer_list[0].getTime() < t:
162 self.doActivate(self.timer_list[0])