获取电池状态以更频繁地更新或通过交流电源/唤醒更新

获取电池状态以更频繁地更新或通过交流电源/唤醒更新

我遇到的一个烦人的问题是,upower 需要几分钟或更长时间才能更新我的电池状态(这个问题类似于这个帖子但我提供了笔记本电脑的详细信息)。这意味着如果我将笔记本电脑从休眠状态唤醒,它会在几分钟内显示错误的电池百分比,并且当我插入充电器时,电池图标不会显示(尽管硬件充电指示灯会亮起)。除了这些情况之外,我不介意更新时间。

奇怪的是,当我拔掉充电器时,图标会立即更新。我想出了一个唤醒时更新的临时解决方案这可能有效也可能无效,但似乎应该有一个很好的解决办法。在 Acer Aspire E5-575G-53VG 上运行 Ubuntu 16.04。

acpi始终显示正确的电池百分比和状态。sudo service upower restart强制更新 upower 的电池状态。

upower --monitor-detail插入充电器时的输出:

Monitoring activity from the power daemon. Press Ctrl+C to cancel.
[00:55:46.411]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.9157 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         6.6933 W
    voltage:             12.562 V
    time to empty:       7.6 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1485928546  6.693   discharging
    1485928537  10.856  discharging
    1485928517  6.083   discharging
    1485928504  10.423  discharging
    1485928485  6.149   discharging
    1485928466  21.190  discharging
    1485928463  6.371   discharging
    1485928449  7.071   discharging

[00:55:46.414]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.9157 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         6.6933 W
    voltage:             12.562 V
    time to empty:       7.6 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1485928546  6.693   discharging
    1485928537  10.856  discharging
    1485928517  6.083   discharging
    1485928504  10.423  discharging
    1485928485  6.149   discharging
    1485928466  21.190  discharging
    1485928463  6.371   discharging
    1485928449  7.071   discharging

[00:55:46.417]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.9157 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         6.6933 W
    voltage:             12.562 V
    time to empty:       7.6 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1485928546  6.693   discharging
    1485928537  10.856  discharging
    1485928517  6.083   discharging
    1485928504  10.423  discharging
    1485928485  6.149   discharging
    1485928466  21.190  discharging
    1485928463  6.371   discharging
    1485928449  7.071   discharging

[00:55:46.419]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.9157 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         6.6933 W
    voltage:             12.562 V
    time to empty:       7.6 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (rate):
    1485928546  6.693   discharging
    1485928537  10.856  discharging
    1485928517  6.083   discharging
    1485928504  10.423  discharging
    1485928485  6.149   discharging
    1485928466  21.190  discharging
    1485928463  6.371   discharging
    1485928449  7.071   discharging

[00:55:46.422]  device changed:     /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              yes
    icon-name:          'ac-adapter-symbolic'

[00:55:46.422]  device changed:     /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:55:46 AM EST (0 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              yes
    icon-name:          'ac-adapter-symbolic'

[00:55:46.422]  daemon changed:
  daemon-version:  0.99.4
  on-battery:      no
  lid-is-closed:   no
  lid-is-present:  yes
  critical-action: HybridSleep

拔下充电器时的输出:

Monitoring activity from the power daemon. Press Ctrl+C to cancel.
[00:58:17.752]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.765]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.773]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.781]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.789]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.797]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.803]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.809]  device changed:     /org/freedesktop/UPower/devices/battery_BAT1
  native-path:          BAT1
  vendor:               PANASONIC
  model:                AS16B5J
  serial:               2217
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               discharging
    warning-level:       none
    energy:              50.838 Wh
    energy-empty:        0 Wh
    energy-full:         55.6554 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         10.8669 W
    voltage:             12.656 V
    time to empty:       4.7 hours
    percentage:          91%
    capacity:            89.5357%
    technology:          lithium-ion
    icon-name:          'battery-full-symbolic'
  History (charge):
    1485928697  91.000  discharging
    1485928672  0.000   unknown
  History (rate):
    1485928697  10.867  discharging
    1485928672  0.000   unknown

[00:58:17.811]  device changed:     /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              no
    icon-name:          'ac-adapter-symbolic'

[00:58:17.811]  device changed:     /org/freedesktop/UPower/devices/line_power_ACAD
  native-path:          ACAD
  power supply:         yes
  updated:              Wed 01 Feb 2017 12:58:17 AM EST (0 seconds ago)
  has history:          no
  has statistics:       no
  line-power
    warning-level:       none
    online:              no
    icon-name:          'ac-adapter-symbolic'

