make some diseqc / continuous tone / toneburst / rotor values (delays,
[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 as secClass, \
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 = secClass.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_a_b", "diseqc_a_b", "diseqc_a_b_c_d"]:
588                                         list.append(self.satList[config.Nims[slotid].diseqcA.index])
589                                 if dm in ["toneburst_a_b", "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 InitSecParams():
644         config.sec = ConfigSubsection()
645
646         x = ConfigInteger(default=15)
647         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_CONT_TONE, configElement.value))
648         config.sec.delay_after_continuous_tone_change = x
649
650         x = ConfigInteger(default=10)
651         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_FINAL_VOLTAGE_CHANGE, configElement.value))
652         config.sec.delay_after_final_voltage_change = x
653
654         x = ConfigInteger(default=120)
655         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_BETWEEN_DISEQC_REPEATS, configElement.value))
656         config.sec.delay_between_diseqc_repeats = x
657
658         x = ConfigInteger(default=50)
659         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_LAST_DISEQC_CMD, configElement.value))
660         config.sec.delay_after_last_diseqc_command = x
661
662         x = ConfigInteger(default=50)
663         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_TONEBURST, configElement.value))
664         config.sec.delay_after_toneburst = x
665
666         x = ConfigInteger(default=200)
667         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS, configElement.value))
668         config.sec.delay_after_enable_voltage_before_switch_command = x
669
670         x = ConfigInteger(default=700)
671         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_BETWEEN_SWITCH_AND_MOTOR_CMD, configElement.value))
672         config.sec.delay_between_switch_and_motor_command = x
673
674         x = ConfigInteger(default=150)
675         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MEASURE_IDLE_INPUTPOWER, configElement.value))
676         config.sec.delay_after_voltage_change_before_measure_idle_inputpower = x
677
678         x = ConfigInteger(default=750)
679         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_MOTOR_CMD, configElement.value))
680         config.sec.delay_after_enable_voltage_before_motor_command = x
681
682         x = ConfigInteger(default=150)
683         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_MOTOR_STOP_CMD, configElement.value))
684         config.sec.delay_after_motor_stop_command = x
685
686         x = ConfigInteger(default=150)
687         x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MOTOR_CMD, configElement.value))
688         config.sec.delay_after_voltage_change_before_motor_command = x
689
690         x = ConfigInteger(default=120)
691         x.addNotifier(lambda configElement: secClass.setParam(secClass.MOTOR_RUNNING_TIMEOUT, configElement.value))
692         config.sec.motor_running_timeout = x
693
694 def InitNimManager(nimmgr):
695         InitSecParams()
696
697         config.Nims = ConfigSubList()
698         for x in range(nimmgr.nimCount):
699                 config.Nims.append(ConfigSubsection())
700                 
701 #       def nimConfigModeChanged(slotid, configElement):
702 #               nimmgr.nimConfigModeChanged(slotid, configElement.value)
703         def nimDiseqcModeChanged(slotid, configElement):
704                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
705                 
706         def nimPortAChanged(slotid, configElement):
707                 nimmgr.nimPortAChanged(slotid, configElement.value)
708         def nimPortBChanged(slotid, configElement):
709                 nimmgr.nimPortBChanged(slotid, configElement.value)
710         def nimPortCChanged(slotid, configElement):
711                 nimmgr.nimPortCChanged(slotid, configElement.value)
712         def nimPortDChanged(slotid, configElement):
713                 nimmgr.nimPortDChanged(slotid, configElement.value)
714
715         for slot in nimmgr.nimslots:
716                 x = slot.slotid
717                 cname = nimmgr.getConfigPrefix(x)
718                 nim = config.Nims[x]
719                 
720                 if slot.nimType == nimmgr.nimType["DVB-S"]:
721                         if slot.slotid == 0:
722                                 nim.configMode = ConfigSelection(
723                                         choices = {
724                                                 "simple": _("simple"),
725                                                 "advanced": _("advanced")},
726                                         default = "simple")
727                         else:
728                                 nim.configMode = ConfigSelection(
729                                         choices = {
730                                                 "equal": _("equal to Socket A"),
731                                                 "loopthrough": _("loopthrough to socket A"),
732                                                 "nothing": _("nothing connected"),
733                                                 "satposdepends": _("second cable of motorized LNB"),
734                                                 "simple": _("simple"),
735                                                 "advanced": _("advanced")},
736                                         default = "loopthrough")
737
738                         #important - check if just the 2nd one is LT only and the first one is DVB-S
739                         if nim.configMode.value in ["loopthrough", "satposdepends", "equal"]:
740                                 if x == 0: # first one can never be linked to anything
741                                         # reset to simple
742                                         nim.configMode.value = "simple"
743                                         nim.configMode.save()
744                                 else:
745                                         #FIXME: make it better
746                                         for y in nimmgr.nimslots:
747                                                 if y.slotid == 0:
748                                                         if y.nimType != nimmgr.nimType["DVB-S"]:
749                                                                 # reset to simple
750                                                                 nim.configMode.value = "simple"
751                                                                 nim.configMode.save()
752
753                         nim.diseqcMode = ConfigSelection(
754                                 choices = [
755                                         ("single", _("Single")),
756                                         ("toneburst_a_b", _("Toneburst A/B")),
757                                         ("diseqc_a_b", _("DiSEqC A/B")),
758                                         ("diseqc_a_b_c_d", _("DiSEqC A/B/C/D")),
759                                         ("positioner", _("Positioner"))],
760                                 default = "diseqc_a_b")
761
762                         nim.diseqcA = getConfigSatlist(192, nimmgr.satList)
763                         nim.diseqcB = getConfigSatlist(130, nimmgr.satList)
764                         nim.diseqcC = ConfigSatlist(list = nimmgr.satList)
765                         nim.diseqcD = ConfigSatlist(list = nimmgr.satList)
766                         nim.positionerMode = ConfigSelection(
767                                 choices = [
768                                         ("usals", _("USALS")),
769                                         ("manual", _("manual"))],
770                                 default = "usals")
771                         nim.longitude = ConfigFloat(default=[5,100], limits=[(0,359),(0,999)])
772                         nim.longitudeOrientation = ConfigSelection(choices={"east": _("East"), "west": _("West")}, default = "east")
773                         nim.latitude = ConfigFloat(default=[50,767], limits=[(0,359),(0,999)])
774                         nim.latitudeOrientation = ConfigSelection(choices={"north": _("North"), "south": _("South")}, default="north")
775                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
776                         satNimListNames = {}
777                         for x in satNimList:
778                                 satNimListNames["Slot_" + ("A", "B", "C", "D")[x] + "_" + nimmgr.getNimName(x)] = _("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x)
779                         if len(satNimListNames):
780                                 nim.equalTo = ConfigSelection(choices = satNimListNames)
781                                 nim.linkedTo = ConfigSelection(choices = satNimListNames)
782                                 nim.satposDependsTo = ConfigSelection(choices = satNimListNames)
783                         
784                         #perhaps the instance of the slot is more useful?
785 #                       nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
786                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
787                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
788                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
789                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
790                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
791                         
792                         # advanced config:
793                         nim.advanced = ConfigSubsection()
794                         nim.advanced.sats = getConfigSatlist(192,nimmgr.satList)
795                         nim.advanced.sat = ConfigSubDict()
796                         lnbs = [("0", "not available")]
797                         for y in range(1, 33):
798                                 lnbs.append((str(y), "LNB " + str(y)))
799
800                         for x in nimmgr.satList:
801                                 nim.advanced.sat[x[0]] = ConfigSubsection()
802                                 nim.advanced.sat[x[0]].voltage = ConfigSelection(choices={"polarization": _("Polarization"), "13V": _("13 V"), "18V": _("18 V")}, default = "polarization")
803                                 nim.advanced.sat[x[0]].tonemode = ConfigSelection(choices={"band": _("Band"), "on": _("On"), "off": _("Off")}, default = "band")
804                                 nim.advanced.sat[x[0]].usals = ConfigYesNo(default=True)
805                                 nim.advanced.sat[x[0]].rotorposition = ConfigInteger(default=1, limits=(1, 255))
806                                 nim.advanced.sat[x[0]].lnb = ConfigSelection(choices = lnbs)
807
808                         csw = [("none", _("None")), ("AA", _("AA")), ("AB", _("AB")), ("BA", _("BA")), ("BB", _("BB"))]
809                         for y in range(0, 16):
810                                 csw.append((str(0xF0|y), "Input " + str(y+1)))
811                         
812                         ucsw = [("0", _("None"))]
813                         for y in range(1, 17):
814                                 ucsw.append((str(y), "Input " + str(y)))
815                         
816                         nim.advanced.lnb = ConfigSubList()
817                         nim.advanced.lnb.append(ConfigNothing())
818                         for x in range(1, 33):
819                                 nim.advanced.lnb.append(ConfigSubsection())
820                                 nim.advanced.lnb[x].lof = ConfigSelection(choices={"universal_lnb": _("Universal LNB"), "c_band": _("C-Band"), "user_defined": _("User defined")}, default="universal_lnb")
821                                 nim.advanced.lnb[x].lofl = ConfigInteger(default=9750, limits = (0, 99999))
822                                 nim.advanced.lnb[x].lofh = ConfigInteger(default=10600, limits = (0, 99999))
823                                 nim.advanced.lnb[x].threshold = ConfigInteger(default=11700, limits = (0, 99999))
824 #                               nim.advanced.lnb[x].output_12v = ConfigSelection(choices = [("0V", _("0 V")), ("12V", _("12 V"))], default="0V")
825                                 nim.advanced.lnb[x].increased_voltage = ConfigYesNo(default=False)
826                                 nim.advanced.lnb[x].toneburst = ConfigSelection(choices = [("none", _("None")), ("A", _("A")), ("B", _("B"))], default = "none")
827                                 nim.advanced.lnb[x].diseqcMode = ConfigSelection(choices = [("none", _("None")), ("1_0", _("1.0")), ("1_1", _("1.1")), ("1_2", _("1.2"))], default = "none")
828                                 nim.advanced.lnb[x].commitedDiseqcCommand = ConfigSelection(choices = csw)
829                                 nim.advanced.lnb[x].fastDiseqc = ConfigYesNo(default=False)
830                                 nim.advanced.lnb[x].sequenceRepeat = ConfigYesNo(default=False)
831                                 nim.advanced.lnb[x].commandOrder1_0 = ConfigSelection(choices = [("ct", "committed, toneburst"), ("tc", "toneburst, committed")], default = "ct")
832                                 nim.advanced.lnb[x].commandOrder = ConfigSelection(choices = [
833                                                 ("ct", "committed, toneburst"), 
834                                                 ("tc", "toneburst, committed"), 
835                                                 ("cut", "committed, uncommitted, toneburst"), 
836                                                 ("tcu", "toneburst, committed, uncommitted"), 
837                                                 ("uct", "uncommitted, committed, toneburst"), 
838                                                 ("tuc", "toneburst, uncommitted, commmitted")],
839                                                 default="ct")
840                                 nim.advanced.lnb[x].uncommittedDiseqcCommand = ConfigSelection(choices = ucsw)
841                                 nim.advanced.lnb[x].diseqcRepeats = ConfigSelection(choices = [("none", _("None")), ("one", _("One")), ("two", _("Two")), ("three", _("Three"))], default = "none")
842                                 nim.advanced.lnb[x].longitude = ConfigFloat(default = [5,100], limits = [(0,359),(0,999)])
843                                 nim.advanced.lnb[x].longitudeOrientation = ConfigSelection(choices = [("east", _("East")), ("west", _("West"))], default = "east")
844                                 nim.advanced.lnb[x].latitude = ConfigFloat(default = [50,767], limits = [(0,359),(0,999)])
845                                 nim.advanced.lnb[x].latitudeOrientation = ConfigSelection(choices = [("north", _("North")), ("south", _("South"))], default = "north")
846                                 nim.advanced.lnb[x].powerMeasurement = ConfigYesNo(default=True)
847                                 nim.advanced.lnb[x].powerThreshold = ConfigInteger(default=50, limits=(0, 100))
848
849                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
850                         nim.cabletype = ConfigSelection(choices = [("quick", _("Quick")), ("complete", _("Complete"))], default = "complete")
851                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
852                         list = []
853                         n = 0
854                         for x in nimmgr.terrestrialsList:
855                                 list.append((str(n), x[0]))
856                                 n += 1
857                         nim.terrestrial = ConfigSelection(choices = list)
858                         nim.terrestrial_5V = ConfigOnOff()
859                 else:
860                         print "pls add support for this frontend type!"         
861 #                       assert False
862
863         nimmgr.sec = SecConfigure(nimmgr)
864
865
866 nimmanager = NimManager()