finally fix satfinder with new config
[enigma2.git] / lib / python / Plugins / SystemPlugins / Satfinder / plugin.py
index d4277af..06a0f3f 100644 (file)
@@ -10,81 +10,32 @@ from Components.TunerInfo import TunerInfo
 from Components.ActionMap import ActionMap
 from Components.NimManager import nimmanager
 from Components.MenuList import MenuList
-from Components.config import config, ConfigSubsection, configElement_nonSave, configNothing, getConfigListEntry, configSelection, currentConfigSelectionElement, configSatlist
+from Components.config import ConfigSelection, ConfigSatlist, getConfigListEntry
 
 class Tuner:
        def __init__(self, frontend):
                self.frontend = frontend
                
        def tune(self, transponder):
-               print "tuning to transponder with data", transponder
-               parm = eDVBFrontendParametersSatellite()
-               parm.frequency = transponder[0] * 1000
-               parm.symbol_rate = transponder[1] * 1000
-               parm.polarisation = transponder[2]
-               parm.fec = transponder[3]
-               parm.inversion = transponder[4]
-               parm.orbital_position = transponder[5]
-               parm.system = 0  # FIXMEE !! HARDCODED DVB-S (add support for DVB-S2)
-               parm.modulation = 1 # FIXMEE !! HARDCODED QPSK 
-               feparm = eDVBFrontendParameters()
-               feparm.setDVBS(parm, True)
-               self.lastparm = feparm
-               self.frontend.tune(feparm)
+               if self.frontend:
+                       print "tuning to transponder with data", transponder
+                       parm = eDVBFrontendParametersSatellite()
+                       parm.frequency = transponder[0] * 1000
+                       parm.symbol_rate = transponder[1] * 1000
+                       parm.polarisation = transponder[2]
+                       parm.fec = transponder[3]
+                       parm.inversion = transponder[4]
+                       parm.orbital_position = transponder[5]
+                       parm.system = 0  # FIXMEE !! HARDCODED DVB-S (add support for DVB-S2)
+                       parm.modulation = 1 # FIXMEE !! HARDCODED QPSK 
+                       feparm = eDVBFrontendParameters()
+                       feparm.setDVBS(parm)
+                       self.lastparm = feparm
+                       self.frontend.tune(feparm)
        
        def retune(self):
-               self.frontend.tune(self.lastparm)
-       
-       def getTransponderData(self):
-               return self.frontend.readTransponderData(True)
-
-class Diseqc:
-       def __init__(self, feid = 0):
-               self.ready = False
-               self.feid = feid
-               res_mgr = eDVBResourceManagerPtr()
-               if eDVBResourceManager.getInstance(res_mgr) == 0:
-                       self.raw_channel = iDVBChannelPtr()
-                       if res_mgr.allocateRawChannel(self.raw_channel, self.feid) == 0:
-                               self.frontend = iDVBFrontendPtr()
-                               if self.raw_channel.getFrontend(self.frontend) == 0:
-                                       self.ready = True
-                               else:
-                                       print "getFrontend failed"
-                       else:
-                               print "getRawChannel failed"
-               else:
-                       print "getResourceManager instance failed"
-       
-       def getFrontend(self):
-               return self.frontend
-               
-       def command(self, what, param = 0):
-               if self.ready:
-                       cmd = eDVBDiseqcCommand()
-                       if what == "moveWest":
-                               string = 'e03169' + ("%02x" % param)
-                       elif what == "moveEast":
-                               string = 'e03168' + ("%02x" % param)
-                       elif what == "moveTo":
-                               string = 'e0316b' + ("%02x" % param)
-                       elif what == "store":
-                               string = 'e0316a' + ("%02x" % param)
-                       elif what == "limitOn":
-                               string = 'e0316a00'
-                       elif what == "limitOff":
-                               string = 'e03163'
-                       elif what == "limitEast":
-                               string = 'e03166'
-                       elif what == "limitWest":
-                               string = 'e03167'
-                       else:
-                               string = 'e03160' #positioner stop
-                       print "diseqc command:",
-                       print string
-                       
-                       cmd.setCommandString(string)
-                       self.frontend.sendDiseqc(cmd)
+               if self.frontend:
+                       self.frontend.tune(self.lastparm)
 
 class Satfinder(ScanSetup):
        skin = """
@@ -104,27 +55,55 @@ class Satfinder(ScanSetup):
                        <widget name="ber_bar" position="60,295" size="150,22" />
                </screen>"""
 
+       def openFrontend(self):
+               res_mgr = eDVBResourceManagerPtr()
+               if eDVBResourceManager.getInstance(res_mgr) == 0:
+                       self.raw_channel = iDVBChannelPtr()
+                       if res_mgr.allocateRawChannel(self.raw_channel, self.feid) == 0:
+                               self.frontend = iDVBFrontendPtr()
+                               if self.raw_channel.getFrontend(self.frontend) == 0:
+                                       return True
+                               else:
+                                       print "getFrontend failed"
+                       else:
+                               print "getRawChannel failed"
+               else:
+                       print "getResourceManager instance failed"
+               return False
+
+       def getFrontend(self):
+               return self.frontend
+
        def __init__(self, session, feid):
                self.initcomplete = False
                self.feid = feid
