add possibility to get frontend type and frontend number
[enigma2.git] / lib / python / Components / Converter / FrontendInfo.py
1 from Components.Converter.Converter import Converter
2 from Components.Element import cached
3
4 class FrontendInfo(Converter, object):
5         BER = 0
6         SNR = 1
7         AGC = 2
8         LOCK = 3
9         SNRdB = 4
10         SLOT_NUMBER = 5
11         TUNER_TYPE = 6
12
13         def __init__(self, type):
14                 Converter.__init__(self, type)
15                 if type == "BER":
16                         self.type = self.BER
17                 elif type == "SNR":
18                         self.type = self.SNR
19                 elif type == "SNRdB":
20                         self.type = self.SNRdB
21                 elif type == "AGC":
22                         self.type = self.AGC
23                 elif type == "NUMBER":
24                         self.type = self.SLOT_NUMBER
25                 elif type == "TYPE":
26                         self.type = self.TUNER_TYPE
27                 else:
28                         self.type = self.LOCK
29
30         @cached
31         def getText(self):
32                 assert self.type not in [self.LOCK, self.SLOT_NUMBER], "the text output of FrontendInfo cannot be used for lock info"
33                 percent = None
34                 if self.type == self.BER: # as count
35                         count = self.source.ber
36                         if count is not None:
37                                 return str(count)
38                         else:
39                                 return "N/A"
40                 elif self.type == self.AGC:
41                         percent = self.source.agc
42                 elif self.type == self.SNR:
43                         percent = self.source.snr
44                 elif self.type == self.SNRdB:
45                         if self.source.snr_db is not None:
46                                 return "%3.02f dB" % (self.source.snr_db / 100.0)
47                         elif self.source.snr is not None: #fallback to normal SNR...
48                                 percent = self.source.snr
49                 elif self.type == self.TUNER_TYPE:
50                         return self.source.frontend_type and self.frontend_type or "Unknown"
51                 if percent is None:
52                         return "N/A"
53                 return "%d %%" % (percent * 100 / 65536)
54
55         @cached
56         def getBool(self):
57                 assert self.type in [self.LOCK, self.BER], "the boolean output of FrontendInfo can only be used for lock or BER info"
58                 if self.type == self.LOCK:
59                         return self.source.lock
60                 else:
61                         return self.source.ber > 0
62
63         text = property(getText)
64
65         boolean = property(getBool)
66
67         @cached
68         def getValue(self):
69                 assert self.type != self.LOCK, "the value/range output of FrontendInfo can not be used for lock info"
70                 if self.type == self.AGC:
71                         return self.source.agc or 0
72                 elif self.type == self.SNR:
73                         return self.source.snr or 0
74                 elif self.type == self.BER:
75                         if self.BER < self.range:
76                                 return self.BER or 0
77                         else:
78                                 return self.range
79                 elif self.type == self.TUNER_TYPE:
80                         type = self.source.frontend_type
81                         if type == 'DVB-S':
82                                 return 0
83                         elif type == 'DVB-C':
84                                 return 1
85                         elif type == 'DVB-T':
86                                 return 2
87                         return -1
88                 elif self.type == self.SLOT_NUMBER:
89                         return self.source.slot_number or -1
90
91         range = 65536
92         value = property(getValue)