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