Subversion Repositories ALCASAR

Rev

Rev 3037 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2775 rexy 1
<?php
2
/**
3
 * Pmset class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_UPS
9
 * @author    Robert Pelletier <drizzt@menzonet.org>
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   SVN: $Id: class.nut.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting ups information from pmset program
17
 *
18
 * @category  PHP
19
 * @package   PSI_UPS
20
 * @author    Robert Pelletier <drizzt@menzonet.org>
21
 * @copyright 2014 phpSysInfo
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
25
 */
26
class Pmset extends UPS
27
{
28
    /**
29
     * internal storage for all gathered data
30
     *
31
     * @var array
32
     */
33
    private $_output = array();
34
 
35
    /**
36
     * get all information from all configured ups and store output in internal array
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
3037 rexy 41
        if (defined('PSI_UPS_PMSET_ACCESS') && (strtolower(trim(PSI_UPS_PMSET_ACCESS))==='data')) {
42
            if (CommonFunctions::rftsdata('upspmset.tmp', $temp)) {
43
                $this->_output[] = $temp;
44
            }
45
        } elseif (PSI_OS == 'Darwin') {
2976 rexy 46
            if (CommonFunctions::executeProgram('pmset', '-g batt', $temp) && !empty($temp)) {
47
                $this->_output[] = $temp;
48
            }
2775 rexy 49
        }
50
    }
51
 
52
    /**
53
     * parse the input and store data in resultset for xml generation
54
     *
55
     * @return void
56
     */
57
   private function _info()
58
    {
59
        if (empty($this->_output)) {
60
            return;
61
        }
62
        $model = array();
63
        $percCharge = array();
64
        $lines = explode(PHP_EOL, implode($this->_output));
65
        if (count($lines)>1) {
3287 rexy 66
            if (strpos($lines[1], 'InternalBattery') === false) {
2775 rexy 67
                $dev = new UPSDevice();
3287 rexy 68
                $dev->setName('UPS');
2775 rexy 69
                $percCharge = explode(';', $lines[1]);
3287 rexy 70
                $model = explode('FW:', $lines[1]);
2775 rexy 71
                if ($model !== false) {
72
                    $dev->setModel(substr(trim($model[0]), 1));
73
                }
74
                if ($percCharge !== false) {
3287 rexy 75
                    if (preg_match("/\s(\d+)\%$/", trim($percCharge[0]), $tmpbuf)) {
76
                        if ($tmpbuf[1]>100) {
77
                            $dev->setBatterCharge(100);
78
                        } else {
79
                            $dev->setBatterCharge($tmpbuf[1]);
80
                        }
2775 rexy 81
                    }
3287 rexy 82
                    $percCharge[1]=trim($percCharge[1]);
83
                    if (preg_match("/^(.+) present:/", $percCharge[1], $tmpbuf)) {
84
                        $dev->setStatus(trim($tmpbuf[1]));
85
                    } else {
86
                        $dev->setStatus($percCharge[1]);
87
                    }
88
                    if (isset($percCharge[2]) && preg_match("/\s(\d+):(\d+)\s/", $percCharge[2], $tmpbuf)) {
89
                         $dev->setTimeLeft($tmpbuf[1]*60+$tmpbuf[2]);
90
                    }
2775 rexy 91
                }
92
                $dev->setMode("pmset");
93
                $this->upsinfo->setUpsDevices($dev);
94
            }
95
        }
96
    }
97
 
98
    /**
99
     * get the information
100
     *
101
     * @see PSI_Interface_UPS::build()
102
     *
3037 rexy 103
     * @return void
2775 rexy 104
     */
105
    public function build()
106
    {
107
        $this->_info();
108
    }
109
}