1 import NavigationInstance
2 from time import localtime, mktime, gmtime
3 from ServiceReference import ServiceReference
4 from enigma import iServiceInformation, eServiceCenter, eServiceReference
6 class TimerSanityCheck:
7 def __init__(self, timerlist, newtimer=None):
9 self.localtimediff = 25*3600 - mktime(gmtime(25*3600))
10 self.timerlist = timerlist
11 self.newtimer = newtimer
13 self.rep_eventlist = []
14 self.nrep_eventlist = []
18 def check(self, ext_timer=1):
21 self.newtimer = ext_timer
22 if self.newtimer is None:
25 self.simultimer = [ self.newtimer ]
26 return self.checkTimerlist()
28 def getSimulTimerList(self):
29 return self.simultimer
31 def doubleCheck(self):
32 if self.newtimer is not None and self.newtimer.service_ref.ref.valid():
33 self.simultimer = [ self.newtimer ]
34 for timer in self.timerlist:
35 if (timer == self.newtimer):
38 if timer.begin == self.newtimer.begin:
39 fl1 = timer.service_ref.ref.flags & eServiceReference.isGroup
40 fl2 = self.newtimer.service_ref.ref.flags & eServiceReference.isGroup
44 return timer.service_ref.ref.getPath() == self.newtimer.service_ref.ref.getPath()
45 getUnsignedDataRef1 = timer.service_ref.ref.getUnsignedData
46 getUnsignedDataRef2 = self.newtimer.service_ref.ref.getUnsignedData
47 for x in (1, 2, 3, 4):
48 if getUnsignedDataRef1(x) != getUnsignedDataRef2(x):
54 def checkTimerlist(self, ext_timer=1):
55 #with special service for external plugins
56 # Entries in eventlist
58 # BeginEndFlag 1 for begin, -1 for end
59 # index -1 for the new Timer, 0..n index of the existing timers
60 # count of running timers
62 serviceHandler = eServiceCenter.getInstance()
63 print "checkTimerlist"
64 # create a list with all start and end times
65 # split it into recurring and singleshot timers
67 ##################################################################################
68 # process the new timer
69 self.rep_eventlist = []
70 self.nrep_eventlist = []
72 self.newtimer = ext_timer
73 if (self.newtimer is not None) and (not self.newtimer.disabled):
74 if not self.newtimer.service_ref.ref.valid():
76 rflags = self.newtimer.repeated
77 rflags = ((rflags & 0x7F)>> 3)|((rflags & 0x07)<<4)
79 begin = self.newtimer.begin % 86400 # map to first day
80 if (self.localtimediff > 0) and ((begin + self.localtimediff) > 86400):
81 rflags = ((rflags >> 1)& 0x3F)|((rflags << 6)& 0x40)
82 elif (self.localtimediff < 0) and (begin < self.localtimediff):
83 rflags = ((rflags << 1)& 0x7E)|((rflags >> 6)& 0x01)
84 while rflags: # then arrange on the week
86 self.rep_eventlist.append((begin, -1))
90 self.nrep_eventlist.extend([(self.newtimer.begin,self.bflag,-1),(self.newtimer.end,self.eflag,-1)])
92 ##################################################################################
93 # now process existing timers
95 for timer in self.timerlist:
96 if (timer != self.newtimer) and (not timer.disabled):
98 rflags = timer.repeated
99 rflags = ((rflags & 0x7F)>> 3)|((rflags & 0x07)<<4)
100 begin = timer.begin % 86400 # map all to first day
101 if (self.localtimediff > 0) and ((begin + self.localtimediff) > 86400):
102 rflags = ((rflags >> 1)& 0x3F)|((rflags << 6)& 0x40)
103 elif (self.localtimediff < 0) and (begin < self.localtimediff):
104 rflags = ((rflags << 1)& 0x7E)|((rflags >> 6)& 0x01)
107 self.rep_eventlist.append((begin, idx))
111 self.nrep_eventlist.extend([(timer.begin,self.bflag,idx),(timer.end,self.eflag,idx)])
114 ################################################################################
115 # journalize timer repeations
116 if self.nrep_eventlist:
117 interval_begin = min(self.nrep_eventlist)[0]
118 interval_end = max(self.nrep_eventlist)[0]
119 offset_0 = interval_begin - (interval_begin % 604800)
120 weeks = (interval_end - offset_0) / 604800
121 if ((interval_end - offset_0) % 604800):
123 for cnt in range(weeks):
124 for event in self.rep_eventlist:
125 if event[1] == -1: # -1 is the identifier of the changed timer
126 event_begin = self.newtimer.begin
127 event_end = self.newtimer.end
129 event_begin = self.timerlist[event[1]].begin
130 event_end = self.timerlist[event[1]].end
131 new_event_begin = event[0] + offset_0 + (cnt * 604800)
132 # summertime correction
133 new_lth = localtime(new_event_begin).tm_hour
134 new_event_begin += 3600 * (localtime(event_begin).tm_hour - new_lth)
135 new_event_end = new_event_begin + (event_end - event_begin)
137 if new_event_begin >= self.newtimer.begin: # is the soap already running?
138 self.nrep_eventlist.extend([(new_event_begin, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
140 if new_event_begin >= self.timerlist[event[1]].begin: # is the soap already running?
141 self.nrep_eventlist.extend([(new_event_begin, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
143 offset_0 = 345600 # the Epoch begins on Thursday
144 for cnt in (0, 1): # test two weeks to take care of Sunday-Monday transitions
145 for event in self.rep_eventlist:
146 if event[1] == -1: # -1 is the identifier of the changed timer
147 event_begin = self.newtimer.begin
148 event_end = self.newtimer.end
150 event_begin = self.timerlist[event[1]].begin
151 event_end = self.timerlist[event[1]].end
152 new_event_begin = event[0] + offset_0 + (cnt * 604800)
153 new_event_end = new_event_begin + (event_end - event_begin)
154 self.nrep_eventlist.extend([(new_event_begin, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
156 ################################################################################
157 # order list chronological
158 self.nrep_eventlist.sort()
160 ##################################################################################
161 # detect overlapping timers and overlapping times
164 ConflictTunerType = None
165 newTimerTunerType = None
169 for event in self.nrep_eventlist:
171 if event[2] == -1: # new timer
172 timer = self.newtimer
174 timer = self.timerlist[event[2]]
175 if event[1] == self.bflag:
177 fakeRecService = NavigationInstance.instance.recordService(timer.service_ref, True)
179 fakeRecResult = fakeRecService.start(True)
182 if not fakeRecResult: # tune okay
183 feinfo = fakeRecService.frontendInfo().getFrontendData()
184 tunerType.append(feinfo.get("tuner_type"))
185 else: # tune failed.. so we must go another way to get service type (DVB-S, DVB-T, DVB-C)
187 def getServiceType(ref): # helper function to get a service type of a service reference
188 serviceInfo = serviceHandler.info(ref)
189 serviceInfo = serviceInfo and serviceInfo.getInfoObject(ref, iServiceInformation.sTransponderData)
190 return serviceInfo and serviceInfo["tuner_type"] or ""
192 ref = timer.service_ref.ref
193 if ref.flags & eServiceReference.isGroup: # service group ?
194 serviceList = serviceHandler.list(ref) # get all alternative services
196 for ref in serviceList.getContent("R"): # iterate over all group service references
197 type = getServiceType(ref)
198 if not type in tunerType: # just add single time
199 tunerType.append(type)
201 tunerType.append(getServiceType(ref))
203 if event[2] == -1: # new timer
204 newTimerTunerType = tunerType
205 overlaplist.append((fakeRecResult, timer, tunerType))
206 fakeRecList.append((timer, fakeRecService))
208 if ConflictTimer is None: # just take care of the first conflict
209 ConflictTimer = timer
210 ConflictTunerType = tunerType
211 elif event[1] == self.eflag:
212 for fakeRec in fakeRecList:
213 if timer == fakeRec[0] and fakeRec[1]:
214 NavigationInstance.instance.stopRecordService(fakeRec[1])
215 fakeRecList.remove(fakeRec)
217 for entry in overlaplist:
218 if entry[1] == timer:
219 overlaplist.remove(entry)
221 print "Bug: unknown flag!"
222 self.nrep_eventlist[idx] = (event[0],event[1],event[2],cnt,overlaplist[:]) # insert a duplicate into current overlaplist
225 if ConflictTimer is None: # no conflict found :)
228 ##################################################################################
229 # we have detected a conflict, now we must figure out the involved timers
231 if self.newtimer is not None: # new timer?
232 if self.newtimer is not ConflictTimer: # the new timer is not the conflicting timer?
233 for event in self.nrep_eventlist:
234 if len(event[4]) > 1: # entry in overlaplist of this event??
237 for entry in event[4]:
238 if entry[1] is ConflictTimer:
240 if entry[1] is self.newtimer:
243 ConflictTimer = self.newtimer
244 ConflictTunerType = newTimerTunerType
247 self.simultimer = [ ConflictTimer ]
248 for event in self.nrep_eventlist:
249 if len(event[4]) > 1: # entry in overlaplist of this event??
250 for entry in event[4]:
251 if entry[1] is ConflictTimer:
255 for entry in event[4]:
256 if not entry[1] in self.simultimer:
258 if x in ConflictTunerType:
259 self.simultimer.append(entry[1])
262 if len(self.simultimer) < 2:
263 print "Bug: unknown Conflict!"
265 return False # conflict detected!