+               self.oldref = None
                
+               if not self.openFrontend():
+                       self.oldref = session.nav.getCurrentlyPlayingServiceReference()
+                       session.nav.stopService() # try to disable foreground service
+                       if not self.openFrontend():
+                               if session.pipshown: # try to disable pip
+                                       session.pipshown = False
+                                       del session.pip
+                                       if not self.openFrontend():
+                                               self.frontend = None # in normal case this should not happen
+                                               self.getFrontend = None
+
                ScanSetup.__init__(self, session)
-               self.session.nav.stopService()
-               
-               self.diseqc = Diseqc(self.feid)
-               self.tuner = Tuner(self.diseqc.getFrontend())
-               
+               self.tuner = Tuner(self.frontend)
+
                self["snr"] = Label()
                self["agc"] = Label()
                self["ber"] = Label()
                self["lock"] = Label()
-               self["snr_percentage"] = TunerInfo(TunerInfo.SNR_PERCENTAGE, frontendfkt = self.diseqc.getFrontend)
-               self["agc_percentage"] = TunerInfo(TunerInfo.AGC_PERCENTAGE, frontendfkt = self.diseqc.getFrontend)
-               self["ber_value"] = TunerInfo(TunerInfo.BER_VALUE, frontendfkt = self.diseqc.getFrontend)
-               self["snr_bar"] = TunerInfo(TunerInfo.SNR_BAR, frontendfkt = self.diseqc.getFrontend)
-               self["agc_bar"] = TunerInfo(TunerInfo.AGC_BAR, frontendfkt = self.diseqc.getFrontend)
-               self["ber_bar"] = TunerInfo(TunerInfo.BER_BAR, frontendfkt = self.diseqc.getFrontend)
-               self["lock_state"] = TunerInfo(TunerInfo.LOCK_STATE, frontendfkt = self.diseqc.getFrontend)
+               self["snr_percentage"] = TunerInfo(TunerInfo.SNR_PERCENTAGE, frontendfkt = self.getFrontend)
+               self["agc_percentage"] = TunerInfo(TunerInfo.AGC_PERCENTAGE, frontendfkt = self.getFrontend)
+               self["ber_value"] = TunerInfo(TunerInfo.BER_VALUE, frontendfkt = self.getFrontend)
+               self["snr_bar"] = TunerInfo(TunerInfo.SNR_BAR, frontendfkt = self.getFrontend)
+               self["agc_bar"] = TunerInfo(TunerInfo.AGC_BAR, frontendfkt = self.getFrontend)
+               self["ber_bar"] = TunerInfo(TunerInfo.BER_BAR, frontendfkt = self.getFrontend)
+               self["lock_state"] = TunerInfo(TunerInfo.LOCK_STATE, frontendfkt = self.getFrontend)
 
                self["introduction"].setText("")
                
@@ -133,6 +112,7 @@ class Satfinder(ScanSetup):
                self.statusTimer.start(50, False)
 
                self.initcomplete = True
+               self.session = session
                
        def updateStatus(self):
                self["snr_percentage"].update()
@@ -142,24 +122,24 @@ class Satfinder(ScanSetup):
                self["agc_bar"].update()
                self["ber_bar"].update()
                self["lock_state"].update()
-               
+
        def createSetup(self):
                self.typeOfTuningEntry = None
                self.satEntry = None
 
                self.list = []
-               self.typeOfTuningEntry = getConfigListEntry(_('Tune'), config.tuning.type)
+               self.typeOfTuningEntry = getConfigListEntry(_('Tune'), self.tuning_type)
                self.list.append(self.typeOfTuningEntry)
-               self.satEntry = getConfigListEntry(_('Satellite'), config.tuning.sat)
+               self.satEntry = getConfigListEntry(_('Satellite'), self.tuning_sat)
                self.list.append(self.satEntry)
-               if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
-                       self.list.append(getConfigListEntry(_('Frequency'), config.scan.sat.frequency))
-                       self.list.append(getConfigListEntry(_('Inversion'), config.scan.sat.inversion))
-                       self.list.append(getConfigListEntry(_('Symbol Rate'), config.scan.sat.symbolrate))
-                       self.list.append(getConfigListEntry(_("Polarity"), config.scan.sat.polarization))
-                       self.list.append(getConfigListEntry(_("FEC"), config.scan.sat.fec))
-               elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
-                       self.list.append(getConfigListEntry(_("Transponder"), config.tuning.transponder))
+               if self.tuning_type.value == "manual_transponder":
+                       self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
+                       self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
+                       self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
+                       self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
+                       self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
+               elif self.tuning_transponder and self.tuning_type.value == "predefined_transponder":
+                       self.list.append(getConfigListEntry(_("Transponder"), self.tuning_transponder))
                self["config"].list = self.list
                self["config"].l.setList(self.list)
 
@@ -170,39 +150,43 @@ class Satfinder(ScanSetup):
                        self.updateSats()
                        self.createSetup()
 
