Skip to content

Commit a2ccf1e

Browse files
committed
更改了是否在充电的判断逻辑
1 parent 4fe5e29 commit a2ccf1e

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

service/exector.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
)
1010

1111
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}'`
1515
)
1616

1717
func checkCmd(cmd ...string) bool {
@@ -48,15 +48,15 @@ func baseShellExec(cmd string, env map[string]string, args ...string) (string, e
4848
func ExecShell(cmd string, env map[string]string, args ...string) (string, error) {
4949
return baseShellExec(cmd, env, args...)
5050
}
51-
func ISPowerSupply() bool {
52-
output, err := ExecShell("bash", nil, "-c", CmdIsPowerSupply)
51+
func IsCharging() bool {
52+
output, err := ExecShell("bash", nil, "-c", CmdIsCharging)
5353
if err != nil {
54-
zap.L().Error("ISPowerSupply failed", zap.Error(err))
54+
zap.L().Error("IsCharging failed", zap.Error(err))
5555
return false
5656
}
57-
zap.L().Debug("ISPowerSupply output:" + output)
57+
zap.L().Debug("IsCharging output:" + output)
5858
//这里经过测试打印出来的是yes\n懒得去处理,直接contains就给过
59-
return strings.Contains(output, "yes")
59+
return !strings.Contains(output, "discharging")
6060
}
6161
func GetACAdapterInfo() string {
6262
output, err := ExecShell("upower", nil, "-i", CmdPowerACInfo)

service/monitoring.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func Monitoring() {
1313
for {
14-
if !ISPowerSupply() {
14+
if !IsCharging() {
1515
ac, b := GetACAdapterInfo(), GetBatteryInfo()
1616
zap.L().Info("PowerSupply no", zap.String("ac", ac), zap.String("b", b))
1717
err := genNotify(global.ToEmail, ac, b)

unit_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import (
99

1010
func TestGetInfo(t *testing.T) {
1111
initial.Init()
12-
fmt.Println(service.ISPowerSupply())
12+
fmt.Println(service.IsCharging())
1313
}

0 commit comments

Comments
 (0)