4.5.0r6
[enigma2.git] / usr / lib / enigma2 / python / Plugins / SystemPlugins / Satfinder / plugin.py
1 from __future__ import division
2 from __future__ import print_function
3 from enigma import eDVBResourceManager,\
4         eDVBFrontendParametersSatellite
5
6 from Screens.Screen import Screen
7 from Screens.ScanSetup import ScanSetup
8 from Screens.MessageBox import MessageBox
9 from Plugins.Plugin import PluginDescriptor
10
11 from Components.Sources.FrontendStatus import FrontendStatus
12 from Components.ActionMap import ActionMap
13 from Components.NimManager import nimmanager, getConfigSatlist
14 from Components.MenuList import MenuList
15 from Components.config import ConfigSelection, getConfigListEntry
16 from Components.TuneTest import Tuner
17
18 class Satfinder(ScanSetup):
19         def openFrontend(self):
20                 res_mgr = eDVBResourceManager.getInstance()
21                 if res_mgr:
22                         self.raw_channel = res_mgr.allocateRawChannel(self.feid)
23                         if self.raw_channel:
24                                 self.frontend = self.raw_channel.getFrontend()
25                                 if self.frontend:
26                                         return True
27                                 else:
28                                         print("getFrontend failed")
29                         else:
30                                 print("getRawChannel failed")
31                 else:
32                         print("getResourceManager instance failed")
33                 return False
34
35         def __init__(self, session, feid):
36                 self.initcomplete = False
37                 self.feid = feid
38                 self.oldref = None
39
40                 if not self.openFrontend():
41                         self.oldref = session.nav.getCurrentlyPlayingServiceReference()
42                         session.nav.stopService() # try to disable foreground service
43                         if not self.openFrontend():
44                                 if session.pipshown: # try to disable pip
45                                         session.pipshown = False
46                                         session.deleteDialog(session.pip)
47                                         del session.pip
48                                         if not self.openFrontend():
49                                                 self.frontend = None # in normal case this should not happen
50
51                 ScanSetup.__init__(self, session, 'S')
52                 self.tuner = Tuner(self.frontend)
53                 self["introduction"].setText("")
54                 self["Frontend"] = FrontendStatus(frontend_source = lambda : self.frontend, update_interval = 100)
55                 self.initcomplete = True
56                 self.onClose.append(self.__onClose)
57
58         def __onClose(self):
59                 self.session.nav.playService(self.oldref)
60
61         def createSetup(self):
62                 self.plpidAutoEntry = None
63                 self.fecEntry = None
64                 self.systemEntry = None
65                 self.modulationEntry = None
66                 self.satelliteEntry = None
67                 self.enableMisEntry = None
68                 self.plsModeEntry = None
69                 self.tunerEntry = None
70
71                 self.list = []
72
73                 self.typeOfScanEntry = getConfigListEntry(_('Tune'), self.tuning_type)
74                 self.list.append(self.typeOfScanEntry)
75                 self.satEntry = getConfigListEntry(_('Satellite'), self.tuning_sat)
76                 self.list.append(self.satEntry)
77
78                 nim = nimmanager.nim_slots[self.feid]
79
80                 self.systemEntry = None
81                 if self.tuning_type.value == "manual_transponder":
82                         if nim.isCompatible("DVB-S2"):
83                                 self.systemEntry = getConfigListEntry(_('System'), self.scan_sat.system)
84                                 self.list.append(self.systemEntry)
85                         else:
86                                 # downgrade to dvb-s, in case a -s2 config was active
87                                 self.scan_sat.system.value = eDVBFrontendParametersSatellite.System_DVB_S
88                         self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
89                         self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
90                         self.list.append(getConfigListEntry(_('Symbol rate'), self.scan_sat.symbolrate))
91                         self.list.append(getConfigListEntry(_('Polarization'), self.scan_sat.polarization))
92                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
93                                 self.modulationEntry = getConfigListEntry(_('Modulation'), nim.can_modulation_auto and self.scan_sat.modulation_auto or self.scan_sat.modulation)
94                                 mod = self.modulationEntry[1].value
95                                 if mod == eDVBFrontendParametersSatellite.Modulation_8PSK:
96                                         self.fecEntry = getConfigListEntry(_("FEC"), nim.can_auto_fec_s2 and self.scan_sat.fec_s2_8psk_auto or self.scan_sat.fec_s2_8psk)
97                                 else:
98                                         self.fecEntry = getConfigListEntry(_("FEC"), nim.can_auto_fec_s2 and self.scan_sat.fec_s2_qpsk_auto or self.scan_sat.fec_s2_qpsk)
99                                 self.list.append(self.fecEntry)
100                                 self.list.append(self.modulationEntry)
101                                 self.list.append(getConfigListEntry(_('Roll-off'), self.scan_sat.rolloff))
102                                 self.list.append(getConfigListEntry(_('Pilot'), self.scan_sat.pilot))
103                                 if nim.can_multistream_s2:
104                                         self.enableMisEntry = getConfigListEntry(_('Multistream'), self.scan_sat.enable_mis)
105                                         self.list.append(self.enableMisEntry)
106                                         if self.scan_sat.enable_mis.value:
107                                                 self.list.append(getConfigListEntry(_('Stream ID'), self.scan_sat.is_id))
108                                 if nim.can_pls_s2:
109                                         self.plsModeEntry = getConfigListEntry(_('PLS Mode'), self.scan_sat.pls_mode)
110                                         self.list.append(self.plsModeEntry)
111                                         if self.scan_sat.pls_mode.value != eDVBFrontendParametersSatellite.PLS_Unknown:
112                                                 self.list.append(getConfigListEntry(_('PLS Code'), self.scan_sat.pls_code))
113                         else:
114                                 self.fecEntry = getConfigListEntry(_("FEC"), self.scan_sat.fec)
115                                 self.list.append(self.fecEntry)
116                 elif self.tuning_transponder and self.tuning_type.value == "predefined_transponder":
117                         self.list.append(getConfigListEntry(_("Transponder"), self.tuning_transponder))
118                 self["config"].list = self.list
119                 self["config"].l.setList(self.list)
120
121         def newConfig(self):
122                 cur = self["config"].getCurrent()
123                 if cur is None:
124                         pass
125                 elif cur == self.satEntry:
126                         self.updateSats()
127                         self.createSetup()
128                 else:
129                         ScanSetup.newConfig(self)
130                 if self.systemEntry and cur == self.systemEntry or \
131                         cur == self.tuning_type:
132                         self.retune(None)
133
134         def sat_changed(self, config_element):
135                 self.newConfig()
136                 self.retune(config_element)
137
138         def retune(self, configElement):
139                 returnvalue = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
140                 satpos = int(self.tuning_sat.value)
141                 if self.tuning_type.value == "manual_transponder":
142                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
143                                 fec = self.scan_sat.fec.value
144                                 mod = eDVBFrontendParametersSatellite.Modulation_QPSK
145                         else:
146                                 mod = self.modulationEntry[1].value
147                                 fec = self.fecEntry[1].value
148                         returnvalue = (
149                                 self.scan_sat.frequency.float,
150                                 self.scan_sat.symbolrate.value,
151                                 self.scan_sat.polarization.value,
152                                 fec,
153                                 self.scan_sat.inversion.value,
154                                 satpos,
155                                 self.scan_sat.system.value,
156                                 mod,
157                                 self.scan_sat.rolloff.value,
158                                 self.scan_sat.pilot.value,
159                                 self.scan_sat.is_id.value if self.scan_sat.enable_mis.value else -1,
160                                 self.scan_sat.pls_mode.value,
161                                 self.scan_sat.pls_code.value if self.scan_sat.pls_mode.value < eDVBFrontendParametersSatellite.PLS_Unknown else 0)
162                         self.tune(returnvalue)
163                 elif self.tuning_type.value == "predefined_transponder":
164                         tps = nimmanager.getTransponders(satpos)
165                         l = len(tps)
166                         if l > self.tuning_transponder.index:
167                                 transponder = tps[self.tuning_transponder.index]
168                                 returnvalue = (transponder[1] // 1000, transponder[2] // 1000,
169                                         transponder[3], transponder[4], 2, satpos, transponder[5], transponder[6], transponder[8], transponder[9])
170                                 self.tune(returnvalue)
171
172         def createConfig(self, foo):
173                 self.tuning_transponder = None
174                 self.tuning_type = ConfigSelection(choices = [("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined transponder"))])
175                 self.tuning_sat = getConfigSatlist(192, nimmanager.getSatListForNim(self.feid))
176                 ScanSetup.createConfig(self, None)
177
178                 self.updateSats()
179
180                 for x in (self.tuning_sat, self.scan_sat.frequency,
181                         self.scan_sat.inversion, self.scan_sat.symbolrate,
182                         self.scan_sat.polarization, self.scan_sat.fec,
183                         self.scan_sat.fec_s2_8psk, self.scan_sat.fec_s2_8psk_auto, 
184                         self.scan_sat.fec_s2_qpsk, self.scan_sat.fec_s2_qpsk_auto,
185                         self.scan_sat.modulation, self.scan_sat.modulation_auto,
186                         self.scan_sat.enable_mis, self.scan_sat.is_id, 
187                         self.scan_sat.pls_mode, self.scan_sat.pls_code,
188                         self.scan_sat.pilot, self.scan_sat.rolloff):
189                         x.addNotifier(self.retune, initial_call = False)
190
191         def updateSats(self):
192                 orb_pos = self.tuning_sat.orbital_position
193                 if orb_pos is not None:
194                         transponderlist = nimmanager.getTransponders(orb_pos)
195                         list = []
196                         default = None
197                         index = 0
198                         for x in transponderlist:
199                                 if x[3] == 0:
200                                         pol = "H"
201                                 elif x[3] == 1:
202                                         pol = "V"
203                                 elif x[3] == 2:
204                                         pol = "CL"
205                                 elif x[3] == 3:
206                                         pol = "CR"
207                                 else:
208                                         pol = "??"
209                                 if x[4] == 0:
210                                         fec = "FEC Auto"
211                                 elif x[4] == 1:
212                                         fec = "FEC 1/2"
213                                 elif x[4] == 2:
214                                         fec = "FEC 2/3"
215                                 elif x[4] == 3:
216                                         fec = "FEC 3/4"
217                                 elif x[4] == 4:
218                                         fec = "FEC 5/6"
219                                 elif x[4] == 5:
220                                         fec = "FEC 7/8"
221                                 elif x[4] == 6:
222                                         fec = "FEC 8/9"
223                                 elif x[4] == 7:
224                                         fec = "FEC 3/5"
225                                 elif x[4] == 8:
226                                         fec = "FEC 4/5"
227                                 elif x[4] == 9:
228                                         fec = "FEC 9/10"
229                                 elif x[4] == 15:
230                                         fec = "FEC None"
231                                 else:
232                                         fec = "FEC Unknown"
233                                 e = str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec
234                                 if default is None:
235                                         default = str(index)
236                                 list.append((str(index), e))
237                                 index += 1
238                         self.tuning_transponder = ConfigSelection(choices = list, default = default)
239                         self.tuning_transponder.addNotifier(self.retune, initial_call = False)
240
241         def keyGo(self):
242                 self.retune(self.tuning_type)
243
244         def restartPrevService(self, yesno):
245                 if yesno:
246                         if self.frontend:
247                                 self.frontend = None
248                                 del self.raw_channel
249                 else:
250                         self.oldref = None
251                 self.close(None)
252
253         def keyCancel(self):
254                 if self.oldref:
255                         self.session.openWithCallback(self.restartPrevService, MessageBox, _("Zap back to service before satfinder?"), MessageBox.TYPE_YESNO)
256                 else:
257                         self.restartPrevService(False)
258
259         def tune(self, transponder):
260                 if self.initcomplete:
261                         if transponder is not None:
262                                 self.tuner.tune(transponder)
263
264 class SatNimSelection(Screen):
265         skin = """
266                 <screen position="140,165" size="400,130" title="select Slot">
267                         <widget name="nimlist" position="20,10" size="360,100" />
268                 </screen>"""
269
270         def __init__(self, session):
271                 Screen.__init__(self, session)
272
273                 nimlist = nimmanager.getNimListOfType("DVB-S")
274                 nimMenuList = []
275                 for x in nimlist:
276                         nimMenuList.append((nimmanager.nim_slots[x].friendly_full_description, x))
277
278                 self["nimlist"] = MenuList(nimMenuList)
279
280                 self["actions"] = ActionMap(["OkCancelActions"],
281                 {
282                         "ok": self.okbuttonClick ,
283                         "cancel": self.close
284                 }, -1)
285
286         def okbuttonClick(self):
287                 selection = self["nimlist"].getCurrent()[1]
288                 self.session.open(Satfinder, selection)
289
290 def SatfinderMain(session, **kwargs):
291         nims = nimmanager.getNimListOfType("DVB-S")
292
293         nimList = []
294         for x in nims:
295                 nim = nimmanager.getNimConfig(x)
296                 if not nim.sat.configMode.value in ("loopthrough", "satposdepends", "nothing"):
297                         nimList.append(x)
298
299         if len(nimList) == 0:
300                 session.open(MessageBox, _("No satellite frontend found!!"), MessageBox.TYPE_ERROR)
301         else:
302                 if session.nav.RecordTimer.isRecording():
303                         session.open(MessageBox, _("A recording is currently running. Please stop the recording before trying to start the satfinder."), MessageBox.TYPE_ERROR)
304                 else:
305                         if len(nimList) == 1:
306                                 session.open(Satfinder, nimList[0])
307                         else:
308                                 session.open(SatNimSelection)
309
310 def SatfinderStart(menuid, **kwargs):
311         if menuid == "scan":
312                 return [(_("Satfinder"), SatfinderMain, "satfinder", None)]
313         else:
314                 return []
315
316 def Plugins(**kwargs):
317         if (nimmanager.hasNimType("DVB-S")):
318                 return PluginDescriptor(name=_("Satfinder"), description=_("Helps setting up your dish"), where = PluginDescriptor.WHERE_MENU, needsRestart = False, fnc=SatfinderStart)
319         else:
320                 return []