From 81877a334900b065af79e1faf3df85fb3e5f238f Mon Sep 17 00:00:00 2001 From: Simon Smith Date: Sat, 1 Jun 2024 23:15:26 +0100 Subject: [PATCH] fix macos battery level --- modules/power-monitor.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/modules/power-monitor.js b/modules/power-monitor.js index 8c6d96f..150f511 100644 --- a/modules/power-monitor.js +++ b/modules/power-monitor.js @@ -143,15 +143,27 @@ function powerMonitor() child.stdin.write(' printf "{\\"ac\\": %s,\\"level\\": %s}",power, chg[1]; '); child.stdin.write("}'\nexit\n"); child.waitExit(); - - try - { + try { var info = JSON.parse(child.stdout.str.trim()); return (info); - } - catch (e) - { - return ({ ac: 1, level: -1 }); + } catch (e) { + child = require('child_process').execFile('/bin/sh', ['sh']); + child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); }); + child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); }); + child.stdin.write("pmset -g batt | tr '\\n' '`' | awk -F'`' '"); + child.stdin.write('{'); + child.stdin.write(' power=split($1,pwr,"AC")>1?"1":"0";'); + child.stdin.write(' split($2, batt, " ");'); + child.stdin.write(' split(batt[3],chg,"%");'); + child.stdin.write(' printf "{\\"ac\\": %s,\\"level\\": %s}",power, chg[1]; '); + child.stdin.write("}'\nexit\n"); + child.waitExit(); + try { + var info = JSON.parse(child.stdout.str.trim()); + return (info); + } catch (er) { + return ({ ac: 1, level: -1 }); + } } }; this._batteryLevelCheck = function _batteryLevelCheck()