honor horizontal alignment also when a mark is present
[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                 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'), config.tuning.type)
132                 self.list.append(self.typeOfTuningEntry)
133                 self.satEntry = getConfigListEntry(_('Satellite'), config.tuning.sat)
134                 self.list.append(self.satEntry)
135                 if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
136                         self.list.append(getConfigListEntry(_('Frequency'), config.scan.sat.frequency))
137                         self.list.append(getConfigListEntry(_('Inversion'), config.scan.sat.inversion))
138                         self.list.append(getConfigListEntry(_('Symbol Rate'), config.scan.sat.symbolrate))
139                         self.list.append(getConfigListEntry(_("Polarity"), config.scan.sat.polarization))
140                         self.list.append(getConfigListEntry(_("FEC"), config.scan.sat.fec))
141                 elif config.tuning.transponder and currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
142                         self.list.append(getConfigListEntry(_("Transponder"), config.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 retune(self, configElement):
154                 returnvalue = (0, 0, 0, 0, 0, 0, 0)
155                 val = config.tuning.sat.value
156                 if val > 0 and len(config.tuning.sat.vals) > val:
157                         satpos = config.tuning.sat.vals[config.tuning.sat.value][1]
158                 elif len(config.tuning.sat.vals) > 0:
159                         satpos = config.tuning.sat.vals[0][1]
160                 else:
161                         satpos = None
162                 if satpos:
163                         if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
164                                 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)
165                         elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
166                                 transponder = nimmanager.getTransponders(config.tuning.sat.vals[config.tuning.sat.value][1])[config.tuning.transponder.value]
167                                 returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, config.tuning.sat.vals[config.tuning.sat.value][1], satpos)
168                         self.tune(returnvalue)
169
170         def createConfig(self, foo):
171                 config.tuning = ConfigSubsection()
172
173                 config.tuning.transponder = None
174                 config.tuning.type = configElement_nonSave("config.tuning.type", configSelection, 0, (("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined satellite"))))
175                 config.tuning.sat = configElement_nonSave("config.tuning.sat", configSatlist, 192, nimmanager.getSatListForNim(self.feid))
176                 ScanSetup.createConfig(self, None)
177                 
178                 self.updateSats()
179
180                 config.tuning.type.addNotifier(self.retune)
181                 config.tuning.sat.addNotifier(self.retune)
182                 config.scan.sat.frequency.addNotifier(self.retune)
183                 config.scan.sat.inversion.addNotifier(self.retune)
184                 config.scan.sat.symbolrate.addNotifier(self.retune)
185                 config.scan.sat.polarization.addNotifier(self.retune)
186                 config.scan.sat.fec.addNotifier(self.retune)
187
188         def updateSats(self):
189                 satnum = config.tuning.sat.value
190                 satlist = config.tuning.sat.vals
191                 if len(satlist):
192                         transponderlist = nimmanager.getTransponders(satlist[satnum][1])
193                         list = []
194                         for x in transponderlist:
195                                 if x[3] == 0:
196                                         pol = "H"
197                                 elif x[3] == 1:
198                                         pol = "V"
199                                 elif x[3] == 2:
200                                         pol = "CL"
201                                 elif x[3] == 3:
202                                         pol = "CR"
203                                 if x[4] == 0:
204                                         fec = "FEC_AUTO"
205                                 elif x[4] == 1:
206                                         fec = "FEC_1_2"
207                                 elif x[4] == 2:
208                                         fec = "FEC_2_3"
209                                 elif x[4] == 3:
210                                         fec = "FEC_3_4"
211                                 elif x[4] == 4:
212                                         fec = "FEC_5_6"
213                                 elif x[4] == 5:
214                                         fec = "FEC_7_8"
215                                 elif x[4] == 5:
216                                         fec = "FEC_8_9"
217                                 elif x[4] == 6:
218                                         fec = "FEC_None"
219                                 list.append(str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec)
220                         config.tuning.transponder = configElement_nonSave("config.tuning.transponder", configSelection, 0, list)
221                         config.tuning.transponder.addNotifier(self.retune)
222         
223         def keyGo(self):
224                 self.retune(config.tuning.type)
225
226         def keyCancel(self):
227                 if self.oldref:
228                         if self.frontend:
229                                 self.frontend = None
230                                 del self.raw_channel
231                         self.session.nav.playService(self.oldref)
232                 self.close(None)
233                 
234         def tune(self, transponder):
235                 if self.initcomplete:
236                         if transponder is not None:
237                                 self.tuner.tune(transponder)
238
239 class NimSelection(Screen):
240         skin = """
241                 <screen position="140,165" size="400,100" title="select Slot">
242                         <widget name="nimlist" position="20,10" size="360,75" />
243                 </screen>"""
244                 
245         def __init__(self, session):
246                 Screen.__init__(self, session)
247
248                 nimlist = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
249                 nimMenuList = []
250                 for x in nimlist:
251                         nimMenuList.append((_("NIM ") + (["A", "B", "C", "D"][x]) + ": " + nimmanager.getNimName(x) + " (" + nimmanager.getNimTypeName(x) + ")", x))
252                 
253                 self["nimlist"] = MenuList(nimMenuList)
254
255                 self["actions"] = ActionMap(["OkCancelActions"],
256                 {
257                         "ok": self.okbuttonClick ,
258                         "cancel": self.close
259                 }, -1)
260
261         def okbuttonClick(self):
262                 selection = self["nimlist"].getCurrent()
263                 self.session.open(Satfinder, selection[1])
264
265 def SatfinderMain(session, **kwargs):
266         nimList = nimmanager.getNimListOfType(nimmanager.nimType["DVB-S"])
267         if len(nimList) == 0:
268                 session.open(MessageBox, _("No positioner capable frontend found."), MessageBox.TYPE_ERROR)
269         else:
270                 if session.nav.RecordTimer.isRecording():
271                         session.open(MessageBox, _("A recording is currently running. Please stop the recording before trying to configure the positioner."), MessageBox.TYPE_ERROR)
272                 else:
273                         if len(nimList) == 1:
274                                 session.open(Satfinder, nimList[0])
275                         elif len(nimList) > 1:
276                                 session.open(NimSelection)
277                         else:
278                                 session.open(MessageBox, _("No tuner is configured for use with a diseqc positioner!"), MessageBox.TYPE_ERROR)
279
280
281 def Plugins(**kwargs):
282         return PluginDescriptor(name="Satfinder", description="Helps setting up your dish", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=SatfinderMain)