web-dev-qa-db-ja.com

バッテリーステータスを取得して、より頻繁に、またはAC電源/スリープ解除時に更新します

厄介な問題は、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_ACADonlineを正しく報告します。この問題の助けをいただければ幸いです。 KDEでこの問題が発生したことは覚えていません。

2
qwr

以前のプロジェクトの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 __== '__main__': main()
3

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 __== '__main__':
    main()

次に、5秒ごとにupdate-battery.pyを呼び出す個別のbashスクリプトを作成しましたが、必要な場合にのみ呼び出します。これを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秒ごとに更新されており、目立ったパフォーマンスの問題はありません

2
Kufii