Skip to content

Commit

Permalink
Merge branch 'dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel authored Jan 28, 2024
2 parents 2da6bae + 3c2401a commit fe52db6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion etc/dbus-serialbattery/bms/jkbms_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, port, baud, address):
super(Jkbms_Ble, self).__init__(address.replace(":", "").lower(), baud, address)
self.address = address
self.type = self.BATTERYTYPE
self.jk = Jkbms_Brn(address)
self.jk = Jkbms_Brn(address, lambda: self.reset_bluetooth())
self.unique_identifier_tmp = ""

logger.info("Init of Jkbms_Ble at " + address)
Expand Down
44 changes: 32 additions & 12 deletions etc/dbus-serialbattery/bms/jkbms_brn.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,14 @@ class Jkbms_Brn:
# translate info placeholder, since it depends on the bms_max_cell_count
translate_cell_info = []

def __init__(self, addr):
def __init__(self, addr, reset_bt_callback=None):
self.address = addr
self.bt_thread = threading.Thread(
target=self.connect_and_scrape, name="Thread-JKBMS-BLE"
self.bt_thread = None
self.bt_thread_monitor = threading.Thread(
target=self.monitor_scraping, name="Thread-JKBMS-Monitor"
)
self.bt_reset = reset_bt_callback
self.should_be_scraping = False
self.trigger_soc_reset = False

async def scanForDevices(self):
Expand Down Expand Up @@ -496,21 +499,36 @@ async def asy_connect_and_scrape(self):

logger.info("--> asy_connect_and_scrape(): Exit")

def monitor_scraping(self):
while self.should_be_scraping == True:
self.bt_thread = threading.Thread(
target=self.connect_and_scrape, name="Thread-JKBMS-Connect-and-Scrape"
)
self.bt_thread.start()
logger.debug(
"scraping thread started -> main thread id: "
+ str(self.main_thread.ident)
+ " scraping thread: "
+ str(self.bt_thread.ident)
)
self.bt_thread.join()
if self.should_be_scraping == True:
logger.debug("scraping thread ended: reseting bluetooth and restarting")
if not self.bt_reset == None:
self.bt_reset()
sleep(2)

def start_scraping(self):
self.main_thread = threading.current_thread()
if self.is_running():
logger.debug("screaping thread already running")
logger.debug("scraping thread already running")
return
self.bt_thread.start()
logger.debug(
"scraping thread started -> main thread id: "
+ str(self.main_thread.ident)
+ " scraping thread: "
+ str(self.bt_thread.ident)
)
self.should_be_scraping = True
self.bt_thread_monitor.start()

def stop_scraping(self):
self.run = False
self.should_be_scraping = False
stop = time()
while self.is_running():
sleep(0.1)
Expand All @@ -519,7 +537,9 @@ def stop_scraping(self):
return True

def is_running(self):
return self.bt_thread.is_alive()
if self.bt_thread is not None:
return self.bt_thread.is_alive()
return False

async def enable_charging(self, c):
# these are the registers for the control-buttons:
Expand Down

0 comments on commit fe52db6

Please sign in to comment.