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