really fix satfinder
[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 config, ConfigSubsection, configElement_nonSave, configNothing, getConfigListEntry, configSelection, currentConfigSelectionElement, configSatlist
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
81                 if not self.openFrontend():
82                         session.nav.stopService() # try to disable foreground service
83                         if not self.openFrontend():
84                                 if session.pipshown: # try to disable pip
85                                         session.pipshown = False
86                                         del session.pip
87                                         if not self.openFrontend():
88                                                 self.frontend = None # in normal case this should not happen
89                                                 self.getFrontend = None
90
91                 ScanSetup.__init__(self, session)
92                 self.tuner = Tuner(self.frontend)
93
94                 self["snr"] = Label()
95                 self["agc"] = Label()
96                 self["ber"] = Label()
97                 self["lock"] = Label()
98                 self["snr_percentage"] = TunerInfo(TunerInfo.SNR_PERCENTAGE, frontendfkt = self.getFrontend)
99                 self["agc_percentage"] = TunerInfo(TunerInfo.AGC_PERCENTAGE, frontendfkt = self.getFrontend)
100                 self["ber_value"] = TunerInfo(TunerInfo.BER_VALUE, frontendfkt = self.getFrontend)
101                 self["snr_bar"] = TunerInfo(TunerInfo.SNR_BAR, frontendfkt = self.getFrontend)
102                 self["agc_bar"] = TunerInfo(TunerInfo.AGC_BAR, frontendfkt = self.getFrontend)
103                 self["ber_bar"] = TunerInfo(TunerInfo.BER_BAR, frontendfkt = self.getFrontend)
104                 self["lock_state"] = TunerInfo(TunerInfo.LOCK_STATE, frontendfkt = self.getFrontend)
105
106                 self["introduction"].setText("")
107                 
108                 self.statusTimer = eTimer()
109                 self.statusTimer.timeout.get().append(self.updateStatus)
110                 self.statusTimer.start(50, False)
111
112                 self.initcomplete = True
113                 
114         def updateStatus(self):
115                 self["snr_percentage"].update()
116                 self["agc_percentage"].update()
117                 self["ber_value"].update()
118                 self["snr_bar"].update()
119                 self["agc_bar"].update()
120                 self["ber_bar"].update()
121                 self["lock_state"].update()
122
123         def createSetup(self):
124                 self.typeOfTuningEntry = None
125                 self.satEntry = None
126
127                 self.list = []
128                 self.typeOfTuningEntry = getConfigListEntry(_('Tune'), config.tuning.type)
129                 self.list.append(self.typeOfTuningEntry)
130                 self.satEntry = getConfigListEntry(_('Satellite'), config.tuning.sat)
131                 self.list.append(self.satEntry)
132                 if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
133                         self.list.append(getConfigListEntry(_('Frequency'), config.scan.sat.frequency))
134                         self.list.append(getConfigListEntry(_('Inversion'), config.scan.sat.inversion))
135                         self.list.append(getConfigListEntry(_('Symbol Rate'), config.scan.sat.symbolrate))
136                         self.list.append(getConfigListEntry(_("Polarity"), config.scan.sat.polarization))
137                         self.list.append(getConfigListEntry(_("FEC"), config.scan.sat.fec))
138                 elif config.tuning.transponder and currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
139                         self.list.append(getConfigListEntry(_("Transponder"), config.tuning.transponder))
140                 self["config"].list = self.list
141                 self["config"].l.setList(self.list)
142
143         def newConfig(self):
144                 if self["config"].getCurrent() == self.typeOfTuningEntry:
145                         self.createSetup()
146                 elif self["config"].getCurrent() == self.satEntry:
147                         self.updateSats()
148                         self.createSetup()
149
150         def retune(self, configElement):
151                 returnvalue = (0, 0, 0, 0, 0, 0, 0)
152                 val = config.tuning.sat.value
153                 if val > 0 and len(config.tuning.sat.vals) > val:
154                         satpos = config.tuning.sat.vals[config.tuning.sat.value][1]
155                 elif len(config.tuning.sat.vals) > 0:
156                         satpos = config.tuning.sat.vals[0][1]
157                 else:
158                         satpos = None
159                 if satpos:
160                         if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
161                                 returnvalue = (config.scan.sat.frequency.value[0], config.scan.sat.symbolrate.value[0], config.scan.sat.polarization.value, config.scan.sat.fec.value, config.scan.sat.inversion.value, satpos)
162                         elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
163                                 transponder = nimmanager.getTransponders(config.tuning.sat.vals[config.tuning.sat.value][1])[config.tuning.transponder.value]
164                                 returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, config.tuning.sat.vals[config.tuning.sat.value][1], satpos)
165                         self.tune(returnvalue)
166
167         def createConfig(self, foo):
168                 config.tuning = ConfigSubsection()
169
170                 config.tuning.transponder = None
171                 config.tuning.type = configElement_nonSave("config.tuning.type", configSelection, 0, (("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined satellite"))))
172                 config.tuning.sat = configElement_nonSave("config.tuning.sat", configSatlist, 192, nimmanager.getSatListForNim(self.feid))
173                 ScanSetup.createConfig(self, None)
174                 
175                 self.updateSats()
176
177                 config.tuning.type.addNotifier(self.retune)
178                 config.tuning.sat.addNotifier(self.retune)
179                 config.scan.sat.frequency.addNotifier(self.retune)
180                 config.scan.sat.inversion.addNotifier(self.retune)
181                 config.scan.sat.symbolrate.addNotifier(self.retune)
182                 config.scan.sat.polarization.addNotifier(self.retune)
183                 config.scan.sat.fec.addNotifier(self.retune)
184
185         def updateSats(self):
186                 satnum = config.tuning.sat.value
187                 satlist = config.tuning.sat.vals
188                 if len(satlist):
189                         transponderlist = nimmanager.getTransponders(satlist[satnum][1])
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                         config.tuning.transponder = configElement_nonSave("config.tuning.transponder", configSelection, 0, list)
218                         config.tuning.transponder.addNotifier(self.retune)
219         
220         def keyGo(self):
221                 self.retune(config.tuning.type)
222
223         def keyCancel(self):
224                 self.close(None)
225                 
226         def tune(self, transponder):
227                 if self.initcomplete:
228                         if transponder is not None:
229                                 self.tuner.tune(transponder)
230
231 class NimSelection(Screen):
232         skin = """
233                 <screen position="140,165" size="400,100" title="select Slot">
234                         <widget name="nimlist" position="20,10" size="360,75" />
235                 </screen>"""
236                 
237         def __init__(self, session):
238                 Screen.__init__(self, session)
239
240                 nimlist = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
241                 nimMenuList = []
242                 for x in nimlist:
243                         nimMenuList.append((_("NIM ") + (["A", "B", "C", "D"][x]) + ": " + nimmanager.getNimName(x) + " (" + nimmanager.getNimTypeName(x) + ")", x))
244                 
245                 self["nimlist"] = MenuList(nimMenuList)
246
247                 self["actions"] = ActionMap(["OkCancelActions"],
248                 {
249                         "ok": self.okbuttonClick ,
250                         "cancel": self.close
251                 }, -1)
252
253         def okbuttonClick(self):
254                 selection = self["nimlist"].getCurrent()
255                 self.session.open(Satfinder, selection[1])
256
257 def SatfinderMain(session, **kwargs):
258         nimList = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
259         if len(nimList) == 0:
260                 session.open(MessageBox, _("No positioner capable 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 configure the positioner."), MessageBox.TYPE_ERROR)
264                 else:
265                         if len(nimList) == 1:
266                                 session.open(Satfinder, nimList[0])
267                         elif len(nimList) > 1:
268                                 session.open(NimSelection)
269                         else:
270                                 session.open(MessageBox, _("No tuner is configured for use with a diseqc positioner!"), MessageBox.TYPE_ERROR)
271
272 def Plugins(**kwargs):
273         return PluginDescriptor(name="Satfinder", description="Helps setting up your dish", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=SatfinderMain)