revert non working code
[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
385                                 self.transponders[self.parsedCab].append((1, freq, sr, mod, fec))
386
387         class parseTerrestrials(ContentHandler):
388                 def __init__(self, terrestrialsList, transponders):
389                         self.isPointsElement, self.isReboundsElement = 0, 0
390                         self.terrestrialsList = terrestrialsList
391                         self.transponders = transponders
392         
393                 def startElement(self, name, attrs):
394                         if (name == "terrestrial"):
395                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
396                                 tname = attrs.get('name',"")
397                                 tflags = attrs.get('flags',"")
398                                 self.terrestrialsList.append((tname, tflags))
399                                 self.parsedTer = str(tname)
400                         elif (name == "transponder"):
401                                 # TODO finish this!
402                                 freq = int(attrs.get('centre_frequency',""))
403                                 bw = int(attrs.get('bandwidth',""))
404                                 const = int(attrs.get('constellation',""))
405                                 crh = int(attrs.get('code_rate_hp',""))
406                                 crl = int(attrs.get('code_rate_lp',""))
407                                 guard = int(attrs.get('guard_interval',""))
408                                 transm = int(attrs.get('transmission_mode',""))
409                                 hierarchy = int(attrs.get('hierarchy_information',""))
410                                 inv = int(attrs.get('inversion',""))
411                                 if self.parsedTer in self.transponders:
412                                         pass
413                                 else:
414                                         self.transponders[self.parsedTer] = [ ]
415
416                                 self.transponders[self.parsedTer].append((2, freq, bw, const, crh, crl, guard, transm, hierarchy, inv))
417
418         def getTransponders(self, pos):
419                 return self.transponders[pos]
420
421         def getConfiguredSats(self):
422                 return self.sec.getSatList()
423
424         def getSatDescription(self, pos):
425                 return self.satellites[pos]
426
427         def readSatsfromFile(self):
428                 self.satellites = { }
429                 self.transponders = { }
430                 self.transponderscable = { }
431                 self.transpondersterrestrial = { }              
432                 
433                 parser = make_parser()
434                 if (self.hasNimType(self.nimType["DVB-S"])):
435                         print "Reading satellites.xml"
436                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
437                         parser.setContentHandler(satHandler)
438                         parser.parse('/etc/tuxbox/satellites.xml')
439                 if (self.hasNimType(self.nimType["DVB-C"])):
440                         print "Reading cables.xml"
441                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
442                         parser.setContentHandler(cabHandler)
443                         parser.parse('/etc/tuxbox/cables.xml')
444
445                 if (self.hasNimType(self.nimType["DVB-T"])):
446                         print "Reading terrestrial.xml"
447                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
448                         parser.setContentHandler(terHandler)
449                         parser.parse('/etc/tuxbox/terrestrial.xml')
450                 
451         def parseProc(self):
452                 self.nimTypes = {}
453                 self.nimNames = {}              
454                 self.nimSocketCount = 0
455                 nimfile = tryOpen("/proc/bus/nim_sockets")
456
457                 if nimfile == "":
458                                 return self.nimType["empty/unknown"]
459                         
460                 lastsocket = -1
461
462                 while 1:                
463                         line = nimfile.readline()
464                         if line == "":
465                                 break
466                         if line.strip().startswith("NIM Socket"):
467                                 parts = line.strip().split(" ")
468                                 id = int(parts[2][:1])
469                                 lastsocket = int(id)
470                                 self.nimSocketCount += 1
471                         elif line.strip().startswith("Type:"):
472                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
473                         elif line.strip().startswith("Name:"):
474                                 self.nimNames[lastsocket] = str(line.strip()[6:])
475                         elif line.strip().startswith("empty"):
476                                 self.nimNames[lastsocket] = _("N/A")
477                                 self.nimTypes[lastsocket] = "empty/unknown"
478
479                 nimfile.close()
480
481         def getNimType(self, slotID):
482                 if slotID >= self.nimCount:
483                         return "empty/unknown"
484                 else:   
485                         return self.nimType[self.nimTypes[slotID]]
486                         
487         def getNimName(self, slotID):
488                 return self.nimNames[slotID]
489
490         def getNimSocketCount(self):
491                 return self.nimSocketCount
492         
493         def hasNimType(self, chktype):
494                 for id, type in self.nimTypes.items():
495                         if (chktype == self.nimType[str(type)]):
496                                 return True
497                 return False
498         
499         def getNimListOfType(self, type, exception = -1):
500                 list = []
501                 for x in self.nimslots:
502                         if ((x.nimType == type) and (x.slotid != exception)):
503                                 list.append(x.slotid)
504                 return list
505
506         def getConfigPrefix(self, slotid):
507                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
508                         
509         def __init__(self):
510                 #use as enum
511                 self.nimType = {                "empty/unknown": -1,
512                                                                                                 "DVB-S": 0,
513                                                                                                 "DVB-C": 1,
514                                                                                                 "DVB-T": 2}
515                 self.satList = [ ]
516                 self.cablesList = []
517                 self.terrestrialsList = []
518                                                                                                 
519                 self.parseProc()
520
521                 self.readSatsfromFile()                                                 
522                 
523                 self.nimCount = self.getNimSocketCount()
524                 
525                 self.nimslots = [ ]
526                 x = 0
527                 while x < self.nimCount:
528                         tType = self.getNimType(x)
529                         tName = self.getNimName(x)
530                         tNim = nimSlot(x, tType, tName)
531                         self.nimslots.append(tNim)
532                         x += 1
533                 
534                 InitNimManager(self)    #init config stuff
535
536         def nimList(self):
537                 list = [ ]
538                 for slot in self.nimslots:
539                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
540                         if slot.nimType == -1:
541                                 nimText += _("empty/unknown")
542                         else:
543                                 nimText += slot.name + " ("     
544                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
545                         list.append((nimText, slot))
546                 return list
547         
548         def getSatListForNim(self, slotid):
549                 list = []
550                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
551                         #print "slotid:", slotid
552                         
553                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
554                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
555                         configMode = currentConfigSelectionElement(config.Nims[slotid].configMode)
556                         if configMode == "simple":
557                                 if (config.Nims[slotid].diseqcMode.value <= 3):
558                                         list.append(self.satList[config.Nims[slotid].diseqcA.value])
559                                 if (0 < config.Nims[slotid].diseqcMode.value <= 3):
560                                         list.append(self.satList[config.Nims[slotid].diseqcB.value])
561                                 if (config.Nims[slotid].diseqcMode.value == 3):
562                                         list.append(self.satList[config.Nims[slotid].diseqcC.value])
563                                         list.append(self.satList[config.Nims[slotid].diseqcD.value])
564                                 if (config.Nims[slotid].diseqcMode.value == 4):
565                                         for x in self.satList:
566                                                 list.append(x)
567                         elif configMode == "advanced":
568                                 for x in self.satList:
569                                         if config.Nims[slotid].advanced.sat[x[1]].lnb.value != 0:
570                                                 list.append(x)
571                 return list
572
573         def nimDiseqcModeChanged(self, slotid, mode):
574                 #print "nimDiseqcModeChanged set to " + str(mode)
575                 pass
576         def nimPortAChanged(self, slotid, val):
577                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
578                 pass
579         def nimPortBChanged(self, slotid, val):
580                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
581                 #print "nimDiseqcB set to " + str(val)
582                 pass
583         def nimPortCChanged(self, slotid, val):
584                 #print "nimDiseqcC set to " + str(val)
585                 pass
586         def nimPortDChanged(self, slotid, val):
587                 #print "nimDiseqcD set to " + str(val)
588                 pass
589
590 def InitNimManager(nimmgr):
591         config.Nims = []
592         for x in range(nimmgr.nimCount):
593                 config.Nims.append(ConfigSubsection())
594                 
595 #       def nimConfigModeChanged(slotid, configElement):
596 #               nimmgr.nimConfigModeChanged(slotid, configElement.value)
597         def nimDiseqcModeChanged(slotid, configElement):
598                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
599                 
600         def nimPortAChanged(slotid, configElement):
601                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
602         def nimPortBChanged(slotid, configElement):
603                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
604         def nimPortCChanged(slotid, configElement):
605                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
606         def nimPortDChanged(slotid, configElement):
607                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
608
609         for slot in nimmgr.nimslots:
610                 x = slot.slotid
611                 cname = nimmgr.getConfigPrefix(x)
612                 nim = config.Nims[x]
613                 
614                 if slot.nimType == nimmgr.nimType["DVB-S"]:
615                         if slot.slotid == 0:
616                                 nim.configMode = configElement(cname + "configMode", configSelection, 0, (
617                                 ("simple", _("Simple")), ("advanced", _("Advanced"))), False)
618                         else:
619                                 nim.configMode = configElement(cname + "configMode", configSelection, 0, (
620                                 ("equal", _("Equal to Socket A")),
621                                 ("loopthrough", _("Loopthrough to Socket A")),
622                                 ("nothing", _("Nothing connected")),
623                                 ("satposdepends", _("Secondary cable from motorized LNB")),
624                                 ("simple", _("Simple")),
625                                 ("advanced", _("Advanced"))), False)
626                         #important - check if just the 2nd one is LT only and the first one is DVB-S
627                         if currentConfigSelectionElement(nim.configMode) in ["loopthrough", "satposdepends", "equal"]:
628                                 if x == 0:                                                                              #first one can never be linked to anything
629                                         nim.configMode.value = getConfigSelectionElement(nim.configMode, "simple")              #reset to simple
630                                         nim.configMode.save()
631                                 else:
632                                         #FIXME: make it better
633                                         for y in nimmgr.nimslots:
634                                                 if y.slotid == 0:
635                                                         if y.nimType != nimmgr.nimType["DVB-S"]:
636                                                                 nim.configMode.value = getConfigSelectionElement(nim.configMode, "simple")              #reset to simple
637                                                                 nim.configMode.save()
638
639                         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);
640                         nim.diseqcA = configElement(cname + "diseqcA", configSatlist, 192, nimmgr.satList, False);
641                         nim.diseqcB = configElement(cname + "diseqcB", configSatlist, 130, nimmgr.satList, False);
642                         nim.diseqcC = configElement(cname + "diseqcC", configSatlist, 0, nimmgr.satList, False);
643                         nim.diseqcD = configElement(cname + "diseqcD", configSatlist, 0, nimmgr.satList, False);
644                         nim.positionerMode = configElement(cname + "positionerMode", configSelection, 0, (("usals", _("USALS")), ("manual", _("manual"))), False);
645                         nim.longitude = configElement(cname + "longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,90),(0,999)]), False);
646                         nim.longitudeOrientation = configElement(cname + "longitudeOrientation", configSelection, 0, (("east", _("East")), ("west", _("West"))), False)
647                         nim.latitude = configElement(cname + "latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,90),(0,999)]), False);
648                         nim.latitudeOrientation = configElement(cname + "latitudeOrientation", configSelection, 0, (("north", _("North")), ("south", _("South"))), False)
649                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
650                         satNimListNames = []
651                         for x in satNimList:
652                                 satNimListNames.append((("Slot_" + ("A", "B", "C", "D")[x] + "_" + nimmgr.getNimName(x)), _("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x)))
653                         nim.equalTo = configElement(cname + "equalTo", configSelection, 0, satNimListNames, False);
654                         nim.linkedTo = configElement(cname + "linkedTo", configSelection, 0, satNimListNames, False);
655                         nim.satposDependsTo = configElement(cname + "satposDependsTo", configSelection, 0, satNimListNames, False);
656                         
657                         #perhaps the instance of the slot is more useful?
658 #                       nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
659                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
660                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
661                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
662                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
663                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
664                         
665                         # advanced config:
666                         nim.advanced = ConfigSubsection()
667                         nim.advanced.sats = configElement(cname + "advanced.sats", configSatlist, 192, nimmgr.satList, False);
668                         nim.advanced.sat = {}
669                         lnbs = ["not available"]
670                         for y in range(1, 33):
671                                 lnbs.append("LNB " + str(y))
672                         for x in nimmgr.satList:
673                                 nim.advanced.sat[x[1]] = ConfigSubsection()
674                                 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)
675                                 nim.advanced.sat[x[1]].tonemode = configElement(cname + "advanced.sat" + str(x[1]) + ".tonemode", configSelection, 0, (("band", _("Band")), ("on", _("On")), ("off", _("Off"))), False)
676                                 nim.advanced.sat[x[1]].usals = configElement(cname + "advanced.sat" + str(x[1]) + ".usals", configSelection, 0, (("yes", _("Yes")), ("no", _("No"))), False)
677                                 nim.advanced.sat[x[1]].rotorposition = configElement(cname + "advanced.sat" + str(x[1]) + ".rotorposition", configSequence, [1], configsequencearg.get("INTEGER", (1, 255)), False)
678                                 nim.advanced.sat[x[1]].lnb = configElement(cname + "advanced.sat" + str(x[1]) + ".lnb", configSelection, 0, lnbs, False)
679
680                         csw = [("none", _("None")), ("AA", _("AA")), ("AB", _("AB")), ("BA", _("BA")), ("BB", _("BB"))]
681                         for y in range(0, 16):
682                                 csw.append((str(0xF0|y), "Input " + str(y+1)))
683
684                         ucsw = [("none", _("None"))]
685                         for y in range(1, 17):
686                                 ucsw.append("Input " + str(y))
687
688                         nim.advanced.lnb = [0]
689                         for x in range(1, 33):
690                                 nim.advanced.lnb.append(ConfigSubsection())
691                                 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)
692                                 nim.advanced.lnb[x].lofl = configElement(cname + "advanced.lnb" + str(x) + ".lofl", configSequence, [9750], configsequencearg.get("INTEGER", (0, 99999)), False)
693                                 nim.advanced.lnb[x].lofh = configElement(cname + "advanced.lnb" + str(x) + ".lofh", configSequence, [10600], configsequencearg.get("INTEGER", (0, 99999)), False)
694                                 nim.advanced.lnb[x].threshold = configElement(cname + "advanced.lnb" + str(x) + ".threshold", configSequence, [11750], configsequencearg.get("INTEGER", (0, 99999)), False)
695                                 nim.advanced.lnb[x].output_12v = configElement(cname + "advanced.lnb" + str(x) + ".output_12v", configSelection, 0, (("0V", _("0 V")), ("12V", _("12 V"))), False)
696                                 nim.advanced.lnb[x].increased_voltage = configElement(cname + "advanced.lnb" + str(x) + ".increased_voltage", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
697                                 nim.advanced.lnb[x].toneburst = configElement(cname + "advanced.lnb" + str(x) + ".toneburst", configSelection, 0, (("none", _("None")), ("A", _("A")), ("B", _("B"))), False)
698                                 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)
699                                 nim.advanced.lnb[x].commitedDiseqcCommand = configElement(cname + "advanced.lnb" + str(x) + ".commitedDiseqcCommand", configSelection, 0, csw, False)
700                                 nim.advanced.lnb[x].fastDiseqc = configElement(cname + "advanced.lnb" + str(x) + ".fastDiseqc", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
701                                 nim.advanced.lnb[x].sequenceRepeat = configElement(cname + "advanced.lnb" + str(x) + ".sequenceRepeat", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
702                                 nim.advanced.lnb[x].commandOrder1_0 = configElement(cname + "advanced.lnb" + str(x) + ".commandOrder1_0", configSelection, 0, ("committed, toneburst", "toneburst, committed"), False)
703                                 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)
704                                 nim.advanced.lnb[x].uncommittedDiseqcCommand = configElement(cname + "advanced.lnb" + str(x) + ".uncommittedDiseqcCommand", configSelection, 0, ucsw, False)
705                                 nim.advanced.lnb[x].diseqcRepeats = configElement(cname + "advanced.lnb" + str(x) + ".diseqcRepeats", configSelection, 0, (("none", _("None")), ("one", _("One")), ("two", _("Two")), ("three", _("Three"))), False)
706                                 nim.advanced.lnb[x].longitude = configElement(cname + "advanced.lnb" + str(x) + ".longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,90),(0,999)]), False)
707                                 nim.advanced.lnb[x].longitudeOrientation = configElement(cname + "advanced.lnb" + str(x) + ".longitudeOrientation", configSelection, 0, (("east", _("East")), ("west", _("West"))), False)
708                                 nim.advanced.lnb[x].latitude = configElement(cname + "advanced.lnb" + str(x) + ".latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,90),(0,999)]), False)
709                                 nim.advanced.lnb[x].latitudeOrientation = configElement(cname + "advanced.lnb" + str(x) + ".latitudeOrientation", configSelection, 0, (("north", _("North")), ("south", _("South"))), False)
710                                 nim.advanced.lnb[x].powerMeasurement = configElement(cname + "advanced.lnb" + str(x) + ".powerMeasurement", configSelection, 0, (("yes", _("Yes")), ("no", _("No"))), False)
711                                 nim.advanced.lnb[x].powerThreshold = configElement(cname + "advanced.lnb" + str(x) + ".powerThreshold", configSequence, [50], configsequencearg.get("INTEGER", (0, 100)), False)
712                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
713                         nim.cable = configElement(cname + "cable", configSelection, 0, nimmgr.cablesList, False);
714                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
715                         nim.cable = configElement(cname + "terrestrial", configSelection, 0, nimmgr.terrestrialsList, False);
716                 else:
717                         print "pls add support for this frontend type!"         
718
719         nimmgr.sec = SecConfigure(nimmgr)
720
721 nimmanager = NimManager()