Subversion Repositories ALCASAR

Rev

Rev 2770 | Rev 3037 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2770 Rev 2976
1
<?php
1
<?php
2
/**
2
/**
3
 * Thermal Zone sensor class, getting information from Thermal Zone WMI class
3
 * Thermal Zone sensor class, getting information from Thermal Zone WMI class
4
 *
4
 *
5
 * PHP version 5
5
 * PHP version 5
6
 *
6
 *
7
 * @category  PHP
7
 * @category  PHP
8
 * @package   PSI_Sensor
8
 * @package   PSI_Sensor
9
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
9
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
10
 * @copyright 2014 phpSysInfo
10
 * @copyright 2014 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @version   Release: 3.0
12
 * @version   Release: 3.0
13
 * @link      http://phpsysinfo.sourceforge.net
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
14
 */
15
class ThermalZone extends Sensors
15
class ThermalZone extends Sensors
16
{
16
{
17
    /**
17
    /**
18
     * holds the COM object that we pull all the WMI data from
18
     * holds the COM object that we pull all the WMI data from
19
     *
19
     *
20
     * @var Object
20
     * @var Object
21
     */
21
     */
22
    private $_buf = array();
22
    private $_buf = array();
23
 
23
 
24
    /**
24
    /**
25
     * fill the private content var
25
     * fill the private content var
26
     */
26
     */
27
    public function __construct()
27
    public function __construct()
28
    {
28
    {
29
        parent::__construct();
29
        parent::__construct();
30
        if (PSI_OS == 'WINNT') {
30
        switch (defined('PSI_SENSOR_THERMALZONE_ACCESS')?strtolower(PSI_SENSOR_THERMALZONE_ACCESS):'command') {
31
            $_wmi = null;
31
        case 'command':
32
            try {
-
 
33
                // initialize the wmi object
32
            if ((PSI_OS == 'WINNT') || defined('PSI_EMU_HOSTNAME')) {
34
                $objLocator = new COM('WbemScripting.SWbemLocator');
33
                if (defined('PSI_EMU_HOSTNAME') || CommonFunctions::isAdmin()) {
35
                $_wmi = $objLocator->ConnectServer('', 'root\WMI');
34
                    $_wmi = CommonFunctions::initWMI('root\WMI', true);
36
            } catch (Exception $e) {
35
                    if ($_wmi) {
-
 
36
                        $this->_buf = CommonFunctions::getWMI($_wmi, 'MSAcpi_ThermalZoneTemperature', array('InstanceName', 'CriticalTripPoint', 'CurrentTemperature'));
-
 
37
                    }
-
 
38
                } else {
37
                $this->error->addError("WMI connect error", "PhpSysInfo can not connect to the WMI interface for ThermalZone data.");
39
                    $this->error->addError("Error reading data from thermalzone sensor", "Allowed only for systems with administrator privileges (run as administrator)");
-
 
40
                }
38
            }
41
            }
39
            if ($_wmi) {
42
            break;
-
 
43
        case 'data':
40
                $this->_buf = CommonFunctions::getWMI($_wmi, 'MSAcpi_ThermalZoneTemperature', array('InstanceName', 'CriticalTripPoint', 'CurrentTemperature'));
44
            if (!defined('PSI_EMU_HOSTNAME') && CommonFunctions::rfts(PSI_APP_ROOT.'/data/thermalzone.txt', $lines, 0, 4096, false)) { //output of "wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CriticalTripPoint,CurrentTemperature,InstanceName"
-
 
45
                $lines = trim(preg_replace('/[\x00-\x09\x0b-\x1F]/', '', $lines));
-
 
46
                $lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
-
 
47
                if ((($clines=count($lines)) > 1) && preg_match("/CriticalTripPoint\s+CurrentTemperature\s+InstanceName/i", $lines[0])) for ($i = 1; $i < $clines; $i++) {
-
 
48
                    $values = preg_split("/\s+/", trim($lines[$i]), -1, PREG_SPLIT_NO_EMPTY);
-
 
49
                    if (count($values)==3) {
-
 
50
                        $this->_buf[] = array('CriticalTripPoint'=>trim($values[0]), 'CurrentTemperature'=>trim($values[1]), 'InstanceName'=>trim($values[2]));
-
 
51
                    }
-
 
52
                }
41
            }
53
            }
-
 
54
            break;
-
 
55
        default:
-
 
56
            $this->error->addConfigError('__construct()', '[sensor_thermalzone] ACCESS');
-
 
57
            break;
42
        }
58
        }
43
    }
59
    }
44
 
60
 
45
    /**
61
    /**
46
     * get temperature information
62
     * get temperature information
47
     *
63
     *
48
     * @return void
64
     * @return void
49
     */
65
     */
50
    private function _temperature()
66
    private function _temperature()
51
    {
67
    {
-
 
68
        $mode = defined('PSI_SENSOR_THERMALZONE_ACCESS')?strtolower(PSI_SENSOR_THERMALZONE_ACCESS):'command';
-
 
69
        if ((($mode == 'command') && ((PSI_OS == 'WINNT') || defined('PSI_EMU_HOSTNAME')))
52
        if (PSI_OS == 'WINNT') {
70
           || (($mode == 'data') && !defined('PSI_EMU_HOSTNAME'))) {
53
            if ($this->_buf) foreach ($this->_buf as $buffer) {
71
            if ($this->_buf) foreach ($this->_buf as $buffer) {
54
                if (isset($buffer['CurrentTemperature']) && (($value = ($buffer['CurrentTemperature'] - 2732)/10) > -100)) {
72
                if (isset($buffer['CurrentTemperature']) && (($value = ($buffer['CurrentTemperature'] - 2732)/10) > -100)) {
55
                    $dev = new SensorDevice();
73
                    $dev = new SensorDevice();
56
                    if (isset($buffer['InstanceName']) && preg_match("/([^\\\\ ]+)$/", $buffer['InstanceName'], $outbuf)) {
74
                    if (isset($buffer['InstanceName']) && preg_match("/([^\\\\ ]+)$/", $buffer['InstanceName'], $outbuf)) {
57
                        $dev->setName('ThermalZone '.$outbuf[1]);
75
                        $dev->setName('ThermalZone '.$outbuf[1]);
58
                    } else {
76
                    } else {
59
                        $dev->setName('ThermalZone THM0_0');
77
                        $dev->setName('ThermalZone THM0_0');
60
                    }
78
                    }
61
                    $dev->setValue($value);
79
                    $dev->setValue($value);
62
                    if (isset($buffer['CriticalTripPoint']) && (($maxvalue = ($buffer['CriticalTripPoint'] - 2732)/10) > 0)) {
80
                    if (isset($buffer['CriticalTripPoint']) && (($maxvalue = ($buffer['CriticalTripPoint'] - 2732)/10) > 0)) {
63
                        $dev->setMax($maxvalue);
81
                        $dev->setMax($maxvalue);
64
                    }
82
                    }
65
                    $this->mbinfo->setMbTemp($dev);
83
                    $this->mbinfo->setMbTemp($dev);
66
                }
84
                }
67
            }
85
            }
68
        } else {
86
        } elseif (($mode == 'command') && (PSI_OS != 'WINNT') && !defined('PSI_EMU_HOSTNAME')) {
69
            $notwas = true;
87
            $notwas = true;
70
            $thermalzones = glob('/sys/class/thermal/thermal_zone*/');
88
            $thermalzones = glob('/sys/class/thermal/thermal_zone*/');
71
            if (is_array($thermalzones) && (count($thermalzones) > 0)) foreach ($thermalzones as $thermalzone) {
89
            if (is_array($thermalzones) && (count($thermalzones) > 0)) foreach ($thermalzones as $thermalzone) {
72
                $thermalzonetemp = $thermalzone.'temp';
90
                $thermalzonetemp = $thermalzone.'temp';
73
                $temp = null;
91
                $temp = null;
74
                if (CommonFunctions::rfts($thermalzonetemp, $temp, 1, 4096, false) && !is_null($temp) && (($temp = trim($temp)) != "")) {
92
                if (CommonFunctions::rfts($thermalzonetemp, $temp, 1, 4096, false) && !is_null($temp) && (($temp = trim($temp)) != "")) {
75
                    if ($temp >= 1000) {
93
                    if ($temp >= 1000) {
76
                        $div = 1000;
94
                        $div = 1000;
77
                    } elseif ($temp >= 200) {
95
                    } elseif ($temp >= 200) {
78
                        $div = 10;
96
                        $div = 10;
79
                    } else {
97
                    } else {
80
                       $div = 1;
98
                       $div = 1;
81
                    }
99
                    }
82
                    $temp = $temp / $div;
100
                    $temp = $temp / $div;
83
 
101
 
84
                    if ($temp > -40) {
102
                    if ($temp > -40) {
85
                        $dev = new SensorDevice();
103
                        $dev = new SensorDevice();
86
                        $dev->setValue($temp);
104
                        $dev->setValue($temp);
87
 
105
 
88
                        $temp_type = null;
106
                        $temp_type = null;
89
                        if (CommonFunctions::rfts($thermalzone.'type', $temp_type, 1, 4096, false) && !is_null($temp_type) && (($temp_type = trim($temp_type)) != "")) {
107
                        if (CommonFunctions::rfts($thermalzone.'type', $temp_type, 1, 4096, false) && !is_null($temp_type) && (($temp_type = trim($temp_type)) != "")) {
90
                            $dev->setName($temp_type);
108
                            $dev->setName($temp_type);
91
                        } else {
109
                        } else {
92
                            $dev->setName("ThermalZone");
110
                            $dev->setName("ThermalZone");
93
                        }
111
                        }
94
 
112
 
95
                        $temp_max = null;
113
                        $temp_max = null;
96
                        if (CommonFunctions::rfts($thermalzone.'trip_point_0_temp', $temp_max, 1, 4096, false) && !is_null($temp_max) && (($temp_max = trim($temp_max)) != "") && ($temp_max > -40)) {
114
                        if (CommonFunctions::rfts($thermalzone.'trip_point_0_temp', $temp_max, 1, 4096, false) && !is_null($temp_max) && (($temp_max = trim($temp_max)) != "") && ($temp_max > -40)) {
97
                            $temp_max = $temp_max / $div;
115
                            $temp_max = $temp_max / $div;
98
                            if (($temp_max != 0) || ($temp != 0)) { // if non-zero values
116
                            if (($temp_max != 0) || ($temp != 0)) { // if non-zero values
99
                                $dev->setMax($temp_max);
117
                                $dev->setMax($temp_max);
100
                                $this->mbinfo->setMbTemp($dev);
118
                                $this->mbinfo->setMbTemp($dev);
101
                            }
119
                            }
102
                        } else {
120
                        } else {
103
                            $this->mbinfo->setMbTemp($dev);
121
                            $this->mbinfo->setMbTemp($dev);
104
                        }
122
                        }
105
                        $notwas = false;
123
                        $notwas = false;
106
                    }
124
                    }
107
                }
125
                }
108
            }
126
            }
109
            if ($notwas) {
127
            if ($notwas) {
110
                $thermalzones = glob('/proc/acpi/thermal_zone/TH*/temperature');
128
                $thermalzones = glob('/proc/acpi/thermal_zone/TH*/temperature');
111
                if (is_array($thermalzones) && (count($thermalzones) > 0)) foreach ($thermalzones as $thermalzone) {
129
                if (is_array($thermalzones) && (count($thermalzones) > 0)) foreach ($thermalzones as $thermalzone) {
112
                    $temp = null;
130
                    $temp = null;
113
                    if (CommonFunctions::rfts($thermalzone, $temp, 1, 4096, false) && !is_null($temp) && (($temp = trim($temp)) != "")) {
131
                    if (CommonFunctions::rfts($thermalzone, $temp, 1, 4096, false) && !is_null($temp) && (($temp = trim($temp)) != "")) {
114
                        $dev = new SensorDevice();
132
                        $dev = new SensorDevice();
115
                        if (preg_match("/^\/proc\/acpi\/thermal_zone\/(.+)\/temperature$/", $thermalzone, $name)) {
133
                        if (preg_match("/^\/proc\/acpi\/thermal_zone\/(.+)\/temperature$/", $thermalzone, $name)) {
116
                           $dev->setName("ThermalZone ".$name[1]);
134
                           $dev->setName("ThermalZone ".$name[1]);
117
                        } else {
135
                        } else {
118
                            $dev->setName("ThermalZone");
136
                            $dev->setName("ThermalZone");
119
                        }
137
                        }
120
                        $dev->setValue(trim(substr($temp, 23, 4)));
138
                        $dev->setValue(trim(substr($temp, 23, 4)));
121
                        $this->mbinfo->setMbTemp($dev);
139
                        $this->mbinfo->setMbTemp($dev);
122
                    }
140
                    }
123
                }
141
                }
124
            }
142
            }
125
        }
143
        }
126
    }
144
    }
127
 
145
 
128
    /**
146
    /**
129
     * get the information
147
     * get the information
130
     *
148
     *
131
     * @see PSI_Interface_Sensor::build()
149
     * @see PSI_Interface_Sensor::build()
132
     *
150
     *
133
     * @return Void
151
     * @return Void
134
     */
152
     */
135
    public function build()
153
    public function build()
136
    {
154
    {
137
      $this->_temperature();
155
      $this->_temperature();
138
    }
156
    }
139
}
157
}
140
 
158