Merge commit 'e8eb8e9694379204' into experimental
[enigma2.git] / lib / python / Components / Sensors.py
1 from Components.FanControl import fancontrol
2
3 class Sensors:
4         # (type, name, unit, directory)
5         TYPE_TEMPERATURE = 0
6         # (type, name, unit, fanid)
7         TYPE_FAN_RPM = 1
8         
9         def __init__(self):
10                 # (type, name, unit, sensor_specific_dict/list)
11                 self.sensors_list = []
12                 self.addSensors()
13                 
14         def getSensorsCount(self, type = None):
15                 if type is None:
16                         return len(self.sensors_list)
17                 count = 0
18                 for sensor in self.sensors_list:
19                         if sensor[0] == type:
20                                 count += 1
21                 return count
22         
23         # returns a list of sensorids of type "type"
24         def getSensorsList(self, type = None):
25                 if type is None:
26                         return range(len(self.sensors_list))
27                 list = []
28                 for sensorid in range(len(self.sensors_list)):
29                         if self.sensors_list[sensorid][0] == type:
30                                 list.append(sensorid)
31                 return list
32         
33         
34         def getSensorType(self, sensorid):
35                 return self.sensors_list[sensorid][0]
36         
37         def getSensorName(self, sensorid):
38                 return self.sensors_list[sensorid][1]
39         
40         def getSensorValue(self, sensorid):
41                 value = -1
42                 sensor = self.sensors_list[sensorid]
43                 if sensor[0] == self.TYPE_TEMPERATURE:
44                         f = open("%s/value" % sensor[3], "r")
45                         value = int(f.readline().strip())
46                         f.close()
47                 elif sensor[0] == self.TYPE_FAN_RPM:
48                         value = fancontrol.getFanSpeed(sensor[3])
49                 return value
50         
51         def getSensorUnit(self, sensorid):
52                 return self.sensors_list[sensorid][2]
53
54         def addSensors(self):
55                 import os
56                 if os.path.exists("/proc/stb/sensors"):
57                         for dirname in os.listdir("/proc/stb/sensors"):
58                                 if dirname.find("temp", 0, 4) == 0:
59                                         f = open("/proc/stb/sensors/%s/name" % dirname, "r")
60                                         name = f.readline().strip()
61                                         f.close()
62                                         
63                                         f = open("/proc/stb/sensors/%s/unit" % dirname, "r")
64                                         unit = f.readline().strip()
65                                         f.close()
66                                         
67                                         self.sensors_list.append((self.TYPE_TEMPERATURE, name, unit, "/proc/stb/sensors/%s" % dirname))
68                 for fanid in range(fancontrol.getFanCount()):
69                         if fancontrol.hasRPMSensor(fanid):
70                                 self.sensors_list.append((self.TYPE_FAN_RPM, _("Fan %d") % (fanid + 1), "rpm", fanid))
71         
72 sensors = Sensors()