Fixed plyer sensors

This commit is contained in:
Mark Qvist 2023-10-20 02:37:27 +02:00
parent 3ea7a9bbe4
commit 79c5aacc6f
3 changed files with 32 additions and 58 deletions

View File

@ -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()

View File

@ -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

View File

@ -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()