systeminformation 5.9.7 → 5.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/battery.js CHANGED
@@ -1,309 +1,309 @@
1
- 'use strict';
2
- // @ts-check;
3
- // ==================================================================================
4
- // battery.js
5
- // ----------------------------------------------------------------------------------
6
- // Description: System Information - library
7
- // for Node.js
8
- // Copyright: (c) 2014 - 2021
9
- // Author: Sebastian Hildebrandt
10
- // ----------------------------------------------------------------------------------
11
- // License: MIT
12
- // ==================================================================================
13
- // 6. Battery
14
- // ----------------------------------------------------------------------------------
15
-
16
- const exec = require('child_process').exec;
17
- const fs = require('fs');
18
- const util = require('./util');
19
-
20
- let _platform = process.platform;
21
-
22
- const _linux = (_platform === 'linux');
23
- const _darwin = (_platform === 'darwin');
24
- const _windows = (_platform === 'win32');
25
- const _freebsd = (_platform === 'freebsd');
26
- const _openbsd = (_platform === 'openbsd');
27
- const _netbsd = (_platform === 'netbsd');
28
- const _sunos = (_platform === 'sunos');
29
-
30
- function parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity) {
31
- const result = {};
32
- let status = util.getValue(lines, 'BatteryStatus', '=').trim();
33
- // 1 = "Discharging"
34
- // 2 = "On A/C"
35
- // 3 = "Fully Charged"
36
- // 4 = "Low"
37
- // 5 = "Critical"
38
- // 6 = "Charging"
39
- // 7 = "Charging High"
40
- // 8 = "Charging Low"
41
- // 9 = "Charging Critical"
42
- // 10 = "Undefined"
43
- // 11 = "Partially Charged"
44
- if (status >= 0) {
45
- const statusValue = status ? parseInt(status) : 0;
46
- result.status = statusValue;
47
- result.hasBattery = true;
48
- result.maxCapacity = fullChargeCapacity || parseInt(util.getValue(lines, 'DesignCapacity', '=') || 0);
49
- result.designedCapacity = parseInt(util.getValue(lines, 'DesignCapacity', '=') || designedCapacity);
50
- result.voltage = parseInt(util.getValue(lines, 'DesignVoltage', '=') || 0) / 1000.0;
51
- result.capacityUnit = 'mWh';
52
- result.percent = parseInt(util.getValue(lines, 'EstimatedChargeRemaining', '=') || 0);
53
- result.currentCapacity = parseInt(result.maxCapacity * result.percent / 100);
54
- result.isCharging = (statusValue >= 6 && statusValue <= 9) || statusValue === 11 || (!(statusValue === 3) && !(statusValue === 1) && result.percent < 100);
55
- result.acConnected = result.isCharging || statusValue === 2;
56
- result.model = util.getValue(lines, 'DeviceID', '=');
57
- } else {
58
- result.status = -1;
59
- }
60
-
61
- return result;
62
- }
63
-
64
- module.exports = function (callback) {
65
-
66
- return new Promise((resolve) => {
67
- process.nextTick(() => {
68
- let result = {
69
- hasBattery: false,
70
- cycleCount: 0,
71
- isCharging: false,
72
- designedCapacity: 0,
73
- maxCapacity: 0,
74
- currentCapacity: 0,
75
- voltage: 0,
76
- capacityUnit: '',
77
- percent: 0,
78
- timeRemaining: null,
79
- acConnected: true,
80
- type: '',
81
- model: '',
82
- manufacturer: '',
83
- serial: ''
84
- };
85
-
86
- if (_linux) {
87
- let battery_path = '';
88
- if (fs.existsSync('/sys/class/power_supply/BAT1/uevent')) {
89
- battery_path = '/sys/class/power_supply/BAT1/';
90
- } else if (fs.existsSync('/sys/class/power_supply/BAT0/uevent')) {
91
- battery_path = '/sys/class/power_supply/BAT0/';
92
- }
93
-
94
- let acConnected = false;
95
- let acPath = '';
96
- if (fs.existsSync('/sys/class/power_supply/AC/online')) {
97
- acPath = '/sys/class/power_supply/AC/online';
98
- } else if (fs.existsSync('/sys/class/power_supply/AC0/online')) {
99
- acPath = '/sys/class/power_supply/AC0/online';
100
- }
101
-
102
- if (acPath) {
103
- const file = fs.readFileSync(acPath);
104
- acConnected = file.toString().trim() === '1';
105
- }
106
-
107
- if (battery_path) {
108
- fs.readFile(battery_path + 'uevent', function (error, stdout) {
109
- if (!error) {
110
- let lines = stdout.toString().split('\n');
111
-
112
- result.isCharging = (util.getValue(lines, 'POWER_SUPPLY_STATUS', '=').toLowerCase() === 'charging');
113
- result.acConnected = acConnected || result.isCharging;
114
- result.voltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_NOW', '='), 10) / 1000000.0;
115
- result.capacityUnit = result.voltage ? 'mWh' : 'mAh';
116
- result.cycleCount = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CYCLE_COUNT', '='), 10);
117
- result.maxCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL', '=', true, true), 10) / 1000.0 * (result.voltage || 1));
118
- const desingedMinVoltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_MIN_DESIGN', '='), 10) / 1000000.0;
119
- result.designedCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL_DESIGN', '=', true, true), 10) / 1000.0 * (desingedMinVoltage || result.voltage || 1));
120
- result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_NOW', '='), 10) / 1000.0 * (result.voltage || 1));
121
- if (!result.maxCapacity) {
122
- result.maxCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL', '=', true, true), 10) / 1000.0;
123
- result.designedCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL_DESIGN', '=', true, true), 10) / 1000.0 | result.maxCapacity;
124
- result.currentCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10) / 1000.0;
125
- }
126
- const percent = util.getValue(lines, 'POWER_SUPPLY_CAPACITY', '=');
127
- const energy = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10);
128
- const power = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_POWER_NOW', '='), 10);
129
- const current = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CURRENT_NOW', '='), 10);
130
-
131
- result.percent = parseInt('0' + percent, 10);
132
- if (result.maxCapacity && result.currentCapacity) {
133
- result.hasBattery = true;
134
- if (!percent) {
135
- result.percent = 100.0 * result.currentCapacity / result.maxCapacity;
136
- }
137
- }
138
- if (result.isCharging) {
139
- result.hasBattery = true;
140
- }
141
- if (energy && power) {
142
- result.timeRemaining = Math.floor(energy / power * 60);
143
- } else if (current && result.currentCapacity) {
144
- result.timeRemaining = Math.floor(result.currentCapacity / current * 60);
145
- }
146
- result.type = util.getValue(lines, 'POWER_SUPPLY_TECHNOLOGY', '=');
147
- result.model = util.getValue(lines, 'POWER_SUPPLY_MODEL_NAME', '=');
148
- result.manufacturer = util.getValue(lines, 'POWER_SUPPLY_MANUFACTURER', '=');
149
- result.serial = util.getValue(lines, 'POWER_SUPPLY_SERIAL_NUMBER', '=');
150
- if (callback) { callback(result); }
151
- resolve(result);
152
- } else {
153
- if (callback) { callback(result); }
154
- resolve(result);
155
- }
156
- });
157
- } else {
158
- if (callback) { callback(result); }
159
- resolve(result);
160
- }
161
- }
162
- if (_freebsd || _openbsd || _netbsd) {
163
- exec('sysctl hw.acpi.battery hw.acpi.acline', function (error, stdout) {
164
- let lines = stdout.toString().split('\n');
165
- const batteries = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.units'), 10);
166
- const percent = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.life'), 10);
167
- result.hasBattery = (batteries > 0);
168
- result.cycleCount = null;
169
- result.isCharging = util.getValue(lines, 'hw.acpi.acline') !== '1';
170
- result.acConnected = result.isCharging;
171
- result.maxCapacity = null;
172
- result.currentCapacity = null;
173
- result.capacityUnit = 'unknown';
174
- result.percent = batteries ? percent : null;
175
- if (callback) { callback(result); }
176
- resolve(result);
177
- });
178
- }
179
-
180
- if (_darwin) {
181
- exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|DesignCapacity|MaxCapacity|CurrentCapacity|BatterySerialNumber|TimeRemaining|Voltage"; pmset -g batt | grep %', function (error, stdout) {
182
- if (stdout) {
183
- let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').replace(/-/g, '').split('\n');
184
- result.cycleCount = parseInt('0' + util.getValue(lines, 'cyclecount', '='), 10);
185
- result.voltage = parseInt('0' + util.getValue(lines, 'voltage', '='), 10) / 1000.0;
186
- result.capacityUnit = result.voltage ? 'mWh' : 'mAh';
187
- result.maxCapacity = Math.round(parseInt('0' + util.getValue(lines, 'maxcapacity', '='), 10) * (result.voltage || 1));
188
- result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'currentcapacity', '='), 10) * (result.voltage || 1));
189
- result.designedCapacity = Math.round(parseInt('0' + util.getValue(lines, 'DesignCapacity', '='), 10) * (result.voltage || 1));
190
- result.manufacturer = 'Apple';
191
- result.serial = util.getValue(lines, 'BatterySerialNumber', '=');
192
- let percent = null;
193
- const line = util.getValue(lines, 'internal', 'Battery');
194
- let parts = line.split(';');
195
- if (parts && parts[0]) {
196
- let parts2 = parts[0].split('\t');
197
- if (parts2 && parts2[1]) {
198
- percent = parseFloat(parts2[1].trim().replace(/%/g, ''));
199
- }
200
- }
201
- if (parts && parts[1]) {
202
- result.isCharging = (parts[1].trim() === 'charging');
203
- result.acConnected = (parts[1].trim() !== 'discharging');
204
- } else {
205
- result.isCharging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes';
206
- result.acConnected = result.isCharging;
207
- }
208
- if (result.maxCapacity && result.currentCapacity) {
209
- result.hasBattery = true;
210
- result.type = 'Li-ion';
211
- result.percent = percent !== null ? percent : Math.round(100.0 * result.currentCapacity / result.maxCapacity);
212
- if (!result.isCharging) {
213
- result.timeRemaining = parseInt('0' + util.getValue(lines, 'TimeRemaining', '='), 10);
214
- }
215
- }
216
- }
217
- if (callback) { callback(result); }
218
- resolve(result);
219
- });
220
- }
221
- if (_sunos) {
222
- if (callback) { callback(result); }
223
- resolve(result);
224
- }
225
- if (_windows) {
226
- try {
227
- const workload = [];
228
- workload.push(util.wmic('Path Win32_Battery Get /value'));
229
- workload.push(util.powerShell('(Get-WmiObject -Class BatteryStaticData -Namespace ROOT/WMI).DesignedCapacity'));
230
- workload.push(util.powerShell('(Get-WmiObject -Class BatteryFullChargedCapacity -Namespace ROOT/WMI).FullChargedCapacity'));
231
- util.promiseAll(
232
- workload
233
- ).then(data => {
234
- if (data) {
235
- // let parts = data.results[0].split(/\n\s*\n/);
236
- let parts = data.results[0].split('\r\n');
237
- let batteries = [];
238
- const hasValue = value => /\S/.test(value);
239
- for (let i = 0; i < parts.length; i++) {
240
- if (hasValue(parts[i]) && (!batteries.length || !hasValue(parts[i - 1]))) {
241
- batteries.push([]);
242
- }
243
- if (hasValue(parts[i])) {
244
- batteries[batteries.length - 1].push(parts[i]);
245
- }
246
- }
247
- let designCapacities = data.results[1].split('\r\n');
248
- let fullChargeCapacities = data.results[2].split('\r\n');
249
- if (batteries.length) {
250
- let first = false;
251
- let additionalBatteries = [];
252
- for (let i = 0; i < batteries.length; i++) {
253
- let lines = batteries[i];
254
- const designedCapacity = designCapacities && designCapacities.length >= (i + 1) && designCapacities[i] ? util.toInt(designCapacities[i]) : 0;
255
- const fullChargeCapacity = fullChargeCapacities && fullChargeCapacities.length >= (i + 1) && fullChargeCapacities[i] ? util.toInt(fullChargeCapacities[i]) : 0;
256
- const parsed = parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity);
257
- if (!first && parsed.status > 0 && parsed.status !== 10) {
258
- result.hasBattery = parsed.hasBattery;
259
- result.maxCapacity = parsed.maxCapacity;
260
- result.designedCapacity = parsed.designedCapacity;
261
- result.voltage = parsed.voltage;
262
- result.capacityUnit = parsed.capacityUnit;
263
- result.percent = parsed.percent;
264
- result.currentCapacity = parsed.currentCapacity;
265
- result.isCharging = parsed.isCharging;
266
- result.acConnected = parsed.acConnected;
267
- result.model = parsed.model;
268
- first = true;
269
- } else if (parsed.status !== -1) {
270
- additionalBatteries.push(
271
- {
272
- hasBattery: parsed.hasBattery,
273
- maxCapacity: parsed.maxCapacity,
274
- designedCapacity: parsed.designedCapacity,
275
- voltage: parsed.voltage,
276
- capacityUnit: parsed.capacityUnit,
277
- percent: parsed.percent,
278
- currentCapacity: parsed.currentCapacity,
279
- isCharging: parsed.isCharging,
280
- timeRemaining: null,
281
- acConnected: parsed.acConnected,
282
- model: parsed.model,
283
- type: '',
284
- manufacturer: '',
285
- serial: ''
286
- }
287
- );
288
- }
289
- }
290
- if (!first && additionalBatteries.length) {
291
- result = additionalBatteries[0];
292
- additionalBatteries.shift();
293
- }
294
- if (additionalBatteries.length) {
295
- result.additionalBatteries = additionalBatteries;
296
- }
297
- }
298
- }
299
- if (callback) { callback(result); }
300
- resolve(result);
301
- });
302
- } catch (e) {
303
- if (callback) { callback(result); }
304
- resolve(result);
305
- }
306
- }
307
- });
308
- });
309
- };
1
+ 'use strict';
2
+ // @ts-check;
3
+ // ==================================================================================
4
+ // battery.js
5
+ // ----------------------------------------------------------------------------------
6
+ // Description: System Information - library
7
+ // for Node.js
8
+ // Copyright: (c) 2014 - 2021
9
+ // Author: Sebastian Hildebrandt
10
+ // ----------------------------------------------------------------------------------
11
+ // License: MIT
12
+ // ==================================================================================
13
+ // 6. Battery
14
+ // ----------------------------------------------------------------------------------
15
+
16
+ const exec = require('child_process').exec;
17
+ const fs = require('fs');
18
+ const util = require('./util');
19
+
20
+ let _platform = process.platform;
21
+
22
+ const _linux = (_platform === 'linux');
23
+ const _darwin = (_platform === 'darwin');
24
+ const _windows = (_platform === 'win32');
25
+ const _freebsd = (_platform === 'freebsd');
26
+ const _openbsd = (_platform === 'openbsd');
27
+ const _netbsd = (_platform === 'netbsd');
28
+ const _sunos = (_platform === 'sunos');
29
+
30
+ function parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity) {
31
+ const result = {};
32
+ let status = util.getValue(lines, 'BatteryStatus', ':').trim();
33
+ // 1 = "Discharging"
34
+ // 2 = "On A/C"
35
+ // 3 = "Fully Charged"
36
+ // 4 = "Low"
37
+ // 5 = "Critical"
38
+ // 6 = "Charging"
39
+ // 7 = "Charging High"
40
+ // 8 = "Charging Low"
41
+ // 9 = "Charging Critical"
42
+ // 10 = "Undefined"
43
+ // 11 = "Partially Charged"
44
+ if (status >= 0) {
45
+ const statusValue = status ? parseInt(status) : 0;
46
+ result.status = statusValue;
47
+ result.hasBattery = true;
48
+ result.maxCapacity = fullChargeCapacity || parseInt(util.getValue(lines, 'DesignCapacity', ':') || 0);
49
+ result.designedCapacity = parseInt(util.getValue(lines, 'DesignCapacity', ':') || designedCapacity);
50
+ result.voltage = parseInt(util.getValue(lines, 'DesignVoltage', ':') || 0) / 1000.0;
51
+ result.capacityUnit = 'mWh';
52
+ result.percent = parseInt(util.getValue(lines, 'EstimatedChargeRemaining', ':') || 0);
53
+ result.currentCapacity = parseInt(result.maxCapacity * result.percent / 100);
54
+ result.isCharging = (statusValue >= 6 && statusValue <= 9) || statusValue === 11 || (!(statusValue === 3) && !(statusValue === 1) && result.percent < 100);
55
+ result.acConnected = result.isCharging || statusValue === 2;
56
+ result.model = util.getValue(lines, 'DeviceID', ':');
57
+ } else {
58
+ result.status = -1;
59
+ }
60
+
61
+ return result;
62
+ }
63
+
64
+ module.exports = function (callback) {
65
+
66
+ return new Promise((resolve) => {
67
+ process.nextTick(() => {
68
+ let result = {
69
+ hasBattery: false,
70
+ cycleCount: 0,
71
+ isCharging: false,
72
+ designedCapacity: 0,
73
+ maxCapacity: 0,
74
+ currentCapacity: 0,
75
+ voltage: 0,
76
+ capacityUnit: '',
77
+ percent: 0,
78
+ timeRemaining: null,
79
+ acConnected: true,
80
+ type: '',
81
+ model: '',
82
+ manufacturer: '',
83
+ serial: ''
84
+ };
85
+
86
+ if (_linux) {
87
+ let battery_path = '';
88
+ if (fs.existsSync('/sys/class/power_supply/BAT1/uevent')) {
89
+ battery_path = '/sys/class/power_supply/BAT1/';
90
+ } else if (fs.existsSync('/sys/class/power_supply/BAT0/uevent')) {
91
+ battery_path = '/sys/class/power_supply/BAT0/';
92
+ }
93
+
94
+ let acConnected = false;
95
+ let acPath = '';
96
+ if (fs.existsSync('/sys/class/power_supply/AC/online')) {
97
+ acPath = '/sys/class/power_supply/AC/online';
98
+ } else if (fs.existsSync('/sys/class/power_supply/AC0/online')) {
99
+ acPath = '/sys/class/power_supply/AC0/online';
100
+ }
101
+
102
+ if (acPath) {
103
+ const file = fs.readFileSync(acPath);
104
+ acConnected = file.toString().trim() === '1';
105
+ }
106
+
107
+ if (battery_path) {
108
+ fs.readFile(battery_path + 'uevent', function (error, stdout) {
109
+ if (!error) {
110
+ let lines = stdout.toString().split('\n');
111
+
112
+ result.isCharging = (util.getValue(lines, 'POWER_SUPPLY_STATUS', '=').toLowerCase() === 'charging');
113
+ result.acConnected = acConnected || result.isCharging;
114
+ result.voltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_NOW', '='), 10) / 1000000.0;
115
+ result.capacityUnit = result.voltage ? 'mWh' : 'mAh';
116
+ result.cycleCount = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CYCLE_COUNT', '='), 10);
117
+ result.maxCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL', '=', true, true), 10) / 1000.0 * (result.voltage || 1));
118
+ const desingedMinVoltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_MIN_DESIGN', '='), 10) / 1000000.0;
119
+ result.designedCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL_DESIGN', '=', true, true), 10) / 1000.0 * (desingedMinVoltage || result.voltage || 1));
120
+ result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_NOW', '='), 10) / 1000.0 * (result.voltage || 1));
121
+ if (!result.maxCapacity) {
122
+ result.maxCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL', '=', true, true), 10) / 1000.0;
123
+ result.designedCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL_DESIGN', '=', true, true), 10) / 1000.0 | result.maxCapacity;
124
+ result.currentCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10) / 1000.0;
125
+ }
126
+ const percent = util.getValue(lines, 'POWER_SUPPLY_CAPACITY', '=');
127
+ const energy = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10);
128
+ const power = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_POWER_NOW', '='), 10);
129
+ const current = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CURRENT_NOW', '='), 10);
130
+
131
+ result.percent = parseInt('0' + percent, 10);
132
+ if (result.maxCapacity && result.currentCapacity) {
133
+ result.hasBattery = true;
134
+ if (!percent) {
135
+ result.percent = 100.0 * result.currentCapacity / result.maxCapacity;
136
+ }
137
+ }
138
+ if (result.isCharging) {
139
+ result.hasBattery = true;
140
+ }
141
+ if (energy && power) {
142
+ result.timeRemaining = Math.floor(energy / power * 60);
143
+ } else if (current && result.currentCapacity) {
144
+ result.timeRemaining = Math.floor(result.currentCapacity / current * 60);
145
+ }
146
+ result.type = util.getValue(lines, 'POWER_SUPPLY_TECHNOLOGY', '=');
147
+ result.model = util.getValue(lines, 'POWER_SUPPLY_MODEL_NAME', '=');
148
+ result.manufacturer = util.getValue(lines, 'POWER_SUPPLY_MANUFACTURER', '=');
149
+ result.serial = util.getValue(lines, 'POWER_SUPPLY_SERIAL_NUMBER', '=');
150
+ if (callback) { callback(result); }
151
+ resolve(result);
152
+ } else {
153
+ if (callback) { callback(result); }
154
+ resolve(result);
155
+ }
156
+ });
157
+ } else {
158
+ if (callback) { callback(result); }
159
+ resolve(result);
160
+ }
161
+ }
162
+ if (_freebsd || _openbsd || _netbsd) {
163
+ exec('sysctl hw.acpi.battery hw.acpi.acline', function (error, stdout) {
164
+ let lines = stdout.toString().split('\n');
165
+ const batteries = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.units'), 10);
166
+ const percent = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.life'), 10);
167
+ result.hasBattery = (batteries > 0);
168
+ result.cycleCount = null;
169
+ result.isCharging = util.getValue(lines, 'hw.acpi.acline') !== '1';
170
+ result.acConnected = result.isCharging;
171
+ result.maxCapacity = null;
172
+ result.currentCapacity = null;
173
+ result.capacityUnit = 'unknown';
174
+ result.percent = batteries ? percent : null;
175
+ if (callback) { callback(result); }
176
+ resolve(result);
177
+ });
178
+ }
179
+
180
+ if (_darwin) {
181
+ exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|DesignCapacity|MaxCapacity|CurrentCapacity|BatterySerialNumber|TimeRemaining|Voltage"; pmset -g batt | grep %', function (error, stdout) {
182
+ if (stdout) {
183
+ let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').replace(/-/g, '').split('\n');
184
+ result.cycleCount = parseInt('0' + util.getValue(lines, 'cyclecount', '='), 10);
185
+ result.voltage = parseInt('0' + util.getValue(lines, 'voltage', '='), 10) / 1000.0;
186
+ result.capacityUnit = result.voltage ? 'mWh' : 'mAh';
187
+ result.maxCapacity = Math.round(parseInt('0' + util.getValue(lines, 'maxcapacity', '='), 10) * (result.voltage || 1));
188
+ result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'currentcapacity', '='), 10) * (result.voltage || 1));
189
+ result.designedCapacity = Math.round(parseInt('0' + util.getValue(lines, 'DesignCapacity', '='), 10) * (result.voltage || 1));
190
+ result.manufacturer = 'Apple';
191
+ result.serial = util.getValue(lines, 'BatterySerialNumber', '=');
192
+ let percent = null;
193
+ const line = util.getValue(lines, 'internal', 'Battery');
194
+ let parts = line.split(';');
195
+ if (parts && parts[0]) {
196
+ let parts2 = parts[0].split('\t');
197
+ if (parts2 && parts2[1]) {
198
+ percent = parseFloat(parts2[1].trim().replace(/%/g, ''));
199
+ }
200
+ }
201
+ if (parts && parts[1]) {
202
+ result.isCharging = (parts[1].trim() === 'charging');
203
+ result.acConnected = (parts[1].trim() !== 'discharging');
204
+ } else {
205
+ result.isCharging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes';
206
+ result.acConnected = result.isCharging;
207
+ }
208
+ if (result.maxCapacity && result.currentCapacity) {
209
+ result.hasBattery = true;
210
+ result.type = 'Li-ion';
211
+ result.percent = percent !== null ? percent : Math.round(100.0 * result.currentCapacity / result.maxCapacity);
212
+ if (!result.isCharging) {
213
+ result.timeRemaining = parseInt('0' + util.getValue(lines, 'TimeRemaining', '='), 10);
214
+ }
215
+ }
216
+ }
217
+ if (callback) { callback(result); }
218
+ resolve(result);
219
+ });
220
+ }
221
+ if (_sunos) {
222
+ if (callback) { callback(result); }
223
+ resolve(result);
224
+ }
225
+ if (_windows) {
226
+ try {
227
+ const workload = [];
228
+ workload.push(util.powerShell('Get-WmiObject Win32_Battery | fl *'));
229
+ workload.push(util.powerShell('(Get-WmiObject -Class BatteryStaticData -Namespace ROOT/WMI).DesignedCapacity'));
230
+ workload.push(util.powerShell('(Get-WmiObject -Class BatteryFullChargedCapacity -Namespace ROOT/WMI).FullChargedCapacity'));
231
+ util.promiseAll(
232
+ workload
233
+ ).then(data => {
234
+ if (data) {
235
+ // let parts = data.results[0].split(/\n\s*\n/);
236
+ let parts = data.results[0].split(/\n\s*\n/);
237
+ let batteries = [];
238
+ const hasValue = value => /\S/.test(value);
239
+ for (let i = 0; i < parts.length; i++) {
240
+ if (hasValue(parts[i]) && (!batteries.length || !hasValue(parts[i - 1]))) {
241
+ batteries.push([]);
242
+ }
243
+ if (hasValue(parts[i])) {
244
+ batteries[batteries.length - 1].push(parts[i]);
245
+ }
246
+ }
247
+ let designCapacities = data.results[1].split('\r\n');
248
+ let fullChargeCapacities = data.results[2].split('\r\n');
249
+ if (batteries.length) {
250
+ let first = false;
251
+ let additionalBatteries = [];
252
+ for (let i = 0; i < batteries.length; i++) {
253
+ let lines = batteries[i];
254
+ const designedCapacity = designCapacities && designCapacities.length >= (i + 1) && designCapacities[i] ? util.toInt(designCapacities[i]) : 0;
255
+ const fullChargeCapacity = fullChargeCapacities && fullChargeCapacities.length >= (i + 1) && fullChargeCapacities[i] ? util.toInt(fullChargeCapacities[i]) : 0;
256
+ const parsed = parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity);
257
+ if (!first && parsed.status > 0 && parsed.status !== 10) {
258
+ result.hasBattery = parsed.hasBattery;
259
+ result.maxCapacity = parsed.maxCapacity;
260
+ result.designedCapacity = parsed.designedCapacity;
261
+ result.voltage = parsed.voltage;
262
+ result.capacityUnit = parsed.capacityUnit;
263
+ result.percent = parsed.percent;
264
+ result.currentCapacity = parsed.currentCapacity;
265
+ result.isCharging = parsed.isCharging;
266
+ result.acConnected = parsed.acConnected;
267
+ result.model = parsed.model;
268
+ first = true;
269
+ } else if (parsed.status !== -1) {
270
+ additionalBatteries.push(
271
+ {
272
+ hasBattery: parsed.hasBattery,
273
+ maxCapacity: parsed.maxCapacity,
274
+ designedCapacity: parsed.designedCapacity,
275
+ voltage: parsed.voltage,
276
+ capacityUnit: parsed.capacityUnit,
277
+ percent: parsed.percent,
278
+ currentCapacity: parsed.currentCapacity,
279
+ isCharging: parsed.isCharging,
280
+ timeRemaining: null,
281
+ acConnected: parsed.acConnected,
282
+ model: parsed.model,
283
+ type: '',
284
+ manufacturer: '',
285
+ serial: ''
286
+ }
287
+ );
288
+ }
289
+ }
290
+ if (!first && additionalBatteries.length) {
291
+ result = additionalBatteries[0];
292
+ additionalBatteries.shift();
293
+ }
294
+ if (additionalBatteries.length) {
295
+ result.additionalBatteries = additionalBatteries;
296
+ }
297
+ }
298
+ }
299
+ if (callback) { callback(result); }
300
+ resolve(result);
301
+ });
302
+ } catch (e) {
303
+ if (callback) { callback(result); }
304
+ resolve(result);
305
+ }
306
+ }
307
+ });
308
+ });
309
+ };