web-dev-qa-db-fra.com

Obtenir l'état de la batterie pour mettre à jour plus souvent ou sur courant alternatif

Un problème gênant que j'ai est que l'upower prend quelques minutes ou plus pour mettre à jour l'état de ma batterie (ce problème est similaire à cet article mais je fournis les détails de mon ordinateur portable). Cela signifie que si je sors mon ordinateur portable de l'hibernation, il affiche un pourcentage de batterie incorrect pendant quelques minutes et lorsque je branche mon chargeur, l'icône de la batterie ne l'indique pas (bien que le voyant de charge du matériel s'allume). Autre que ces cas, le temps de mise à jour ne me dérange pas.

Curieusement, lorsque je débranche mon chargeur, l’icône se met immédiatement à jour. J'ai mis au point un solution temporaire à mettre à jour après le réveil qui peut ne pas fonctionner, mais il semble qu'il devrait y avoir une bonne solution. Exécuter Ubuntu 16.04 sur Acer Aspire E5-575G-53VG.

acpi indique toujours le pourcentage et l'état de la batterie corrects. Sudo service upower restart force la mise à jour de l'état de la batterie de l'upower.

Sortie de upower --monitor-detail lors du branchement du chargeur:

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

Sortie lorsque je débranche le chargeur:

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

Ce que j'ai remarqué, c'est que lorsque le chargeur est branché, l'état de la batterie est toujours discharging. Cependant, /org/freedesktop/UPower/devices/line_power_ACAD rapporte online correctement. Toute aide concernant ce problème est appréciée. Je ne me souviens pas d’avoir ce numéro sur KDE.

2
qwr

Alors que je travaillais sur l'un de mes projets précédents, j'ai découvert que l'indicateur de puissance sur Unity avait apparemment un délai de mise à jour lié à un événement. Il provient du démon UPower, qui semble être utilisé par l'indicateur pour obtenir des informations sur l'alimentation. Cela gêne les scripts personnalisés qui tentent de surveiller l'utilisation de la batterie via le démon UPower avec une certaine précision. Cependant, le service UPower a des appels pour mettre à jour/actualiser ces informations. Le script ci-dessous tire parti de cet appel dbus. Le script doit être copié et enregistré sous forme de fichier, de préférence dans le dossier ~/bin.

L'utilisation est très simple: ./update_battery.py <SECONDS>, où <SECONDS> correspond à la fréquence d'actualisation des informations. Évidemment, vous devez garder à l'esprit qu'un rafraîchissement très fréquent entraînera une utilisation accrue des ressources. Je recommanderais donc quelque chose de plus ou de moins judicieux, par exemple 3 à 10 secondes.

#!/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 Kolodyazhnyy

La réponse de Sergiy a fonctionné, mais cela me causait quelques problèmes de performances. Je l'ai modifié pour être plus efficace.

tout d'abord, j'ai modifié update-battery.py pour supprimer la boucle de sommeil:

#!/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()

j'ai ensuite créé un script bash distinct qui appelle update-battery.py toutes les 5 secondes, mais uniquement lorsque cela est nécessaire. J'ai appelé cette mise à jour de la batterie et l'ajouté en tant que script de démarrage.

#!/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

Désormais, la batterie est mise à jour toutes les 5 secondes, sans aucun problème de performances notable jusqu'à présent.

2
Kufii