call nimmanager->update()
[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
12 import xml.dom.minidom
13 from xml.dom import EMPTY_NAMESPACE
14 from skin import elementsWithTag
15 from Tools import XMLTools
16
17 from xml.sax import make_parser
18 from xml.sax.handler import ContentHandler
19
20 class boundFunction:
21         def __init__(self, fnc, *args):
22                 self.fnc = fnc
23                 self.args = args
24         def __call__(self, *args):
25                 self.fnc(*self.args + args)
26
27 class nimSlot:
28         def __init__(self, slotid, nimtype, name):
29                 self.slotid = slotid
30                 self.nimType = nimtype
31                 self.name = name
32
33 class NimManager:
34         class parseSats(ContentHandler):
35                 def __init__(self, satList, satellites):
36                         self.isPointsElement, self.isReboundsElement = 0, 0
37                         self.satList = satList
38                         self.satellites = satellites
39         
40                 def startElement(self, name, attrs):
41                         if (name == "sat"):
42                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
43                                 tpos = attrs.get('position',"")
44                                 tname = attrs.get('name',"")
45                                 self.satellites[tpos] = tname
46                                 self.satList.append( (tname, tpos) )
47
48         def readSatsfromFile(self):
49                 self.satellites = { }
50
51                 #FIXME: path ok???
52                 print "Reading satellites.xml"
53                 parser = make_parser()
54                 satHandler = self.parseSats(self.satList, self.satellites)
55                 parser.setContentHandler(satHandler)
56                 parser.parse('/etc/tuxbox/satellites.xml')
57     
58                 #satdom = xml.dom.minidom.parse('/etc/tuxbox/satellites.xml')
59
60
61                 #for entries in elementsWithTag(satdom.childNodes, "satellites"):
62                         #for x in elementsWithTag(entries.childNodes, "sat"):
63                                 ##print "found sat " + x.getAttribute('name') + " " + str(x.getAttribute('position'))
64                                 #tpos = x.getAttribute('position')
65                                 #tname = x.getAttribute('name')
66                                 ##tname.encode('utf8')
67                                 #self.satellites[tpos] = tname
68                                 #self.satList.append( (tname, tpos) )
69
70         def getNimType(self, slotID):
71                 #FIXME get it from /proc
72                 if slotID == 0:
73                         return self.nimType["DVB-S"]
74                 else:
75                         return self.nimType["empty/unknown"]
76
77         def getNimName(self, slotID):
78                 #FIXME get it from /proc
79                 return "Alps BSBE1"
80
81         def getNimSocketCount(self):
82                 #FIXME get it from /proc
83                 return 2
84
85         def getConfigPrefix(self, slotid):
86                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
87                         
88         def __init__(self):
89                 #use as enum
90                 self.nimType = {                "empty/unknown": -1,
91                                                                                                 "DVB-S": 0,
92                                                                                                 "DVB-C": 1,
93                                                                                                 "DVB-T": 2}
94                 self.satList = [ ]                                                                              
95                                                                                                 
96                 self.readSatsfromFile()                                                                         
97                                                                                                 
98                 self.nimCount = self.getNimSocketCount()
99                 
100                 self.nimslots = [ ]
101                 x = 0
102                 while x < self.nimCount:
103                         tType = self.getNimType(x)
104                         tName = self.getNimName(x)
105                         tNim = nimSlot(x, tType, tName)
106                         self.nimslots.append(tNim)
107                         x += 1
108                 
109                 InitNimManager(self)    #init config stuff
110
111         def nimList(self):
112                 list = [ ]
113                 for slot in self.nimslots:
114                         nimText = "Socket " + ("A", "B", "C", "D")[slot.slotid] + ": "
115                         if slot.nimType == -1:
116                                 nimText += "empty/unknown"
117                         else:
118                                 nimText += slot.name + " ("     
119                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
120                         list.append((nimText, slot))
121                 return list
122         
123         def getSatListForNim(self, slotid):
124                 print "slotid:", slotid
125                 list = []
126                 print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
127                 print "diseqcA:", config.Nims[slotid].diseqcA.value
128                 if (config.Nims[slotid].diseqcMode.value <= 3):
129                         list.append(self.satList[config.Nims[slotid].diseqcA.value])
130                 if (0 < config.Nims[slotid].diseqcMode.value <= 3):
131                         list.append(self.satList[config.Nims[slotid].diseqcB.value])
132                 if (config.Nims[slotid].diseqcMode.value == 3):
133                         list.append(self.satList[config.Nims[slotid].diseqcC.value])
134                         list.append(self.satList[config.Nims[slotid].diseqcD.value])
135                 return list
136
137         #callbacks for c++ config
138         def nimConfigModeChanged(self, slotid, mode):
139                 print "nimConfigModeChanged set to " + str(mode)
140         def nimDiseqcModeChanged(self, slotid, mode):
141                 print "nimDiseqcModeChanged set to " + str(mode)
142         def nimPortAChanged(self, slotid, val):
143                 print "nimDiseqcA set to " + str(val)
144         def nimPortBChanged(self, slotid, val):
145                 print "nimDiseqcB set to " + str(val)
146         def nimPortCChanged(self, slotid, val):
147                 print "nimDiseqcC set to " + str(val)
148         def nimPortDChanged(self, slotid, val):
149                 print "nimDiseqcD set to " + str(val)
150
151
152 def InitNimManager(nimmgr):
153         config.Nims = [ConfigSubsection()] * nimmgr.nimCount
154
155         def nimConfigModeChanged(slotid, configElement):
156                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
157         def nimDiseqcModeChanged(slotid, configElement):
158                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
159                 
160         def nimPortAChanged(slotid, configElement):
161                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
162         def nimPortBChanged(slotid, configElement):
163                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
164         def nimPortCChanged(slotid, configElement):
165                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
166         def nimPortDChanged(slotid, configElement):
167                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
168
169         for slot in nimmgr.nimslots:
170                 x = slot.slotid
171                 cname = nimmgr.getConfigPrefix(x)
172                 
173                 if slot.nimType == nimmgr.nimType["DVB-S"]:
174                         config.Nims[x].configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
175                         config.Nims[x].diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D", "Positioner"));
176                         config.Nims[x].diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
177                         config.Nims[x].diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
178                         config.Nims[x].diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
179                         config.Nims[x].diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
180                         config.Nims[x].longitude = configElement(cname + "longitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
181                         config.Nims[x].latitude = configElement(cname + "latitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
182                         
183                         #perhaps the instance of the slot is more useful?
184                         config.Nims[x].configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
185                         config.Nims[x].diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
186                         config.Nims[x].diseqcA.addNotifier(boundFunction(nimPortAChanged,x))
187                         config.Nims[x].diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
188                         config.Nims[x].diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
189                         config.Nims[x].diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
190                 else:
191                         print "pls add support for this frontend type!"         
192
193 nimmanager = NimManager()