~ubuntu-branches/ubuntu/maverick/awn-extras-applets/maverick

« back to all changes in this revision

Viewing changes to applets/maintained/hardware-sensors/interfaces/i8ksensors.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2010-08-29 14:29:52 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100829142952-rhvuetyms9bv5uu7
Tags: upstream-0.4.0+bzr1372
ImportĀ upstreamĀ versionĀ 0.4.0+bzr1372

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#coding: utf-8
 
3
#
 
4
#   Copyright 2008-2010 Grega Podlesek <grega.podlesek@gmail.com>
 
5
#
 
6
#   This program is free software; you can redistribute it and/or modify
 
7
#   it under the terms of the GNU General Public License as published by
 
8
#   the Free Software Foundation; either version 2 of the License, or
 
9
#   (at your option) any later version.
 
10
#
 
11
#   This program is distributed in the hope that it will be useful,
 
12
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#   GNU General Public License for more details.
 
15
#
 
16
#   You should have received a copy of the GNU General Public License
 
17
#   along with this program; if not, write to the Free Software
 
18
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
19
#   MA 02110-1301, USA.
 
20
 
 
21
import os.path
 
22
 
 
23
from sensorinterface import Sensor
 
24
from sensorinterface import Updater
 
25
from sensorvalues.tempvalue import TempValue
 
26
from sensorvalues.rpmvalue import RPMValue
 
27
 
 
28
i8k_sensors_path = "/proc/i8k"
 
29
 
 
30
interface_name = "i8k"
 
31
 
 
32
class I8kSensor(Sensor):
 
33
    '''
 
34
    Sensors from i8kutils
 
35
    '''
 
36
 
 
37
    def __init__(self, id, name, updater, value):
 
38
        Sensor.__init__(self, id, name, value)
 
39
        self.updater = updater
 
40
        self.interface = interface_name
 
41
 
 
42
    def read_sensor(self):
 
43
        i8k_output = self.updater.get_update()
 
44
        if len(i8k_output) != 10:
 
45
            self.value = -273
 
46
            return False
 
47
 
 
48
        self.value = i8k_output[self.id]
 
49
        return True
 
50
 
 
51
 
 
52
def get_sensors(timeout=1):
 
53
    i8k_output = get_i8kutil_ouput()
 
54
    if len(i8k_output) != 10:
 
55
        return []
 
56
 
 
57
    updater = Updater(timeout, get_i8kutil_ouput)
 
58
 
 
59
    try:
 
60
        i8k_sensors = []
 
61
        # If value is < 0, BIOS is not reporting it, and the value is invalid
 
62
        if int(i8k_output[3]) > 0:
 
63
            i8k_sensors.append(I8kSensor(3, "CPU temp", updater, TempValue()))
 
64
        if int(i8k_output[4]) > 0:
 
65
            i8k_sensors.append(I8kSensor(6, "Left fan", updater, RPMValue()))
 
66
        if int(i8k_output[5]) > 0:
 
67
            i8k_sensors.append(I8kSensor(7, "Right fan", updater, RPMValue()))
 
68
 
 
69
        return i8k_sensors
 
70
    except ValueError:
 
71
        # This happends if one of the above strings is not an int
 
72
        return []
 
73
 
 
74
def get_i8kutil_ouput():
 
75
    """
 
76
    Return the content of /proc/i8k in a list of the form:
 
77
    
 
78
    1.0 A17 2J59L02 52 2 1 8040 6420 1 2
 
79
    |   |   |       |  | | |    |    | |
 
80
    |   |   |       |  | | |    |    | +------- 10. buttons status
 
81
    |   |   |       |  | | |    |    +--------- 9.  ac status
 
82
    |   |   |       |  | | |    +-------------- 8.  right fan rpm
 
83
    |   |   |       |  | | +------------------- 7.  left fan rpm
 
84
    |   |   |       |  | +--------------------- 6.  right fan status
 
85
    |   |   |       |  +----------------------- 5.  left fan status
 
86
    |   |   |       +-------------------------- 4.  CPU temperature (Celsius)
 
87
    |   |   +---------------------------------- 3.  serial number
 
88
    |   +-------------------------------------- 2.  BIOS version
 
89
    +------------------------------------------ 1.  /proc/i8k format version
 
90
    
 
91
    A negative value, for example -22, indicates that the BIOS doesn't return
 
92
    the corresponding information. This is normal on some models/bioses.
 
93
    
 
94
    The above was copied from i8kutils' README for convenience
 
95
    
 
96
    """
 
97
    if os.path.exists(i8k_sensors_path):
 
98
        try:
 
99
            sensorfile = open(i8k_sensors_path, 'r')
 
100
        except IOError, (errno, errst):
 
101
            print "i8k sensor interface:", "I/O error(%s): %s" % (errno, errst)
 
102
            return None
 
103
        output = sensorfile.read().split()
 
104
        sensorfile.close()
 
105
        return output
 
106
    else:
 
107
        return []