ef1cc42def220b0babd357b3e944d5292c50c8f3
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / TimerSanityCheck.py
1 import NavigationInstance
2 from time import localtime, mktime, gmtime
3 from ServiceReference import ServiceReference
4 from enigma import iServiceInformation, eServiceCenter, eServiceReference, getBestPlayableServiceReference
5 from timer import TimerEntry
6
7 class TimerSanityCheck:
8         def __init__(self, timerlist, newtimer=None):
9                 self.localtimediff = 25*3600 - mktime(gmtime(25*3600))
10                 self.timerlist = timerlist
11                 self.newtimer = newtimer
12                 self.simultimer = []
13                 self.rep_eventlist = []
14                 self.nrep_eventlist = []
15                 self.bflag = -1
16                 self.eflag = 1
17
18         def check(self, ext_timer=1):
19                 if ext_timer != 1:
20                         self.newtimer = ext_timer
21                 if self.newtimer is None:
22                         self.simultimer = []
23                 else:
24                         self.simultimer = [ self.newtimer ]
25                 return self.checkTimerlist()
26
27         def getSimulTimerList(self):
28                 return self.simultimer
29
30         def doubleCheck(self):
31                 if self.newtimer is not None and self.newtimer.service_ref.ref.valid():
32                         self.simultimer = [ self.newtimer ]
33                         for timer in self.timerlist:
34                                 if timer == self.newtimer:
35                                         return True
36                                 if self.newtimer.begin >= timer.begin and self.newtimer.end <= timer.end:
37                                         if timer.justplay and not self.newtimer.justplay:
38                                                 continue
39                                         if timer.service_ref.ref.flags & eServiceReference.isGroup:
40                                                 if self.newtimer.service_ref.ref.flags & eServiceReference.isGroup and timer.service_ref.ref.getPath() == self.newtimer.service_ref.ref.getPath():
41                                                         return True
42                                                 continue
43                                         getUnsignedDataRef1 = timer.service_ref.ref.getUnsignedData
44                                         getUnsignedDataRef2 = self.newtimer.service_ref.ref.getUnsignedData
45                                         for x in (1, 2, 3, 4):
46                                                 if getUnsignedDataRef1(x) != getUnsignedDataRef2(x):
47                                                         break;
48                                         else:
49                                                 return True
50                 return False
51
52         def checkTimerlist(self, ext_timer=1):
53                 #with special service for external plugins
54                 # Entries in eventlist
55                 # timeindex
56                 # BeginEndFlag 1 for begin, -1 for end
57                 # index -1 for the new Timer, 0..n index of the existing timers
58                 # count of running timers
59
60                 serviceHandler = eServiceCenter.getInstance()
61 # create a list with all start and end times
62 # split it into recurring and singleshot timers
63
64 ##################################################################################
65 # process the new timer
66                 self.rep_eventlist = []
67                 self.nrep_eventlist = []
68                 if ext_timer != 1:
69                         self.newtimer = ext_timer
70                 if (self.newtimer is not None) and (not self.newtimer.disabled):
71                         if not self.newtimer.service_ref.ref.valid():
72                                 return False
73                         rflags = self.newtimer.repeated
74                         rflags = ((rflags & 0x7F)>> 3)|((rflags & 0x07)<<4)
75                         if rflags:
76                                 begin = self.newtimer.begin % 86400 # map to first day
77                                 if (self.localtimediff > 0) and ((begin + self.localtimediff) > 86400):
78                                         rflags = ((rflags >> 1)& 0x3F)|((rflags << 6)& 0x40)
79                                 elif (self.localtimediff < 0) and (begin < self.localtimediff):
80                                         rflags = ((rflags << 1)& 0x7E)|((rflags >> 6)& 0x01)
81                                 while rflags: # then arrange on the week
82                                         if rflags & 1:
83                                                 self.rep_eventlist.append((begin, -1))
84                                         begin += 86400
85                                         rflags >>= 1
86                         else:
87                                 self.nrep_eventlist.extend([(self.newtimer.begin,self.bflag,-1),(self.newtimer.end,self.eflag,-1)])
88
89 ##################################################################################
90 # now process existing timers
91                 idx = 0
92                 for timer in self.timerlist:
93                         if (timer != self.newtimer) and (not timer.disabled):
94                                 if timer.repeated:
95                                         rflags = timer.repeated
96                                         rflags = ((rflags & 0x7F)>> 3)|((rflags & 0x07)<<4)
97                                         begin = timer.begin % 86400 # map all to first day
98                                         if (self.localtimediff > 0) and ((begin + self.localtimediff) > 86400):
99                                                 rflags = ((rflags >> 1)& 0x3F)|((rflags << 6)& 0x40)
100                                         elif (self.localtimediff < 0) and (begin < self.localtimediff):
101                                                 rflags = ((rflags << 1)& 0x7E)|((rflags >> 6)& 0x01)
102                                         while rflags:
103                                                 if rflags & 1:
104                                                         self.rep_eventlist.append((begin, idx))
105                                                 begin += 86400
106                                                 rflags >>= 1
107                                 elif timer.state < TimerEntry.StateEnded:
108                                         self.nrep_eventlist.extend([(timer.begin,self.bflag,idx),(timer.end,self.eflag,idx)])
109                         idx += 1
110
111 ################################################################################
112 # journalize timer repeations
113                 if self.nrep_eventlist:
114                         interval_begin = min(self.nrep_eventlist)[0]
115                         interval_end = max(self.nrep_eventlist)[0]
116                         offset_0 = interval_begin - (interval_begin % 604800)
117                         weeks = (interval_end - offset_0) / 604800
118                         if ((interval_end - offset_0) % 604800):
119                                 weeks += 1
120                         for cnt in range(weeks):
121                                 for event in self.rep_eventlist:
122                                         if event[1] == -1: # -1 is the identifier of the changed timer
123                                                 event_begin = self.newtimer.begin
124                                                 event_end = self.newtimer.end
125                                         else:
126                                                 event_begin = self.timerlist[event[1]].begin
127                                                 event_end = self.timerlist[event[1]].end
128                                         new_event_begin = event[0] + offset_0 + (cnt * 604800)
129                                         # summertime correction
130                                         new_lth = localtime(new_event_begin).tm_hour
131                                         new_event_begin += 3600 * (localtime(event_begin).tm_hour - new_lth)
132                                         new_event_end = new_event_begin + (event_end - event_begin)
133                                         if event[1] == -1:
134                                                 if new_event_begin >= self.newtimer.begin: # is the soap already running?
135                                                         self.nrep_eventlist.extend([(new_event_begin, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
136                                         else:
137                                                 if new_event_begin >= self.timerlist[event[1]].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])])
139                 else:
140                         offset_0 = 345600 # the Epoch begins on Thursday
141                         for cnt in (0, 1): # test two weeks to take care of Sunday-Monday transitions
142                                 for event in self.rep_eventlist:
143                                         if event[1] == -1: # -1 is the identifier of the changed timer
144                                                 event_begin = self.newtimer.begin
145                                                 event_end = self.newtimer.end
146                                         else:
147                                                 event_begin = self.timerlist[event[1]].begin
148                                                 event_end = self.timerlist[event[1]].end
149                                         new_event_begin = event[0] + offset_0 + (cnt * 604800)
150                                         new_event_end = new_event_begin + (event_end - event_begin)
151                                         self.nrep_eventlist.extend([(new_event_begin, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
152
153 ################################################################################
154 # order list chronological
155                 self.nrep_eventlist.sort()
156
157 ##################################################################################
158 # detect overlapping timers and overlapping times
159                 fakeRecList = []
160                 ConflictTimer = None
161                 ConflictTunerType = None
162                 newTimerTunerType = None
163                 cnt = 0
164                 idx = 0
165                 overlaplist = []
166                 for event in self.nrep_eventlist:
167                         cnt += event[1]
168                         if event[2] == -1: # new timer
169                                 timer = self.newtimer
170                         else:
171                                 timer = self.timerlist[event[2]]
172                         if event[1] == self.bflag:
173                                 tunerType = [ ]
174                                 if timer.service_ref.ref and timer.service_ref.ref.flags & eServiceReference.isGroup:
175                                         fakeRecService = NavigationInstance.instance.recordService(getBestPlayableServiceReference(timer.service_ref.ref, eServiceReference(), True), True)
176                                 else:
177                                         fakeRecService = NavigationInstance.instance.recordService(timer.service_ref, True)
178                                 if fakeRecService:
179                                         fakeRecResult = fakeRecService.start(True)
180                                 else:
181                                         fakeRecResult = -1
182                                 if not fakeRecResult: # tune okay
183                                         if hasattr(fakeRecService, 'frontendInfo') and hasattr(fakeRecService.frontendInfo(), 'getFrontendData'):
184                                                 feinfo = fakeRecService.frontendInfo().getFrontendData()
185                                                 tunerType.append(feinfo.get("tuner_type"))
186                                 else: # tune failed.. so we must go another way to get service type (DVB-S, DVB-T, DVB-C)
187
188                                         def getServiceType(ref): # helper function to get a service type of a service reference
189                                                 serviceInfo = serviceHandler.info(ref)
190                                                 serviceInfo = serviceInfo and serviceInfo.getInfoObject(ref, iServiceInformation.sTransponderData)
191                                                 return serviceInfo and serviceInfo["tuner_type"] or ""
192
193                                         ref = timer.service_ref.ref
194                                         if ref.flags & eServiceReference.isGroup: # service group ?
195                                                 serviceList = serviceHandler.list(ref) # get all alternative services
196                                                 if serviceList:
197                                                         for ref in serviceList.getContent("R"): # iterate over all group service references
198                                                                 type = getServiceType(ref)
199                                                                 if not type in tunerType: # just add single time
200                                                                         tunerType.append(type)
201                                         else:
202                                                 tunerType.append(getServiceType(ref))
203
204                                 if event[2] == -1: # new timer
205                                         newTimerTunerType = tunerType
206                                 overlaplist.append((fakeRecResult, timer, tunerType))
207                                 fakeRecList.append((timer, fakeRecService))
208                                 if fakeRecResult:
209                                         if ConflictTimer is None: # just take care of the first conflict
210                                                 ConflictTimer = timer
211                                                 ConflictTunerType = tunerType
212                         elif event[1] == self.eflag:
213                                 for fakeRec in fakeRecList:
214                                         if timer == fakeRec[0] and fakeRec[1]:
215                                                 NavigationInstance.instance.stopRecordService(fakeRec[1])
216                                                 fakeRecList.remove(fakeRec)
217                                 fakeRec = None
218                                 for entry in overlaplist:
219                                         if entry[1] == timer:
220                                                 overlaplist.remove(entry)
221                         else:
222                                 print "Bug: unknown flag!"
223                         self.nrep_eventlist[idx] = (event[0],event[1],event[2],cnt,overlaplist[:]) # insert a duplicate into current overlaplist
224                         idx += 1
225
226                 if ConflictTimer is None: # no conflict found :)
227                         return True
228
229 ##################################################################################
230 # we have detected a conflict, now we must figure out the involved timers
231
232                 if self.newtimer is not None: # new timer?
233                         if self.newtimer is not ConflictTimer: # the new timer is not the conflicting timer?
234                                 for event in self.nrep_eventlist:
235                                         if len(event[4]) > 1: # entry in overlaplist of this event??
236                                                 kt = False
237                                                 nt = False
238                                                 for entry in event[4]:
239                                                         if entry[1] is ConflictTimer:
240                                                                 kt = True
241                                                         if entry[1] is self.newtimer:
242                                                                 nt = True
243                                                 if nt and kt:
244                                                         ConflictTimer = self.newtimer
245                                                         ConflictTunerType = newTimerTunerType
246                                                         break
247
248                 self.simultimer = [ ConflictTimer ]
249                 for event in self.nrep_eventlist:
250                         if len(event[4]) > 1: # entry in overlaplist of this event??
251                                 for entry in event[4]:
252                                         if entry[1] is ConflictTimer:
253                                                 break
254                                 else:
255                                         continue
256                                 for entry in event[4]:
257                                         if not entry[1] in self.simultimer:
258                                                 for x in entry[2]:
259                                                         if x in ConflictTunerType:
260                                                                 self.simultimer.append(entry[1])
261                                                                 break
262
263                 if len(self.simultimer) < 2:
264                         print "Possible Bug: unknown Conflict!"
265                         return True
266
267                 return False # conflict detected!