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