change order of index and begin/end flag and swap begin/endflags value to
[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                 # BeginEndFlag 1 for begin, -1 for end
54                 # index -1 for the new Timer, 0..n index of the existing timers
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,self.bflag,-1),(self.newtimer.end,self.eflag,-1)])
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,self.bflag,idx),(timer.end,self.eflag,idx)])
98                         idx += 1
99
100 ################################################################################
101 # journalize timer repeations
102                 if 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, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
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, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
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, self.bflag, event[1]),(new_event_end, self.eflag, event[1])])
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[1]
160                         if event[2] == -1: # new timer
161                                 timer = self.newtimer
162                         else:
163                                 timer = self.timerlist[event[2]]
164                         if event[1] == 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[2] == -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                                                 ConflictTunerSlot = tunerSlot
180                         elif event[1] == 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                                 for entry in overlaplist:
187                                         if entry[1] == timer:
188                                                 overlaplist.remove(entry)
189                         else:
190                                 print "Bug: unknown flag!"
191                         self.nrep_eventlist[idx] = (event[0],event[1],event[2],cnt,overlaplist[:]) # insert a duplicate into current overlaplist
192                         idx += 1
193
194                 if ConflictTimer is None: # no conflict found :)
195                         return True
196
197 ##################################################################################
198 # we have detected a conflict, now we must figure out the involved timers
199
200                 if self.newtimer is not None: # new timer?
201                         if self.newtimer is not ConflictTimer: # the new timer is not the conflicting timer?
202                                 for event in self.nrep_eventlist:
203                                         if len(event[4]) > 1: # entry in overlaplist of this event??
204                                                 kt = False
205                                                 nt = False
206                                                 for entry in event[4]:
207                                                         if entry[1] is ConflictTimer:
208                                                                 kt = True
209                                                         if entry[1] is self.newtimer:
210                                                                 nt = True
211                                                 if nt and kt:
212                                                         ConflictTimer = self.newtimer
213                                                         ConflictTunerType = newTimerTunerType
214                                                         ConflictSlot = newTimerTunerSlot
215                                                         break
216
217                 self.simultimer = [ ConflictTimer ]
218                 for event in self.nrep_eventlist:
219                         if len(event[4]) > 1: # entry in overlaplist of this event??
220                                 for entry in event[4]:
221                                         if entry[1] is ConflictTimer:
222                                                 break
223                                 else:
224                                         continue
225                                 for entry in event[4]:
226                                         if not self.simultimer.count(entry[1]) and (entry[2] == ConflictTunerType or entry[3] == ConflictTunerSlot):
227                                                 self.simultimer.append(entry[1])
228
229                 if len(self.simultimer) < 2:
230                         print "Bug: unknown Conflict!"
231
232                 return False # conflict detected!