Subversion Repositories ALCASAR

Rev

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

Rev 2775 Rev 2976
1
<?php
1
<?php
2
/**
2
/**
3
 * Nut class
3
 * Nut class
4
 *
4
 *
5
 * PHP version 5
5
 * PHP version 5
6
 *
6
 *
7
 * @category  PHP
7
 * @category  PHP
8
 * @package   PSI_UPS
8
 * @package   PSI_UPS
9
 * @author    Artem Volk <artvolk@mail.ru>
9
 * @author    Artem Volk <artvolk@mail.ru>
10
 * @author    Anders Häggström <hagge@users.sourceforge.net>
10
 * @author    Anders Häggström <hagge@users.sourceforge.net>
11
 * @copyright 2009 phpSysInfo
11
 * @copyright 2009 phpSysInfo
12
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
13
 * @version   SVN: $Id: class.nut.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @version   SVN: $Id: class.nut.inc.php 661 2012-08-27 11:26:39Z namiltd $
14
 * @link      http://phpsysinfo.sourceforge.net
14
 * @link      http://phpsysinfo.sourceforge.net
15
 */
15
 */
16
 /**
16
 /**
17
 * getting ups information from upsc program
17
 * getting ups information from upsc program
18
 *
18
 *
19
 * @category  PHP
19
 * @category  PHP
20
 * @package   PSI_UPS
20
 * @package   PSI_UPS
21
 * @author    Artem Volk <artvolk@mail.ru>
21
 * @author    Artem Volk <artvolk@mail.ru>
22
 * @author    Anders Häggström <hagge@users.sourceforge.net>
22
 * @author    Anders Häggström <hagge@users.sourceforge.net>
23
 * @copyright 2009 phpSysInfo
23
 * @copyright 2009 phpSysInfo
24
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
24
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
25
 * @version   Release: 3.0
25
 * @version   Release: 3.0
26
 * @link      http://phpsysinfo.sourceforge.net
26
 * @link      http://phpsysinfo.sourceforge.net
27
 */
27
 */
