487dcd7284f3158aa2e90016ed736d4d5b2f2af8
[enigma2.git] / lib / python / Plugins / SystemPlugins / Satfinder / plugin.py
1 from enigma import eTimer, eDVBSatelliteEquipmentControl, eDVBResourceManager, eDVBDiseqcCommand, eDVBResourceManagerPtr, iDVBChannelPtr, iDVBFrontendPtr, iDVBFrontend, eDVBFrontendParametersSatellite, eDVBFrontendParameters
2 from Screens.Screen import Screen
3 from Screens.ScanSetup import ScanSetup
4 from Screens.MessageBox import MessageBox
5 from Plugins.Plugin import PluginDescriptor
6
7 from Components.Label import Label
8 from Components.ConfigList import ConfigList
9 from Components.TunerInfo import TunerInfo
10 from Components.ActionMap import ActionMap
11 from Components.NimManager import nimmanager
12 from Components.MenuList import MenuList
13 from Components.config import ConfigSelection, ConfigSatlist, getConfigListEntry
14
15 class Tuner:
16         def __init__(self, frontend):
17                 self.frontend = frontend
18                 
19         def tune(self, transponder):
20                 if self.frontend:
21                         print "tuning to transponder with data", transponder
22                         parm = eDVBFrontendParametersSatellite()
23                         parm.frequency = transponder[0] * 1000
24                         parm.symbol_rate = transponder[1] * 1000
25                         parm.polarisation = transponder[2]
26                         parm.fec = transponder[3]
27                         parm.inversion = transponder[4]
28                         parm.orbital_position = transponder[5]
29                         parm.system = 0  # FIXMEE !! HARDCODED DVB-S (add support for DVB-S2)
30                         parm.modulation = 1 # FIXMEE !! HARDCODED QPSK 
31                         feparm = eDVBFrontendParameters()
32                         feparm.setDVBS(parm)
33                         self.lastparm = feparm
34                         self.frontend.tune(feparm)
35         
36         def retune(self):
37                 if self.frontend:
38                         self.frontend.tune(self.lastparm)
39
40 class Satfinder(ScanSetup):
41         skin = """
42                 <screen position="90,100" size="520,400" title="Tune">
43                         <widget name="config" position="20,10" size="460,240" scrollbarMode="showOnDemand" />
44                         <widget name="introduction" position="20,360" zPosition="-10" size="350,30" font="Regular;23" />
45                         <widget name="snr" text="SNR:" position="0,245" size="60,22" font="Regular;21" />
46                         <widget name="agc" text="AGC:" position="0,270" size="60,22" font="Regular;21" />
47                         <widget name="ber" text="BER:" position="0,295" size="60,22" font="Regular;21" />
48                         <widget name="lock" text="Lock:" position="0,320" size="60,22" font="Regular;21" />
49                         <widget name="snr_percentage" position="220,245" size="60,22" font="Regular;21" />
50                         <widget name="agc_percentage" position="220,270" size="60,22" font="Regular;21" />
51                         <widget name="ber_value" position="220,295" size="60,22" font="Regular;21" />
52                         <widget name="lock_state" position="60,320" size="150,22" font="Regular;21" />
53                         <widget name="snr_bar" position="60,245" size="150,22" />
54                         <widget name="agc_bar" position="60,270" size="150,22" />
55                         <widget name="ber_bar" position="60,295" size="150,22" />
56                 </screen>"""
57
58         def openFrontend(self):
59                 res_mgr = eDVBResourceManagerPtr()
60                 if eDVBResourceManager.getInstance(res_mgr) == 0:
61                         self.raw_channel = iDVBChannelPtr()
62                         if res_mgr.allocateRawChannel(self.raw_channel, self.feid) == 0:
63                                 self.frontend = iDVBFrontendPtr()
64                                 if self.raw_channel.getFrontend(self.frontend) == 0:
65                                         return True
66                                 else:
67                                         print "getFrontend failed"
68                         else:
69                                 print "getRawChannel failed"
70                 else:
71                         print "getResourceManager instance failed"
72                 return False
73
74         def getFrontend(self):
75                 return self.frontend
76
77         def __init__(self, session, feid):
78                 self.initcomplete = False
79                 self.feid = feid
80                 self.oldref = None
81                 
82                 if not self.openFrontend():
83                         self.oldref = session.nav.getCurrentlyPlayingServiceReference()
84                         session.nav.stopService() # try to disable foreground service
85                         if not self.openFrontend():
86                                 if session.pipshown: # try to disable pip
87                                         session.pipshown = False
88                                         del session.pip
89                                         if not self.openFrontend():
90                                                 self.frontend = None # in normal case this should not happen
91                                                 self.getFrontend = None
92
93                 ScanSetup.__init__(self, session)
94                 self.tuner = Tuner(self.frontend)
95
96                 self["snr"] = Label()
97                 self["agc"] = Label()
98                 self["ber"] = Label()
99                 self["lock"] = Label()
100                 self["snr_percentage"] = TunerInfo(TunerInfo.SNR_PERCENTAGE, frontendfkt = self.getFrontend)
101                 self["agc_percentage"] = TunerInfo(TunerInfo.AGC_PERCENTAGE, frontendfkt = self.getFrontend)
102                 self["ber_value"] = TunerInfo(TunerInfo.BER_VALUE, frontendfkt = self.getFrontend)
103                 self["snr_bar"] = TunerInfo(TunerInfo.SNR_BAR, frontendfkt = self.getFrontend)
104                 self["agc_bar"] = TunerInfo(TunerInfo.AGC_BAR, frontendfkt = self.getFrontend)
105                 self["ber_bar"] = TunerInfo(TunerInfo.BER_BAR, frontendfkt = self.getFrontend)
106                 self["lock_state"] = TunerInfo(TunerInfo.LOCK_STATE, frontendfkt = self.getFrontend)
107
108                 self["introduction"].setText("")
109                 
110                 self.statusTimer = eTimer()
111                 self.statusTimer.timeout.get().append(self.updateStatus)
112                 self.statusTimer.start(50, False)
113
114                 self.initcomplete = True
115                 self.session = session
116                 
117         def updateStatus(self):
118                 self["snr_percentage"].update()
119                 self["agc_percentage"].update()
120                 self["ber_value"].update()
121                 self["snr_bar"].update()
122                 self["agc_bar"].update()
123                 self["ber_bar"].update()
124                 self["lock_state"].update()
125
126         def createSetup(self):
127                 self.typeOfTuningEntry = None
128                 self.satEntry = None
129
130                 self.list = []
131                 self.typeOfTuningEntry = getConfigListEntry(_('Tune'), self.tuning_type)
132                 self.list.append(self.typeOfTuningEntry)
133                 self.satEntry = getConfigListEntry(_('Satellite'), self.tuning_sat)
134                 self.list.append(self.satEntry)
135                 if self.tuning_type.value == "manual_transponder":
136                         self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
137                         self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
138                         self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
139                         self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
140                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
141                 elif self.tuning_transponder and self.tuning_type.value == "predefined_transponder":
142                         self.list.append(getConfigListEntry(_("Transponder"), self.tuning_transponder))
143                 self["config"].list = self.list
144                 self["config"].l.setList(self.list)
145
146         def newConfig(self):
147                 if self["config"].getCurrent() == self.typeOfTuningEntry:
148                         self.createSetup()
149                 elif self["config"].getCurrent() == self.satEntry:
150                         self.updateSats()
151                         self.createSetup()
152
153         def sat_changed(self, config_element):
154                 self.newConfig()
155                 self.retune(config_element)
156
157         def retune(self, configElement):
158                 returnvalue = (0, 0, 0, 0, 0, 0, 0)
159                 satpos = self.tuning_sat.orbital_position
160                 
161                 if satpos is not None:
162                         if self.tuning_type.value == "manual_transponder":
163                                 returnvalue = (self.scan_sat.frequency.value, self.scan_sat.symbolrate.value, self.scan_sat.polarization.index, self.scan_sat.fec.index, self.scan_sat.inversion.index, satpos)
164                         elif self.tuning_type.value == "predefined_transponder":
165                                 transponder = nimmanager.getTransponders(satpos)[self.tuning_transponder.index]
166                                 returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, satpos)
167                         self.tune(returnvalue)
168
169         def createConfig(self, foo):
170
171                 self.tuning_transponder = None
172                 self.tuning_type = ConfigSelection(choices = [("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined transponder"))])
173                 self.tuning_sat = ConfigSatlist(default = 192, list = nimmanager.getSatListForNim(self.feid))
174                 ScanSetup.createConfig(self, None)
175                 
176                 self.updateSats()
177
178                 self.tuning_type.addNotifier(self.retune, initial_call = False)
179                 self.tuning_sat.addNotifier(self.sat_changed, initial_call = False)
180                 self.scan_sat.frequency.addNotifier(self.retune, initial_call = False)
181                 self.scan_sat.inversion.addNotifier(self.retune, initial_call = False)
182                 self.scan_sat.symbolrate.addNotifier(self.retune, initial_call = False)
183                 self.scan_sat.polarization.addNotifier(self.retune, initial_call = False)
184                 self.scan_sat.fec.addNotifier(self.retune, initial_call = False)
185
186         def updateSats(self):
187                 orb_pos = self.tuning_sat.orbital_position
188                 if orb_pos is not None:
189                         transponderlist = nimmanager.getTransponders(orb_pos)
190                         list = []
191                         for x in transponderlist:
192                                 if x[3] == 0:
193                                         pol = "H"
194                                 elif x[3] == 1:
195                                         pol = "V"
196                                 elif x[3] == 2:
197                                         pol = "CL"
198                                 elif x[3] == 3:
199                                         pol = "CR"
200                                 if x[4] == 0:
201                                         fec = "FEC_AUTO"
202                                 elif x[4] == 1:
203                                         fec = "FEC_1_2"
204                                 elif x[4] == 2:
205                                         fec = "FEC_2_3"
206                                 elif x[4] == 3:
207                                         fec = "FEC_3_4"
208                                 elif x[4] == 4:
209                                         fec = "FEC_5_6"
210                                 elif x[4] == 5:
211                                         fec = "FEC_7_8"
212                                 elif x[4] == 5:
213                                         fec = "FEC_8_9"
214                                 elif x[4] == 6:
215                                         fec = "FEC_None"
216                                 list.append(str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec)
217                         self.tuning_transponder = ConfigSelection(choices = list)
218                         self.tuning_transponder.addNotifier(self.retune, initial_call = False)
219         
220         def keyGo(self):
221                 self.retune(self.tuning_type)
222
223         def keyCancel(self):
224                 if self.oldref:
225                         if self.frontend:
226                                 self.frontend = None
227                                 del self.raw_channel
228                         self.session.nav.playService(self.oldref)
229                 self.close(None)
230                 
231         def tune(self, transponder):
232                 if self.initcomplete:
233                         if transponder is not None:
234                                 self.tuner.tune(transponder)
235
236 class NimSelection(Screen):
237         skin = """
238                 <screen position="140,165" size="400,100" title="select Slot">
239                         <widget name="nimlist" position="20,10" size="360,75" />
240                 </screen>"""
241                 
242         def __init__(self, session):
243                 Screen.__init__(self, session)
244
245                 nimlist = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
246                 nimMenuList = []
247                 for x in nimlist:
248                         nimMenuList.append((_("NIM ") + (["A", "B", "C", "D"][x]) + ": " + nimmanager.getNimName(x) + " (" + nimmanager.getNimTypeName(x) + ")", x))
249                 
250                 self["nimlist"] = MenuList(nimMenuList)
251
252                 self["actions"] = ActionMap(["OkCancelActions"],
253                 {
254                         "ok": self.okbuttonClick ,
255                         "cancel": self.close
256                 }, -1)
257
258         def okbuttonClick(self):
259                 selection = self["nimlist"].getCurrent()
260                 self.session.open(Satfinder, selection[1])
261
262 def SatfinderMain(session, **kwargs):
263         nimList = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
264         if len(nimList) == 0:
265                 session.open(MessageBox, _("No positioner capable frontend found."), MessageBox.TYPE_ERROR)
266         else:
267                 if session.nav.RecordTimer.isRecording():
268                         session.open(MessageBox, _("A recording is currently running. Please stop the recording before trying to configure the positioner."), MessageBox.TYPE_ERROR)
269                 else:
270                         if len(nimList) == 1:
271                                 session.open(Satfinder, nimList[0])
272                         elif len(nimList) > 1:
273                                 session.open(NimSelection)
274                         else:
275                                 session.open(MessageBox, _("No tuner is configured for use with a diseqc positioner!"), MessageBox.TYPE_ERROR)
276
277
278 def Plugins(**kwargs):
279         return PluginDescriptor(name="Satfinder", description="Helps setting up your dish", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=SatfinderMain)