-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpower_model.php
executable file
·375 lines (343 loc) · 14.1 KB
/
power_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
use CFPropertyList\CFPropertyList;
class Power_model extends \Model
{
public function __construct($serial = '')
{
parent::__construct('id', 'power'); //primary key, tablename
$this->rs['id'] = '';
$this->rs['serial_number'] = $serial;
$this->rs['manufacture_date'] = '';
$this->rs['design_capacity'] = 0;
$this->rs['max_capacity'] = 0;
$this->rs['max_percent'] = 0;
$this->rs['current_capacity'] = 0;
$this->rs['current_percent'] = 0;
$this->rs['cycle_count'] = 0;
$this->rs['temperature'] = 0;
$this->rs['condition'] = '';
$this->rs['sleep_prevented_by'] = '';
$this->rs['hibernatefile'] = '';
$this->rs['schedule'] = '';
$this->rs['adapter_id'] = '';
$this->rs['family_code'] = '';
$this->rs['adapter_serial_number'] = '';
$this->rs['combined_sys_load'] = '';
$this->rs['user_sys_load'] = '';
$this->rs['thermal_level'] = '';
$this->rs['battery_level'] = '';
$this->rs['ups_name'] = '';
$this->rs['active_profile'] = '';
$this->rs['ups_charging_status'] = '';
$this->rs['externalconnected'] = '';
$this->rs['cellvoltage'] = '';
$this->rs['manufacturer'] = '';
$this->rs['batteryserialnumber'] = '';
$this->rs['fullycharged'] = '';
$this->rs['ischarging'] = '';
$this->rs['standbydelay'] = 0;
$this->rs['standby'] = 0;
$this->rs['womp'] = 0;
$this->rs['halfdim'] = 0;
$this->rs['gpuswitch'] = 0;
$this->rs['sms'] = 0;
$this->rs['networkoversleep'] = 0;
$this->rs['disksleep'] = 0;
$this->rs['sleep'] = 0;
$this->rs['autopoweroffdelay'] = 0;
$this->rs['hibernatemode'] = 0;
$this->rs['autopoweroff'] = 0;
$this->rs['ttyskeepawake'] = 0;
$this->rs['displaysleep'] = 0;
$this->rs['acwake'] = 0;
$this->rs['lidwake'] = 0;
$this->rs['sleep_on_power_button'] = 0;
$this->rs['autorestart'] = 0;
$this->rs['destroyfvkeyonstandby'] = 0;
$this->rs['powernap'] = 0;
$this->rs['haltlevel'] = 0;
$this->rs['haltafter'] = 0;
$this->rs['haltremain'] = 0;
$this->rs['lessbright'] = 0;
$this->rs['sleep_count'] = 0;
$this->rs['dark_wake_count'] = 0;
$this->rs['user_wake_count'] = 0;
$this->rs['wattage'] = 0;
$this->rs['backgroundtask'] = 0;
$this->rs['applepushservicetask'] = 0;
$this->rs['userisactive'] = 0;
$this->rs['preventuseridledisplaysleep'] = 0;
$this->rs['preventsystemsleep'] = 0;
$this->rs['externalmedia'] = 0;
$this->rs['preventuseridlesystemsleep'] = 0;
$this->rs['networkclientactive'] = 0;
$this->rs['cpu_scheduler_limit'] = 0;
$this->rs['cpu_available_cpus'] = 0;
$this->rs['cpu_speed_limit'] = 0;
$this->rs['ups_percent'] = 0;
$this->rs['timeremaining'] = 0;
$this->rs['instanttimetoempty'] = 0;
$this->rs['voltage'] = 0.0;
$this->rs['permanentfailurestatus'] = 0;
$this->rs['packreserve'] = 0;
$this->rs['avgtimetofull'] = 0;
$this->rs['amperage'] = 0.0;
$this->rs['designcyclecount'] = 0;
$this->rs['avgtimetoempty'] = 0;
$this->rs['adapter_current'] = 0;
$this->rs['adapter_voltage'] = 0;
$this->rs['charging_current'] = 0;
$this->rs['charging_voltage'] = 0;
$this->rs['adapter_manufacturer'] = '';
$this->rs['adapter_name'] = '';
if ($serial) {
$this->retrieve_record($serial);
}
$this->serial = $serial;
}
/**
* Get Power statistics
*
*
**/
public function get_stats()
{
$sql = "SELECT COUNT(CASE WHEN max_percent>89 THEN 1 END) AS success,
COUNT(CASE WHEN max_percent BETWEEN 80 AND 89 THEN 1 END) AS warning,
COUNT(CASE WHEN max_percent BETWEEN 0 AND 79 THEN 1 END) AS danger
FROM power
LEFT JOIN reportdata USING(serial_number)
".get_machine_group_filter();
return current($this->query($sql));
}
// ------------------------------------------------------------------------
/**
* Process data sent by postflight
*
* @param string data
*
**/
public function process($data)
{
// If data is empty, throw error
if (! $data) {
throw new Exception("Error Processing Power Module Request: No data found", 1);
}
// Switch between old style and xml style reports
if (substr( $data, 0, 30 ) == '<?xml version="1.0" encoding="' ) {
$this->_process_xml($data);
} else {
$this->_process_legacy($data);
}
// Massage data into the right format for the database
$this->_adjust_battery_data();
$this->save();
}
/**
* Process legacy
*
*
* @param Array $data Data array
**/
private function _process_legacy($data)
{
// Translate network strings to db fields
$translate = [
'manufacture_date = ' => 'manufacture_date',
'design_capacity = ' => 'design_capacity',
'max_capacity = ' => 'max_capacity',
'max_percent = ' => 'max_percent',
'current_capacity = ' => 'current_capacity',
'cycle_count = ' => 'cycle_count',
'temperature = ' => 'temperature',
'condition = ' => 'condition',
];
// Reset values
$this->manufacture_date = '';
$this->design_capacity = 1;
$this->max_capacity = 1;
$this->max_percent = 100;
$this->current_capacity = 0;
$this->current_percent = 0;
$this->cycle_count = 0;
$this->temperature = 0;
$this->condition = '';
$this->timestamp = 0;
// Parse data
foreach(explode("\n", $data) as $line) {
// Translate standard entries
foreach($translate as $search => $field) {
if(strpos($line, $search) === 0) {
$value = substr($line, strlen($search));
$this->$field = $value;
break;
}
}
} // End foreach explode lines
}
/**
* Process xml
*
*
* @param Array $data Client data
**/
private function _process_xml($data)
{
// Process incoming powerinfo.xml
$parser = new CFPropertyList();
$parser->parse($data, CFPropertyList::FORMAT_XML);
$plist = $parser->toArray();
// Translate battery strings to db fields
$translate = array(
'ManufactureDate' => 'manufacture_date',
'DesignCapacity' => 'design_capacity',
'CurrentCapacity' => 'current_capacity',
'CycleCount' => 'cycle_count',
'Temperature' => 'temperature',
'MaxCapacity' => 'max_capacity',
'condition' => 'condition',
'standbydelay' => 'standbydelay',
'standby' => 'standby',
'womp' => 'womp',
'halfdim' => 'halfdim',
'hibernatefile' => 'hibernatefile',
'gpuswitch' => 'gpuswitch',
'sms' => 'sms',
'networkoversleep' => 'networkoversleep',
'disksleep' => 'disksleep',
'sleep' => 'sleep',
'autopoweroffdelay' => 'autopoweroffdelay',
'hibernatemode' => 'hibernatemode',
'autopoweroff' => 'autopoweroff',
'ttyskeepawake' => 'ttyskeepawake',
'displaysleep' => 'displaysleep',
'acwake' => 'acwake',
'lidwake' => 'lidwake',
'SleepOn' => 'sleep_on_power_button',
'powernap' => 'powernap',
'autorestart' => 'autorestart',
'DestroyFVKeyOnStandby' => 'destroyfvkeyonstandby',
'schedule' => 'schedule',
'haltlevel' => 'haltlevel',
'haltafter' => 'haltafter',
'haltremain' => 'haltremain',
'lessbright' => 'lessbright',
'SleepCount' => 'sleep_count',
'DarkWake' => 'dark_wake_count',
'UserWake' => 'user_wake_count',
'attage' => 'wattage', // This is spelled correctly
'AdapterID' => 'adapter_id',
'FamilyCode' => 'family_code',
'SerialNumber' => 'adapter_serial_number',
'adapter_current' => 'adapter_current',
'adapter_voltage' => 'adapter_voltage',
'adapter_manufacturer' => 'adapter_manufacturer',
'adapter_name' => 'adapter_name',
'charging_current' => 'charging_current',
'charging_voltage' => 'charging_voltage',
'CPUSchedulerLimit' => 'cpu_scheduler_limit',
'CPUAvailableCPUs' => 'cpu_available_cpus',
'CPUSpeedLimit' => 'cpu_speed_limit',
'BackgroundTask' => 'backgroundtask',
'ApplePushServiceTask' => 'applepushservicetask',
'UserIsActive' => 'userisactive',
'PreventUserIdleDisplaySleep' => 'preventuseridledisplaysleep',
'PreventSystemSleep' => 'preventsystemsleep',
'ExternalMedia' => 'externalmedia',
'PreventUserIdleSystemSleep' => 'preventuseridlesystemsleep',
'NetworkClientActive' => 'networkclientactive',
'combinedlevel' => 'combined_sys_load',
'user' => 'user_sys_load',
'battery' => 'battery_level',
'thermal' => 'thermal_level',
'UPSName' => 'ups_name',
'Nowdrawing' => 'active_profile',
'UPSPercent' => 'ups_percent',
'UPSStatus' => 'ups_charging_status',
'ExternalConnected' => 'externalconnected',
'TimeRemaining' => 'timeremaining',
'InstantTimeToEmpty' => 'instanttimetoempty',
'CellVoltage' => 'cellvoltage',
'Voltage' => 'voltage',
'PermanentFailureStatus' => 'permanentfailurestatus',
'Manufacturer' => 'manufacturer',
'PackReserve' => 'packreserve',
'AvgTimeToFull' => 'avgtimetofull',
'BatterySerialNumber' => 'batteryserialnumber',
'AmperagemA' => 'amperage',
'FullyCharged' => 'fullycharged',
'IsCharging' => 'ischarging',
'DesignCycleCount9C' => 'designcyclecount',
'AvgTimeToEmpty' => 'avgtimetoempty',
'sleep_prevented_by' => 'sleep_prevented_by'
);
// Array of strings
$strings = array('manufacture_date', 'condition', 'hibernatefile', 'adapter_id', 'family_code', 'adapter_serial_number', 'adapter_manufacturer', 'adapter_name', 'combined_sys_load', 'user_sys_load', 'thermal_level', 'battery_level', 'ups_name', 'active_profile', 'ups_charging_status', 'externalconnected', 'cellvoltage', 'manufacturer', 'batteryserialnumber', 'fullycharged', 'ischarging','sleep_prevented_by','schedule');
// Traverse the xml with translations
foreach ($translate as $search => $field) {
// If key does not exist in $plist, null it
if ( ! array_key_exists($search, $plist)) {
$this->$field = null;
// Else if is not a string and is numeric, save the value
} else if ( ! in_array($field, $strings) && is_numeric($plist[$search])) {
$this->$field = $plist[$search];
// Else if a string, save the value
} else if ( in_array($field, $strings)) {
$this->$field = $plist[$search];
// Else null the field
} else {
$this->$field = null;
}
}
}
/**
* Adjust battery data
*
**/
private function _adjust_battery_data()
{
// Check if no battery is inserted and adjust values
if ( $this->condition == "No Battery" || $this->condition == "") {
$this->manufacture_date = null;
$this->design_capacity = null;
$this->max_capacity = null;
$this->max_percent = null;
$this->current_percent = null;
$this->current_capacity = null;
$this->cycle_count = null;
$this->temperature = null;
} else {
// Calculate maximum capacity as percentage of original capacity
if ( $this->design_capacity > 0) {
$this->max_percent = round(($this->max_capacity / $this->design_capacity * 100 ), 0 );
} else {
$this->max_percent = 0;
}
// Calculate percentage of current maximum capacity
if ( $this->max_capacity > 0) {
$this->current_percent = round(($this->current_capacity / $this->max_capacity * 100 ), 0 );
} else {
$this->current_percent = 0;
}
// Convert battery manufacture date to calendar date.
// Bits 0...4 => day (value 1-31; 5 bits)
// Bits 5...8 => month (value 1-12; 4 bits)
// Bits 9...15 => years since 1980 (value 0-127; 7 bits)
$ManufactureDate = $this->manufacture_date;
if (is_numeric($this->manufacture_date)) {
$mfgday = $this->manufacture_date & 31;
$mfgmonth = ($this->manufacture_date >> 5) & 15;
$mfgyear = (($this->manufacture_date >> 9) & 127) + 1980;
$this->manufacture_date = sprintf("%d-%02d-%02d", $mfgyear, $mfgmonth, $mfgday);
} else {
// Hardcode the date to be 'null' for the time being until
// data from BigSur can be evaluated correctly.
$this->manufacture_date = null;
}
}
// Timestamp added by the server
$this->timestamp = time();
// Fix condition
$this->condition = str_replace(array('Normal','Check Battery','ServiceBattery','Service Battery','ReplaceSoon','Replace Soon','ReplaceNow','Replace Now'),array('Good','Service','Service','Service','Fair','Fair','Poor','Poor'), $this->condition);
}
}