Support duo4k.
[vuplus_dvbapp] / 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" , tp.System_DVB_S2X : "DVB-S2X"}.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",
60                                                                 tp.Modulation_8APSK : "8APSK", tp.Modulation_16APSK : "16APSK",
61                                                                 tp.Modulation_32APSK : "32APSK"}.get(tp.modulation, tp.Modulation_QPSK),
62                                                         tp.frequency/1000,
63                                                         { tp.Polarisation_Horizontal : 'H', tp.Polarisation_Vertical : 'V', tp.Polarisation_CircularLeft : 'L',
64                                                                 tp.Polarisation_CircularRight : 'R' }.get(tp.polarisation, tp.Polarisation_Horizontal),
65                                                         tp.symbol_rate/1000,
66                                                         { tp.FEC_Auto : "AUTO", tp.FEC_1_2 : "1/2", tp.FEC_2_3 : "2/3",
67                                                                 tp.FEC_3_4 : "3/4", tp.FEC_5_6 : "5/6", tp.FEC_7_8 : "7/8",
68                                                                 tp.FEC_8_9 : "8/9", tp.FEC_3_5 : "3/5", tp.FEC_4_5 : "4/5",
69                                                                 tp.FEC_9_10 : "9/10", tp.FEC_None : "NONE",
70                                                                 tp.FEC_13_45 : "13/45", tp.FEC_9_20 : "9/20", tp.FEC_11_20 : "11/20", tp.FEC_23_36 : "23/36", tp.FEC_25_36 : "25/36",
71                                                                 tp.FEC_13_18 : "13/18", tp.FEC_26_45 : "26/45", tp.FEC_28_45 : "28/45", tp.FEC_7_9 : "7/9", tp.FEC_77_90 : "77/90",
72                                                                 tp.FEC_32_45 : "32/45", tp.FEC_11_15 : "11/15", tp.FEC_1_2_L : "1/2-L", tp.FEC_8_15_L : "8/15-L", tp.FEC_3_5_L : "3/5-L",
73                                                                 tp.FEC_2_3_L : "2/3-L", tp.FEC_5_9_L : "5/9-L", tp.FEC_26_45_L : "26/45-L"}.get(tp.fec, tp.FEC_Auto))
74                                                 if tp.is_id > -1 and tp.system in (tp.System_DVB_S2, tp.System_DVB_S2X):
75                                                         tp_text = ("%s IS %d") % (tp_text, tp.is_id)
76                                         elif tp_type == iDVBFrontend.feCable:
77                                                 network = _("Cable")
78                                                 tp = transponder.getDVBC()
79                                                 tp_text = ("DVB-C %s %d / %d / %s") %( { tp.Modulation_Auto : "AUTO",
80                                                         tp.Modulation_QAM16 : "QAM16", tp.Modulation_QAM32 : "QAM32",
81                                                         tp.Modulation_QAM64 : "QAM64", tp.Modulation_QAM128 : "QAM128",
82                                                         tp.Modulation_QAM256 : "QAM256" }.get(tp.modulation, tp.Modulation_Auto),
83                                                         tp.frequency,
84                                                         tp.symbol_rate/1000,
85                                                         { tp.FEC_Auto : "AUTO", tp.FEC_1_2 : "1/2", tp.FEC_2_3 : "2/3",
86                                                                 tp.FEC_3_4 : "3/4", tp.FEC_5_6 : "5/6", tp.FEC_7_8 : "7/8",
87                                                                 tp.FEC_8_9 : "8/9", tp.FEC_None : "NONE" }.get(tp.fec_inner, tp.FEC_Auto))
88                                         elif tp_type == iDVBFrontend.feTerrestrial:
89                                                 network = _("Terrestrial")
90                                                 tp = transponder.getDVBT()
91                                                 tp_text = ("%s %s %d %s") %(
92                                                         {
93                                                                 tp.System_DVB_T : "DVB-T",
94                                                                 tp.System_DVB_T2 : "DVB-T2"
95                                                         }.get(tp.system, ""),
96                                                         {
97                                                                 tp.Modulation_QPSK : "QPSK",
98                                                                 tp.Modulation_QAM16 : "QAM16",
99                                                                 tp.Modulation_QAM64 : "QAM64",
100                                                                 tp.Modulation_QAM256 : "QAM256",
101                                                                 tp.Modulation_Auto : "AUTO"
102                                                         }.get(tp.modulation, tp.Modulation_Auto),
103                                                         tp.frequency,
104                                                         {
105                                                                 tp.Bandwidth_8MHz : "Bw 8MHz",
106                                                                 tp.Bandwidth_7MHz : "Bw 7MHz",
107                                                                 tp.Bandwidth_6MHz : "Bw 6MHz",
108                                                                 tp.Bandwidth_Auto : "Bw Auto",
109                                                                 tp.Bandwidth_5MHz : "Bw 5MHz",
110                                                                 tp.Bandwidth_10MHz : "Bw 10MHz",
111                                                                 tp.Bandwidth_1_712MHz : "Bw 1.712MHz"
112                                                         }.get(tp.bandwidth, tp.Bandwidth_Auto))
113                                         else:
114                                                 print "unknown transponder type in scanStatusChanged"
115                                 self.network.setText(network)
116                                 self.transponder.setText(tp_text)
117                 
118                 if self.state == self.Done:
119                         if self.scan.getNumServices() == 0:
120                                 self.text.setText(_("scan done!") + ' ' + _("%d services found!") % 0 )
121                         else:
122                                 self.text.setText(_("scan done!") + ' ' + _("%d services found!") % (self.foundServices + self.scan.getNumServices()))
123                 
124                 if self.state == self.Error:
125                         self.text.setText(_("ERROR - failed to scan (%s)!") % (self.Errors[self.errorcode]) )
126                         
127                 if self.state == self.Done or self.state == self.Error:
128                         if self.run != len(self.scanList) - 1:
129                                 self.foundServices += self.scan.getNumServices()
130                                 self.execEnd(False) # reset eComponentScan
131                                 self.run += 1
132                                 self.execBegin()
133         
134         def __init__(self, progressbar, text, servicelist, passNumber, scanList, network, transponder, frontendInfo, lcd_summary):
135                 self.foundServices = 0
136                 self.progressbar = progressbar
137                 self.text = text
138                 self.servicelist = servicelist
139                 self.passNumber = passNumber
140                 self.scanList = scanList
141                 self.frontendInfo = frontendInfo
142                 self.transponder = transponder
143                 self.network = network
144                 self.run = 0
145                 self.lcd_summary = lcd_summary
146                 self.scan = None
147
148         def doRun(self):
149                 if self.scan is None:
150                         self.scan = eComponentScan()
151                 self.frontendInfo.frontend_source = lambda : self.scan.getFrontend()
152                 self.feid = self.scanList[self.run]["feid"]
153                 self.flags = self.scanList[self.run]["flags"]
154                 self.state = self.Idle
155                 self.scanStatusChanged()
156                 
157                 for x in self.scanList[self.run]["transponders"]:
158                         self.scan.addInitial(x)
159
160         def updatePass(self):
161                 size = len(self.scanList)
162                 if size > 1:
163                         self.passNumber.setText(_("pass") + " " + str(self.run + 1) + "/" + str(size) + " (" + _("Tuner") + " " + str(self.scanList[self.run]["feid"]) + ")")
164                 
165         def execBegin(self):
166                 self.doRun()
167                 self.updatePass()
168                 self.scan.statusChanged.get().append(self.scanStatusChanged)
169                 self.scan.newService.get().append(self.newService)
170                 self.servicelist.clear()
171                 self.state = self.Running
172                 err = self.scan.start(self.feid, self.flags)
173                 self.frontendInfo.updateFrontendData()
174                 if err:
175                         self.state = self.Error
176                         self.errorcode = 0
177                 self.scanStatusChanged()
178         
179         def execEnd(self, onClose = True):
180                 # when closing screen, destroy eComponentScan. otherwise reset.
181                 self.scan.statusChanged.get().remove(self.scanStatusChanged)
182                 self.scan.newService.get().remove(self.newService)
183                 if not self.isDone():
184                         print "*** warning *** scan was not finished!"
185
186                 if onClose:
187                         self.scan = None
188                 else:
189                         self.scan.clearAll()
190
191         def isDone(self):
192                 return self.state == self.Done or self.state == self.Error
193
194         def newService(self):
195                 newServiceName = self.scan.getLastServiceName()
196                 self.servicelist.addItem(newServiceName)
197                 self.lcd_summary.updateService(self.scan.getLastServiceName())
198
199         def destroy(self):
200                 pass