783288ec5d643c271d6a6df68c07dde49adeb6b1
[enigma2.git] / lib / python / Components / NimManager.py
1 from config import config, ConfigSubsection, ConfigSelection, ConfigFloat, ConfigSatlist, ConfigYesNo, ConfigInteger, ConfigSubList, ConfigDummy, ConfigSubDict
2
3 from enigma import eDVBSatelliteEquipmentControl, \
4         eDVBSatelliteLNBParameters as lnbParam, \
5         eDVBSatelliteDiseqcParameters as diseqcParam, \
6         eDVBSatelliteSwitchParameters as switchParam, \
7         eDVBSatelliteRotorParameters as rotorParam
8
9 import xml.dom.minidom
10 from xml.dom import EMPTY_NAMESPACE
11 from skin import elementsWithTag
12 from Tools import XMLTools
13
14 from xml.sax import make_parser
15 from xml.sax.handler import ContentHandler
16
17 from Tools.BoundFunction import boundFunction
18
19 def tryOpen(filename):
20         try:
21                 procFile = open(filename)
22         except IOError:
23                 return ""
24         return procFile
25
26 class SecConfigure:
27         def addLNBSimple(self, sec, slotid, diseqcmode, toneburstmode = 0, diseqcpos = 0, orbpos = 0, longitude = 0, latitude = 0, loDirection = 0, laDirection = 0):
28                 #simple defaults
29                 sec.addLNB()
30                 tunermask = 1 << slotid
31                 if self.equal.has_key(slotid):
32                         tunermask |= (1 << self.equal[slotid])
33                 elif self.linked.has_key(slotid):
34                         tunermask |= (1 << self.linked[slotid])
35                 sec.setLNBTunerMask(tunermask)
36                 sec.setLNBLOFL(9750000)
37                 sec.setLNBLOFH(10600000)
38                 sec.setLNBThreshold(11700000)
39                 sec.setLNBIncreasedVoltage(lnbParam.OFF)
40                 sec.setRepeats(0)
41                 sec.setFastDiSEqC(0)
42                 sec.setSeqRepeat(0)
43                 sec.setVoltageMode(switchParam.HV)
44                 sec.setToneMode(switchParam.HILO)
45                 sec.setCommandOrder(0)
46
47                 #user values
48                 sec.setDiSEqCMode(diseqcmode)
49                 sec.setToneburst(toneburstmode)
50                 sec.setCommittedCommand(diseqcpos)
51                 #print "set orbpos to:" + str(orbpos)
52
53                 if (0 <= diseqcmode < 3):
54                         sec.addSatellite(orbpos)
55                         self.satList.append(orbpos)
56                 elif (diseqcmode == 3): # diseqc 1.2
57                         if self.satposdepends.has_key(slotid):
58                                 tunermask |= (1 << self.satposdepends[slotid])
59                                 sec.setLNBTunerMask(tunermask)
60                         sec.setLatitude(latitude)
61                         sec.setLaDirection(laDirection)
62                         sec.setLongitude(longitude)
63                         sec.setLoDirection(loDirection)
64                         sec.setUseInputpower(True)
65                         sec.setInputpowerDelta(50)
66
67                         for x in self.NimManager.satList:
68                                 print "Add sat " + str(x[0])
69                                 sec.addSatellite(int(x[0]))
70                                 sec.setVoltageMode(0)
71                                 sec.setToneMode(0)
72                                 sec.setRotorPosNum(0) # USALS
73                                 self.satList.append(int(x[0]))
74
75         def setSatposDepends(self, sec, nim1, nim2):
76                 print "tuner", nim1, "depends on satpos of", nim2
77                 sec.setTunerDepends(nim1, nim2)
78
79         def linkNIMs(self, sec, nim1, nim2):
80                 print "link tuner", nim1, "to tuner", nim2
81                 sec.setTunerLinked(nim1, nim2)
82
83         def getSatList(self):
84                 return self.satList
85
86         def update(self):
87                 sec = eDVBSatelliteEquipmentControl.getInstance()
88                 sec.clear() ## this do unlinking NIMs too !!
89                 print "sec config cleared"
90                 self.satList = []
91
92                 self.linked = { }
93                 self.satposdepends = { }
94                 self.equal = { }
95                 for slot in self.NimManager.nimslots:
96                         x = slot.slotid
97                         nim = config.Nims[x]
98                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
99                                 if nim.configMode.value == "equal":
100                                         self.equal[nim.equalTo.value]=x
101                                 if nim.configMode.value == "loopthrough":
102                                         self.linkNIMs(sec, x, nim.linkedTo.value)
103                                         self.linked[nim.linkedTo.value]=x
104                                 elif nim.configMode.value == "satposdepends":
105                                         self.setSatposDepends(sec, x, nim.satposDependsTo.index)
106                                         self.satposdepends[nim.satposDependsTo.value]=x
107
108                 for slot in self.NimManager.nimslots:
109                         x = slot.slotid
110                         nim = config.Nims[x]
111                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
112                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
113                                 print "diseqcmode: ", nim.configMode.value
114                                 if nim.configMode.value in [ "loopthrough", "satposdepends", "equal", "nothing" ]:
115                                         pass
116                                 elif nim.configMode.value == "simple":          #simple config
117                                         if nim.diseqcMode.value == "single":                    #single
118                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcA.orbital_position, toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.NONE, diseqcpos = diseqcParam.SENDNO)
119                                         elif nim.diseqcMode.value == "toneburst_a_b":           #Toneburst A/B
120                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcA.orbital_position, toneburstmode = diseqcParam.A, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.SENDNO)
121                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcB.orbital_position, toneburstmode = diseqcParam.B, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.SENDNO)
122                                         elif nim.diseqcMode.value == "diseqc_a_b":              #DiSEqC A/B
123                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcA.orbital_position, toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.AA)
124                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcB.orbital_position, toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.AB)
125                                         elif nim.diseqcMode.value == "diseqc_a_b_c_d":          #DiSEqC A/B/C/D
126                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcA.orbital_position, toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.AA)
127                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcB.orbital_position, toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.AB)
128                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcC.orbital_position, toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.BA)
129                                                 self.addLNBSimple(sec, slotid = x, orbpos = nim.diseqcD.orbital_position, toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.BB)
130                                         elif nim.diseqcMode.value == "positioner":              #Positioner
131                                                 if nim.latitudeOrientation.value == "north":
132                                                         laValue = rotorParam.NORTH
133                                                 else:
134                                                         laValue = rotorParam.SOUTH
135                                                 if nim.longitudeOrientation.value == "east":
136                                                         loValue = rotorParam.EAST
137                                                 else:
138                                                         loValue = rotorParam.WEST
139                                                 self.addLNBSimple(sec, slotid = x, diseqcmode = 3,
140                                                         longitude = nim.longitude.float,
141                                                         loDirection = loValue,
142                                                         latitude = nim.latitude.float,
143                                                         laDirection = laValue)
144                                 elif nim.configMode.value == "advanced": #advanced config
145                                         self.updateAdvanced(sec, x)
146                 print "sec config completed"
147
148         def updateAdvanced(self, sec, slotid):
149                 lnbSat = {}
150                 for x in range(1,33):
151                         lnbSat[x] = []
152                 for x in self.NimManager.satList:
153                         lnb = int(config.Nims[slotid].advanced.sat[x[0]].lnb.value)
154                         if lnb != 0:
155                                 print "add", x[0], "to", lnb
156                                 lnbSat[lnb].append(x[0])
157                 for x in range(1,33):
158                         if len(lnbSat[x]) > 0:
159                                 currLnb = config.Nims[slotid].advanced.lnb[x]
160                                 sec.addLNB()
161
162                                 tunermask = 1 << slotid
163                                 if self.equal.has_key(slotid):
164                                         tunermask |= (1 << self.equal[slotid])
165                                 elif self.linked.has_key(slotid):
166                                         tunermask |= (1 << self.linked[slotid])
167
168                                 if currLnb.lof.value == "universal_lnb":
169                                         sec.setLNBLOFL(9750000)
170                                         sec.setLNBLOFH(10600000)
171                                         sec.setLNBThreshold(11700000)
172                                 elif currLnb.lof.value == "c_band":
173                                         sec.setLNBLOFL(5150000)
174                                         sec.setLNBLOFH(5150000)
175                                         sec.setLNBThreshold(5150000)
176                                 elif currLnb.lof.value == "user_defined":
177                                         sec.setLNBLOFL(currLnb.lofl.value * 1000)
178                                         sec.setLNBLOFH(currLnb.lofh.value * 1000)
179                                         sec.setLNBThreshold(currLnb.threshold.value * 1000)
180                                         
181                                 if currLnb.output_12v.value == "0V":
182                                         pass # nyi in drivers
183                                 elif currLnb.output_12v.value == "12V":
184                                         pass # nyi in drivers
185                                         
186                                 if currLnb.increased_voltage.value:
187                                         sec.setLNBIncreasedVoltage(lnbParam.ON)
188                                 else:
189                                         sec.setLNBIncreasedVoltage(lnbParam.OFF)
190                                 
191                                 dm = currLnb.diseqcMode.value 
192                                 if dm == "none":
193                                         sec.setDiSEqCMode(diseqcParam.NONE)
194                                 elif dm == "1_0":
195                                         sec.setDiSEqCMode(diseqcParam.V1_0)
196                                 elif dm == "1_1":
197                                         sec.setDiSEqCMode(diseqcParam.V1_1)
198                                 elif dm == "1_2":
199                                         sec.setDiSEqCMode(diseqcParam.V1_2)
200
201                                         if self.satposdepends.has_key(slotid):  # only useable with rotors
202                                                 tunermask |= (1 << self.satposdepends[slotid])
203
204                                 if dm != "none":
205                                         if currLnb.toneburst.value == "none":
206                                                 sec.setToneburst(diseqcParam.NO)
207                                         elif currLnb.toneburst.value == "A":
208                                                 sec.setToneburst(diseqcParam.A)
209                                         elif currLnb.toneburst.value == "B":
210                                                 sec.setToneburst(diseqcParam.B)
211                                         
212                                         # Committed Diseqc Command
213                                         cdc = currLnb.commitedDiseqcCommand.value
214                                         
215                                         c = [
216                                                 ("none", diseqcParam.SENDNO),
217                                                 ("AA", diseqcParam.AA),
218                                                 ("AB", diseqcParam.AB),
219                                                 ("BA", diseqcParam.BA),
220                                                 ("BB", diseqcParam.BB)]
221                                                 
222                                         if cdc in c:
223                                                 sec.setCommittedCommand(c[cdc])
224                                         else:
225                                                 sec.setCommittedCommand(long(cdc))
226
227                                         sec.setFastDiSEqC(currLnb.fastDiseqc.value)
228                                                 
229                                         sec.setSeqRepeat(currLnb.sequenceRepeat.value)
230                                                 
231                                         if currLnb.diseqcMode.value == "1_0":
232                                                 currCO = currLnb.commandOrder1_0.value
233                                         else:
234                                                 currCO = currLnb.commandOrder.value
235
236                                                 udc = int(currLnb.uncommittedDiseqcCommand.value)
237                                                 if udc > 0:
238                                                         sec.setUncommittedCommand(0xF0|(udc-1))
239                                                 else:
240                                                         sec.setUncommittedCommand(0) # SENDNO
241
242                                                 sec.setRepeats({"none": 0, "one": 1, "two": 2, "three": 3}[currLnb.diseqcRepeats.value])
243
244                                         setCommandOrder = False
245                                         
246                                         # 0 "committed, toneburst", 
247                                         # 1 "toneburst, committed", 
248                                         # 2 "committed, uncommitted, toneburst",
249                                         # 3 "toneburst, committed, uncommitted",
250                                         # 4 "uncommitted, committed, toneburst"
251                                         # 5 "toneburst, uncommitted, commmitted"
252                                         order_map = {"ct": 0, "tc": 1, "cut": 2, "tcu": 3, "uct": 4, "tuc": 5}
253                                         sec.setCommandOrder(order_map[currCO])
254
255                                 if dm == "1_2":
256                                         latitude = currLnb.latitude.float
257                                         sec.setLatitude(latitude)
258                                         longitude = currLnb.longitude.float
259                                         sec.setLongitude(longitude)
260                                         if currLnb.latitudeOrientation.value == "north":
261                                                 sec.setLaDirection(rotorParam.NORTH)
262                                         else:
263                                                 sec.setLaDirection(rotorParam.SOUTH)
264                                         if currLnb.longitudeOrientation.value == "east":
265                                                 sec.setLoDirection(rotorParam.EAST)
266                                         else:
267                                                 sec.setLoDirection(rotorParam.WEST)
268                                                 
269                                 if currLnb.powerMeasurement.value:
270                                         sec.setUseInputpower(True)
271                                         sec.setInputpowerDelta(currLnb.powerThreshold.value)
272                                 else:
273                                         sec.setUseInputpower(False)
274
275                                 sec.setLNBTunerMask(tunermask)
276
277                                 # finally add the orbital positions
278                                 for y in lnbSat[x]:
279                                         sec.addSatellite(y)
280                                         currSat = config.Nims[slotid].advanced.sat[y]
281
282                                         if currSat.voltage.value == "polarization":
283                                                 sec.setVoltageMode(switchParam.HV)
284                                         elif currSat.voltage.value == "13V":
285                                                 sec.setVoltageMode(switchParam._14V)
286                                         elif currSat.voltage.value == "18V":
287                                                 sec.setVoltageMode(switchParam._18V)
288                                                 
289                                         if currSat.tonemode == "band":
290                                                 sec.setToneMode(switchParam.HILO)
291                                         elif currSat.tonemode == "on":
292                                                 sec.setToneMode(switchParam.ON)
293                                         elif currSat.tonemode == "off":
294                                                 sec.setToneMode(switchParam.OFF)
295                                                 
296                                         if not currSat.usals.value:
297                                                 sec.setRotorPosNum(currSat.rotorposition.value)
298                                         else:
299                                                 sec.setRotorPosNum(0) #USALS
300
301         def __init__(self, nimmgr):
302                 self.NimManager = nimmgr
303                 self.update()
304
305 class nimSlot:
306         def __init__(self, slotid, nimtype, name):
307                 self.slotid = slotid
308                 self.nimType = nimtype
309                 self.name = name
310
311 class NimManager:
312         class parseSats(ContentHandler):
313                 def __init__(self, satList, satellites, transponders):
314                         self.isPointsElement, self.isReboundsElement = 0, 0
315                         self.satList = satList
316                         self.satellites = satellites
317                         self.transponders = transponders
318         
319                 def startElement(self, name, attrs):
320                         if (name == "sat"):
321                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
322                                 tpos = int(attrs.get('position',""))
323                                 if tpos < 0:
324                                         tpos = 3600 + tpos
325                                 tname = attrs.get('name',"").encode("UTF-8")
326                                 self.satellites[tpos] = tname
327                                 self.satList.append( (tpos, tname) )
328                                 self.parsedSat = int(tpos)
329                         elif (name == "transponder"):
330                                 modulation = int(attrs.get('modulation',"1")) # QPSK default
331                                 system = int(attrs.get('system',"0")) # DVB-S default
332                                 freq = int(attrs.get('frequency',""))
333                                 sr = int(attrs.get('symbol_rate',""))
334                                 pol = int(attrs.get('polarization',""))
335                                 fec = int(attrs.get('fec_inner',"0")) # AUTO default
336                                 if self.parsedSat in self.transponders:
337                                         pass
338                                 else:
339                                         self.transponders[self.parsedSat] = [ ]
340
341                                 self.transponders[self.parsedSat].append((0, freq, sr, pol, fec, system, modulation))
342
343         class parseCables(ContentHandler):
344                 def __init__(self, cablesList, transponders):
345                         self.isPointsElement, self.isReboundsElement = 0, 0
346                         self.cablesList = cablesList
347                         self.transponders = transponders
348         
349                 def startElement(self, name, attrs):
350                         if (name == "cable"):
351                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
352                                 tname = attrs.get('name',"").encode("UTF-8")
353                                 self.cablesList.append(str(tname))
354                                 self.parsedCab = str(tname)
355                         elif (name == "transponder"):
356                                 freq = int(attrs.get('frequency',""))
357                                 sr = int(attrs.get('symbol_rate',""))
358                                 mod = int(attrs.get('modulation',"3")) # QAM64 default
359                                 fec = int(attrs.get('fec_inner',"0")) # AUTO default
360                                 if self.parsedCab in self.transponders:
361                                         pass
362                                 else:
363                                         self.transponders[self.parsedCab] = [ ]
364                                 self.transponders[self.parsedCab].append((1, freq, sr, mod, fec))
365
366         class parseTerrestrials(ContentHandler):
367                 def __init__(self, terrestrialsList, transponders):
368                         self.isPointsElement, self.isReboundsElement = 0, 0
369                         self.terrestrialsList = terrestrialsList
370                         self.transponders = transponders
371         
372                 def startElement(self, name, attrs):
373                         if (name == "terrestrial"):
374                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
375                                 tname = attrs.get('name',"").encode("UTF-8")
376                                 tflags = attrs.get('flags',"")
377                                 self.terrestrialsList.append((tname, tflags))
378                                 self.parsedTer = str(tname)
379                         elif (name == "transponder"):
380                                 # TODO finish this!
381                                 freq = int(attrs.get('centre_frequency',""))
382                                 bw = int(attrs.get('bandwidth',"3")) # AUTO
383                                 const = int(attrs.get('constellation',"1")) # AUTO
384                                 crh = int(attrs.get('code_rate_hp',"5")) # AUTO
385                                 if crh > 5: # our terrestrial.xml is buggy... 6 for AUTO
386                                         crh = 5
387                                 crl = int(attrs.get('code_rate_lp',"5")) # AUTO
388                                 if crl > 5: # our terrestrial.xml is buggy... 6 for AUTO
389                                         crl = 5
390                                 guard = int(attrs.get('guard_interval',"4")) # AUTO
391                                 transm = int(attrs.get('transmission_mode',"2")) # AUTO
392                                 hierarchy = int(attrs.get('hierarchy_information',"4")) # AUTO
393                                 inv = int(attrs.get('inversion',"2")) # AUTO
394                                 if self.parsedTer in self.transponders:
395                                         pass
396                                 else:
397                                         self.transponders[self.parsedTer] = [ ]
398
399                                 self.transponders[self.parsedTer].append((2, freq, bw, const, crh, crl, guard, transm, hierarchy, inv))
400
401         def getTransponders(self, pos):
402                 if self.transponders.has_key(pos):
403                         return self.transponders[pos]
404                 else:
405                         return []
406
407         def getTranspondersCable(self, cable):
408                 return self.transponderscable[cable]
409
410         def getTranspondersTerrestrial(self, region):
411                 return self.transpondersterrestrial[region]
412         
413         def getCableDescription(self, nim):
414                 return self.cablesList[config.Nims[nim].cable.value]
415
416         def getTerrestrialDescription(self, nim):
417                 return self.terrestrialsList[config.Nims[nim].terrestrial.value][0]
418
419         def getTerrestrialFlags(self, nim):
420                 return self.terrestrialsList[config.Nims[nim].terrestrial.value][1]
421
422         def getConfiguredSats(self):
423                 return self.sec.getSatList()
424
425         def getSatDescription(self, pos):
426                 return self.satellites[pos]
427
428         def readSatsfromFile(self):
429                 self.satellites = { }
430                 self.transponders = { }
431                 self.transponderscable = { }
432                 self.transpondersterrestrial = { }              
433                 
434                 parser = make_parser()
435                 if (self.hasNimType(self.nimType["DVB-S"])):
436                         print "Reading satellites.xml"
437                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
438                         parser.setContentHandler(satHandler)
439                         parser.parse('/etc/tuxbox/satellites.xml')
440                 if (self.hasNimType(self.nimType["DVB-C"])):
441                         print "Reading cables.xml"
442                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
443                         parser.setContentHandler(cabHandler)
444                         parser.parse('/etc/tuxbox/cables.xml')
445
446                 if (self.hasNimType(self.nimType["DVB-T"])):
447                         print "Reading terrestrial.xml"
448                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
449                         parser.setContentHandler(terHandler)
450                         parser.parse('/etc/tuxbox/terrestrial.xml')
451                 
452         def parseProc(self):
453                 self.nimTypes = {}
454                 self.nimNames = {}              
455                 self.nimSocketCount = 0
456                 nimfile = tryOpen("/proc/bus/nim_sockets")
457
458                 if nimfile == "":
459                                 return self.nimType["empty/unknown"]
460                         
461                 lastsocket = -1
462
463                 while 1:                
464                         line = nimfile.readline()
465                         if line == "":
466                                 break
467                         if line.strip().startswith("NIM Socket"):
468                                 parts = line.strip().split(" ")
469                                 id = int(parts[2][:1])
470                                 lastsocket = int(id)
471                                 self.nimSocketCount += 1
472                         elif line.strip().startswith("Type:"):
473                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
474                                 #self.nimTypes[lastsocket] = str("DVB-T")
475                         elif line.strip().startswith("Name:"):
476                                 self.nimNames[lastsocket] = str(line.strip()[6:])
477                         elif line.strip().startswith("empty"):
478                                 self.nimNames[lastsocket] = _("N/A")
479                                 self.nimTypes[lastsocket] = "empty/unknown"
480
481                 nimfile.close()
482
483         def getNimType(self, slotID):
484                 if slotID >= self.nimCount:
485                         return self.nimType["empty/unknown"]
486                 else:   
487                         return self.nimType[self.nimTypes[slotID]]
488                         
489         def getNimTypeName(self, slotID):
490                 if slotID >= self.nimCount:
491                         return "empty/unknown"
492                 else:   
493                         return self.nimTypes[slotID]
494                 
495         def getNimName(self, slotID):
496                 return self.nimNames[slotID]
497
498         def getNimSocketCount(self):
499                 return self.nimSocketCount
500         
501         def hasNimType(self, chktype):
502                 for id, type in self.nimTypes.items():
503                         if (chktype == self.nimType[str(type)]):
504                                 return True
505                 return False
506         
507         def getNimListOfType(self, type, exception = -1):
508                 list = []
509                 for x in self.nimslots:
510                         if ((x.nimType == type) and (x.slotid != exception)):
511                                 list.append(x.slotid)
512                 return list
513
514         def getConfigPrefix(self, slotid):
515                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
516                         
517         def __init__(self):
518                 #use as enum
519                 self.nimType = {                "empty/unknown": -1,
520                                                                                                 "DVB-S": 0,
521                                                                                                 "DVB-C": 1,
522                                                                                                 "DVB-T": 2}
523                 self.satList = [ ]
524                 self.cablesList = []
525                 self.terrestrialsList = []
526                                                                                                 
527                 self.parseProc()
528
529                 self.readSatsfromFile()                                                 
530                 
531                 self.nimCount = self.getNimSocketCount()
532                 
533                 self.nimslots = [ ]
534                 x = 0
535                 while x < self.nimCount:
536                         tType = self.getNimType(x)
537                         tName = self.getNimName(x)
538                         tNim = nimSlot(x, tType, tName)
539                         self.nimslots.append(tNim)
540                         x += 1
541                 
542                 InitNimManager(self)    #init config stuff
543
544         def nimList(self):
545                 list = [ ]
546                 for slot in self.nimslots:
547                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
548                         if slot.nimType == -1:
549                                 nimText += _("empty/unknown")
550                         else:
551                                 nimText += slot.name + " ("     
552                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
553                         list.append((nimText, slot))
554                 return list
555
556         def getNimConfigMode(self, slotid):
557                 return config.Nims[slotid].configMode.value
558         
559         def getSatList(self):
560                 return self.satList
561
562         def getSatListForNim(self, slotid):
563                 list = []
564                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
565                         #print "slotid:", slotid
566                         
567                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
568                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
569                         configMode = config.Nims[slotid].configMode.value
570
571                         if configMode == "equal":
572                                 slotid=0 #FIXME add handling for more than two tuners !!!
573                                 configMode = config.Nims[slotid].configMode.value
574
575                         if configMode == "simple":
576                                 dm = config.Nims[slotid].diseqcMode.value
577                                 if dm in ["single", "toneburst", "diseqc_a_b", "diseqc_a_b_c_d"]:
578                                         list.append(self.satList[config.Nims[slotid].diseqcA.index])
579                                 if dm in ["toneburst", "diseqc_a_b", "diseqc_a_b_c_d"]:
580                                         list.append(self.satList[config.Nims[slotid].diseqcB.index])
581                                 if dm == "diseqc_a_b_c_d":
582                                         list.append(self.satList[config.Nims[slotid].diseqcC.index])
583                                         list.append(self.satList[config.Nims[slotid].diseqcD.index])
584                                 if dm == "positioner":
585                                         for x in self.satList:
586                                                 list.append(x)
587                         elif configMode == "advanced":
588                                 for x in self.satList:
589                                         if int(config.Nims[slotid].advanced.sat[x[0]].lnb.value) != 0:
590                                                 list.append(x)
591
592                 return list
593
594         def getRotorSatListForNim(self, slotid):
595                 list = []
596                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
597                         #print "slotid:", slotid
598
599                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
600                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
601                         configMode = config.Nims[slotid].configMode.value
602                         if configMode == "simple":
603                                 if config.Nims[slotid].diseqcMode.value == "positioner":
604                                         for x in self.satList:
605                                                 list.append(x)
606                         elif configMode == "advanced":
607                                 for x in self.satList:
608                                         nim = config.Nims[slotid]
609                                         lnbnum = int(nim.advanced.sat[x[0]].lnb.value)
610                                         if lnbnum != 0:
611                                                 lnb = nim.advanced.lnb[lnbnum]
612                                                 if lnb.diseqcMode.value == "diseqc_a_b_c_d":
613                                                         list.append(x)
614                 return list
615
616         def nimDiseqcModeChanged(self, slotid, mode):
617                 #print "nimDiseqcModeChanged set to " + str(mode)
618                 pass
619         def nimPortAChanged(self, slotid, val):
620                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
621                 pass
622         def nimPortBChanged(self, slotid, val):
623                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
624                 #print "nimDiseqcB set to " + str(val)
625                 pass
626         def nimPortCChanged(self, slotid, val):
627                 #print "nimDiseqcC set to " + str(val)
628                 pass
629         def nimPortDChanged(self, slotid, val):
630                 #print "nimDiseqcD set to " + str(val)
631                 pass
632
633 def InitNimManager(nimmgr):
634         config.Nims = ConfigSubList()
635         for x in range(nimmgr.nimCount):
636                 config.Nims.append(ConfigSubsection())
637                 
638 #       def nimConfigModeChanged(slotid, configElement):
639 #               nimmgr.nimConfigModeChanged(slotid, configElement.value)
640         def nimDiseqcModeChanged(slotid, configElement):
641                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
642                 
643         def nimPortAChanged(slotid, configElement):
644                 nimmgr.nimPortAChanged(slotid, configElement.value)
645         def nimPortBChanged(slotid, configElement):
646                 nimmgr.nimPortBChanged(slotid, configElement.value)
647         def nimPortCChanged(slotid, configElement):
648                 nimmgr.nimPortCChanged(slotid, configElement.value)
649         def nimPortDChanged(slotid, configElement):
650                 nimmgr.nimPortDChanged(slotid, configElement.value)
651
652         for slot in nimmgr.nimslots:
653                 x = slot.slotid
654                 cname = nimmgr.getConfigPrefix(x)
655                 nim = config.Nims[x]
656                 
657                 if slot.nimType == nimmgr.nimType["DVB-S"]:
658                         if slot.slotid == 0:
659                                 nim.configMode = ConfigSelection(
660                                         choices = {
661                                                 "simple": _("simple"),
662                                                 "advanced": _("advanced")},
663                                         default = "simple")
664                         else:
665
666                                 nim.configMode = ConfigSelection(
667                                         choices = {
668                                                 "equal": _("equal to Socket A"),
669                                                 "looptrough": _("loopthrough to socket A"),
670                                                 "nothing": _("nothing connected"),
671                                                 "satposdepends": _("second cable of motorized LNB"),
672                                                 "simple": _("simple"),
673                                                 "advanced": _("advanced")},
674                                         default = "looptrough")
675
676                         #important - check if just the 2nd one is LT only and the first one is DVB-S
677                         if nim.configMode.value in ["loopthrough", "satposdepends", "equal"]:
678                                 if x == 0: # first one can never be linked to anything
679                                         # reset to simple
680                                         nim.configMode.value = "simple"
681                                         nim.configMode.save()
682                                 else:
683                                         #FIXME: make it better
684                                         for y in nimmgr.nimslots:
685                                                 if y.slotid == 0:
686                                                         if y.nimType != nimmgr.nimType["DVB-S"]:
687                                                                 # reset to simple
688                                                                 nim.configMode.value = "simple"
689                                                                 nim.configMode.save()
690
691                         nim.diseqcMode = ConfigSelection(
692                                 choices = [
693                                         ("single", _("Single")),
694                                         ("toneburst_a_b", _("Toneburst A/B")),
695                                         ("diseqc_a_b", _("DiSEqC A/B")),
696                                         ("diseqc_a_b_c_d", _("DiSEqC A/B/C/D")),
697                                         ("positioner", _("Positioner"))],
698                                 default = "diseqc_a_b")
699                         nim.diseqcA = ConfigSatlist(default = 192, list = nimmgr.satList)
700                         nim.diseqcB = ConfigSatlist(default = 130, list = nimmgr.satList)
701                         nim.diseqcC = ConfigSatlist(list = nimmgr.satList)
702                         nim.diseqcD = ConfigSatlist(list = nimmgr.satList)
703                         nim.positionerMode = ConfigSelection(
704                                 choices = [
705                                         ("usals", _("USALS")),
706                                         ("manual", _("manual"))],
707                                 default = "usals")
708                         nim.longitude = ConfigFloat(default=[5,100], limits=[(0,359),(0,999)])
709                         nim.longitudeOrientation = ConfigSelection(choices={"east": _("East"), "west": _("West")}, default = "east")
710                         nim.latitude = ConfigFloat(default=[50,767], limits=[(0,359),(0,999)])
711                         nim.latitudeOrientation = ConfigSelection(choices={"north": _("North"), "south": _("South")}, default="north")
712                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
713                         satNimListNames = {}
714                         for x in satNimList:
715                                 satNimListNames["Slot_" + ("A", "B", "C", "D")[x] + "_" + nimmgr.getNimName(x)] = _("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x)
716                         if len(satNimListNames):
717                                 nim.equalTo = ConfigSelection(choices = satNimListNames)
718                                 nim.linkedTo = ConfigSelection(choices = satNimListNames)
719                                 nim.satposDependsTo = ConfigSelection(choices = satNimListNames)
720                         
721                         #perhaps the instance of the slot is more useful?
722 #                       nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
723                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
724                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
725                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
726                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
727                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
728                         
729                         # advanced config:
730                         nim.advanced = ConfigSubsection()
731                         nim.advanced.sats = ConfigSatlist(default = 192, list = nimmgr.satList)
732                         nim.advanced.sat = ConfigSubDict()
733                         lnbs = [("0", "not available")]
734                         for y in range(1, 33):
735                                 lnbs.append((str(y), "LNB " + str(y)))
736
737                         for x in nimmgr.satList:
738                                 nim.advanced.sat[x[0]] = ConfigSubsection()
739                                 nim.advanced.sat[x[0]].voltage = ConfigSelection(choices={"polarization": _("Polarization"), "13V": _("13 V"), "18V": _("18 V")}, default = "polarization")
740                                 nim.advanced.sat[x[0]].tonemode = ConfigSelection(choices={"band": _("Band"), "on": _("On"), "off": _("Off")}, default = "band")
741                                 nim.advanced.sat[x[0]].usals = ConfigYesNo(default=True)
742                                 nim.advanced.sat[x[0]].rotorposition = ConfigInteger(default=1, limits=(1, 255))
743                                 nim.advanced.sat[x[0]].lnb = ConfigSelection(choices = lnbs)
744
745                         csw = [("none", _("None")), ("AA", _("AA")), ("AB", _("AB")), ("BA", _("BA")), ("BB", _("BB"))]
746                         for y in range(0, 16):
747                                 csw.append((str(0xF0|y), "Input " + str(y+1)))
748
749                         ucsw = [("0", _("None"))]
750                         for y in range(1, 17):
751                                 ucsw.append((str(y), "Input " + str(y)))
752
753                         nim.advanced.lnb = ConfigSubList()
754                         nim.advanced.lnb.append(ConfigDummy())
755                         for x in range(1, 33):
756                                 nim.advanced.lnb.append(ConfigSubsection())
757                                 nim.advanced.lnb[x].lof = ConfigSelection(choices={"universal_lnb": _("Universal LNB"), "c_band": _("C-Band"), "user_defined": _("User defined")}, default="universal_lnb")
758                                 nim.advanced.lnb[x].lofl = ConfigInteger(default=9750, limits = (0, 99999))
759                                 nim.advanced.lnb[x].lofh = ConfigInteger(default=10600, limits = (0, 99999))
760                                 nim.advanced.lnb[x].threshold = ConfigInteger(default=11700, limits = (0, 99999))
761                                 nim.advanced.lnb[x].output_12v = ConfigSelection(choices = [("0V", _("0 V")), ("12V", _("12 V"))], default="0V")
762                                 nim.advanced.lnb[x].increased_voltage = ConfigYesNo(default=False)
763                                 nim.advanced.lnb[x].toneburst = ConfigSelection(choices = [("none", _("None")), ("A", _("A")), ("B", _("B"))], default = "none")
764                                 nim.advanced.lnb[x].diseqcMode = ConfigSelection(choices = [("none", _("None")), ("1_0", _("1.0")), ("1_1", _("1.1")), ("1_2", _("1.2"))], default = "none")
765                                 nim.advanced.lnb[x].commitedDiseqcCommand = ConfigSelection(choices = csw)
766                                 nim.advanced.lnb[x].fastDiseqc = ConfigYesNo(default=False)
767                                 nim.advanced.lnb[x].sequenceRepeat = ConfigYesNo(default=False)
768                                 nim.advanced.lnb[x].commandOrder1_0 = ConfigSelection(choices = [("ct", "committed, toneburst"), ("tc", "toneburst, committed")], default = "ct")
769                                 nim.advanced.lnb[x].commandOrder = ConfigSelection(choices = [
770                                                 ("ct", "committed, toneburst"), 
771                                                 ("tc", "toneburst, committed"), 
772                                                 ("cut", "committed, uncommitted, toneburst"), 
773                                                 ("tcu", "toneburst, committed, uncommitted"), 
774                                                 ("uct", "uncommitted, committed, toneburst"), 
775                                                 ("tuc", "toneburst, uncommitted, commmitted")],
776                                                 default="ct")
777                                 nim.advanced.lnb[x].uncommittedDiseqcCommand = ConfigSelection(choices = ucsw)
778                                 nim.advanced.lnb[x].diseqcRepeats = ConfigSelection(choices = [("none", _("None")), ("one", _("One")), ("two", _("Two")), ("three", _("Three"))], default = "none")
779                                 nim.advanced.lnb[x].longitude = ConfigFloat(default = [5,100], limits = [(0,359),(0,999)])
780                                 nim.advanced.lnb[x].longitudeOrientation = ConfigSelection(choices = [("east", _("East")), ("west", _("West"))], default = "east")
781                                 nim.advanced.lnb[x].latitude = ConfigFloat(default = [50,767], limits = [(0,359),(0,999)])
782                                 nim.advanced.lnb[x].latitudeOrientation = ConfigSelection(choices = [("north", _("North")), ("south", _("South"))], default = "north")
783                                 nim.advanced.lnb[x].powerMeasurement = ConfigYesNo(default=True)
784                                 nim.advanced.lnb[x].powerThreshold = ConfigInteger(default=50, limits=(0, 100))
785
786                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
787                         nim.cable = ConfigSelection(choices = nimmgr.cablesList)
788                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
789                         list = []
790                         for x in nimmgr.terrestrialsList:
791                                 list.append(x[0])
792                         nim.terrestrial = ConfigSelection(choices = list)
793                         nim.terrestrial_5V = ConfigOnOff()
794                 else:
795                         print "pls add support for this frontend type!"         
796 #                       assert False
797
798         nimmgr.sec = SecConfigure(nimmgr)
799
800
801 nimmanager = NimManager()