28
class Nut extends UPS
28
class Nut extends UPS
29
{
29
{
30
    /**
30
    /**
31
     * internal storage for all gathered data
31
     * internal storage for all gathered data
32
     *
32
     *
33
     * @var array
33
     * @var array
34
     */
34
     */
35
    private $_output = array();
35
    private $_output = array();
36
 
36
 
37
    /**
37
    /**
38
     * get all information from all configured ups and store output in internal array
38
     * get all information from all configured ups and store output in internal array
39
     */
39
     */
40
    public function __construct()
40
    public function __construct()
41
    {
41
    {
42
        parent::__construct();
42
        parent::__construct();
43
        if (defined('PSI_UPS_NUT_LIST') && is_string(PSI_UPS_NUT_LIST)) {
43
        if (defined('PSI_UPS_NUT_LIST') && is_string(PSI_UPS_NUT_LIST)) {
44
            if (preg_match(ARRAY_EXP, PSI_UPS_NUT_LIST)) {
44
            if (preg_match(ARRAY_EXP, PSI_UPS_NUT_LIST)) {
45
                $upses = eval(PSI_UPS_NUT_LIST);
45
                $upses = eval(PSI_UPS_NUT_LIST);
46
            } else {
46
            } else {
47
                $upses = array(PSI_UPS_NUT_LIST);
47
                $upses = array(PSI_UPS_NUT_LIST);
48
            }
48
            }
49
            foreach ($upses as $ups) {
49
            foreach ($upses as $ups) {
50
                CommonFunctions::executeProgram('upsc', '-l '.trim($ups), $output, PSI_DEBUG);
50
                CommonFunctions::executeProgram('upsc', '-l '.trim($ups), $output, PSI_DEBUG);
51
                $ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
51
                $ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
52
                foreach ($ups_names as $ups_name) {
52
                foreach ($ups_names as $ups_name) {
53
                    CommonFunctions::executeProgram('upsc', trim($ups_name).'@'.trim($ups), $temp, PSI_DEBUG);
53
                    CommonFunctions::executeProgram('upsc', trim($ups_name).'@'.trim($ups), $temp, PSI_DEBUG);
54
                    if (! empty($temp)) {
54
                    if (! empty($temp)) {
55
                        $this->_output[trim($ups_name).'@'.trim($ups)] = $temp;
55
                        $this->_output[trim($ups_name).'@'.trim($ups)] = $temp;
56
                    }
56
                    }
57
                }
57
                }
58
            }
58
            }
59
        } else { //use default if address and port not defined
59
        } else { //use default if address and port not defined
-
 
60
            if (!defined('PSI_EMU_HOSTNAME')) {
60
            CommonFunctions::executeProgram('upsc', '-l', $output, PSI_DEBUG);
61
                CommonFunctions::executeProgram('upsc', '-l', $output, PSI_DEBUG);
-
 
62
            } else {
-
 
63
                CommonFunctions::executeProgram('upsc', '-l '.PSI_EMU_HOSTNAME, $output, PSI_DEBUG);
-
 
64
            }
61
            $ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
65
            $ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
62
            foreach ($ups_names as $ups_name) {
66
            foreach ($ups_names as $ups_name) {
63
                CommonFunctions::executeProgram('upsc', trim($ups_name), $temp, PSI_DEBUG);
67
                CommonFunctions::executeProgram('upsc', trim($ups_name), $temp, PSI_DEBUG);
64
                if (! empty($temp)) {
68
                if (! empty($temp)) {
65
                    $this->_output[trim($ups_name)] = $temp;
69
                    $this->_output[trim($ups_name)] = $temp;
66
                }
70
                }
67
            }
71
            }
68
        }
72
        }
69
    }
73
    }
70
 
74
 
71
    /**
75
    /**
72
     * parse the input and store data in resultset for xml generation
76
     * parse the input and store data in resultset for xml generation
73
     *
77
     *
74
     * @return void
78
     * @return void
75
     */
79
     */
76
    private function _info()
80
    private function _info()
77
    {
81
    {
78
        if (! empty($this->_output)) {
82
        if (! empty($this->_output)) {
79
            foreach ($this->_output as $name => $value) {
83
            foreach ($this->_output as $name => $value) {
80
                $temp = preg_split("/\n/", $value, -1, PREG_SPLIT_NO_EMPTY);
84
                $temp = preg_split("/\n/", $value, -1, PREG_SPLIT_NO_EMPTY);
81
                $ups_data = array();
85
                $ups_data = array();
82
                foreach ($temp as $valueTemp) {
86
                foreach ($temp as $valueTemp) {
83
                    $line = preg_split('/: /', $valueTemp, 2);
87
                    $line = preg_split('/: /', $valueTemp, 2);
84
                    $ups_data[$line[0]] = isset($line[1]) ? trim($line[1]) : '';
88
                    $ups_data[$line[0]] = isset($line[1]) ? trim($line[1]) : '';
85
                }
89
                }
86
                $dev = new UPSDevice();
90
                $dev = new UPSDevice();
87
                //General
91
                //General
88
                $dev->setName($name);
92
                $dev->setName($name);
89
                if (isset($ups_data['ups.model'])) {
93
                if (isset($ups_data['ups.model'])) {
90
                    $dev->setModel($ups_data['ups.model']);
94
                    $dev->setModel($ups_data['ups.model']);
91
                }
95
                }
92
                if (isset($ups_data['driver.name'])) {
96
                if (isset($ups_data['driver.name'])) {
93
                    $dev->setMode($ups_data['driver.name']);
97
                    $dev->setMode($ups_data['driver.name']);
94
                }
98
                }
95
                if (isset($ups_data['ups.status'])) {
99
                if (isset($ups_data['ups.status'])) {
96
                    $dev->setStatus($ups_data['ups.status']);
100
                    $dev->setStatus($ups_data['ups.status']);
97
                }
101
                }
98
 
102
 
99
                //Line
103
                //Line
100
                if (isset($ups_data['input.voltage'])) {
104
                if (isset($ups_data['input.voltage'])) {
101
                    $dev->setLineVoltage($ups_data['input.voltage']);
105
                    $dev->setLineVoltage($ups_data['input.voltage']);
102
                }
106
                }
103
                if (isset($ups_data['input.frequency'])) {
107
                if (isset($ups_data['input.frequency'])) {
104
                    $dev->setLineFrequency($ups_data['input.frequency']);
108
                    $dev->setLineFrequency($ups_data['input.frequency']);
105
                }
109
                }
106
                if (isset($ups_data['ups.load'])) {
110
                if (isset($ups_data['ups.load'])) {
107
                    $dev->setLoad($ups_data['ups.load']);
111
                    $dev->setLoad($ups_data['ups.load']);
108
                }
112
                }
109
 
113
 
110
                //Battery
114
                //Battery
111
                if (isset($ups_data['battery.voltage'])) {
115
                if (isset($ups_data['battery.voltage'])) {
112
                    $dev->setBatteryVoltage($ups_data['battery.voltage']);
116
                    $dev->setBatteryVoltage($ups_data['battery.voltage']);
113
                }
117
                }
114
                if (isset($ups_data['battery.charge'])) {
118
                if (isset($ups_data['battery.charge'])) {
115
                    $dev->setBatterCharge($ups_data['battery.charge']);
119
                    $dev->setBatterCharge($ups_data['battery.charge']);
116
                }
120
                }
117
                if (isset($ups_data['battery.runtime'])) {
121
                if (isset($ups_data['battery.runtime'])) {
118
                    $dev->setTimeLeft(round($ups_data['battery.runtime']/60, 2));
122
                    $dev->setTimeLeft(round($ups_data['battery.runtime']/60, 2));
119
                }
123
                }
120
 
124
 
121
                //Temperature
125
                //Temperature
122
                if (isset($ups_data['ups.temperature'])) {
126
                if (isset($ups_data['ups.temperature'])) {
123
                    $dev->setTemperatur($ups_data['ups.temperature']);
127
                    $dev->setTemperatur($ups_data['ups.temperature']);
124
                }
128
                }
125
 
129
 
126
                $this->upsinfo->setUpsDevices($dev);
130
                $this->upsinfo->setUpsDevices($dev);
127
            }
131
            }
128
        }
132
        }
129
    }
133
    }
130
 
134
 
131
    /**
135
    /**
132
     * get the information
136
     * get the information
133
     *
137
     *
134
     * @see PSI_Interface_UPS::build()
138
     * @see PSI_Interface_UPS::build()
135
     *
139
     *
136
     * @return Void
140
     * @return Void
137
     */
141
     */
138
    public function build()
142
    public function build()
139
    {
143
    {
140
        $this->_info();
144
        $this->_info();
141
    }
145
    }
142
}
146
}
143
 
147