[00:58:17.811]  daemon changed:
  daemon-version:  0.99.4
  on-battery:      yes
  lid-is-closed:   no
  lid-is-present:  yes
  critical-action: HybridSleep

我注意到,当充电器插入时,电池状态仍为discharging。但是,/org/freedesktop/UPower/devices/line_power_ACAD报告online正确。任何有关此问题的帮助都将不胜感激。我不记得在 KDE 上遇到过此问题。

答案1

在处理我之前的一个项目时,我发现 Unity 上的电源指示器确实存在明显的事件驱动更新延迟,它源于 UPower 守护进程,指示器似乎使用该守护进程来获取电源信息。这会妨碍自定义脚本尝试通过 UPower 守护进程以一定精度监控电池使用情况。但是,UPower 服务确实有更新/刷新该信息的调用。下面的脚本利用了该 dbus 调用。脚本将被复制并保存为文件,最好保存在~/bin文件夹中。

使用方法非常简单:./update_battery.py <SECONDS>,其中<SECONDS>是您希望刷新信息的频率。显然,您必须记住,非常频繁的刷新会导致更高的资源使用率,因此我建议一个或多或少合理的时间,例如 3 - 10 秒。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import dbus
import time
import sys

def run_dbus_method(bus_type, obj, path, interface, method, arg):
    if bus_type == "session":
        bus = dbus.SessionBus()
    elif bus_type == "system":
        bus = dbus.SystemBus()
    else:
        return None

    proxy = bus.get_object(obj, path)
    dbus_method = proxy.get_dbus_method(method, interface)

    return dbus_method(arg) if arg else dbus_method()

def find_battery_path():
    call = [ 'system', 'org.freedesktop.UPower', 
             '/org/freedesktop/UPower', 'org.freedesktop.UPower',
             'EnumerateDevices', None ]
    devices = run_dbus_method(*call)
    for i in devices:
        if 'BAT' in i: return str(i)

def main():
    bat_path = find_battery_path()
    call = [ 'system', 'org.freedesktop.UPower',
             bat_path, 'org.freedesktop.UPower.Device',
             'Refresh', None ]

    timeout = int(sys.argv[1])
    while True:
        run_dbus_method(*call)
        time.sleep(timeout)

if __name__ == '__main__': main()

答案2

Sergiy 的答案有效,但它给我带来了一些微卡顿性能问题。我对其进行了修改,以提高效率。

首先,我修改了 update-battery.py 以删除睡眠循环:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import dbus


def run_dbus_method(bus_type, obj, path, interface, method, arg):
    if bus_type == "session":
        bus = dbus.SessionBus()
    elif bus_type == "system":
        bus = dbus.SystemBus()
    else:
        return None

    proxy = bus.get_object(obj, path)
    dbus_method = proxy.get_dbus_method(method, interface)

    return dbus_method(arg) if arg else dbus_method()


def find_battery_path():
    call = ['system', 'org.freedesktop.UPower',
            '/org/freedesktop/UPower', 'org.freedesktop.UPower',
            'EnumerateDevices', None]
    devices = run_dbus_method(*call)
    for i in devices:
        if 'BAT' in i:
            return str(i)
    return None


def main():
    bat_path = find_battery_path()
    call = ['system', 'org.freedesktop.UPower',
            bat_path, 'org.freedesktop.UPower.Device',
            'Refresh', None]

    run_dbus_method(*call)


if __name__ == '__main__':
    main()

然后我创建了一个单独的 bash 脚本,每 5 秒调用一次 update-battery.py,但只在必要时调用它。我将其命名为 keep-battery-updated 并将其添加为启动脚本。

#!/bin/bash
#
# @version      1.0
# @script       keep-battery-updated
# @description  keeps the battery status updated
#
##

while sleep 5; do
    badbattery=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep state: | cut -d ':' -f 2 | xargs)
    case $badbattery in
        discharging)
            badbattery="Discharging"
            ;;
        charging)
            badbattery="Charging"
            ;;
        fully-charged)
            badbattery="Full"
            ;;
    esac
    goodbattery=$(acpi -V | grep Battery | sed -n 1p | cut -d ':' -f 2 | cut -d ',' -f 1 | xargs)

    if [ $badbattery != $goodbattery ]; then
        python3 /usr/local/bin/update-battery.py
    fi
done

现在电池每 5 秒更新一次,到目前为止没有明显的性能问题

相关内容