[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: batterie sous openbox et compression video sous linux ?



Le 2019-08-31 15:52, toto a écrit :
Bonjour à tous !

1) BATTERIE SOUS OPENBOX
Je souhaiterais savoir comment afficher simplement (à la manière de
"xclock") le niveau de ma batterie (j'utilise de temps en temps à la main la commade "acpi -b" mais il faut y penser !). Peut être un petit progamme en
"bash" lancé dans un "xterm" par exemple ?

Bonjour,
J’ai un petit écran et préfère ne pas utiliser de panel qui « gaspille » de la place. Si j’ai besoin de quelque-chose ça doit être accessible par raccourci clavier au-dessus la fenêtre de travail sans encombrer mon écran. Donc voici pour la batterie j’utilise notify-send de temps en temps sur openbox.

-----------------
#!/bin/bash

NOTIFY_DURATION=3000

#ICON=$HOME/.local/share/icons/clock.png

SUMMARY=$(\date '+%T')
REMAINING=$(acpi -b | cut -s --fields="2 3" --delimiter=",")

DATE=$(\date '+%A %d %B %Y')
BODY="Le $DATE\nLa batterie est à $REMAINING"
notify-send -u Low -t $NOTIFY_DURATION -c device "$SUMMARY" "$BODY"
--------------------

Dans .config/openbox/rc.xml

------------------
    <keybind key="C-A-h">
      <action name="Execute">
        <execute>nomScript</execute>
      </action>
    </keybind>
-----------------

De plus, j’ai écrit ce petit démon en python qui me prévient quand la batterie commence à atteindre la fin de son autonomie. La mise en vielle se fait avec Laptop-mode-tools, ce script se contente de prévenir à plusieurs reprises «while (True)» de la décharge «isLowBattery()» et juste avant l’hibernation «isCriticalBattery()».

---------------------------------
#!/usr/bin/python3

import notify2
from pathlib import Path
import time
import subprocess
from datetime import datetime, timedelta
from daemonize import Daemonize

# Chemain des fichiers acpi
charge_full_file = "/sys/class/power_supply/CMB1/charge_full"
charge_now_file = "/sys/class/power_supply/CMB1/charge_now"
is_online_file = "/sys/class/power_supply/AC/online"
path_log_file = "/tmp/battery.log"
pid = "/tmp/battery.pid"

get_str_remaining_time = "acpi -b | cut --fields=5 --delimiter=' '"

# A quel moment critique notifier (en % de charge)
pc_critical_battery = 5

# A quel moment informatif notifier batterie faible (en % de charge)
pc_low_battery = 10

# Durée de la notification
notify_duration = 7000


icons = Path.home().joinpath(".local/share/icons")
icone_warn = str(icons.joinpath("battery-caution.png"))
icone_crit = str(icons.joinpath("dialog-warning.png"))

# Charge max de la battarie
charge_full = int(open(charge_full_file, "r").readline())

# Calcule de la valeur de low_battery
low_battery = int(pc_low_battery * charge_full / 100)

# Calcul de la valeur de CRITICAL_BATTERY
critical_battery = int(pc_critical_battery * charge_full / 100)

# Charge actuelle


def get_charge_now():
    return int(open(charge_now_file, "r").readline())


def is_online():
    return bool(int(open(is_online_file, "r").readline()))


def send_log(message):
    log_file = open(path_log_file, "a")
    log_file.write(time.ctime())
    log_file.write(" " + message + "\n")
    log_file.close()


def get_remaining_time():
    # extrait la sous chaine du caractère 2 à 10 du résultat
remaining_time = str(subprocess.check_output(get_str_remaining_time, shell=True))[2:10]
    fmt = "%H:%M:%S"
    # On instencie un objet datetime avec la chaîne remaining_time en
    # lui retirant 5 minutes av mise en veille
remainingHMS = datetime.strptime(remaining_time, fmt) - timedelta(minutes=5)
    return remainingHMS.strftime("%M minutes %S secondes")


def sendWarning():
    summary = "Attention batterie faible"
    remaining_time = get_remaining_time()
    send_log(remaining_time)
body = "Il reste " + remaining_time + " avant la mise en veille de l'ordinateur"
    notify2.init('warningbattery', mainloop=None)
    n = notify2.Notification(summary, body, icone_warn)
    n.set_urgency(notify2.URGENCY_NORMAL)
    n.set_timeout(notify_duration)
    n.show()


def isLowBattery():
    curent_battery = get_charge_now()
    send_log("CURENT BATTERY : " + str(curent_battery))
    send_log("LOW BATTERY    : " + str(low_battery))

if(curent_battery < low_battery and curent_battery > critical_battery):
        sendWarning()
        send_log("low")


def isCriticalBattery():
    curent_battery = get_charge_now()
    if(curent_battery < critical_battery):
        summary = "ATTENTION BATTERIE VIDE !!"
        body = "MISE EN VEILLE IMMINENTE DE L'ORDINATEUR !!"
        notify2.init('warningbattery')
        n = notify2.Notification(summary, body, icone_crit)
        n.set_urgency(notify2.URGENCY_CRITICAL)
        n.set_timeout(notify_duration)
        n.show()


def main():
    while (True):
        # Est-il branché su secteur ?
        if(is_online()):
            send_log("c'est branché")
            time.sleep(30)
        else:
            send_log("c'est sur batterie !!!")
            isLowBattery()
            isCriticalBattery()
            time.sleep(60)


daemon = Daemonize(app="warningbattery", pid=pid, action=main)

if __name__ == "__main__":
    daemon.start()

---------------------------------

Il est lancé dans .config/openbox/autostart.sh
voir :
warningbattery
-----------------------
#! /bin/bash -x

# Dispose les bureaux dans openbox
setlayout 0 2 3 0
/usr/lib/notification-daemon/notification-daemon &
xset m 7/1 0
stalonetray &
warningbattery
wbar &
nm-applet &
~

--
Benoit

Ps.
Les critiques sont les bienvenues, mais c’est du bricolage maison.


Reply to: