add support for /proc/stb/frontend/X/lnb_sense
[enigma2.git] / lib / python / Components / ServiceScan.py
1 from enigma import eComponentScan, iDVBFrontend
2 from Components.NimManager import nimmanager as nimmgr
3
4 class ServiceScan:
5         
6         Idle = 1
7         Running = 2
8         Done = 3
9         Error = 4
10         
11         Errors = { 
12                 0: "error starting scanning",
13                 1: "error while scanning",
14                 2: "no resource manager",
15                 3: "no channel list"
16                 }
17         
18         def scanStatusChanged(self):
19                 if self.state == self.Running:
20                         self.progressbar.setValue(self.scan.getProgress())
21                         self.lcd_summary.updateProgress(self.scan.getProgress())
22                         if self.scan.isDone():
23                                 errcode = self.scan.getError()
24                                 
25                                 if errcode == 0:
26                                         self.state = self.Done
27                                 else:
28                                         self.state = self.Error
29                                         self.errorcode = errcode
30                                 self.network.setText("")
31                                 self.transponder.setText("")
32                         else:
33                                 self.text.setText(_("scan in progress - %d%% done!") % self.scan.getProgress() + ' ' + _("%d services found!") % (self.foundServices + self.scan.getNumServices()))
34                                 transponder = self.scan.getCurrentTransponder()
35                                 network = ""
36                                 tp_text = ""
37                                 if transponder:
38                                         tp_type = transponder.getSystem()
39                                         if tp_type == iDVBFrontend.feSatellite:
40                                                 network = _("Satellite")
41                                                 tp = transponder.getDVBS()
42                                                 orb_pos = tp.orbital_position
43                                                 try:
44                                                         sat_name = str(nimmgr.getSatDescription(orb_pos))
45                                                 except KeyError:
46                                                         sat_name = ""
47                                                 if orb_pos > 1800: # west
48                                                         orb_pos = 3600 - orb_pos
49                                                         h = _("W")
50                                                 else:
51                                                         h = _("E")
52                                                 if sat_name.find("%d.%d" % (orb_pos/10, orb_pos%10)) != -1:
53                                                         network = sat_name
54                                                 else:
55                                                         network = ("%s %d.%d %s") % (sat_name, orb_pos / 10, orb_pos % 10, h)
56                                                 tp_text = ("%s %s %d%c / %d / %s") %( { tp.System_DVB_S : "DVB-S",
57                                                         tp.System_DVB_S2 : "DVB-S2" }.get(tp.system, tp.System_DVB_S),
58                                                         { tp.Modulation_Auto : "Auto", tp.Modulation_QPSK : "QPSK",
59                                                                 tp.Modulation_8PSK : "8PSK", tp.Modulation_QAM16 : "QAM16" }.get(tp.modulation, tp.Modulation_QPSK),
60                                                         tp.frequency/1000,
61                                                         { tp.Polarisation_Horizontal : 'H', tp.Polarisation_Vertical : 'V', tp.Polarisation_CircularLeft : 'L',
62                                                                 tp.Polarisation_CircularRight : 'R' }.get(tp.polarisation, tp.Polarisation_Horizontal),
63                                                         tp.symbol_rate/1000,
64                                                         { tp.FEC_Auto : "AUTO", tp.FEC_1_2 : "1/2", tp.FEC_2_3 : "2/3",
65                                                                 tp.FEC_3_4 : "3/4", tp.FEC_5_6 : "5/6", tp.FEC_7_8 : "7/8",
66                                                                 tp.FEC_8_9 : "8/9", tp.FEC_3_5 : "3/5", tp.FEC_4_5 : "4/5",
67                                                                 tp.FEC_9_10 : "9/10", tp.FEC_None : "NONE" }.get(tp.fec, tp.FEC_Auto))
68                                         elif tp_type == iDVBFrontend.feCable:
69                                                 network = _("Cable")
70                                                 tp = transponder.getDVBC()
71                                                 tp_text = ("DVB-C %s %d / %d / %s") %( { tp.Modulation_Auto : "AUTO",
72                                                         tp.Modulation_QAM16 : "QAM16", tp.Modulation_QAM32 : "QAM32",
73                                                         tp.Modulation_QAM64 : "QAM64", tp.Modulation_QAM128 : "QAM128",
74                                                         tp.Modulation_QAM256 : "QAM256" }.get(tp.modulation, tp.Modulation_Auto),
75                                                         tp.frequency,
76                                                         tp.symbol_rate/1000,
77                                                         { tp.FEC_Auto : "AUTO", tp.FEC_1_2 : "1/2", tp.FEC_2_3 : "2/3",
78                                                                 tp.FEC_3_4 : "3/4", tp.FEC_5_6 : "5/6", tp.FEC_7_8 : "7/8",
79                                                                 tp.FEC_8_9 : "8/9", tp.FEC_None : "NONE" }.get(tp.fec_inner, tp.FEC_Auto))
80                                         elif tp_type == iDVBFrontend.feTerrestrial:
81                                                 network = _("Terrestrial")
82                                                 tp = transponder.getDVBT()
83                                                 tp_text = ("DVB-T %s %d %s") %( { tp.Modulation_QPSK : "QPSK",
84                                                         tp.Modulation_QAM16 : "QAM16", tp.Modulation_QAM64 : "QAM64",
85                                                         tp.Modulation_Auto : "AUTO" }.get(tp.modulation, tp.Modulation_Auto),
86                                                         tp.frequency,
87                                                         { tp.Bandwidth_8MHz : "Bw 8MHz", tp.Bandwidth_7MHz : "Bw 7MHz", tp.Bandwidth_6MHz : "Bw 6MHz",
88                                                                 tp.Bandwidth_Auto : "Bw Auto" }.get(tp.bandwidth, tp.Bandwidth_Auto))
89                                         else:
90                                                 print "unknown transponder type in scanStatusChanged"
91                                 self.network.setText(network)
92                                 self.transponder.setText(tp_text)
93                 
94                 if self.state == self.Done:
95                         if self.scan.getNumServices() == 0:
96                                 self.text.setText(_("scan done!") + ' ' + _("%d services found!") % 0 )
97                         else:
98                                 self.text.setText(_("scan done!") + ' ' + _("%d services found!") % (self.foundServices + self.scan.getNumServices()))
99                 
100                 if self.state == self.Error:
101                         self.text.setText(_("ERROR - failed to scan (%s)!") % (self.Errors[self.errorcode]) )
102                         
103                 if self.state == self.Done or self.state == self.Error:
104                         if self.run != len(self.scanList) - 1:
105                                 self.foundServices += self.scan.getNumServices()
106                                 self.execEnd()
107                                 self.run += 1
108                                 self.execBegin()
109         
110         def __init__(self, progressbar, text, servicelist, passNumber, scanList, network, transponder, frontendInfo, lcd_summary):
111                 self.foundServices = 0
112                 self.progressbar = progressbar
113                 self.text = text
114                 self.servicelist = servicelist
115                 self.passNumber = passNumber
116                 self.scanList = scanList
117                 self.frontendInfo = frontendInfo
118                 self.transponder = transponder
119                 self.network = network
120                 self.run = 0
121                 self.lcd_summary = lcd_summary
122
123         def doRun(self):
124                 self.scan = eComponentScan()
125                 self.frontendInfo.frontend_source = lambda : self.scan.getFrontend()
126                 self.feid = self.scanList[self.run]["feid"]
127                 self.flags = self.scanList[self.run]["flags"]
128                 self.state = self.Idle
129                 self.scanStatusChanged()
130                 
131                 for x in self.scanList[self.run]["transponders"]:
132                         self.scan.addInitial(x)
133
134         def updatePass(self):
135                 size = len(self.scanList)
136                 if size > 1:
137                         self.passNumber.setText(_("pass") + " " + str(self.run + 1) + "/" + str(size) + " (" + _("Tuner") + " " + str(self.scanList[self.run]["feid"]) + ")")
138                 
139         def execBegin(self):
140                 self.doRun()
141                 self.updatePass()
142                 self.scan.statusChanged.get().append(self.scanStatusChanged)
143                 self.scan.newService.get().append(self.newService)
144                 self.servicelist.clear()
145                 self.state = self.Running
146                 err = self.scan.start(self.feid, self.flags)
147                 self.frontendInfo.updateFrontendData()
148                 if err:
149                         self.state = self.Error
150                         self.errorcode = 0
151                 self.scanStatusChanged()
152         
153         def execEnd(self):
154                 self.scan.statusChanged.get().remove(self.scanStatusChanged)
155                 self.scan.newService.get().remove(self.newService)
156                 if not self.isDone():
157                         print "*** warning *** scan was not finished!"
158                 
159                 del self.scan
160
161         def isDone(self):
162                 return self.state == self.Done or self.state == self.Error
163
164         def newService(self):
165                 newServiceName = self.scan.getLastServiceName()
166                 self.servicelist.addItem(newServiceName)
167                 self.lcd_summary.updateService(self.scan.getLastServiceName())
168
169         def destroy(self):
170                 pass