Skip to content

Commit

Permalink
Moved some bluetooth specific code from each implementation to a comm…
Browse files Browse the repository at this point in the history
…on place (#56)
  • Loading branch information
calledit committed Jul 16, 2024
1 parent ed2f95d commit e083904
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
3 changes: 1 addition & 2 deletions etc/dbus-serialbattery/bms/jkbms_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class Jkbms_Ble(Battery):
resetting = False

def __init__(self, port, baud, address):
# add "ble_" to the port name, since only numbers are not valid
super(Jkbms_Ble, self).__init__(
"ble_" + address.replace(":", "").lower(), baud, address
port, baud, address
)
self.address = address
self.type = self.BATTERYTYPE
Expand Down
3 changes: 1 addition & 2 deletions etc/dbus-serialbattery/bms/lltjbd_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class LltJbd_Ble(LltJbd):
BATTERYTYPE = "LltJbd_Ble"

def __init__(self, port: Optional[str], baud: Optional[int], address: str):
# add "ble_" to the port name, since only numbers are not valid
super(LltJbd_Ble, self).__init__(
"ble_" + address.replace(":", "").lower(), -1, address
port, -1, address
)

self.address = address
Expand Down
34 changes: 20 additions & 14 deletions etc/dbus-serialbattery/dbus-serialbattery.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,30 @@ def get_port() -> str:
# else the error throw a lot of timeouts
sleep(16)

if port.endswith("_Ble") and len(sys.argv) > 2:
if port.endswith("_Ble"):
"""
Import ble classes only, if it's a ble port, else the driver won't start due to missing python modules
This prevent problems when using the driver only with a serial connection
"""
if port == "Jkbms_Ble":
# noqa: F401 --> ignore flake "imported but unused" error
from bms.jkbms_ble import Jkbms_Ble # noqa: F401

if port == "LltJbd_Ble":
# noqa: F401 --> ignore flake "imported but unused" error
from bms.lltjbd_ble import LltJbd_Ble # noqa: F401

class_ = eval(port)
testbms = class_("", 9600, sys.argv[2])
if testbms.test_connection():
logger.info("Connection established to " + testbms.__class__.__name__)
battery = testbms

if len(sys.argv) <= 2:
logger.error("missing bluetooth address")
else:
ble_address = sys.argv[2]

if port == "Jkbms_Ble":
# noqa: F401 --> ignore flake "imported but unused" error
from bms.jkbms_ble import Jkbms_Ble # noqa: F401

if port == "LltJbd_Ble":
# noqa: F401 --> ignore flake "imported but unused" error
from bms.lltjbd_ble import LltJbd_Ble # noqa: F401

class_ = eval(port)
testbms = class_("ble_" + ble_address.replace(":", "").lower(), 9600, ble_address)
if testbms.test_connection():
logger.info("Connection established to " + testbms.__class__.__name__)
battery = testbms

elif port.startswith("can"):
"""
Expand Down

0 comments on commit e083904

Please sign in to comment.