make it compatible to smaller screens, even if it need 2 bytes more
[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
43         def update(self):
44                 eDVBSatelliteEquipmentControl.getInstance().clear()
45
46                 for slot in self.NimManager.nimslots:
47                         x = slot.slotid
48                         nim = config.Nims[x]
49                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
50                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
51                                 if nim.configMode.value == 0:           #simple config
52                                         if nim.diseqcMode.value == 0:                   #single
53                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 0, 4)
54                                         elif nim.diseqcMode.value == 1:         #Toneburst A/B
55                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 1, 0, 4)
56                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 1, 0, 4)
57                                         elif nim.diseqcMode.value == 2:         #DiSEqC A/B
58                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
59                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
60                                         elif nim.diseqcMode.value == 3:         #DiSEqC A/B/C/D
61                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
62                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
63                                                 self.addLNBSimple(x, int(nim.diseqcC.vals[nim.diseqcC.value][1]), 0, 1, 2)
64                                                 self.addLNBSimple(x, int(nim.diseqcD.vals[nim.diseqcD.value][1]), 0, 1, 3)
65                                         elif nim.diseqcMode.value == 4:         #Positioner
66                                                 print "FIXME: positioner suppport"
67                                         pass
68                                 else:                                                                                                                                   #advanced config
69                                         print "FIXME add support for advanced config"
70                 
71         def __init__(self, nimmgr):
72                 self.NimManager = nimmgr
73                 self.update()
74                 
75 class boundFunction:
76         def __init__(self, fnc, *args):
77                 self.fnc = fnc
78                 self.args = args
79         def __call__(self, *args):
80                 self.fnc(*self.args + args)
81
82 class nimSlot:
83         def __init__(self, slotid, nimtype, name):
84                 self.slotid = slotid
85                 self.nimType = nimtype
86                 self.name = name
87
88 class NimManager:
89         class parseSats(ContentHandler):
90                 def __init__(self, satList, satellites):
91                         self.isPointsElement, self.isReboundsElement = 0, 0
92                         self.satList = satList
93                         self.satellites = satellites
94         
95                 def startElement(self, name, attrs):
96                         if (name == "sat"):
97                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
98                                 tpos = attrs.get('position',"")
99                                 tname = attrs.get('name',"")
100                                 self.satellites[tpos] = tname
101                                 self.satList.append( (tname, tpos) )
102
103         def readSatsfromFile(self):
104                 self.satellites = { }
105
106                 print "Reading satellites.xml"
107                 parser = make_parser()
108                 satHandler = self.parseSats(self.satList, self.satellites)
109                 parser.setContentHandler(satHandler)
110                 parser.parse('/etc/tuxbox/satellites.xml')
111
112         def getNimType(self, slotID):
113                 #FIXME get it from /proc
114                 if slotID == 0:
115                         return self.nimType["DVB-S"]
116                 else:
117                         return self.nimType["empty/unknown"]
118
119         def getNimName(self, slotID):
120                 #FIXME get it from /proc
121                 return "Alps BSBE1"
122
123         def getNimSocketCount(self):
124                 #FIXME get it from /proc
125                 return 2
126
127         def getConfigPrefix(self, slotid):
128                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
129                         
130         def __init__(self):
131                 #use as enum
132                 self.nimType = {                "empty/unknown": -1,
133                                                                                                 "DVB-S": 0,
134                                                                                                 "DVB-C": 1,
135                                                                                                 "DVB-T": 2}
136                 self.satList = [ ]                                                                              
137                                                                                                 
138                 self.readSatsfromFile()                                                                         
139                                                                                                 
140                 self.nimCount = self.getNimSocketCount()
141                 
142                 self.nimslots = [ ]
143                 x = 0
144                 while x < self.nimCount:
145                         tType = self.getNimType(x)
146                         tName = self.getNimName(x)
147                         tNim = nimSlot(x, tType, tName)
148                         self.nimslots.append(tNim)
149                         x += 1
150                 
151                 InitNimManager(self)    #init config stuff
152
153         def nimList(self):
154                 list = [ ]
155                 for slot in self.nimslots:
156                         nimText = "Socket " + ("A", "B", "C", "D")[slot.slotid] + ": "
157                         if slot.nimType == -1:
158                                 nimText += "empty/unknown"
159                         else:
160                                 nimText += slot.name + " ("     
161                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
162                         list.append((nimText, slot))
163                 return list
164         
165         def getSatListForNim(self, slotid):
166                 #print "slotid:", slotid
167                 list = []
168                 #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
169                 #print "diseqcA:", config.Nims[slotid].diseqcA.value
170                 if (config.Nims[slotid].diseqcMode.value <= 3):
171                         list.append(self.satList[config.Nims[slotid].diseqcA.value])
172                 if (0 < config.Nims[slotid].diseqcMode.value <= 3):
173                         list.append(self.satList[config.Nims[slotid].diseqcB.value])
174                 if (config.Nims[slotid].diseqcMode.value == 3):
175                         list.append(self.satList[config.Nims[slotid].diseqcC.value])
176                         list.append(self.satList[config.Nims[slotid].diseqcD.value])
177                 return list
178
179         #callbacks for c++ config
180         def nimConfigModeChanged(self, slotid, mode):
181                 #print "nimConfigModeChanged set to " + str(mode)
182                 pass
183         def nimDiseqcModeChanged(self, slotid, mode):
184                 #print "nimDiseqcModeChanged set to " + str(mode)
185                 pass
186         def nimPortAChanged(self, slotid, val):
187                 #print "nimDiseqcA set to " + str(val)
188                 pass
189         def nimPortBChanged(self, slotid, val):
190                 #print "nimDiseqcB set to " + str(val)
191                 pass
192         def nimPortCChanged(self, slotid, val):
193                 #print "nimDiseqcC set to " + str(val)
194                 pass
195         def nimPortDChanged(self, slotid, val):
196                 #print "nimDiseqcD set to " + str(val)
197                 pass
198
199
200 def InitNimManager(nimmgr):
201         config.Nims = [ConfigSubsection()] * nimmgr.nimCount
202
203         def nimConfigModeChanged(slotid, configElement):
204                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
205         def nimDiseqcModeChanged(slotid, configElement):
206                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
207                 
208         def nimPortAChanged(slotid, configElement):
209                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
210         def nimPortBChanged(slotid, configElement):
211                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
212         def nimPortCChanged(slotid, configElement):
213                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
214         def nimPortDChanged(slotid, configElement):
215                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
216
217         for slot in nimmgr.nimslots:
218                 x = slot.slotid
219                 cname = nimmgr.getConfigPrefix(x)
220                 nim = config.Nims[x]
221                 
222                 if slot.nimType == nimmgr.nimType["DVB-S"]:
223                         nim.configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
224                         nim.diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D", "Positioner"));
225                         nim.diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
226                         nim.diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
227                         nim.diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
228                         nim.diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
229                         nim.longitude = configElement(cname + "longitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
230                         nim.latitude = configElement(cname + "latitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
231                         
232                         #perhaps the instance of the slot is more useful?
233                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
234                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
235                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,x))
236                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
237                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
238                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
239                 else:
240                         print "pls add support for this frontend type!"         
241                         
242         nimmgr.sec = SecConfigure(nimmgr)
243
244 nimmanager = NimManager()