diff --git a/etc/dbus-serialbattery/lltjbd.py b/etc/dbus-serialbattery/lltjbd.py index e616171..62aa2ff 100644 --- a/etc/dbus-serialbattery/lltjbd.py +++ b/etc/dbus-serialbattery/lltjbd.py @@ -44,8 +44,27 @@ class LltJbdProtection(Protection): class LltJbd(Battery): - def __init__(self, port,baud): - super(LltJbd, self).__init__(port,baud) + def __init__(self, port: str, baud): + if '?' in port: + import atexit, signal, sys + BLE_SERVICE_UUID = "0000ff00-0000-1000-8000-00805f9b34fb" + BLE_CHARACTERISTICS_TX_UUID = "0000ff02-0000-1000-8000-00805f9b34fb" + BLE_CHARACTERISTICS_RX_UUID = "0000ff01-0000-1000-8000-00805f9b34fb" + (port, query) = port.split('?', 2) + if 'ble=' in query: + serial = query[4:] + import subprocess + ble_serial_args = [sys.executable, '-m', 'ble_serial', '-p', port, '-s', BLE_SERVICE_UUID, '-w', BLE_CHARACTERISTICS_TX_UUID, '-r', BLE_CHARACTERISTICS_RX_UUID] + if serial: + ble_serial_args += ['-d', serial] + self.ble = subprocess.Popen(ble_serial_args, stderr=subprocess.PIPE) + for line in iter(self.ble.stderr.readline, ''): + if b"Running main loop" in line: + atexit.register(self.ble.send_signal, signal.SIGINT) + break + if b"Shutdown complete" in line: + break + super(LltJbd, self).__init__(port, baud) self.protection = LltJbdProtection() self.type = self.BATTERYTYPE @@ -181,4 +200,14 @@ class LltJbd(Battery): return data[4:length + 4] else: logger.error(">>> ERROR: Incorrect Reply") - return False \ No newline at end of file + return False + + +def testBLE(): + import sys + bat = LltJbd(sys.argv[1], sys.argv[2]) + bat.refresh_data() + + +if __name__ == "__main__": + testBLE() diff --git a/requirements.txt b/requirements.txt index 4cffd76..9f0f03a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pyserial==3.5 velib_python==2.80 -minimalmodbus==2.0.1 \ No newline at end of file +minimalmodbus==2.0.1 +ble-serial==2.6.1 \ No newline at end of file