56c671894eae995ea8b8576413edb4882028d6b0
[openblackhole/openblackhole-enigma2.git] / lib / python / Tools / GetEcmInfo.py
1 import os
2 import time
3
4 ECM_INFO = '/tmp/ecm.info'
5 EMPTY_ECM_INFO = '','0','0','0'
6
7 old_ecm_time = time.time()
8 info = {}
9 ecm = ''
10 data = EMPTY_ECM_INFO
11
12 class GetEcmInfo:
13         def pollEcmData(self):
14                 global data
15                 global old_ecm_time
16                 global info
17                 global ecm
18                 try:
19                         ecm_time = os.stat(ECM_INFO).st_mtime
20                 except:
21                         ecm_time = old_ecm_time
22                         data = EMPTY_ECM_INFO
23                         info = {}
24                         ecm = ''
25                 if ecm_time != old_ecm_time:
26                         oecmi1 = info.get('ecminterval1','')
27                         oecmi0 = info.get('ecminterval0','')
28                         info = {}
29                         info['ecminterval2'] = oecmi1
30                         info['ecminterval1'] = oecmi0
31                         old_ecm_time = ecm_time
32                         try:
33                                 ecm = open(ECM_INFO, 'rb').readlines()
34                         except:
35                                 ecm = ''
36                         for line in ecm:
37                                 d = line.split(':', 1)
38                                 if len(d) > 1:
39                                         info[d[0].strip()] = d[1].strip()
40                         data = self.getText()
41                 else:
42                         info['ecminterval0'] = int(time.time()-ecm_time+0.5)
43
44         def getEcmData(self):
45                 self.pollEcmData()
46                 return data
47
48         def getInfo(self, member, ifempty = ''):
49                 self.pollEcmData()
50                 return str(info.get(member, ifempty))
51
52         def getText(self):
53                 global ecm
54                 try:
55                         # info is dictionary
56                         using = info.get('using', '')
57                         if using:
58                                 # CCcam
59                                 if using == 'fta':
60                                         self.textvalue = _("FTA")
61                                 elif using == 'emu':
62                                         self.textvalue = "EMU (%ss)" % (info.get('ecm time', '?'))
63                                 else:
64                                         hops = info.get('hops', None)
65                                         if hops and hops != '0':
66                                                 hops = ' @' + hops
67                                         else:
68                                                 hops = ''
69                                         self.textvalue = info.get('address', '?') + hops + " (%ss)" % info.get('ecm time', '?')
70                         else:
71                                 decode = info.get('decode', None)
72                                 if decode:
73                                         # gbox (untested)
74                                         if info['decode'] == 'Network':
75                                                 cardid = 'id:' + info.get('prov', '')
76                                                 try:
77                                                         share = open('/tmp/share.info', 'rb').readlines()
78                                                         for line in share:
79                                                                 if cardid in line:
80                                                                         self.textvalue = line.strip()
81                                                                         break
82                                                         else:
83                                                                 self.textvalue = cardid
84                                                 except:
85                                                         self.textvalue = decode
86                                         else:
87                                                 self.textvalue = decode
88                                         if ecm[1].startswith('SysID'):
89                                                 info['prov'] = ecm[1].strip()[6:]
90                                         if info['response'] and 'CaID 0x' in ecm[0] and 'pid 0x' in ecm[0]:
91                                                 self.textvalue = self.textvalue + " (0.%ss)" % info['response']
92                                                 info['caid'] = ecm[0][ecm[0].find('CaID 0x')+7:ecm[0].find(',')]
93                                                 info['pid'] = ecm[0][ecm[0].find('pid 0x')+6:ecm[0].find(' =')]
94                                                 info['provid'] = info.get('prov', '0')[:4]
95                                 else:
96                                         source = info.get('source', None)
97                                         if source:
98                                                 # MGcam
99                                                 info['caid'] = info['caid'][2:]
100                                                 info['pid'] = info['pid'][2:]
101                                                 info['provid'] = info['prov'][2:]
102                                                 time = ""
103                                                 for line in ecm:
104                                                         if line.find('msec') != -1:
105                                                                 line = line.split(' ')
106                                                                 if line[0]:
107                                                                         time = " (%ss)" % (float(line[0])/1000)
108                                                                         continue
109                                                 self.textvalue = source + time
110                                         else:
111                                                 reader = info.get('reader', '')
112                                                 if reader:
113                                                         hops = info.get('hops', None)
114                                                         if hops and hops != '0':
115                                                                 hops = ' @' + hops
116                                                         else:
117                                                                 hops = ''
118                                                         self.textvalue = reader + hops + " (%ss)" % info.get('ecm time', '?')
119                                                 else:
120                                                         self.textvalue = ""
121                         decCI = info.get('caid', '0')
122                         provid = info.get('provid', '0')
123                         if provid == '0':
124                                 provid = info.get('prov', '0')
125                         ecmpid = info.get('pid', '0')
126                 except:
127                         ecm = None
128                         self.textvalue = ""
129                         decCI='0'
130                         provid='0'
131                         ecmpid='0'
132                 return self.textvalue,decCI,provid,ecmpid