add SEC 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                 eDVBSatelliteEquipmentControl.getInstance().addLNB()
25                 eDVBSatelliteEquipmentControl.getInstance().setLNBTunerMask(1 << slotid)
26                 eDVBSatelliteEquipmentControl.getInstance().setLNBLOFL(9750000)
27                 eDVBSatelliteEquipmentControl.getInstance().setLNBLOFH(10600000)
28                 eDVBSatelliteEquipmentControl.getInstance().setLNBThreshold(11750000)
29                 eDVBSatelliteEquipmentControl.getInstance().setRepeats(0)
30                 eDVBSatelliteEquipmentControl.getInstance().setFastDiSEqC(0)
31                 eDVBSatelliteEquipmentControl.getInstance().setSeqRepeat(0)
32                 eDVBSatelliteEquipmentControl.getInstance().setVoltageMode(0) #HV
33                 eDVBSatelliteEquipmentControl.getInstance().setToneMode(0)              #HILO
34                 eDVBSatelliteEquipmentControl.getInstance().setCommandOrder(0)
35                 #user values
36                 eDVBSatelliteEquipmentControl.getInstance().setDiSEqCMode(diseqcmode)
37                 eDVBSatelliteEquipmentControl.getInstance().setToneburst(toneburstmode)
38                 eDVBSatelliteEquipmentControl.getInstance().setCommittedCommand(diseqcpos)
39
40                 #print "set orbpos to:" + str(orbpos)
41                 eDVBSatelliteEquipmentControl.getInstance().addSatellite(orbpos)
42
43         def update(self):
44                 eDVBSatelliteEquipmentControl.getInstance().clear()
45
46                 for slot in self.NimManager.nimslots:
47                         x = slot.slotid
48                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
49                                 print "slot: " + str(x) + " configmode: " + str(config.Nims[x].configMode.value)
50                                 if config.Nims[x].configMode.value == 0:                #simple config
51                                         if config.Nims[x].diseqcMode.value == 0:                        #single
52                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcA.vals[config.Nims[x].diseqcA.value][1]), 0, 0, 4)
53                                         elif config.Nims[x].diseqcMode.value == 1:              #Toneburst A/B
54                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcA.vals[config.Nims[x].diseqcA.value][1]), 1, 0, 4)
55                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcB.vals[config.Nims[x].diseqcB.value][1]), 1, 0, 4)
56                                         elif config.Nims[x].diseqcMode.value == 2:              #DiSEqC A/B
57                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcA.vals[config.Nims[x].diseqcA.value][1]), 0, 1, 0)
58                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcB.vals[config.Nims[x].diseqcB.value][1]), 0, 1, 1)
59                                         elif config.Nims[x].diseqcMode.value == 3:              #DiSEqC A/B/C/D
60                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcA.vals[config.Nims[x].diseqcA.value][1]), 0, 1, 0)
61                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcB.vals[config.Nims[x].diseqcB.value][1]), 0, 1, 1)
62                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcC.vals[config.Nims[x].diseqcC.value][1]), 0, 1, 2)
63                                                 self.addLNBSimple(x, int(config.Nims[x].diseqcD.vals[config.Nims[x].diseqcD.value][1]), 0, 1, 3)
64                                                 pass
65                                         elif config.Nims[x].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                 
221                 if slot.nimType == nimmgr.nimType["DVB-S"]:
222                         config.Nims[x].configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
223                         config.Nims[x].diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D", "Positioner"));
224                         config.Nims[x].diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
225                         config.Nims[x].diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
226                         config.Nims[x].diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
227                         config.Nims[x].diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
228                         config.Nims[x].longitude = configElement(cname + "longitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
229                         config.Nims[x].latitude = configElement(cname + "latitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
230                         
231                         #perhaps the instance of the slot is more useful?
232                         config.Nims[x].configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
233                         config.Nims[x].diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
234                         config.Nims[x].diseqcA.addNotifier(boundFunction(nimPortAChanged,x))
235                         config.Nims[x].diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
236                         config.Nims[x].diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
237                         config.Nims[x].diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
238                 else:
239                         print "pls add support for this frontend type!"         
240                         
241         nimmgr.sec = SecConfigure(nimmgr)
242
243 nimmanager = NimManager()