21644aace95265544824b67165f01cb1d0a56ee2
[openblackhole/openblackhole-enigma2.git] / lib / python / Components / Converter / FrontendInfo.py
1 from Components.Converter.Converter import Converter
2 from Components.Element import cached
3 from Components.config import config
4 from Components.NimManager import nimmanager
5
6 class FrontendInfo(Converter, object):
7         BER = 0
8         SNR = 1
9         AGC = 2
10         LOCK = 3
11         SNRdB = 4
12         SLOT_NUMBER = 5
13         TUNER_TYPE = 6
14         STRING = 7
15
16         def __init__(self, type):
17                 Converter.__init__(self, type)
18                 if type == "BER":
19                         self.type = self.BER
20                 elif type == "SNR":
21                         self.type = self.SNR
22                 elif type == "SNRdB":
23                         self.type = self.SNRdB
24                 elif type == "AGC":
25                         self.type = self.AGC
26                 elif type == "NUMBER":
27                         self.type = self.SLOT_NUMBER
28                 elif type == "TYPE":
29                         self.type = self.TUNER_TYPE
30                 elif type == "STRING":
31                         self.type = self.STRING
32                 else:
33                         self.type = self.LOCK
34
35         @cached
36         def getText(self):
37                 assert self.type not in (self.LOCK, self.SLOT_NUMBER), "the text output of FrontendInfo cannot be used for lock info"
38                 percent = None
39                 swapsnr = config.usage.swap_snr_on_osd.value
40                 if self.type == self.BER: # as count
41                         count = self.source.ber
42                         if count is not None:
43                                 return str(count)
44                         else:
45                                 return "N/A"
46                 elif self.type == self.AGC:
47                         percent = self.source.agc
48                 elif (self.type == self.SNR and not swapsnr) or (self.type == self.SNRdB and swapsnr):
49                         percent = self.source.snr
50                 elif self.type  == self.SNR or self.type == self.SNRdB:
51                         if self.source.snr_db is not None:
52                                 return "%3.01f dB" % (self.source.snr_db / 100.0)
53                         elif self.source.snr is not None: #fallback to normal SNR...
54                                 percent = self.source.snr
55                 elif self.type == self.TUNER_TYPE:
56                         return self.source.frontend_type and self.frontend_type or "Unknown"
57                 elif self.type == self.STRING:
58                         string = ""
59                         for n in nimmanager.nim_slots:
60                                 if n.type:
61                                         if string:
62                                                 string += " "
63                                         if n.slot == self.source.slot_number:
64                                                 string += "\c0000??00"
65                                         elif self.source.tuner_mask & 1 << n.slot:
66                                                 string += "\c00????00"
67                                         else:
68                                                 string += "\c007?7?7?"
69                                         string += chr(ord("A")+n.slot)
70                         return string
71                 if percent is None:
72                         return "N/A"
73                 return "%d %%" % (percent * 100 / 65536)
74
75         @cached
76         def getBool(self):
77                 assert self.type in (self.LOCK, self.BER), "the boolean output of FrontendInfo can only be used for lock or BER info"
78                 if self.type == self.LOCK:
79                         lock = self.source.lock
80                         if lock is None:
81                                 lock = False
82                         return lock
83                 else:
84                         ber = self.source.ber
85                         if ber is None:
86                                 ber = 0
87                         return ber > 0
88
89         text = property(getText)
90
91         boolean = property(getBool)
92
93         @cached
94         def getValue(self):
95                 assert self.type != self.LOCK, "the value/range output of FrontendInfo can not be used for lock info"
96                 if self.type == self.AGC:
97                         return self.source.agc or 0
98                 elif self.type == self.SNR:
99                         return self.source.snr or 0
100                 elif self.type == self.BER:
101                         if self.BER < self.range:
102                                 return self.BER or 0
103                         else:
104                                 return self.range
105                 elif self.type == self.TUNER_TYPE:
106                         type = self.source.frontend_type
107                         if type == 'DVB-S':
108                                 return 0
109                         elif type == 'DVB-C':
110                                 return 1
111                         elif type == 'DVB-T':
112                                 return 2
113                         elif type == 'ATSC':
114                                 return 3
115                         return -1
116                 elif self.type == self.SLOT_NUMBER:
117                         num = self.source.slot_number
118                         return num is None and -1 or num
119
120         range = 65536
121         value = property(getValue)