+       def sat_changed(self, config_element):
+               self.newConfig()
+               self.retune(config_element)
+
        def retune(self, configElement):
                returnvalue = (0, 0, 0, 0, 0, 0, 0)
-               satpos = config.tuning.sat.vals[config.tuning.sat.value][1]
-               if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
-                       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)
-               elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
-                       transponder = nimmanager.getTransponders(config.tuning.sat.vals[config.tuning.sat.value][1])[config.tuning.transponder.value]
-                       returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, config.tuning.sat.vals[config.tuning.sat.value][1], satpos)
-               self.tune(returnvalue)
+               satpos = self.tuning_sat.orbital_position
+               
+               if satpos is not None:
+                       if self.tuning_type.value == "manual_transponder":
+                               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)
+                       elif self.tuning_type.value == "predefined_transponder":
+                               transponder = nimmanager.getTransponders(satpos)[self.tuning_transponder.index]
+                               returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, satpos)
+                       self.tune(returnvalue)
 
        def createConfig(self, foo):
-               config.tuning = ConfigSubsection()
-               
-               config.tuning.type = configElement_nonSave("config.tuning.type", configSelection, 0, (("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined satellite"))))
-               config.tuning.sat = configElement_nonSave("config.tuning.sat", configSatlist, 192, nimmanager.getRotorSatListForNim(self.feid))
+
+               self.tuning_transponder = None
+               self.tuning_type = ConfigSelection(choices = [("manual_transponder", _("Manual transponder")), ("predefined_transponder", _("Predefined satellite"))])
+               self.tuning_sat = ConfigSatlist(default = 192, list = nimmanager.getSatListForNim(self.feid))
                ScanSetup.createConfig(self, None)
                
                self.updateSats()
 
-               config.tuning.type.addNotifier(self.retune)
-               config.tuning.sat.addNotifier(self.retune)
-               config.scan.sat.frequency.addNotifier(self.retune)
-               config.scan.sat.inversion.addNotifier(self.retune)
-               config.scan.sat.symbolrate.addNotifier(self.retune)
-               config.scan.sat.polarization.addNotifier(self.retune)
-               config.scan.sat.fec.addNotifier(self.retune)
-               config.tuning.transponder.addNotifier(self.retune)
-               
+               self.tuning_type.addNotifier(self.retune)
+               self.tuning_sat.addNotifier(self.sat_changed)
+               self.scan_sat.frequency.addNotifier(self.retune)
+               self.scan_sat.inversion.addNotifier(self.retune)
+               self.scan_sat.symbolrate.addNotifier(self.retune)
+               self.scan_sat.polarization.addNotifier(self.retune)
+               self.scan_sat.fec.addNotifier(self.retune)
+
        def updateSats(self):
-               satnum = config.tuning.sat.value
-               satlist = config.tuning.sat.vals
-               if len(satlist):
-                       transponderlist = nimmanager.getTransponders(satlist[satnum][1])
+               orb_pos = self.tuning_sat.orbital_position
+               if orb_pos is not None:
+                       transponderlist = nimmanager.getTransponders(orb_pos)
                        list = []
                        for x in transponderlist:
                                if x[3] == 0:
@@ -230,19 +214,18 @@ class Satfinder(ScanSetup):
                                elif x[4] == 6:
                                        fec = "FEC_None"
                                list.append(str(x[1]) + "," + str(x[2]) + "," + pol + "," + fec)
-                       config.tuning.transponder = configElement_nonSave("config.tuning.transponder", configSelection, 0, list)
+                       self.tuning_transponder = ConfigSelection(choices = list)
+                       self.tuning_transponder.addNotifier(self.retune)
        
        def keyGo(self):
-               returnvalue = (0, 0, 0, 0, 0, 0, 0)
-               satpos = config.tuning.sat.vals[config.tuning.sat.value][1]
-               if currentConfigSelectionElement(config.tuning.type) == "manual_transponder":
-                       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)
-               elif currentConfigSelectionElement(config.tuning.type) == "predefined_transponder":
-                       transponder = nimmanager.getTransponders(config.tuning.sat.vals[config.tuning.sat.value][1])[config.tuning.transponder.value]
-                       returnvalue = (int(transponder[1] / 1000), int(transponder[2] / 1000), transponder[3], transponder[4], 2, config.tuning.sat.vals[config.tuning.sat.value][1], satpos)
-               self.close(returnvalue)
+               self.retune(self.tuning_type)
 
        def keyCancel(self):
+               if self.oldref:
+                       if self.frontend:
+                               self.frontend = None
+                               del self.raw_channel
+                       self.session.nav.playService(self.oldref)
                self.close(None)
                
        def tune(self, transponder):
@@ -291,5 +274,6 @@ def SatfinderMain(session, **kwargs):
                        else:
                                session.open(MessageBox, _("No tuner is configured for use with a diseqc positioner!"), MessageBox.TYPE_ERROR)
 
+
 def Plugins(**kwargs):
        return PluginDescriptor(name="Satfinder", description="Helps setting up your dish", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=SatfinderMain)