Obtenha o status da bateria para atualizar com mais frequência ou com energia AC / wake

2

Um problema incômodo que eu tenho é que up up levará alguns minutos ou mais para atualizar o status da minha bateria (Esse problema é semelhante ao postar mas eu forneço detalhes do meu laptop). Isso significa que, se eu acordar meu laptop da hibernação, ele mostra a porcentagem errada da bateria por alguns minutos e, quando eu conecto o carregador, o ícone da bateria não indica isso (embora a luz de carga do hardware acenda). Além desses casos, não me importo com o tempo de atualização.

Curiosamente, quando eu desconecto meu carregador, o ícone é atualizado imediatamente. Eu criei uma solução temporária para atualizar no despertar que pode ou não funcionar, mas parece estar lá. seja uma boa correção. Rodar o Ubuntu 16.04 no Acer Aspire E5-575G-53VG.

acpi sempre mostra a porcentagem e o status corretos da bateria. sudo service upower restart força o status da bateria de upstream a atualizar.

Saída de upower --monitor-detail ao conectar o carregador:

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

Saída quando eu desconectar o carregador:

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

O que eu notei é que quando o carregador está conectado, o estado da bateria ainda é discharging . No entanto, /org/freedesktop/UPower/devices/line_power_ACAD reporta online corretamente. Qualquer ajuda com este problema é apreciada. Não me lembro de ter esse problema no KDE.

    
por qwr 01.02.2017 / 07:12

2 respostas

3

Enquanto trabalhava em um dos meus projetos anteriores, descobri que o indicador de energia no Unity, na verdade, aparentemente tem atrasos por atualização, e se origina do daemon UPower, que é o que o indicador parece usar para obter informações de energia. Isso atrapalha os scripts personalizados que tentam monitorar o uso da bateria através do daemon UPower com certa precisão. No entanto, o serviço UPower tem chamadas para atualizar / atualizar essas informações. O script abaixo aproveita essa chamada dbus. O script deve ser copiado e salvo como um arquivo, preferencialmente na pasta ~/bin .

O uso é muito simples: ./update_battery.py <SECONDS> , em que <SECONDS> é a frequência com que você deseja atualizar as informações. Obviamente, você deve ter em mente que a atualização muito frequente levará a um maior uso de recursos, portanto, eu recomendaria algo mais ou menos sensato, por exemplo, de 3 a 10 segundos.

#!/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()
    
por Sergiy Kolodyazhnyy 01.02.2017 / 09:24
2

A resposta de Sergiy funcionou, mas estava causando alguns problemas de desempenho de micro-gaguejar para mim. Eu modifiquei para ser mais eficiente.

primeiro, modifiquei update-battery.py para remover o loop de suspensão:

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

Eu fiz um script bash separado que chama update-battery.py a cada 5 segundos, mas só o chama quando necessário. Eu chamei isso de keep-battery-updated e o adicionei como um script de inicialização.

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

Agora, a bateria é atualizada a cada 5 segundos, sem problemas de desempenho perceptíveis até o momento

    
por Kufii 27.05.2018 / 19:14