Subversion Repositories ALCASAR

Rev

Rev 2770 | Rev 3037 | 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')) {
33
                if (defined('PSI_EMU_HOSTNAME') || CommonFunctions::isAdmin()) {
34
                    $_wmi = CommonFunctions::initWMI('root\WMI', true);
35
                    if ($_wmi) {
36
                        $this->_buf = CommonFunctions::getWMI($_wmi, 'MSAcpi_ThermalZoneTemperature', array('InstanceName', 'CriticalTripPoint', 'CurrentTemperature'));
37
                    }
38
                } else {
39
                    $this->error->addError("Error reading data from thermalzone sensor", "Allowed only for systems with administrator privileges (run as administrator)");
40
                }
2770 rexy 41
            }
2976 rexy 42
            break;
43
        case 'data':
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
                }
2770 rexy 53
            }
2976 rexy 54
            break;
55
        default:
56
            $this->error->addConfigError('__construct()', '[sensor_thermalzone] ACCESS');
57
            break;
2770 rexy 58
        }
59
    }
60
 
61
    /**
62
     * get temperature information
63
     *
64
     * @return void
65
     */
66
    private function _temperature()
67
    {
2976 rexy 68
        $mode = defined('PSI_SENSOR_THERMALZONE_ACCESS')?strtolower(PSI_SENSOR_THERMALZONE_ACCESS):'command';
69
        if ((($mode == 'command') && ((PSI_OS == 'WINNT') || defined('PSI_EMU_HOSTNAME')))
70
           || (($mode == 'data') && !defined('PSI_EMU_HOSTNAME'))) {
2770 rexy 71
            if ($this->_buf) foreach ($this->_buf as $buffer) {
72
                if (isset($buffer['CurrentTemperature']) && (($value = ($buffer['CurrentTemperature'] - 2732)/10) > -100)) {
73
                    $dev = new SensorDevice();
74
                    if (isset($buffer['InstanceName']) && preg_match("/([^\\\\ ]+)$/", $buffer['InstanceName'], $outbuf)) {
75
                        $dev->setName('ThermalZone '.$outbuf[1]);
76
                    } else {
77
                        $dev->setName('ThermalZone THM0_0');
78
                    }
79
                    $dev->setValue($value);
80
                    if (isset($buffer['CriticalTripPoint']) && (($maxvalue = ($buffer['CriticalTripPoint'] - 2732)/10) > 0)) {
81
                        $dev->setMax($maxvalue);
82
                    }
83
                    $this->mbinfo->setMbTemp($dev);
84
                }
85
            }
2976 rexy 86
        } elseif (($mode == 'command') && (PSI_OS != 'WINNT') && !defined('PSI_EMU_HOSTNAME')) {
2770 rexy 87
            $notwas = true;
88
            $thermalzones = glob('/sys/class/thermal/thermal_zone*/');
89
            if (is_array($thermalzones) && (count($thermalzones) > 0)) foreach ($thermalzones as $thermalzone) {
90
                $thermalzonetemp = $thermalzone.'temp';
91
                $temp = null;
92
                if (CommonFunctions::rfts($thermalzonetemp, $temp, 1, 4096, false) && !is_null($temp) && (($temp = trim($temp)) != "")) {
93
                    if ($temp >= 1000) {
94
                        $div = 1000;
95
                    } elseif ($temp >= 200) {
96
                        $div = 10;
97
                    } else {
98
                       $div = 1;
99
                    }
100
                    $temp = $temp / $div;
101
 
102
                    if ($temp > -40) {
103
                        $dev = new SensorDevice();
104
                        $dev->setValue($temp);
105
 
106
                        $temp_type = null;
107
                        if (CommonFunctions::rfts($thermalzone.'type', $temp_type, 1, 4096, false) && !is_null($temp_type) && (($temp_type = trim($temp_type)) != "")) {
108
                            $dev->setName($temp_type);
109
                        } else {
110
                            $dev->setName("ThermalZone");
111
                        }
112
 
113
                        $temp_max = null;
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)) {
115
                            $temp_max = $temp_max / $div;
116
                            if (($temp_max != 0) || ($temp != 0)) { // if non-zero values
117
                                $dev->setMax($temp_max);
118
                                $this->mbinfo->setMbTemp($dev);
119
                            }
120
                        } else {
121
                            $this->mbinfo->setMbTemp($dev);
122
                        }
123
                        $notwas = false;
124
                    }
125
                }
126
            }
127
            if ($notwas) {
128
                $thermalzones = glob('/proc/acpi/thermal_zone/TH*/temperature');
129
                if (is_array($thermalzones) && (count($thermalzones) > 0)) foreach ($thermalzones as $thermalzone) {
130
                    $temp = null;
131
                    if (CommonFunctions::rfts($thermalzone, $temp, 1, 4096, false) && !is_null($temp) && (($temp = trim($temp)) != "")) {
132
                        $dev = new SensorDevice();
133
                        if (preg_match("/^\/proc\/acpi\/thermal_zone\/(.+)\/temperature$/", $thermalzone, $name)) {
134
                           $dev->setName("ThermalZone ".$name[1]);
135
                        } else {
136
                            $dev->setName("ThermalZone");
137
                        }
138
                        $dev->setValue(trim(substr($temp, 23, 4)));
139
                        $this->mbinfo->setMbTemp($dev);
140
                    }
141
                }
142
            }
143
        }
144
    }
145
 
146
    /**
147
     * get the information
148
     *
149
     * @see PSI_Interface_Sensor::build()
150
     *
151
     * @return Void
152
     */
153
    public function build()
154
    {
155
      $this->_temperature();
156
    }
157
}