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