servicemp3.cpp: more simple/flexible streaming detection
[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                         lock = self.source.lock
60                         if lock is None:
61                                 lock = False
62                         return lock
63                 else:
64                         ber = self.source.ber
65                         if ber is None:
66                                 ber = 0
67                         return ber > 0
68
69         text = property(getText)
70
71         boolean = property(getBool)
72
73         @cached
74         def getValue(self):
75                 assert self.type != self.LOCK, "the value/range output of FrontendInfo can not be used for lock info"
76                 if self.type == self.AGC:
77                         return self.source.agc or 0
78                 elif self.type == self.SNR:
79                         return self.source.snr or 0
80                 elif self.type == self.BER:
81                         if self.BER < self.range:
82                                 return self.BER or 0
83                         else:
84                                 return self.range
85                 elif self.type == self.TUNER_TYPE:
86                         type = self.source.frontend_type
87                         if type == 'DVB-S':
88                                 return 0
89                         elif type == 'DVB-C':
90                                 return 1
91                         elif type == 'DVB-T':
92                                 return 2
93                         return -1
94                 elif self.type == self.SLOT_NUMBER:
95                         num = self.source.slot_number
96                         return num is None and -1 or num
97
98         range = 65536
99         value = property(getValue)