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