add some stuff
[enigma2.git] / lib / python / Components / NimManager.py
1 from config import config       #global config instance
2
3 from config import configElement
4 from config import ConfigSubsection
5 from config import ConfigSlider
6 from config import configSelection
7 from config import configSequence
8 from config import configsequencearg
9 from config import configSatlist
10
11 from enigma import *
12
13 import xml.dom.minidom
14 from xml.dom import EMPTY_NAMESPACE
15 from skin import elementsWithTag
16 from Tools import XMLTools
17
18 from xml.sax import make_parser
19 from xml.sax.handler import ContentHandler
20
21 class SecConfigure:
22         def addLNBSimple(self, slotid, orbpos, toneburstmode, diseqcmode, diseqcpos):
23                 #simple defaults
24                 sec = eDVBSatelliteEquipmentControl.getInstance()
25                 sec.addLNB()
26                 sec.setLNBTunerMask(1 << slotid)
27                 sec.setLNBLOFL(9750000)
28                 sec.setLNBLOFH(10600000)
29                 sec.setLNBThreshold(11750000)
30                 sec.setRepeats(0)
31                 sec.setFastDiSEqC(0)
32                 sec.setSeqRepeat(0)
33                 sec.setVoltageMode(0) #HV
34                 sec.setToneMode(0)              #HILO
35                 sec.setCommandOrder(0)
36                 #user values
37                 sec.setDiSEqCMode(diseqcmode)
38                 sec.setToneburst(toneburstmode)
39                 sec.setCommittedCommand(diseqcpos)
40                 #print "set orbpos to:" + str(orbpos)
41                 sec.addSatellite(orbpos)
42                 self.satList.append(orbpos)
43
44         def getSatList(self):
45                 return self.satList
46
47         def update(self):
48                 eDVBSatelliteEquipmentControl.getInstance().clear()
49                 self.satList = []
50
51                 for slot in self.NimManager.nimslots:
52                         x = slot.slotid
53                         nim = config.Nims[x]
54                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
55                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
56                                 if nim.configMode.value == 0:           #simple config
57                                         if nim.diseqcMode.value == 0:                   #single
58                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 0, 4)
59                                         elif nim.diseqcMode.value == 1:         #Toneburst A/B
60                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 1, 0, 4)
61                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 1, 0, 4)
62                                         elif nim.diseqcMode.value == 2:         #DiSEqC A/B
63                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
64                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
65                                         elif nim.diseqcMode.value == 3:         #DiSEqC A/B/C/D
66                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
67                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
68                                                 self.addLNBSimple(x, int(nim.diseqcC.vals[nim.diseqcC.value][1]), 0, 1, 2)
69                                                 self.addLNBSimple(x, int(nim.diseqcD.vals[nim.diseqcD.value][1]), 0, 1, 3)
70                                         elif nim.diseqcMode.value == 4:         #Positioner
71                                                 print "FIXME: positioner suppport"
72                                         pass
73                                 else:                                                                                                                                   #advanced config
74                                         print "FIXME add support for advanced config"
75                 
76         def __init__(self, nimmgr):
77                 self.NimManager = nimmgr
78                 self.update()
79                 
80 class boundFunction:
81         def __init__(self, fnc, *args):
82                 self.fnc = fnc
83                 self.args = args
84         def __call__(self, *args):
85                 self.fnc(*self.args + args)
86
87 class nimSlot:
88         def __init__(self, slotid, nimtype, name):
89                 self.slotid = slotid
90                 self.nimType = nimtype
91                 self.name = name
92
93 class NimManager:
94         class parseSats(ContentHandler):
95                 def __init__(self, satList, satellites):
96                         self.isPointsElement, self.isReboundsElement = 0, 0
97                         self.satList = satList
98                         self.satellites = satellites
99         
100                 def startElement(self, name, attrs):
101                         if (name == "sat"):
102                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
103                                 tpos = attrs.get('position',"")
104                                 tname = attrs.get('name',"")
105                                 self.satellites[tpos] = tname
106                                 self.satList.append( (tname, tpos) )
107
108         def getConfiguredSats(self):
109                 return self.sec.getSatList()
110
111         def getSatDescription(self, pos):
112                 return self.satellites[str(pos)]
113
114         def readSatsfromFile(self):
115                 self.satellites = { }
116
117                 print "Reading satellites.xml"
118                 parser = make_parser()
119                 satHandler = self.parseSats(self.satList, self.satellites)
120                 parser.setContentHandler(satHandler)
121                 parser.parse('/etc/tuxbox/satellites.xml')
122
123         def getNimType(self, slotID):
124                 #FIXME get it from /proc
125                 if slotID == 0:
126                         return self.nimType["DVB-S"]
127                 else:
128                         return self.nimType["empty/unknown"]
129
130         def getNimName(self, slotID):
131                 #FIXME get it from /proc
132                 return "Alps BSBE1"
133
134         def getNimSocketCount(self):
135                 #FIXME get it from /proc
136                 return 2
137
138         def getConfigPrefix(self, slotid):
139                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
140                         
141         def __init__(self):
142                 #use as enum
143                 self.nimType = {                "empty/unknown": -1,
144                                                                                                 "DVB-S": 0,
145                                                                                                 "DVB-C": 1,
146                                                                                                 "DVB-T": 2}
147                 self.satList = [ ]                                                                              
148                                                                                                 
149                 self.readSatsfromFile()                                                                         
150                                                                                                 
151                 self.nimCount = self.getNimSocketCount()
152                 
153                 self.nimslots = [ ]
154                 x = 0
155                 while x < self.nimCount:
156                         tType = self.getNimType(x)
157                         tName = self.getNimName(x)
158                         tNim = nimSlot(x, tType, tName)
159                         self.nimslots.append(tNim)
160                         x += 1
161                 
162                 InitNimManager(self)    #init config stuff
163
164         def nimList(self):
165                 list = [ ]
166                 for slot in self.nimslots:
167                         nimText = "Socket " + ("A", "B", "C", "D")[slot.slotid] + ": "
168                         if slot.nimType == -1:
169                                 nimText += "empty/unknown"
170                         else:
171                                 nimText += slot.name + " ("     
172                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
173                         list.append((nimText, slot))
174                 return list
175         
176         def getSatListForNim(self, slotid):
177                 #print "slotid:", slotid
178                 list = []
179                 #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
180                 #print "diseqcA:", config.Nims[slotid].diseqcA.value
181                 if (config.Nims[slotid].diseqcMode.value <= 3):
182                         list.append(self.satList[config.Nims[slotid].diseqcA.value])
183                 if (0 < config.Nims[slotid].diseqcMode.value <= 3):
184                         list.append(self.satList[config.Nims[slotid].diseqcB.value])
185                 if (config.Nims[slotid].diseqcMode.value == 3):
186                         list.append(self.satList[config.Nims[slotid].diseqcC.value])
187                         list.append(self.satList[config.Nims[slotid].diseqcD.value])
188                 return list
189
190         #callbacks for c++ config
191         def nimConfigModeChanged(self, slotid, mode):
192                 #print "nimConfigModeChanged set to " + str(mode)
193                 pass
194         def nimDiseqcModeChanged(self, slotid, mode):
195                 #print "nimDiseqcModeChanged set to " + str(mode)
196                 pass
197         def nimPortAChanged(self, slotid, val):
198                 #print "nimDiseqcA set to " + str(val)
199                 pass
200         def nimPortBChanged(self, slotid, val):
201                 #print "nimDiseqcB set to " + str(val)
202                 pass
203         def nimPortCChanged(self, slotid, val):
204                 #print "nimDiseqcC set to " + str(val)
205                 pass
206         def nimPortDChanged(self, slotid, val):
207                 #print "nimDiseqcD set to " + str(val)
208                 pass
209
210
211 def InitNimManager(nimmgr):
212         config.Nims = [ConfigSubsection()] * nimmgr.nimCount
213
214         def nimConfigModeChanged(slotid, configElement):
215                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
216         def nimDiseqcModeChanged(slotid, configElement):
217                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
218                 
219         def nimPortAChanged(slotid, configElement):
220                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
221         def nimPortBChanged(slotid, configElement):
222                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
223         def nimPortCChanged(slotid, configElement):
224                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
225         def nimPortDChanged(slotid, configElement):
226                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
227
228         for slot in nimmgr.nimslots:
229                 x = slot.slotid
230                 cname = nimmgr.getConfigPrefix(x)
231                 nim = config.Nims[x]
232                 
233                 if slot.nimType == nimmgr.nimType["DVB-S"]:
234                         nim.configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
235                         nim.diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D", "Positioner"));
236                         nim.diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
237                         nim.diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
238                         nim.diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
239                         nim.diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
240                         nim.longitude = configElement(cname + "longitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
241                         nim.latitude = configElement(cname + "latitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
242                         
243                         #perhaps the instance of the slot is more useful?
244                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
245                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
246                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,x))
247                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
248                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
249                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
250                 else:
251                         print "pls add support for this frontend type!"         
252                         
253         nimmgr.sec = SecConfigure(nimmgr)
254
255 nimmanager = NimManager()