PEP8: Fix whitespace
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / TuneTest.py
1 from enigma import eDVBFrontendParametersSatellite, eDVBFrontendParametersTerrestrial, eDVBFrontendParametersCable, eDVBFrontendParameters, eDVBResourceManager, eTimer
2
3 class Tuner:
4         def __init__(self, frontend, ignore_rotor=False):
5                 self.frontend = frontend
6                 self.ignore_rotor = ignore_rotor
7
8         # transponder = (frequency, symbolrate, polarisation, fec, inversion, orbpos, system, modulation, rolloff, pilot, tsid, onid)
9         #                    0         1             2         3       4         5       6        7          8       9      10    11
10         def tune(self, transponder):
11                 if self.frontend:
12                         print "[TuneTest] tuning to transponder with data", transponder
13                         parm = eDVBFrontendParametersSatellite()
14                         parm.frequency = transponder[0] * 1000
15                         parm.symbol_rate = transponder[1] * 1000
16                         parm.polarisation = transponder[2]
17                         parm.fec = transponder[3]
18                         parm.inversion = transponder[4]
19                         parm.orbital_position = transponder[5]
20                         parm.system = transponder[6]
21                         parm.modulation = transponder[7]
22                         parm.rolloff = transponder[8]
23                         parm.pilot = transponder[9]
24                         self.tuneSatObj(parm)
25
26         def tuneSatObj(self, transponderObj):
27                 if self.frontend:
28                         feparm = eDVBFrontendParameters()
29                         feparm.setDVBS(transponderObj, self.ignore_rotor)
30                         self.lastparm = feparm
31                         self.frontend.tune(feparm)
32
33         def tuneTerr(self, frequency,
34                 inversion=2, bandwidth = 7000000, fechigh = 6, feclow = 6,
35                 modulation = 2, transmission = 2, guard = 4,
36                 hierarchy = 4, system = 0, plpid = 0):
37                 if self.frontend:
38                         print "[TuneTest] tuning to transponder with data", [frequency, inversion, bandwidth, fechigh, feclow, modulation, transmission, guard, hierarchy, system, plpid]
39                         parm = eDVBFrontendParametersTerrestrial()
40                         parm.frequency = frequency
41                         parm.inversion = inversion
42                         parm.bandwidth = bandwidth
43                         parm.code_rate_HP = fechigh
44                         parm.code_rate_LP = feclow
45                         parm.modulation = modulation
46                         parm.transmission_mode = transmission
47                         parm.guard_interval = guard
48                         parm.hierarchy = hierarchy
49                         parm.system = system
50                         parm.plpid = plpid
51                         self.tuneTerrObj(parm)
52
53         def tuneTerrObj(self, transponderObj):
54                 if self.frontend:
55                         feparm = eDVBFrontendParameters()
56                         feparm.setDVBT(transponderObj)
57                         self.lastparm = feparm
58                         self.frontend.tune(feparm)
59
60         def tuneCab(self, transponder):
61                 if self.frontend:
62                         print "[TuneTest] tuning to transponder with data", transponder
63                         parm = eDVBFrontendParametersCable()
64                         parm.frequency = transponder[0]
65                         parm.symbol_rate = transponder[1]
66                         parm.modulation = transponder[2]
67                         parm.fec_inner = transponder[3]
68                         parm.inversion = transponder[4]
69                         #parm.system = transponder[5]
70                         self.tuneCabObj(parm)
71
72         def tuneCabObj(self, transponderObj):
73                 if self.frontend:
74                         feparm = eDVBFrontendParameters()
75                         feparm.setDVBC(transponderObj)
76                         self.lastparm = feparm
77                         self.frontend.tune(feparm)
78
79         def retune(self):
80                 if self.frontend:
81                         self.frontend.tune(self.lastparm)
82
83         def getTransponderData(self):
84                 ret = { }
85                 if self.frontend:
86                         self.frontend.getTransponderData(ret, True)
87                 return ret
88
89 # tunes a list of transponders and checks, if they lock and optionally checks the onid/tsid combination
90 # 1) add transponders with addTransponder()
91 # 2) call run(<checkPIDs = True>)
92 # 3) finishedChecking() is called, when the run is finished
93 class TuneTest:
94         def __init__(self, feid, stopOnSuccess = -1, stopOnError = -1):
95                 self.stopOnSuccess = stopOnSuccess
96                 self.stopOnError = stopOnError
97                 self.feid = feid
98                 self.transponderlist = []
99                 self.currTuned = None
100                 print "TuneTest for feid %d" % self.feid
101                 if not self.openFrontend():
102                         self.oldref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
103                         self.session.nav.stopService() # try to disable foreground service
104                         if not self.openFrontend():
105                                 if self.session.pipshown: # try to disable pip
106                                         if hasattr(self.session, 'infobar'):
107                                                 if self.session.infobar.servicelist.dopipzap:
108                                                         self.session.infobar.servicelist.togglePipzap()
109                                         if hasattr(self.session, 'pip'):
110                                                 del self.session.pip
111                                         self.session.pipshown = False
112                                         if not self.openFrontend():
113                                                 self.frontend = None # in normal case this should not happen
114                 self.tuner = Tuner(self.frontend)
115                 self.timer = eTimer()
116                 self.timer.callback.append(self.updateStatus)
117
118         def gotTsidOnid(self, tsid, onid):
119                 print "******** got tsid, onid:", tsid, onid
120                 if tsid is not -1 and onid is not -1:
121                         self.pidStatus = self.INTERNAL_PID_STATUS_SUCCESSFUL
122                         self.tsid = tsid
123                         self.onid = onid
124                 else:
125                         self.pidStatus = self.INTERNAL_PID_STATUS_FAILED
126                         self.tsid = -1
127                         self.onid = -1
128                 self.timer.start(100, True)
129
130         def updateStatus(self):
131                 dict = {}
132                 self.frontend.getFrontendStatus(dict)
133                 stop = False
134
135                 print "status:", dict
136                 if dict["tuner_state"] == "TUNING":
137                         print "TUNING"
138                         self.timer.start(100, True)
139                         self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_TUNING, self.currTuned))
140                 elif self.checkPIDs and self.pidStatus == self.INTERNAL_PID_STATUS_NOOP:
141                         print "2nd choice"
142                         if dict["tuner_state"] == "LOCKED":
143                                 print "acquiring TSID/ONID"
144                                 self.raw_channel.receivedTsidOnid.get().append(self.gotTsidOnid)
145                                 self.raw_channel.requestTsidOnid()
146                                 self.pidStatus = self.INTERNAL_PID_STATUS_WAITING
147                         else:
148                                 self.pidStatus = self.INTERNAL_PID_STATUS_FAILED
149                 elif self.checkPIDs and self.pidStatus == self.INTERNAL_PID_STATUS_WAITING:
150                         print "waiting for pids"
151                 else:
152                         if dict["tuner_state"] == "LOSTLOCK" or dict["tuner_state"] == "FAILED":
153                                 self.tuningtransponder = self.nextTransponder()
154                                 self.failedTune.append([self.currTuned, self.oldTuned, "tune_failed", dict])  # last parameter is the frontend status)
155                                 if self.stopOnError != -1 and self.stopOnError <= len(self.failedTune):
156                                         stop = True
157                         elif dict["tuner_state"] == "LOCKED":
158                                 pidsFailed = False
159                                 if self.checkPIDs:
160                                         if self.currTuned is not None:
161                                                 if self.tsid != self.currTuned[10] or self.onid != self.currTuned[11]:
162                                                         self.failedTune.append([self.currTuned, self.oldTuned, "pids_failed", {"real": (self.tsid, self.onid), "expected": (self.currTuned[10], self.currTuned[11])}, dict])  # last parameter is the frontend status
163                                                         pidsFailed = True
164                                                 else:
165                                                         self.successfullyTune.append([self.currTuned, self.oldTuned, dict])  # 3rd parameter is the frontend status
166                                                         if self.stopOnSuccess != -1 and self.stopOnSuccess <= len(self.successfullyTune):
167                                                                 stop = True
168                                 elif not self.checkPIDs or (self.checkPids and not pidsFailed):
169                                         self.successfullyTune.append([self.currTuned, self.oldTuned, dict]) # 3rd parameter is the frontend status
170                                         if self.stopOnSuccess != -1 and self.stopOnSuccess <= len(self.successfullyTune):
171                                                                 stop = True
172                                 self.tuningtransponder = self.nextTransponder()
173                         else:
174                                 print "************* tuner_state:", dict["tuner_state"]
175
176                         self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_NOOP, self.currTuned))
177
178                         if not stop:
179                                 self.tune()
180                 if self.tuningtransponder < len(self.transponderlist) and not stop:
181                         if self.pidStatus != self.INTERNAL_PID_STATUS_WAITING:
182                                 self.timer.start(100, True)
183                                 print "restart timer"
184                         else:
185                                 print "not restarting timers (waiting for pids)"
186                 else:
187                         self.progressCallback((self.getProgressLength(), len(self.transponderlist), self.STATUS_DONE, self.currTuned))
188                         print "finishedChecking"
189                         self.finishedChecking()
190
191         def firstTransponder(self):
192                 print "firstTransponder:"
193                 index = 0
194                 if self.checkPIDs:
195                         print "checkPIDs-loop"
196                         # check for tsid != -1 and onid != -1
197                         print "index:", index
198                         print "len(self.transponderlist):", len(self.transponderlist)
199                         while (index < len(self.transponderlist) and (self.transponderlist[index][10] == -1 or self.transponderlist[index][11] == -1)):
200                                 index += 1
201                 print "FirstTransponder final index:", index
202                 return index
203
204         def nextTransponder(self):
205                 print "getting next transponder", self.tuningtransponder
206                 index = self.tuningtransponder + 1
207                 if self.checkPIDs:
208                         print "checkPIDs-loop"
209                         # check for tsid != -1 and onid != -1
210                         print "index:", index
211                         print "len(self.transponderlist):", len(self.transponderlist)
212                         while (index < len(self.transponderlist) and (self.transponderlist[index][10] == -1 or self.transponderlist[index][11] == -1)):
213                                 index += 1
214
215                 print "next transponder index:", index
216                 return index
217
218         def finishedChecking(self):
219                 print "finished testing"
220                 print "successfull:", self.successfullyTune
221                 print "failed:", self.failedTune
222
223         def openFrontend(self):
224                 res_mgr = eDVBResourceManager.getInstance()
225                 if res_mgr:
226                         self.raw_channel = res_mgr.allocateRawChannel(self.feid)
227                         if self.raw_channel:
228                                 self.frontend = self.raw_channel.getFrontend()
229                                 if self.frontend:
230                                         return True
231                                 else:
232                                         print "getFrontend failed"
233                         else:
234                                 print "getRawChannel failed"
235                 else:
236                         print "getResourceManager instance failed"
237                 return False
238
239         def tune(self):
240                 print "tuning to", self.tuningtransponder
241                 if self.tuningtransponder < len(self.transponderlist):
242                         self.pidStatus = self.INTERNAL_PID_STATUS_NOOP
243                         self.oldTuned = self.currTuned
244                         self.currTuned = self.transponderlist[self.tuningtransponder]
245                         self.tuner.tune(self.transponderlist[self.tuningtransponder])
246
247         INTERNAL_PID_STATUS_NOOP = 0
248         INTERNAL_PID_STATUS_WAITING = 1
249         INTERNAL_PID_STATUS_SUCCESSFUL = 2
250         INTERNAL_PID_STATUS_FAILED = 3
251
252         def run(self, checkPIDs = False):
253                 self.checkPIDs = checkPIDs
254                 self.pidStatus = self.INTERNAL_PID_STATUS_NOOP
255                 self.failedTune = []
256                 self.successfullyTune = []
257                 self.tuningtransponder = self.firstTransponder()
258                 self.tune()
259                 self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_START, self.currTuned))
260                 self.timer.start(100, True)
261
262         # transponder = (frequency, symbolrate, polarisation, fec, inversion, orbpos, <system>, <modulation>, <rolloff>, <pilot>, <tsid>, <onid>)
263         #                    0         1             2         3       4         5       6        7              8         9        10       11
264         def addTransponder(self, transponder):
265                 self.transponderlist.append(transponder)
266
267         def clearTransponder(self):
268                 self.transponderlist = []
269
270         def getProgressLength(self):
271                 count = 0
272                 if self.stopOnError == -1:
273                         count = len(self.transponderlist)
274                 else:
275                         if count < self.stopOnError:
276                                 count = self.stopOnError
277                 if self.stopOnSuccess == -1:
278                         count = len(self.transponderlist)
279                 else:
280                         if count < self.stopOnSuccess:
281                                 count = self.stopOnSuccess
282                 return count
283
284         STATUS_START = 0
285         STATUS_TUNING = 1
286         STATUS_DONE = 2
287         STATUS_NOOP = 3
288         # can be overwritten
289         # progress = (range, value, status, transponder)
290         def progressCallback(self, progress):
291                 pass