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