Skip to content

Commit

Permalink
Merge pull request #4 from transistorgit/jkbms_ble
Browse files Browse the repository at this point in the history
fix disconnection behavior
  • Loading branch information
mr-manuel authored May 4, 2023
2 parents 56fecf3 + d647b1e commit f496103
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 38 deletions.
80 changes: 49 additions & 31 deletions etc/dbus-serialbattery/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,18 @@ def __init__(self, port, baud, address):
self.type = "Generic"
self.poll_interval = 1000
self.online = True

self.hardware_version = None
self.cell_count = None
# max battery charge/discharge current
self.max_battery_charge_current = None
self.max_battery_discharge_current = None

self.init_values()

# used to identify a BMS when multiple BMS are connected - planned for future use
self.unique_identifier = None

def init_values(self):
self.voltage = None
self.current = None
self.capacity_remain = None
Expand All @@ -78,7 +88,6 @@ def __init__(self, port, baud, address):
self.charge_fet = None
self.discharge_fet = None
self.balance_fet = None
self.cell_count = None
self.temp_sensors = None
self.temp1 = None
self.temp2 = None
Expand All @@ -101,12 +110,6 @@ def __init__(self, port, baud, address):
self.control_charge_current = None
self.control_allow_charge = None
self.control_allow_discharge = None
# max battery charge/discharge current
self.max_battery_charge_current = None
self.max_battery_discharge_current = None

# used to identify a BMS when multiple BMS are connected - planned for future use
self.unique_identifier = None

@abstractmethod
def test_connection(self) -> bool:
Expand Down Expand Up @@ -784,38 +787,53 @@ def extract_from_temp_values(self, extractor) -> Union[float, None]:
return None

def get_temp(self) -> Union[float, None]:
if utils.TEMP_BATTERY == 1:
return self.temp1
elif utils.TEMP_BATTERY == 2:
return self.temp2
else:
return self.extract_from_temp_values(
extractor=lambda temp1, temp2: round(
(float(temp1) + float(temp2)) / 2, 2
try:
if utils.TEMP_BATTERY == 1:
return self.temp1
elif utils.TEMP_BATTERY == 2:
return self.temp2
else:
return self.extract_from_temp_values(
extractor=lambda temp1, temp2: round(
(float(temp1) + float(temp2)) / 2, 2
)
)
)
except TypeError:
return None

def get_min_temp(self) -> Union[float, None]:
return self.extract_from_temp_values(
extractor=lambda temp1, temp2: min(temp1, temp2)
)
try:
return self.extract_from_temp_values(
extractor=lambda temp1, temp2: min(temp1, temp2)
)
except TypeError:
return None

def get_min_temp_id(self) -> Union[str, None]:
if self.temp1 < self.temp2:
return utils.TEMP_1_NAME
else:
return utils.TEMP_2_NAME
try:
if self.temp1 < self.temp2:
return utils.TEMP_1_NAME
else:
return utils.TEMP_2_NAME
except TypeError:
return None

def get_max_temp(self) -> Union[float, None]:
return self.extract_from_temp_values(
extractor=lambda temp1, temp2: max(temp1, temp2)
)
try:
return self.extract_from_temp_values(
extractor=lambda temp1, temp2: max(temp1, temp2)
)
except TypeError:
return None

def get_max_temp_id(self) -> Union[str, None]:
if self.temp1 > self.temp2:
return utils.TEMP_1_NAME
else:
return utils.TEMP_2_NAME
try:
if self.temp1 > self.temp2:
return utils.TEMP_1_NAME
else:
return utils.TEMP_2_NAME
except TypeError:
return None

def get_mos_temp(self) -> Union[float, None]:
if self.temp_mos is not None:
Expand Down
25 changes: 18 additions & 7 deletions etc/dbus-serialbattery/dbushelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ def publish_battery(self, loop):
# If the battery is offline for more than 10 polls (polled every second for most batteries)
if self.error_count >= 10:
self.battery.online = False
self.battery.init_values()

# Has it completely failed
if self.error_count >= 60:
loop.quit()
Expand All @@ -352,11 +354,19 @@ def publish_battery(self, loop):
def publish_dbus(self):
# Update SOC, DC and System items
self._dbusservice["/System/NrOfCellsPerBattery"] = self.battery.cell_count
self._dbusservice["/Soc"] = round(self.battery.soc, 2)
self._dbusservice["/Dc/0/Voltage"] = round(self.battery.voltage, 2)
self._dbusservice["/Dc/0/Current"] = round(self.battery.current, 2)
self._dbusservice["/Dc/0/Power"] = round(
self.battery.voltage * self.battery.current, 2
self._dbusservice["/Soc"] = (
round(self.battery.soc, 2) if self.battery.soc is not None else None
)
self._dbusservice["/Dc/0/Voltage"] = (
round(self.battery.voltage, 2) if self.battery.voltage is not None else None
)
self._dbusservice["/Dc/0/Current"] = (
round(self.battery.current, 2) if self.battery.current is not None else None
)
self._dbusservice["/Dc/0/Power"] = (
round(self.battery.voltage * self.battery.current, 2)
if self.battery.current is not None and self.battery.current is not None
else None
)
self._dbusservice["/Dc/0/Temperature"] = self.battery.get_temp()
self._dbusservice["/Capacity"] = self.battery.get_capacity_remain()
Expand Down Expand Up @@ -540,5 +550,6 @@ def publish_dbus(self):
except:
pass

logger.debug("logged to dbus [%s]" % str(round(self.battery.soc, 2)))
self.battery.log_cell_data()
if self.battery.soc is not None:
logger.debug("logged to dbus [%s]" % str(round(self.battery.soc, 2)))
self.battery.log_cell_data()

0 comments on commit f496103

Please sign in to comment.