8174eb644504f4dbe3664c4e97f03174d5172c60
[enigma2.git] / lib / python / Plugins / SystemPlugins / Satfinder / plugin.py
1 from enigma import eDVBResourceManager,\
2         eDVBFrontendParametersSatellite, eDVBFrontendParameters
3
4 from Screens.Screen import Screen
5 from Screens.ScanSetup import ScanSetup
6 from Screens.MessageBox import MessageBox
7 from Plugins.Plugin import PluginDescriptor
8
9 from Components.Label import Label
10 from Components.Sources.FrontendStatus import FrontendStatus
11 from Components.ActionMap import ActionMap
12 from Components.NimManager import nimmanager, getConfigSatlist
13 from Components.MenuList import MenuList
14 from Components.config import ConfigSelection, getConfigListEntry
15 from Components.TuneTest import Tuner
16
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                                         del session.pip
47                                         if not self.openFrontend():
48                                                 self.frontend = None # in normal case this should not happen
49
50                 ScanSetup.__init__(self, session)
51                 self.tuner = Tuner(self.frontend)
52                 self["introduction"].setText("")
53                 self["Frontend"] = FrontendStatus(frontend_source = lambda : self.frontend, update_interval = 100)
54                 self.initcomplete = True
55                 self.onClose.append(self.__onClose)
56
57         def __onClose(self):
58                 self.session.nav.playService(self.oldref)
59
60         def createSetup(self):
61                 self.typeOfTuningEntry = None
62                 self.satEntry = None
63                 
64                 self.list = []
65
66                 self.typeOfTuningEntry = getConfigListEntry(_('Tune'), self.tuning_type)
67                 self.list.append(self.typeOfTuningEntry)
68                 self.satEntry = getConfigListEntry(_('Satellite'), self.tuning_sat)
69                 self.list.append(self.satEntry)
70
71                 nim = nimmanager.nim_slots[self.feid]
72
73                 self.systemEntry = None
74                 if self.tuning_type.value == "manual_transponder":
75                         if nim.isCompatible("DVB-S2"):
76                                 self.systemEntry = getConfigListEntry(_('System'), self.scan_sat.system)
77                                 self.list.append(self.systemEntry)
78                         else:
79                                 # downgrade to dvb-s, in case a -s2 config was active
80                                 self.scan_sat.system.value = eDVBFrontendParametersSatellite.System_DVB_S
81                         self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
82                         self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
83                         self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
84                         self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
85                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
86                                 self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
87                         elif self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
88                                 self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec_s2))
89                                 self.modulationEntry = getConfigListEntry(_('Modulation'), self.scan_sat.modulation)
90                                 self.list.append(self.modulationEntry)
91                                 self.list.append(getConfigListEntry(_('Rolloff'), self.scan_sat.rolloff))
92                                 self.list.append(getConfigListEntry(_('Pilot'), self.scan_sat.pilot))
93                 elif self.tuning_transponder and self.tuning_type.value == "predefined_transponder":
94                         self.list.append(getConfigListEntry(_("Transponder"), self.tuning_transponder))
95                 self["config"].list = self.list
96                 self["config"].l.setList(self.list)
97
98         def newConfig(self):
99                 cur = self["config"].getCurrent()
100                 if cur in (self.typeOfTuningEntry, self.systemEntry):
101                         self.createSetup()
102                 elif cur == self.satEntry:
103                         self.updateSats()
104                         self.createSetup()
105
106         def sat_changed(self, config_element):
107                 self.newConfig()
108                 self.retune(config_element)
109
110         def retune(self, configElement):
111                 returnvalue = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
112                 satpos = int(self.tuning_sat.value)
113                 if self.tuning_type.value == "manual_transponder":
114                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
115                                 fec = self.scan_sat.fec_s2.value
116                         else:
117                                 fec = self.scan_sat.fec.value
118                         returnvalue = (
119                                 self.scan_sat.frequency.value,
120                                 self.scan_sat.symbolrate.value,
121                                 self.scan_sat.polarization.value,
122                                 fec,
123                                 self.scan_sat.inversion.value,
124                                 satpos,
125                                 self.scan_sat.system.value,
126                                 self.scan_sat.modulation.value,
127                                 self.scan_sat.rolloff.value,
128                                 self.scan_sat.pilot.value)
129                         self.tune(returnvalue)
130                 elif self.tuning_type.value == "predefined_transponder":
131                         tps = nimmanager.getTransponders(satpos)
132                         l = len(tps)
133                         if l > self.tuning_transponder.index:
134                                 transponder = tps[self.tuning_transponder.index]
135                                 returnvalue = (transponder[1] / 1000, transponder[2] / 1000,
136                                         transponder[3], transponder[4], 2, satpos, transponder[5], transponder[6], transponder[8], transponder[9])
137                                 self.tune(returnvalue)
138
139         def createConfig(self, foo):
140                 self.tuning_transponder = None
141                 self.tuning_type = ConfigSelection(choices = [("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined transponder"))])
142                 self.tuning_sat = getConfigSatlist(192, nimmanager.getSatListForNim(self.feid))
143                 ScanSetup.createConfig(self, None)
144                 
145                 self.updateSats()
146
147                 for x in (self.tuning_type, self.tuning_sat, self.scan_sat.frequency,
148                         self.scan_sat.inversion, self.scan_sat.symbolrate,
149                         self.scan_sat.polarization, self.scan_sat.fec, self.scan_sat.pilot,
150                         self.scan_sat.fec_s2, self.scan_sat.fec, self.scan_sat.modulation,
151                         self.scan_sat.rolloff, self.scan_sat.system):
152                         x.addNotifier(self.retune, initial_call = False)
153
154         def updateSats(self):
155                 orb_pos = self.tuning_sat.orbital_position
156                 if orb_pos is not None:
157                         transponderlist = nimmanager.getTransponders(orb_pos)
158                         list = []
159                         default = None
160                         for x in transponderlist:
161                                 if x[3] == 0:
162                                         pol = "H"
163                                 elif x[3] == 1:
164                                         pol = "V"
165                                 elif x[3] == 2:
166                                         pol = "CL"
167                                 elif x[3] == 3:
168                                         pol = "CR"
169                                 else:
170                                         pol = "??"
171                                 if x[4] == 0:
172                                         fec = "FEC_AUTO"
173                                 elif x[4] == 1:
174                                         fec = "FEC_1_2"
175                                 elif x[4] == 2:
176                                         fec = "FEC_2_3"
177                                 elif x[4] == 3:
178                                         fec = "FEC_3_4"
179                                 elif x[4] == 4:
180                                         fec = "FEC_5_6"
181                                 elif x[4] == 5:
182                                         fec = "FEC_7_8"
183                                 elif x[4] == 6:
184                                         fec = "FEC_8_9"
185                                 elif x[4] == 7:
186                                         fec = "FEC_3_5"
187                                 elif x[4] == 8:
188                                         fec = "FEC_4_5"
189                                 elif x[4] == 9:
190                                         fec = "FEC_9_10"
191                                 elif x[4] == 15:
192                                         fec = "FEC_None"
193                                 else:
194                                         fec = "FEC_Unknown"
195                                 e = str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec
196                                 if default is None:
197                                         default = e
198                                 list.append(e)
199                         self.tuning_transponder = ConfigSelection(choices = list, default = default)
200                         self.tuning_transponder.addNotifier(self.retune, initial_call = False)
201
202         def keyGo(self):
203                 self.retune(self.tuning_type)
204
205         def restartPrevService(self, yesno):
206                 if yesno:
207                         if self.frontend:
208                                 self.frontend = None
209                                 del self.raw_channel
210                 else:
211                         self.oldref = None
212                 self.close(None)
213
214         def keyCancel(self):
215                 if self.oldref:
216                         self.session.openWithCallback(self.restartPrevService, MessageBox, _("Zap back to service before satfinder?"), MessageBox.TYPE_YESNO)
217                 else:
218                         self.restartPrevService(False)
219
220         def tune(self, transponder):
221                 if self.initcomplete:
222                         if transponder is not None:
223                                 self.tuner.tune(transponder)
224
225 class SatNimSelection(Screen):
226         skin = """
227                 <screen position="140,165" size="400,130" title="select Slot">
228                         <widget name="nimlist" position="20,10" size="360,100" />
229                 </screen>"""
230                 
231         def __init__(self, session):
232                 Screen.__init__(self, session)
233
234                 nimlist = nimmanager.getNimListOfType("DVB-S")
235                 nimMenuList = []
236                 for x in nimlist:
237                         nimMenuList.append((nimmanager.nim_slots[x].friendly_full_description, x))
238
239                 self["nimlist"] = MenuList(nimMenuList)
240
241                 self["actions"] = ActionMap(["OkCancelActions"],
242                 {
243                         "ok": self.okbuttonClick ,
244                         "cancel": self.close
245                 }, -1)
246
247         def okbuttonClick(self):
248                 selection = self["nimlist"].getCurrent()[1]
249                 self.session.open(Satfinder, selection)
250
251 def SatfinderMain(session, **kwargs):
252         nims = nimmanager.getNimListOfType("DVB-S")
253
254         nimList = []
255         for x in nims:
256                 if not nimmanager.getNimConfig(x).configMode.value in ("loopthrough", "satposdepends", "nothing"):
257                         nimList.append(x)
258
259         if len(nimList) == 0:
260                 session.open(MessageBox, _("No satellite frontend found!!"), MessageBox.TYPE_ERROR)
261         else:
262                 if session.nav.RecordTimer.isRecording():
263                         session.open(MessageBox, _("A recording is currently running. Please stop the recording before trying to start the satfinder."), MessageBox.TYPE_ERROR)
264                 else:
265                         if len(nimList) == 1:
266                                 session.open(Satfinder, nimList[0])
267                         else:
268                                 session.open(SatNimSelection)
269
270 def SatfinderStart(menuid, **kwargs):
271         if menuid == "scan":
272                 return [(_("Satfinder"), SatfinderMain, "satfinder", None)]
273         else:
274                 return []
275
276 def Plugins(**kwargs):
277         if (nimmanager.hasNimType("DVB-S")):
278                 return PluginDescriptor(name=_("Satfinder"), description="Helps setting up your dish", where = PluginDescriptor.WHERE_MENU, fnc=SatfinderStart)
279         else:
280                 return []