Subversion Repositories ALCASAR

Rev

Rev 2976 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

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