From 79c5aacc6fa191313c8408b1b7ab86ba61e01a3c Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 20 Oct 2023 02:37:27 +0200 Subject: [PATCH] Fixed plyer sensors --- sbapp/plyer/platforms/android/gps.py | 8 ++- sbapp/plyer/platforms/android/humidity.py | 3 + sbapp/plyer/platforms/linux/battery.py | 79 +++++++---------------- 3 files changed, 32 insertions(+), 58 deletions(-) diff --git a/sbapp/plyer/platforms/android/gps.py b/sbapp/plyer/platforms/android/gps.py index 17fd86e..0d12cfd 100644 --- a/sbapp/plyer/platforms/android/gps.py +++ b/sbapp/plyer/platforms/android/gps.py @@ -29,6 +29,12 @@ class _LocationListener(PythonJavaClass): altitude=location.getAltitude(), accuracy=location.getAccuracy()) + @java_method('(Ljava/util/List;)V', name='onLocationChanged') + def onLocationChangedList(self, location_list): + ''' Called when location data is sent in a batch (API31) ''' + location = location_list.get(location_list.size() - 1) + self.onLocationChanged(location) + @java_method('(Ljava/lang/String;)V') def onProviderEnabled(self, status): if self.root.on_status: @@ -79,4 +85,4 @@ class AndroidGPS(GPS): def instance(): - return AndroidGPS() + return AndroidGPS() \ No newline at end of file diff --git a/sbapp/plyer/platforms/android/humidity.py b/sbapp/plyer/platforms/android/humidity.py index 78e17ac..437823c 100644 --- a/sbapp/plyer/platforms/android/humidity.py +++ b/sbapp/plyer/platforms/android/humidity.py @@ -82,6 +82,9 @@ class AndroidHumidity(Humidity): Tc = self.listener_a.value A = 6.112 K = 273.15 + if Rh == None or Tc == None: + return None + humidity = (Ta * (Rh / 100) * A * exp(m * Tc / (Tn + Tc)) / (K + Tc)) return humidity diff --git a/sbapp/plyer/platforms/linux/battery.py b/sbapp/plyer/platforms/linux/battery.py index fa96592..b0c4691 100644 --- a/sbapp/plyer/platforms/linux/battery.py +++ b/sbapp/plyer/platforms/linux/battery.py @@ -2,6 +2,7 @@ Module of Linux API for plyer.battery. ''' +import os from math import floor from os import environ from os.path import exists, join @@ -19,10 +20,10 @@ class LinuxBattery(Battery): def _get_state(self): status = {"isCharging": None, "percentage": None} - kernel_bat_path = join('/sys', 'class', 'power_supply', 'BAT0') + kernel_bat_path = join('/sys', 'class', 'power_supply', self.node_name) uevent = join(kernel_bat_path, 'uevent') - with open(uevent) as fle: + with open(uevent, "rb") as fle: lines = [ line.decode('utf-8').strip() for line in fle.readlines() @@ -33,70 +34,34 @@ class LinuxBattery(Battery): } is_charging = output['POWER_SUPPLY_STATUS'] == 'Charging' - total = float(output['POWER_SUPPLY_CHARGE_FULL']) - now = float(output['POWER_SUPPLY_CHARGE_NOW']) + charge_percent = float(output['POWER_SUPPLY_CAPACITY']) - capacity = floor(now / total * 100) - - status['percentage'] = capacity + status['percentage'] = charge_percent status['isCharging'] = is_charging return status -class UPowerBattery(Battery): - ''' - Implementation of UPower battery API. - ''' - - def _get_state(self): - # if no LANG specified, return empty string - old_lang = environ.get('LANG', '') - environ['LANG'] = 'C' - status = {"isCharging": None, "percentage": None} - - # We are supporting only one battery now - # this will fail if there is no object with such path, - # however it's safer than 'upower -d' which provides - # multiple unrelated 'state' and 'percentage' keywords - dev = "/org/freedesktop/UPower/devices/battery_BAT0" - upower_process = Popen( - ["upower", "--show-info", dev], - stdout=PIPE - ) - output = upower_process.communicate()[0].decode() - environ['LANG'] = old_lang - if not output: - return status - state = percentage = None - - for line in output.splitlines(): - if 'state' in line: - state = line.rpartition(':')[-1].strip() - - if 'percentage' in line: - percentage = line.rpartition(':')[-1].strip()[:-1] - - # switching decimal comma to dot - # (different LC_NUMERIC locale) - percentage = float( - percentage.replace(',', '.') - ) - - if state: - status['isCharging'] = state == "charging" - status['percentage'] = percentage - return status - - def instance(): ''' Instance for facade proxy. ''' import sys - if whereis_exe('upower'): - return UPowerBattery() - sys.stderr.write("upower not found.") + # if whereis_exe('upower'): + # return UPowerBattery() + # sys.stderr.write("upower not found.") + + node_exists = False + bn = 0 + node_name = None + for bi in range(0,10): + path = join('/sys', 'class', 'power_supply', 'BAT'+str(bi)) + if os.path.isdir(path): + node_name = "BAT"+str(bi) + break + + if node_name: + b = LinuxBattery() + b.node_name = node_name + return b - if exists(join('/sys', 'class', 'power_supply', 'BAT0')): - return LinuxBattery() return Battery()