Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…albattery into dev
  • Loading branch information
mr-manuel committed Jun 5, 2023
2 parents aec7471 + f6219f6 commit 43cf469
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
13 changes: 12 additions & 1 deletion etc/dbus-serialbattery/battery.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from typing import Union, Tuple, List
from typing import Union, Tuple, List, Callable

from utils import logger
import utils
Expand Down Expand Up @@ -150,6 +150,17 @@ def get_settings(self) -> bool:
"""
return False

def use_callback(self, callback: Callable) -> bool:
"""
Each driver may override this function to indicate whether it is
able to provide value updates on its own.
:return: false when battery cannot provide updates by itself and will be polled
every poll_interval milliseconds for new values
true if callable should be used for updates as they arrive from the battery
"""
return False

@abstractmethod
def refresh_data(self) -> bool:
"""
Expand Down
5 changes: 5 additions & 0 deletions etc/dbus-serialbattery/bms/jkbms_ble.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from battery import Battery, Cell
from typing import Callable
from utils import logger
from bms.jkbms_brn import Jkbms_Brn
from bleak import BleakScanner, BleakError
Expand Down Expand Up @@ -141,6 +142,10 @@ def get_settings(self):
logger.info("BAT: " + self.hardware_version)
return True

def use_callback(self, callback: Callable) -> bool:
self.jk.set_callback(callback)
return callback is not None

def refresh_data(self):
# call all functions that will refresh the battery data.
# This will be called for every iteration (1 second)
Expand Down
7 changes: 7 additions & 0 deletions etc/dbus-serialbattery/bms/jkbms_brn.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class Jkbms_Brn:
waiting_for_response = ""
last_cell_info = 0

_new_data_callback = None

def __init__(self, addr):
self.address = addr
self.bt_thread = threading.Thread(target=self.connect_and_scrape)
Expand Down Expand Up @@ -240,6 +242,9 @@ def decode(self):
if self.waiting_for_response == "device_info":
self.waiting_for_response = ""

def set_callback(self, callback):
self._new_data_callback = callback

def assemble_frame(self, data: bytearray):
if len(self.frame_buffer) > MAX_RESPONSE_SIZE:
info("data dropped because it alone was longer than max frame length")
Expand All @@ -261,6 +266,8 @@ def assemble_frame(self, data: bytearray):
debug("great success! frame complete and sane, lets decode")
self.decode()
self.frame_buffer = []
if self._new_data_callback is not None:
self._new_data_callback()

def ncallback(self, sender: int, data: bytearray):
debug(f"------> NEW PACKAGE!laenge: {len(data)}")
Expand Down
8 changes: 6 additions & 2 deletions etc/dbus-serialbattery/dbus-serialbattery.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ def get_port() -> str:
logger.error("ERROR >>> Problem with battery set up at " + port)
sys.exit(1)

# Poll the battery at INTERVAL and run the main loop
gobject.timeout_add(battery.poll_interval, lambda: poll_battery(mainloop))
# try using active callback on this battery
if not battery.use_callback(lambda: poll_battery(mainloop)):
# if not possible, poll the battery every poll_interval milliseconds
gobject.timeout_add(battery.poll_interval, lambda: poll_battery(mainloop))

# Run the main loop
try:
mainloop.run()
except KeyboardInterrupt:
Expand Down

0 comments on commit 43cf469

Please sign in to comment.