| 1 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
|
| 3 |
""" |
| 4 |
This will do until I can be bothered to create indicator applet to do battery level |
| 5 |
""" |
| 6 |
import logging |
| 7 |
import threading |
| 8 |
import datetime |
| 9 |
import time |
| 10 |
import subprocess |
| 11 |
|
| 12 |
|
| 13 |
class BatteryNotifier(threading.Thread): |
| 14 |
""" |
| 15 |
Thread to notify about battery |
| 16 |
""" |
| 17 |
|
| 18 |
def __init__(self, parent, device_id, device_name): |
| 19 |
super().__init__() |
| 20 |
self._logger = logging.getLogger('razer.device{0}.batterynotifier'.format(device_id)) |
| 21 |
|
| 22 |
self.event = threading.Event() |
| 23 |
self.frequency = 0 |
| 24 |
self.percent = 0 |
| 25 |
|
| 26 |
self._shutdown = False |
| 27 |
self._device_name = device_name |
| 28 |
|
| 29 |
# Could save reference to parent but only need battery level function |
| 30 |
self._get_battery_func = parent.getBattery |
| 31 |
|
| 32 |
self._last_notify_time = datetime.datetime(1970, 1, 1) |
| 33 |
|
| 34 |
@property |
| 35 |
def shutdown(self): |
| 36 |
""" |
| 37 |
Get the shutdown flag |
| 38 |
""" |
| 39 |
return self._shutdown |
| 40 |
|
| 41 |
@shutdown.setter |
| 42 |
def shutdown(self, value): |
| 43 |
""" |
| 44 |
Set the shutdown flag |
| 45 |
|
| 46 |
:param value: Shutdown |
| 47 |
:type value: bool |
| 48 |
""" |
| 49 |
self._shutdown = value |
| 50 |
|
| 51 |
def show_notification(self, summary: str, message: str, icon: str) -> None: |
| 52 |
try: |
| 53 |
subprocess.run(["notify-send", "-a", "OpenRazer", "-i", icon, "-t", "4000", summary, message], |
| 54 |
check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 55 |
except subprocess.CalledProcessError as e: |
| 56 |
self._logger.warning(f"Failed to show notification: {e.output.strip()}") |
| 57 |
|
| 58 |
def notify_battery(self): |
| 59 |
now = datetime.datetime.now() |
| 60 |
|
| 61 |
if (now - self._last_notify_time).seconds > self.frequency: |
| 62 |
battery_level = self._get_battery_func() |
| 63 |
battery_percent = round(battery_level) |
| 64 |
|
| 65 |
# Sometimes due to various issues we don't get the percentage correctly. |
| 66 |
# Just ignore them and don't show a bogus notification. |
| 67 |
# See also: https://github.com/openrazer/openrazer/issues/2122 |
| 68 |
if battery_level in (0.0, -1.0): |
| 69 |
self._logger.debug("Got bogus battery value: {0}, ignoring.".format(battery_level)) |
| 70 |
# Since we don't update _last_notify_time here we're going to retry very soon again. |
| 71 |
# Sleep a bit so we don't spam the device with requests. |
| 72 |
time.sleep(10) |
| 73 |
return |
| 74 |
|
| 75 |
# Update the last notified time so that we alert in the configured frequency. |
| 76 |
self._last_notify_time = now |
| 77 |
|
| 78 |
title = self._device_name |
| 79 |
message = "Battery is {0}%".format(battery_percent) |
| 80 |
icon = "battery-full" |
| 81 |
|
| 82 |
if battery_level <= 10.0: |
| 83 |
message = "Battery is low ({0}%). Please charge your device".format(battery_percent) |
| 84 |
icon = "battery-empty" |
| 85 |
|
| 86 |
elif battery_level <= 30.0: |
| 87 |
icon = "battery-low" |
| 88 |
|
| 89 |
elif battery_level <= 70.0: |
| 90 |
icon = "battery-good" |
| 91 |
|
| 92 |
elif battery_level == 100.0: |
| 93 |
message = "Battery is fully charged ({0}%)".format(battery_percent) |
| 94 |
|
| 95 |
self._logger.debug("{0} Battery at {1}%".format(self._device_name, battery_percent)) |
| 96 |
|
| 97 |
if battery_level <= self.percent: |
| 98 |
self.show_notification(summary=title, message=message, icon=icon) |
| 99 |
|
| 100 |
def run(self): |
| 101 |
""" |
| 102 |
Main thread function |
| 103 |
""" |
| 104 |
|
| 105 |
while not self._shutdown: |
| 106 |
if self.event.is_set() and self.frequency > 0: |
| 107 |
self.notify_battery() |
| 108 |
|
| 109 |
time.sleep(0.1) |
| 110 |
|
| 111 |
self._logger.debug("Shutting down battery notifier") |
| 112 |
|
| 113 |
|
| 114 |
class BatteryManager(object): |
| 115 |
""" |
| 116 |
Class which manages the overall process of notifing battery levels |
| 117 |
""" |
| 118 |
|
| 119 |
def __init__(self, parent, device_number, device_name): |
| 120 |
self._logger = logging.getLogger('razer.device{0}.batterymanager'.format(device_number)) |
| 121 |
self._parent = parent |
| 122 |
|
| 123 |
self._battery_thread = BatteryNotifier(parent, device_number, device_name) |
| 124 |
self._battery_thread.start() |
| 125 |
|
| 126 |
self._is_closed = False |
| 127 |
|
| 128 |
def close(self): |
| 129 |
""" |
| 130 |
Close the manager, stop ripple thread |
| 131 |
""" |
| 132 |
if not self._is_closed: |
| 133 |
self._logger.debug("Closing Battery Manager") |
| 134 |
self._is_closed = True |
| 135 |
|
| 136 |
self._battery_thread.shutdown = True |
| 137 |
self._battery_thread.join(timeout=2) |
| 138 |
if self._battery_thread.is_alive(): |
| 139 |
self._logger.error("Could not stop BatteryNotify thread") |
| 140 |
|
| 141 |
def __del__(self): |
| 142 |
self.close() |
| 143 |
|
| 144 |
@property |
| 145 |
def active(self): |
| 146 |
return self._battery_thread.event.is_set() |
| 147 |
|
| 148 |
@active.setter |
| 149 |
def active(self, value): |
| 150 |
if value: |
| 151 |
self._battery_thread.event.set() |
| 152 |
else: |
| 153 |
self._battery_thread.event.clear() |
| 154 |
|
| 155 |
@property |
| 156 |
def frequency(self): |
| 157 |
return self._battery_thread.frequency |
| 158 |
|
| 159 |
@frequency.setter |
| 160 |
def frequency(self, frequency): |
| 161 |
self._battery_thread.frequency = frequency |
| 162 |
|
| 163 |
@property |
| 164 |
def percent(self): |
| 165 |
return self._battery_thread.percent |
| 166 |
|
| 167 |
@percent.setter |
| 168 |
def percent(self, percent): |
| 169 |
self._battery_thread.percent = percent |