|
9 | 9 | )
|
10 | 10 |
|
11 | 11 | var (
|
12 |
| - CmdBatteryInfo = `/org/freedesktop/UPower/devices/battery_BAT0` |
13 |
| - CmdPowerACInfo = `/org/freedesktop/UPower/devices/line_power_AC` |
14 |
| - CmdIsPowerSupply = `upower -i /org/freedesktop/UPower/devices/line_power_AC | grep 'power supply' | awk '{print $NF}'` |
| 12 | + CmdBatteryInfo = `/org/freedesktop/UPower/devices/battery_BAT0` |
| 13 | + CmdPowerACInfo = `/org/freedesktop/UPower/devices/line_power_AC` |
| 14 | + CmdIsCharging = `upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep state | awk '{print $NF}'` |
15 | 15 | )
|
16 | 16 |
|
17 | 17 | func checkCmd(cmd ...string) bool {
|
@@ -48,15 +48,15 @@ func baseShellExec(cmd string, env map[string]string, args ...string) (string, e
|
48 | 48 | func ExecShell(cmd string, env map[string]string, args ...string) (string, error) {
|
49 | 49 | return baseShellExec(cmd, env, args...)
|
50 | 50 | }
|
51 |
| -func ISPowerSupply() bool { |
52 |
| - output, err := ExecShell("bash", nil, "-c", CmdIsPowerSupply) |
| 51 | +func IsCharging() bool { |
| 52 | + output, err := ExecShell("bash", nil, "-c", CmdIsCharging) |
53 | 53 | if err != nil {
|
54 |
| - zap.L().Error("ISPowerSupply failed", zap.Error(err)) |
| 54 | + zap.L().Error("IsCharging failed", zap.Error(err)) |
55 | 55 | return false
|
56 | 56 | }
|
57 |
| - zap.L().Debug("ISPowerSupply output:" + output) |
| 57 | + zap.L().Debug("IsCharging output:" + output) |
58 | 58 | //这里经过测试打印出来的是yes\n懒得去处理,直接contains就给过
|
59 |
| - return strings.Contains(output, "yes") |
| 59 | + return !strings.Contains(output, "discharging") |
60 | 60 | }
|
61 | 61 | func GetACAdapterInfo() string {
|
62 | 62 | output, err := ExecShell("upower", nil, "-i", CmdPowerACInfo)
|
|
0 commit comments