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