1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BAT Plugin, which displays battery state |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PSI_Plugin_BAT |
7
|
|
|
* @author Erkan V |
8
|
|
|
* @copyright 2009 phpSysInfo |
9
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
10
|
|
|
* @version $Id: class.bat.inc.php 661 2012-08-27 11:26:39Z namiltd $ |
11
|
|
|
* @link http://phpsysinfo.sourceforge.net |
12
|
|
|
*/ |
13
|
|
|
class BAT extends PSI_Plugin |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* variable, which holds the content of the command |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $_filecontent = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* variable, which holds the result before the xml is generated out of this array |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $_result = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* read the data into an internal array and also call the parent constructor |
29
|
|
|
* |
30
|
|
|
* @param String $enc encoding |
31
|
|
|
*/ |
32
|
|
|
public function __construct($enc) |
33
|
|
|
{ |
34
|
|
|
parent::__construct(__CLASS__, $enc); |
35
|
|
|
switch (strtolower(PSI_PLUGIN_BAT_ACCESS)) { |
36
|
|
|
case 'command': |
37
|
|
|
if (PSI_OS == 'WINNT') { |
38
|
|
|
$_cim = null; //root\CIMv2 |
39
|
|
|
$_wmi = null; //root\WMI |
40
|
|
|
// don't set this params for local connection, it will not work |
41
|
|
|
$strHostname = ''; |
42
|
|
|
$strUser = ''; |
43
|
|
|
$strPassword = ''; |
44
|
|
|
try { |
45
|
|
|
// initialize the wmi object |
46
|
|
|
$objLocatorCIM = new COM('WbemScripting.SWbemLocator'); |
|
|
|
|
47
|
|
|
if ($strHostname == "") { |
48
|
|
|
$_cim = $objLocatorCIM->ConnectServer(); |
49
|
|
|
|
50
|
|
|
} else { |
51
|
|
|
$_cim = $objLocatorCIM->ConnectServer($strHostname, 'root\CIMv2', $strHostname.'\\'.$strUser, $strPassword); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// initialize the wmi object |
55
|
|
|
$objLocatorWMI = new COM('WbemScripting.SWbemLocator'); |
|
|
|
|
56
|
|
|
if ($strHostname == "") { |
57
|
|
|
$_wmi = $objLocatorWMI->ConnectServer($strHostname, 'root\WMI'); |
58
|
|
|
|
59
|
|
|
} else { |
60
|
|
|
$_wmi = $objLocatorWMI->ConnectServer($strHostname, 'root\WMI', $strHostname.'\\'.$strUser, $strPassword); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} catch (Exception $e) { |
64
|
|
|
$this->global_error->addError("WMI connect error", "PhpSysInfo can not connect to the WMI interface for security reasons.\nCheck an authentication mechanism for the directory where phpSysInfo is installed."); |
65
|
|
|
} |
66
|
|
|
$buffer_info = ''; |
67
|
|
|
$buffer_state = ''; |
68
|
|
|
$bufferWB = CommonFunctions::getWMI($_cim, 'Win32_Battery', array('EstimatedChargeRemaining', 'DesignVoltage', 'BatteryStatus', 'Chemistry')); |
69
|
|
|
if (sizeof($bufferWB)>0) { |
70
|
|
|
$capacity = ''; |
71
|
|
|
if (isset($bufferWB[0]['EstimatedChargeRemaining'])) { |
72
|
|
|
$capacity = $bufferWB[0]['EstimatedChargeRemaining']; |
73
|
|
|
} |
74
|
|
|
if (isset($bufferWB[0]['BatteryStatus'])) { |
75
|
|
|
switch ($bufferWB[0]['BatteryStatus']) { |
76
|
|
|
case 1: $batstat = 'Discharging'; break; |
|
|
|
|
77
|
|
|
case 2: $batstat = 'AC connected'; break; |
|
|
|
|
78
|
|
|
case 3: $batstat = 'Fully Charged'; break; |
|
|
|
|
79
|
|
|
case 4: $batstat = 'Low'; break; |
|
|
|
|
80
|
|
|
case 5: $batstat = 'Critical'; break; |
|
|
|
|
81
|
|
|
case 6: $batstat = 'Charging'; break; |
|
|
|
|
82
|
|
|
case 7: $batstat = 'Charging and High'; break; |
|
|
|
|
83
|
|
|
case 8: $batstat = 'Charging and Low'; break; |
|
|
|
|
84
|
|
|
case 9: $batstat = 'Charging and Critical'; break; |
|
|
|
|
85
|
|
|
case 10: $batstat = 'Undefined'; break; |
|
|
|
|
86
|
|
|
case 11: $batstat = 'Partially Charged'; break; |
|
|
|
|
87
|
|
|
default: $batstat = ''; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
if ($batstat != '') $buffer_state .= 'POWER_SUPPLY_STATUS='.$batstat."\n"; |
90
|
|
|
} |
91
|
|
|
$techn = ''; |
92
|
|
|
if (isset($bufferWB[0]['Chemistry'])) { |
93
|
|
View Code Duplication |
switch ($bufferWB[0]['Chemistry']) { |
|
|
|
|
94
|
|
|
case 1: $techn = 'Other'; break; |
|
|
|
|
95
|
|
|
case 2: $techn = 'Unknown'; break; |
|
|
|
|
96
|
|
|
case 3: $techn = 'PbAc'; break; |
|
|
|
|
97
|
|
|
case 4: $techn = 'NiCd'; break; |
|
|
|
|
98
|
|
|
case 5: $techn = 'NiMH'; break; |
|
|
|
|
99
|
|
|
case 6: $techn = 'Li-ion'; break; |
|
|
|
|
100
|
|
|
case 7: $techn = 'Zinc-air'; break; |
|
|
|
|
101
|
|
|
case 8: $techn = 'Li-poly'; break; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
$bufferWPB = CommonFunctions::getWMI($_cim, 'Win32_PortableBattery', array('DesignVoltage', 'Chemistry', 'DesignCapacity', 'FullChargeCapacity')); |
105
|
|
|
if (isset($bufferWPB[0]['DesignVoltage'])) { |
106
|
|
|
$buffer_info .= 'POWER_SUPPLY_VOLTAGE_MIN_DESIGN='.($bufferWPB[0]['DesignVoltage']*1000)."\n"; |
107
|
|
|
} |
108
|
|
|
// sometimes Chemistry from Win32_Battery returns 2 but Win32_PortableBattery returns e.g. 6 |
109
|
|
|
if ((($techn == '') || ($techn == 'Unknown')) && isset($bufferWPB[0]['Chemistry'])) { |
110
|
|
View Code Duplication |
switch ($bufferWPB[0]['Chemistry']) { |
|
|
|
|
111
|
|
|
case 1: $techn = 'Other'; break; |
|
|
|
|
112
|
|
|
case 2: $techn = 'Unknown'; break; |
|
|
|
|
113
|
|
|
case 3: $techn = 'PbAc'; break; |
|
|
|
|
114
|
|
|
case 4: $techn = 'NiCd'; break; |
|
|
|
|
115
|
|
|
case 5: $techn = 'NiMH'; break; |
|
|
|
|
116
|
|
|
case 6: $techn = 'Li-ion'; break; |
|
|
|
|
117
|
|
|
case 7: $techn = 'Zinc-air'; break; |
|
|
|
|
118
|
|
|
case 8: $techn = 'Li-poly'; break; |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
if ($techn != '') $buffer_info .= 'POWER_SUPPLY_TECHNOLOGY='.$techn."\n"; |
122
|
|
|
|
123
|
|
|
$bufferBS = CommonFunctions::getWMI($_wmi, 'BatteryStatus', array('RemainingCapacity', 'Voltage')); |
124
|
|
|
if (sizeof($bufferBS)>0) { |
125
|
|
|
if (isset($bufferBS[0]['RemainingCapacity']) && ($bufferBS[0]['RemainingCapacity']>0)) { |
126
|
|
|
$buffer_state .= 'POWER_SUPPLY_ENERGY_NOW='.($bufferBS[0]['RemainingCapacity']*1000)."\n"; |
127
|
|
|
$capacity = ''; |
128
|
|
|
} |
129
|
|
|
if (isset($bufferBS[0]['Voltage']) && ($bufferBS[0]['Voltage']>0)) { |
130
|
|
|
$buffer_state .= 'POWER_SUPPLY_VOLTAGE_NOW='.($bufferBS[0]['Voltage']*1000)."\n"; |
131
|
|
|
} elseif (isset($bufferWB[0]['DesignVoltage'])) { |
132
|
|
|
$buffer_state .= 'POWER_SUPPLY_VOLTAGE_NOW='.($bufferWB[0]['DesignVoltage']*1000)."\n"; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (!isset($bufferWPB[0]['FullChargeCapacity'])) { |
137
|
|
|
$bufferBFCC = CommonFunctions::getWMI($_wmi, 'BatteryFullChargedCapacity', array('FullChargedCapacity')); |
138
|
|
|
if ((sizeof($bufferBFCC)>0) && isset($bufferBFCC[0]['FullChargedCapacity'])) { |
139
|
|
|
$bufferWPB[0]['FullChargeCapacity'] = $bufferBFCC[0]['FullChargedCapacity']; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
if (isset($bufferWPB[0]['FullChargeCapacity'])) { |
143
|
|
|
$buffer_info .= 'POWER_SUPPLY_ENERGY_FULL='.($bufferWPB[0]['FullChargeCapacity']*1000)."\n"; |
144
|
|
View Code Duplication |
if ($capacity != '') $buffer_state .= 'POWER_SUPPLY_ENERGY_NOW='.(round($capacity*$bufferWPB[0]['FullChargeCapacity']*10)."\n"); |
|
|
|
|
145
|
|
|
if (isset($bufferWPB[0]['DesignCapacity']) && ($bufferWPB[0]['DesignCapacity']!=0)) |
146
|
|
|
$buffer_info .= 'POWER_SUPPLY_ENERGY_FULL_DESIGN='.($bufferWPB[0]['DesignCapacity']*1000)."\n"; |
147
|
|
|
} elseif (isset($bufferWPB[0]['DesignCapacity'])) { |
148
|
|
|
$buffer_info .= 'POWER_SUPPLY_ENERGY_FULL_DESIGN='.($bufferWPB[0]['DesignCapacity']*1000)."\n"; |
149
|
|
View Code Duplication |
if ($capacity != '') $buffer_state .= 'POWER_SUPPLY_ENERGY_NOW='.(round($capacity*$bufferWPB[0]['DesignCapacity']*10)."\n"); |
|
|
|
|
150
|
|
|
} else { |
151
|
|
|
if ($capacity != '') $buffer_state .= 'POWER_SUPPLY_CAPACITY='.$capacity."\n"; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$bufferBCC = CommonFunctions::getWMI($_wmi, 'BatteryCycleCount', array('CycleCount')); |
155
|
|
|
if ((sizeof($bufferBCC)>0) && isset($bufferBCC[0]['CycleCount']) && ($bufferBCC[0]['CycleCount']>0)) { |
156
|
|
|
$buffer_info .= 'POWER_SUPPLY_CYCLE_COUNT='.$bufferBCC[0]['CycleCount']."\n"; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} elseif (PSI_OS == 'Darwin') { |
160
|
|
|
$buffer_info = ''; |
161
|
|
|
$buffer_state = ''; |
162
|
|
|
CommonFunctions::executeProgram('ioreg', '-w0 -l -n AppleSmartBattery -r', $buffer_info, false); |
163
|
|
|
} elseif (PSI_OS == 'FreeBSD') { |
164
|
|
|
$buffer_info = ''; |
165
|
|
|
$buffer_state = ''; |
166
|
|
|
CommonFunctions::executeProgram('acpiconf', '-i batt', $buffer_info, false); |
167
|
|
|
} elseif (PSI_OS == 'OpenBSD') { |
168
|
|
|
$buffer_info = ''; |
169
|
|
|
$buffer_state = ''; |
170
|
|
|
CommonFunctions::executeProgram('sysctl', 'hw.sensors.acpibat0', $buffer_info, false); |
171
|
|
|
} else { |
172
|
|
|
$buffer_info = ''; |
173
|
|
|
$buffer_state = ''; |
174
|
|
|
$bat_name = PSI_PLUGIN_BAT_DEVICE; |
175
|
|
|
$rfts_bi = CommonFunctions::rfts('/proc/acpi/battery/'.$bat_name.'/info', $buffer_info, 0, 4096, false); |
176
|
|
|
$rfts_bs = CommonFunctions::rfts('/proc/acpi/battery/'.$bat_name.'/state', $buffer_state, 0, 4096, false); |
177
|
|
|
if (!$rfts_bi && !$rfts_bs) { |
178
|
|
|
$buffer_info = ''; |
179
|
|
|
$buffer_state = ''; |
180
|
|
|
if (!CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/uevent', $buffer_info, 0, 4096, false)) { |
181
|
|
|
if (CommonFunctions::rfts('/sys/class/power_supply/battery/uevent', $buffer_info, 0, 4096, false)) { |
182
|
|
|
$bat_name = 'battery'; |
183
|
|
|
} else { |
184
|
|
|
CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/uevent', $buffer_info, 0, 4096, PSI_DEBUG); // Once again but with debug |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/voltage_min_design', $buffer1, 1, 4096, false)) { |
|
|
|
|
188
|
|
|
$buffer_state .= 'POWER_SUPPLY_VOLTAGE_MIN_DESIGN='.$buffer1."\n"; |
189
|
|
|
} |
190
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/voltage_max_design', $buffer1, 1, 4096, false)) { |
|
|
|
|
191
|
|
|
$buffer_state .= 'POWER_SUPPLY_VOLTAGE_MAX_DESIGN='.$buffer1."\n"; |
192
|
|
|
} |
193
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/voltage_now', $buffer1, 1, 4096, false)) { |
|
|
|
|
194
|
|
|
$buffer_state .= 'POWER_SUPPLY_VOLTAGE_NOW='.$buffer1."\n"; |
195
|
|
|
} |
196
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/energy_full', $buffer1, 1, 4096, false)) { |
|
|
|
|
197
|
|
|
$buffer_state .= 'POWER_SUPPLY_ENERGY_FULL='.$buffer1."\n"; |
198
|
|
|
} |
199
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/energy_now', $buffer1, 1, 4096, false)) { |
|
|
|
|
200
|
|
|
$buffer_state .= 'POWER_SUPPLY_ENERGY_NOW='.$buffer1."\n"; |
201
|
|
|
} |
202
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/charge_full', $buffer1, 1, 4096, false)) { |
|
|
|
|
203
|
|
|
$buffer_state .= 'POWER_SUPPLY_CHARGE_FULL='.$buffer1."\n"; |
204
|
|
|
} |
205
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/charge_now', $buffer1, 1, 4096, false)) { |
|
|
|
|
206
|
|
|
$buffer_state .= 'POWER_SUPPLY_CHARGE_NOW='.$buffer1."\n"; |
207
|
|
|
} |
208
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/capacity', $buffer1, 1, 4096, false)) { |
|
|
|
|
209
|
|
|
$buffer_state .= 'POWER_SUPPLY_CAPACITY='.$buffer1; |
210
|
|
|
} |
211
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/technology', $buffer1, 1, 4096, false)) { |
|
|
|
|
212
|
|
|
$buffer_state .= 'POWER_SUPPLY_TECHNOLOGY='.$buffer1; |
213
|
|
|
} |
214
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/status', $buffer1, 1, 4096, false)) { |
|
|
|
|
215
|
|
|
$buffer_state .= 'POWER_SUPPLY_STATUS='.$buffer1; |
216
|
|
|
} |
217
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/batt_temp', $buffer1, 1, 4096, false)) { |
|
|
|
|
218
|
|
|
$buffer_state .= 'POWER_SUPPLY_TEMP='.$buffer1; |
219
|
|
|
} |
220
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/batt_vol', $buffer1, 1, 4096, false)) { |
|
|
|
|
221
|
|
|
$buffer_state .= 'POWER_SUPPLY_VOLTAGE_NOW='.$buffer1; |
222
|
|
|
} |
223
|
|
View Code Duplication |
if (CommonFunctions::rfts('/sys/class/power_supply/'.$bat_name.'/health', $buffer1, 1, 4096, false)) { |
|
|
|
|
224
|
|
|
$buffer_state .= 'POWER_SUPPLY_HEALTH='.$buffer1; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
break; |
229
|
|
|
case 'data': |
230
|
|
|
CommonFunctions::rfts(APP_ROOT."/data/bat_info.txt", $buffer_info); |
231
|
|
|
CommonFunctions::rfts(APP_ROOT."/data/bat_state.txt", $buffer_state); |
232
|
|
|
break; |
233
|
|
|
default: |
234
|
|
|
$this->global_error->addConfigError("__construct()", "PSI_PLUGIN_BAT_ACCESS"); |
235
|
|
|
break; |
236
|
|
|
} |
237
|
|
|
$this->_filecontent['info'] = preg_split("/\n/", $buffer_info, -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
238
|
|
|
$this->_filecontent['state'] = preg_split("/\n/", $buffer_state, -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* doing all tasks to get the required informations that the plugin needs |
243
|
|
|
* result is stored in an internal array |
244
|
|
|
* |
245
|
|
|
* @return void |
246
|
|
|
*/ |
247
|
|
|
public function execute() |
248
|
|
|
{ |
249
|
|
|
if (empty($this->_filecontent)) { |
250
|
|
|
return; |
251
|
|
|
} |
252
|
|
|
foreach ($this->_filecontent['info'] as $roworig) { |
253
|
|
|
$roworig = trim($roworig); |
254
|
|
|
if (preg_match('/^[dD]esign capacity:\s*(.*) (.*)$/', $roworig, $data)) { |
255
|
|
|
$bat['design_capacity'] = $data[1]; |
|
|
|
|
256
|
|
View Code Duplication |
if (!isset($bat['capacity_unit'])) { |
|
|
|
|
257
|
|
|
$bat['capacity_unit'] = trim($data[2]); |
|
|
|
|
258
|
|
|
} elseif ($bat['capacity_unit'] != trim($data[2])) { |
259
|
|
|
$bat['capacity_unit'] = "???"; |
260
|
|
|
} |
261
|
|
|
} elseif (preg_match('/^[lL]ast full capacity:\s*(.*) (.*)$/', $roworig, $data)) { |
262
|
|
|
$bat['full_capacity'] = $data[1]; |
263
|
|
View Code Duplication |
if (!isset($bat['capacity_unit'])) { |
|
|
|
|
264
|
|
|
$bat['capacity_unit'] = trim($data[2]); |
265
|
|
|
} elseif ($bat['capacity_unit'] != trim($data[2])) { |
266
|
|
|
$bat['capacity_unit'] = "???"; |
267
|
|
|
} |
268
|
|
|
} elseif (preg_match('/^cycle count:\s*(.*)$/', $roworig, $data) && ($data[1]>0)) { |
269
|
|
|
$bat['cycle_count'] = $data[1]; |
270
|
|
|
} elseif (preg_match('/^[dD]esign voltage:\s*(.*) (.*)$/', $roworig, $data)) { |
271
|
|
|
if ($data[2]=="mV") { // uV or mV detection |
272
|
|
|
$bat['design_voltage'] = $data[1]; |
273
|
|
|
} else { |
274
|
|
|
$bat['design_voltage'] = round($data[1]/1000); |
275
|
|
|
} |
276
|
|
|
} elseif (preg_match('/^battery type:\s*(.*)$/', $roworig, $data)) { |
277
|
|
|
$bat['battery_type'] = $data[1]; |
278
|
|
|
|
279
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_CYCLE_COUNT=(.*)$/', $roworig, $data) && ($data[1]>0)) { |
280
|
|
|
$bat['cycle_count'] = $data[1]; |
281
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_MIN_DESIGN=(.*)$/', $roworig, $data)) { |
282
|
|
|
if ($data[1]<100000) { // uV or mV detection |
283
|
|
|
$bat['design_voltage'] = $data[1]; |
284
|
|
|
} else { |
285
|
|
|
$bat['design_voltage'] = round($data[1]/1000); |
286
|
|
|
} |
287
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_MAX_DESIGN=(.*)$/', $roworig, $data)) { |
288
|
|
|
if ($data[1]<100000) { // uV or mV detection |
289
|
|
|
$bat['design_voltage_max'] = $data[1]; |
290
|
|
|
} else { |
291
|
|
|
$bat['design_voltage_max'] = round($data[1]/1000); |
292
|
|
|
} |
293
|
|
View Code Duplication |
} elseif (preg_match('/^POWER_SUPPLY_ENERGY_FULL=(.*)$/', $roworig, $data)) { |
|
|
|
|
294
|
|
|
$bat['full_capacity'] = ($data[1]/1000); |
295
|
|
|
if (!isset($bat['capacity_unit'])) { |
296
|
|
|
$bat['capacity_unit'] = "mWh"; |
297
|
|
|
} elseif ($bat['capacity_unit'] != "mWh") { |
298
|
|
|
$bat['capacity_unit'] = "???"; |
299
|
|
|
} |
300
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_CHARGE_FULL=(.*)$/', $roworig, $data)) { |
301
|
|
|
$bat['full_capacity'] = ($data[1]/1000); |
302
|
|
|
if (!isset($bat['capacity_unit'])) { |
303
|
|
|
$bat['capacity_unit'] = "mAh"; |
304
|
|
|
} elseif ($bat['capacity_unit'] != "mAh") { |
305
|
|
|
$bat['capacity_unit'] = "???"; |
306
|
|
|
} |
307
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_ENERGY_NOW=(.*)$/', $roworig, $data)) { |
308
|
|
|
if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == "mWh")) { |
309
|
|
|
$bat['capacity_unit'] = "mWh"; |
310
|
|
|
$bat['remaining_capacity'] = ($data[1]/1000); |
311
|
|
|
} |
312
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_CHARGE_NOW=(.*)$/', $roworig, $data)) { |
313
|
|
|
if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == "mAh")) { |
314
|
|
|
$bat['capacity_unit'] = "mAh"; |
315
|
|
|
$bat['remaining_capacity'] = ($data[1]/1000); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/* auxiary */ |
319
|
|
View Code Duplication |
} elseif (preg_match('/^POWER_SUPPLY_ENERGY_FULL_DESIGN=(.*)$/', $roworig, $data)) { |
|
|
|
|
320
|
|
|
$bat['design_capacity'] = ($data[1]/1000); |
321
|
|
|
if (!isset($bat['capacity_unit'])) { |
322
|
|
|
$bat['capacity_unit'] = "mWh"; |
323
|
|
|
} elseif ($bat['capacity_unit'] != "mWh") { |
324
|
|
|
$bat['capacity_unit'] = "???"; |
325
|
|
|
} |
326
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_CHARGE_FULL_DESIGN=(.*)$/', $roworig, $data)) { |
327
|
|
|
$bat['design_capacity'] = ($data[1]/1000); |
328
|
|
|
if (!isset($bat['capacity_unit'])) { |
329
|
|
|
$bat['capacity_unit'] = "mAh"; |
330
|
|
|
} elseif ($bat['capacity_unit'] != "mAh") { |
331
|
|
|
$bat['capacity_unit'] = "???"; |
332
|
|
|
} |
333
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_NOW=(.*)$/', $roworig, $data)) { |
334
|
|
|
if ($data[1]<100000) { // uV or mV detection |
335
|
|
|
$bat['present_voltage'] = $data[1]; |
336
|
|
|
} else { |
337
|
|
|
$bat['present_voltage'] = round($data[1]/1000); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_CAPACITY=(.*)$/', $roworig, $data)) { |
341
|
|
|
$bat['capacity'] = $data[1]; |
342
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_TEMP=(.*)$/', $roworig, $data)) { |
343
|
|
|
$bat['battery_temperature'] = $data[1]/10; |
344
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_TECHNOLOGY=(.*)$/', $roworig, $data)) { |
345
|
|
|
$bat['battery_type'] = $data[1]; |
346
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_STATUS=(.*)$/', $roworig, $data)) { |
347
|
|
|
$bat['charging_state'] = $data[1]; |
348
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_HEALTH=(.*)$/', $roworig, $data)) { |
349
|
|
|
$bat['battery_condition'] = $data[1]; |
350
|
|
|
|
351
|
|
|
/* Darwin */ |
352
|
|
|
} elseif (preg_match('/^"MaxCapacity"\s*=\s*(.*)$/', $roworig, $data)) { |
353
|
|
|
$bat['full_capacity'] = $data[1]; |
354
|
|
|
} elseif (preg_match('/^"CurrentCapacity"\s*=\s*(.*)$/', $roworig, $data)) { |
355
|
|
|
$bat['remaining_capacity'] = $data[1]; |
356
|
|
|
} elseif (preg_match('/^"Voltage"\s*=\s*(.*)$/', $roworig, $data)) { |
357
|
|
|
$bat['present_voltage'] = $data[1]; |
358
|
|
|
} elseif (preg_match('/^"BatteryType"\s*=\s*"(.*)"$/', $roworig, $data)) { |
359
|
|
|
$bat['battery_type'] = $data[1]; |
360
|
|
|
} elseif (preg_match('/^"Temperature"\s*=\s*(.*)$/', $roworig, $data)) { |
361
|
|
|
if ($data[1]>0) $bat['battery_temperature'] = $data[1]/100; |
362
|
|
|
} elseif (preg_match('/^"DesignCapacity"\s*=\s*(.*)$/', $roworig, $data)) { |
363
|
|
|
$bat['design_capacity'] = $data[1]; |
364
|
|
|
} elseif (preg_match('/^"CycleCount"\s*=\s*(.*)$/', $roworig, $data) && ($data[1]>0)) { |
365
|
|
|
$bat['cycle_count'] = $data[1]; |
366
|
|
|
/* auxiary */ |
367
|
|
|
} elseif (preg_match('/^"FullyCharged"\s*=\s*Yes$/', $roworig, $data)) { |
368
|
|
|
$bat['charging_state_f'] = true; |
369
|
|
|
} elseif (preg_match('/^"IsCharging"\s*=\s*Yes$/', $roworig, $data)) { |
370
|
|
|
$bat['charging_state_i'] = true; |
371
|
|
|
} elseif (preg_match('/^"ExternalConnected"\s*=\s*Yes$/', $roworig, $data)) { |
372
|
|
|
$bat['charging_state_e'] = true; |
373
|
|
|
|
374
|
|
|
/* FreeBSD */ |
375
|
|
|
} elseif (preg_match('/^Type:\s*(.*)$/', $roworig, $data)) { |
376
|
|
|
$bat['battery_type'] = $data[1]; |
377
|
|
|
} elseif (preg_match('/^State:\s*(.*)$/', $roworig, $data)) { |
378
|
|
|
$bat['charging_state'] = $data[1]; |
379
|
|
|
} elseif (preg_match('/^Present voltage:\s*(.*) (.*)$/', $roworig, $data)) { |
380
|
|
|
if ($data[2]=="mV") { // uV or mV detection |
381
|
|
|
$bat['present_voltage'] = $data[1]; |
382
|
|
|
} else { |
383
|
|
|
$bat['present_voltage'] = round($data[1]/1000); |
384
|
|
|
} |
385
|
|
|
} elseif (preg_match('/^Voltage:\s*(.*) (.*)$/', $roworig, $data)) { |
386
|
|
|
if ($data[2]=="mV") { // uV or mV detection |
387
|
|
|
$bat['present_voltage'] = $data[1]; |
388
|
|
|
} else { |
389
|
|
|
$bat['present_voltage'] = round($data[1]/1000); |
390
|
|
|
} |
391
|
|
|
} elseif (preg_match('/^Remaining capacity:\s*(.*)%$/', $roworig, $data)) { |
392
|
|
|
$bat['capacity'] = $data[1]; |
393
|
|
|
|
394
|
|
|
/* OpenBSD */ |
395
|
|
|
} elseif (preg_match('/^hw.sensors.acpibat0.volt0=(.*) VDC \(voltage\)$/', $roworig, $data)) { |
396
|
|
|
$bat['design_voltage'] = 1000*$data[1]; |
397
|
|
|
} elseif (preg_match('/^hw.sensors.acpibat0.volt1=(.*) VDC \(current voltage\)$/', $roworig, $data)) { |
398
|
|
|
$bat['present_voltage'] = 1000*$data[1]; |
399
|
|
View Code Duplication |
} elseif (preg_match('/^hw.sensors.acpibat0.watthour0=(.*) Wh \(last full capacity\)$/', $roworig, $data)) { |
|
|
|
|
400
|
|
|
$bat['full_capacity'] = 1000*$data[1]; |
401
|
|
|
if (!isset($bat['capacity_unit'])) { |
402
|
|
|
$bat['capacity_unit'] = "mWh"; |
403
|
|
|
} elseif ($bat['capacity_unit'] != "mWh") { |
404
|
|
|
$bat['capacity_unit'] = "???"; |
405
|
|
|
} |
406
|
|
|
} elseif (preg_match('/^hw.sensors.acpibat0.watthour4=(.*) Wh \(design capacity\)$/', $roworig, $data)) { |
407
|
|
|
$bat['design_capacity'] = 1000*$data[1]; |
408
|
|
|
if (!isset($bat['capacity_unit'])) { |
409
|
|
|
$bat['capacity_unit'] = "mWh"; |
410
|
|
|
} elseif ($bat['capacity_unit'] != "mWh") { |
411
|
|
|
$bat['capacity_unit'] = "???"; |
412
|
|
|
} |
413
|
|
View Code Duplication |
} elseif (preg_match('/^hw.sensors.acpibat0.watthour3=(.*) Wh \(remaining capacity\)/', $roworig, $data)) { |
|
|
|
|
414
|
|
|
$bat['remaining_capacity'] = 1000*$data[1]; |
415
|
|
|
if (!isset($bat['capacity_unit'])) { |
416
|
|
|
$bat['capacity_unit'] = "mWh"; |
417
|
|
|
} elseif ($bat['capacity_unit'] != "mWh") { |
418
|
|
|
$bat['capacity_unit'] = "???"; |
419
|
|
|
} |
420
|
|
|
} elseif (preg_match('/^hw.sensors.acpibat0.raw0=.* \((.*)\)/', $roworig, $data)) { |
421
|
|
|
$bat['charging_state'] = $data[1]; |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
foreach ($this->_filecontent['state'] as $roworig) { |
425
|
|
|
$roworig = trim($roworig); |
426
|
|
|
if (preg_match('/^remaining capacity:\s*(.*) (.*)$/', $roworig, $data)) { |
427
|
|
View Code Duplication |
if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == trim($data[2]))) { |
|
|
|
|
428
|
|
|
$bat['capacity_unit'] = trim($data[2]); |
429
|
|
|
$bat['remaining_capacity'] = $data[1]; |
430
|
|
|
} |
431
|
|
|
} elseif (preg_match('/^present voltage:\s*(.*) (.*)$/', $roworig, $data)) { |
432
|
|
|
if ($data[2]=="mV") { // uV or mV detection |
433
|
|
|
$bat['present_voltage'] = $data[1]; |
434
|
|
|
} else { |
435
|
|
|
$bat['present_voltage'] = round($data[1]/1000); |
436
|
|
|
} |
437
|
|
|
} elseif (preg_match('/^charging state:\s*(.*)$/', $roworig, $data)) { |
438
|
|
|
$bat['charging_state'] = $data[1]; |
439
|
|
|
|
440
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_MIN_DESIGN=(.*)$/', $roworig, $data)) { |
441
|
|
|
if ($data[1]<100000) { // uV or mV detection |
442
|
|
|
$bat['design_voltage'] = $data[1]; |
443
|
|
|
} else { |
444
|
|
|
$bat['design_voltage'] = round($data[1]/1000); |
445
|
|
|
} |
446
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_MAX_DESIGN=(.*)$/', $roworig, $data)) { |
447
|
|
|
if ($data[1]<100000) { // uV or mV detection |
448
|
|
|
$bat['design_voltage_max'] = $data[1]; |
449
|
|
|
} else { |
450
|
|
|
$bat['design_voltage_max'] = round($data[1]/1000); |
451
|
|
|
} |
452
|
|
View Code Duplication |
} elseif (preg_match('/^POWER_SUPPLY_ENERGY_FULL=(.*)$/', $roworig, $data)) { |
|
|
|
|
453
|
|
|
$bat['full_capacity'] = ($data[1]/1000); |
454
|
|
|
if (!isset($bat['capacity_unit'])) { |
455
|
|
|
$bat['capacity_unit'] = "mWh"; |
456
|
|
|
} elseif ($bat['capacity_unit'] != "mWh") { |
457
|
|
|
$bat['capacity_unit'] = "???"; |
458
|
|
|
} |
459
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_CHARGE_FULL=(.*)$/', $roworig, $data)) { |
460
|
|
|
$bat['full_capacity'] = ($data[1]/1000); |
461
|
|
|
if (!isset($bat['capacity_unit'])) { |
462
|
|
|
$bat['capacity_unit'] = "mAh"; |
463
|
|
|
} elseif ($bat['capacity_unit'] != "mAh") { |
464
|
|
|
$bat['capacity_unit'] = "???"; |
465
|
|
|
} |
466
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_ENERGY_NOW=(.*)$/', $roworig, $data)) { |
467
|
|
|
if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == "mWh")) { |
468
|
|
|
$bat['capacity_unit'] = "mWh"; |
469
|
|
|
$bat['remaining_capacity'] = ($data[1]/1000); |
470
|
|
|
} |
471
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_CHARGE_NOW=(.*)$/', $roworig, $data)) { |
472
|
|
|
if (!isset($bat['capacity_unit']) || ($bat['capacity_unit'] == "mAh")) { |
473
|
|
|
$bat['capacity_unit'] = "mAh"; |
474
|
|
|
$bat['remaining_capacity'] = ($data[1]/1000); |
475
|
|
|
} |
476
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_VOLTAGE_NOW=(.*)$/', $roworig, $data)) { |
477
|
|
|
if ($data[1]<100000) { // uV or mV detection |
478
|
|
|
$bat['present_voltage'] = $data[1]; |
479
|
|
|
} else { |
480
|
|
|
$bat['present_voltage'] = round($data[1]/1000); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_CAPACITY=(.*)$/', $roworig, $data)) { |
484
|
|
|
$bat['capacity'] = $data[1]; |
485
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_TEMP=(.*)$/', $roworig, $data)) { |
486
|
|
|
$bat['battery_temperature'] = $data[1]/10; |
487
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_TECHNOLOGY=(.*)$/', $roworig, $data)) { |
488
|
|
|
$bat['battery_type'] = $data[1]; |
489
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_STATUS=(.*)$/', $roworig, $data)) { |
490
|
|
|
$bat['charging_state'] = $data[1]; |
491
|
|
|
} elseif (preg_match('/^POWER_SUPPLY_HEALTH=(.*)$/', $roworig, $data)) { |
492
|
|
|
$bat['battery_condition'] = $data[1]; |
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
if (isset($bat)) $this->_result[0] = $bat; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* generates the XML content for the plugin |
501
|
|
|
* |
502
|
|
|
* @return SimpleXMLElement entire XML content for the plugin |
503
|
|
|
*/ |
504
|
|
|
public function xml() |
505
|
|
|
{ |
506
|
|
|
foreach ($this->_result as $bat_item) { |
507
|
|
|
$xmlbat = $this->xml->addChild("Bat"); |
508
|
|
|
if ((!isset($bat_item['remaining_capacity']) || (isset($bat_item['full_capacity']) && ($bat_item['full_capacity'] == 0))) && |
509
|
|
|
isset($bat_item['capacity']) && ($bat_item['capacity']>=0)) { |
510
|
|
|
if (isset($bat_item['capacity_unit']) && ($bat_item['capacity_unit'] !== "???") |
511
|
|
|
&& (isset($bat_item['full_capacity']) && ($bat_item['full_capacity'] > 0))) { |
512
|
|
|
$xmlbat->addAttribute("CapacityUnit", $bat_item['capacity_unit']); |
513
|
|
|
$xmlbat->addAttribute("RemainingCapacity", round($bat_item['capacity']*$bat_item['full_capacity']/100)); |
514
|
|
|
$xmlbat->addAttribute("FullCapacity", $bat_item['full_capacity']); |
515
|
|
|
if (isset($bat_item['design_capacity'])) { |
516
|
|
|
$xmlbat->addAttribute("DesignCapacity", $bat_item['design_capacity']); |
517
|
|
|
} |
518
|
|
|
} else { |
519
|
|
|
$xmlbat->addAttribute("FullCapacity", 100); |
520
|
|
|
$xmlbat->addAttribute("RemainingCapacity", $bat_item['capacity']); |
521
|
|
|
$xmlbat->addAttribute("CapacityUnit", "%"); |
522
|
|
|
} |
523
|
|
|
} else { |
524
|
|
|
if (isset($bat_item['full_capacity'])) { |
525
|
|
|
if (isset($bat_item['design_capacity'])) { |
526
|
|
|
$xmlbat->addAttribute("DesignCapacity", $bat_item['design_capacity']); |
527
|
|
|
} |
528
|
|
|
$xmlbat->addAttribute("FullCapacity", $bat_item['full_capacity']); |
529
|
|
|
} elseif (isset($bat_item['design_capacity'])) { |
530
|
|
|
$xmlbat->addAttribute("FullCapacity", $bat_item['design_capacity']); |
531
|
|
|
} |
532
|
|
|
if (isset($bat_item['remaining_capacity'])) { |
533
|
|
|
$xmlbat->addAttribute("RemainingCapacity", $bat_item['remaining_capacity']); |
534
|
|
|
} |
535
|
|
|
if (isset($bat_item['capacity_unit'])) { |
536
|
|
|
$xmlbat->addAttribute("CapacityUnit", $bat_item['capacity_unit']); |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
if (isset($bat_item['design_voltage'])) { |
540
|
|
|
$xmlbat->addAttribute("DesignVoltage", $bat_item['design_voltage']); |
541
|
|
|
if (isset($bat_item['design_voltage_max']) && ($bat_item['design_voltage_max'] != $bat_item['design_voltage'])) { |
542
|
|
|
$xmlbat->addAttribute("DesignVoltageMax", $bat_item['design_voltage_max']); |
543
|
|
|
} |
544
|
|
|
} elseif (isset($bat_item['design_voltage_max'])) { |
545
|
|
|
$xmlbat->addAttribute("DesignVoltage", $bat_item['design_voltage_max']); |
546
|
|
|
} |
547
|
|
|
if (isset($bat_item['present_voltage'])) { |
548
|
|
|
$xmlbat->addAttribute("PresentVoltage", $bat_item['present_voltage']); |
549
|
|
|
} |
550
|
|
|
if (isset($bat_item['charging_state'])) { |
551
|
|
|
$xmlbat->addAttribute("ChargingState", $bat_item['charging_state']); |
552
|
|
|
} else { |
553
|
|
|
if (isset($bat_item['charging_state_i'])) { |
554
|
|
|
$xmlbat->addAttribute("ChargingState", 'Charging'); |
555
|
|
|
} elseif (!isset($bat_item['charging_state_e'])) { |
556
|
|
|
$xmlbat->addAttribute("ChargingState", 'Discharging'); |
557
|
|
|
} elseif (isset($bat_item['charging_state_f'])) { |
558
|
|
|
$xmlbat->addAttribute("ChargingState", 'Fully Charged'); |
559
|
|
|
} else { |
560
|
|
|
$xmlbat->addAttribute("ChargingState", 'Unknown state'); |
561
|
|
|
} |
562
|
|
|
} |
563
|
|
|
if (isset($bat_item['battery_type'])) { |
564
|
|
|
$xmlbat->addAttribute("BatteryType", $bat_item['battery_type']); |
565
|
|
|
} |
566
|
|
|
if (isset($bat_item['battery_temperature'])) { |
567
|
|
|
$xmlbat->addAttribute("BatteryTemperature", $bat_item['battery_temperature']); |
568
|
|
|
} |
569
|
|
|
if (isset($bat_item['battery_condition'])) { |
570
|
|
|
$xmlbat->addAttribute("BatteryCondition", $bat_item['battery_condition']); |
571
|
|
|
} |
572
|
|
|
if (isset($bat_item['cycle_count'])) { |
573
|
|
|
$xmlbat->addAttribute("CycleCount", $bat_item['cycle_count']); |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
return $this->xml->getSimpleXmlElement(); |
|
|
|
|
578
|
|
|
} |
579
|
|
|
} |
580
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.