From 3153962aaa924065ad24ef2992545c8c0b3c53b6 Mon Sep 17 00:00:00 2001 From: Bernd Stahlbock Date: Mon, 1 May 2023 09:24:24 +0000 Subject: [PATCH 1/3] black reformatting --- etc/dbus-serialbattery/bms/daly.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/etc/dbus-serialbattery/bms/daly.py b/etc/dbus-serialbattery/bms/daly.py index 16fef5dd..917b1aa0 100644 --- a/etc/dbus-serialbattery/bms/daly.py +++ b/etc/dbus-serialbattery/bms/daly.py @@ -285,8 +285,8 @@ def read_cells_volts(self, ser): # logger.warning("data " + bytes(cells_volts_data).hex()) - while ( - bufIdx <= len(cells_volts_data) - (4 + 8 + 1) + while bufIdx <= len(cells_volts_data) - ( + 4 + 8 + 1 ): # we at least need 13 bytes to extract the identifiers + 8 bytes payload + checksum b1, b2, b3, b4 = unpack_from(">BBBB", cells_volts_data, bufIdx) if b1 == 0xA5 and b2 == 0x01 and b3 == 0x95 and b4 == 0x08: @@ -313,7 +313,7 @@ def read_cells_volts(self, ser): ) bufIdx += 13 # BBBBBhhhBB -> 13 byte else: - bufIdx += 1 # step through buffer to find valid start + bufIdx += 1 # step through buffer to find valid start logger.warning("bad cell voltages header") return True @@ -409,20 +409,22 @@ def read_serial_data_daly(self, ser, command): if len(data) <= 12: logger.debug("Too short reply to cmd " + bytes(command).hex()) - return False; + return False # search sentence start try: idx = data.index(0xA5) except ValueError: - logger.debug("No Sentence Start found for reply to cmd " + bytes(command).hex()) + logger.debug( + "No Sentence Start found for reply to cmd " + bytes(command).hex() + ) return False if len(data[idx:]) <= 12: logger.debug("Too short reply to cmd " + bytes(command).hex()) - return False; + return False - if data[12+idx] != sum(data[idx:12+idx]) & 0xFF: + if data[12 + idx] != sum(data[idx : 12 + idx]) & 0xFF: logger.debug("Bad checksum in reply to cmd " + bytes(command).hex()) return False @@ -431,7 +433,12 @@ def read_serial_data_daly(self, ser, command): if length == 8: return data[4 + idx : length + 4 + idx] else: - logger.debug(">>> ERROR: Incorrect Reply to CMD " + bytes(command).hex() + ": 0x" + bytes(data).hex()) + logger.debug( + ">>> ERROR: Incorrect Reply to CMD " + + bytes(command).hex() + + ": 0x" + + bytes(data).hex() + ) return False # Read data from previously openned serial port @@ -510,4 +517,4 @@ def read_serialport_data( except Exception as e: logger.error(e) - return False \ No newline at end of file + return False From bbb7b1bfb26c9dc1afb17bb51ca085585b0cc586 Mon Sep 17 00:00:00 2001 From: Bernd Stahlbock Date: Wed, 3 May 2023 20:23:32 +0000 Subject: [PATCH 2/3] fix disconnection behaviour: on disconnect, show '---' after 10s and 'not connected' after 60s --- etc/dbus-serialbattery/battery.py | 121 ++++++++++++++++++--------- etc/dbus-serialbattery/dbushelper.py | 25 ++++-- 2 files changed, 100 insertions(+), 46 deletions(-) diff --git a/etc/dbus-serialbattery/battery.py b/etc/dbus-serialbattery/battery.py index 6b40846a..68e6e77b 100644 --- a/etc/dbus-serialbattery/battery.py +++ b/etc/dbus-serialbattery/battery.py @@ -62,8 +62,15 @@ def __init__(self, port, baud, address): self.type = "Generic" self.poll_interval = 1000 self.online = True - self.hardware_version = None + self.cell_count = 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 @@ -78,7 +85,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 @@ -103,9 +109,6 @@ def __init__(self, port, baud, address): 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: """ @@ -306,12 +309,20 @@ def manage_charge_current(self) -> None: # Manage Charge Current Limitations charge_limits = [ self.max_battery_charge_current + if self.max_battery_charge_current is not None + else 0 ] # gets removed after finished testing - charge_limits_new = {self.max_battery_charge_current: "None (Max Config Limit)"} + charge_limits_new = { + self.max_battery_charge_current + if self.max_battery_charge_current is not None + else 0: "None (Max Config Limit)" + } if utils.CCCM_CV_ENABLE: tmp = self.calcMaxChargeCurrentReferringToCellVoltage() - charge_limits.append(tmp) # gets removed after finished testing + charge_limits.append( + tmp if tmp is not None else 0 + ) # gets removed after finished testing # logging.error("self.max_battery_charge_current: " # + str(self.max_battery_charge_current) @@ -327,7 +338,9 @@ def manage_charge_current(self) -> None: if utils.CCCM_T_ENABLE: tmp = self.calcMaxChargeCurrentReferringToTemperature() - charge_limits.append(tmp) # gets removed after finished testing + charge_limits.append( + tmp if tmp is not None else 0 + ) # gets removed after finished testing # logging.error("self.max_battery_charge_current: " # + str(self.max_battery_charge_current) @@ -341,7 +354,9 @@ def manage_charge_current(self) -> None: if utils.CCCM_SOC_ENABLE: tmp = self.calcMaxChargeCurrentReferringToSoc() - charge_limits.append(tmp) # gets removed after finished testing + charge_limits.append( + tmp if tmp is not None else 0 + ) # gets removed after finished testing # logging.error("self.max_battery_charge_current: " # + str(self.max_battery_charge_current) @@ -372,14 +387,20 @@ def manage_charge_current(self) -> None: # Manage Discharge Current Limitations discharge_limits = [ self.max_battery_discharge_current + if self.max_battery_discharge_current is not None + else 0 ] # gets removed after finished testing discharge_limits_new = { - self.max_battery_discharge_current: "None (Max Config Limit)" + self.max_battery_discharge_current + if self.max_battery_discharge_current is not None + else 0: "None (Max Config Limit)" } if utils.DCCM_CV_ENABLE: tmp = self.calcMaxDischargeCurrentReferringToCellVoltage() - discharge_limits.append(tmp) # gets removed after finished testing + discharge_limits.append( + tmp if tmp is not None else 0 + ) # gets removed after finished testing # logging.error("self.max_battery_discharge_current: " # + str(self.max_battery_discharge_current) @@ -395,7 +416,9 @@ def manage_charge_current(self) -> None: if utils.DCCM_T_ENABLE: tmp = self.calcMaxDischargeCurrentReferringToTemperature() - discharge_limits.append(tmp) # gets removed after finished testing + discharge_limits.append( + tmp if tmp is not None else 0 + ) # gets removed after finished testing # logging.error("self.max_battery_discharge_current: " # + str(self.max_battery_discharge_current) @@ -411,7 +434,9 @@ def manage_charge_current(self) -> None: if utils.DCCM_SOC_ENABLE: tmp = self.calcMaxDischargeCurrentReferringToSoc() - discharge_limits.append(tmp) # gets removed after finished testing + discharge_limits.append( + tmp if tmp is not None else 0 + ) # gets removed after finished testing # logging.error("self.max_battery_discharge_current: " # + str(self.max_battery_discharge_current) @@ -772,43 +797,61 @@ 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: + 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: + 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: + 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: + 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: + return None def get_mos_temp(self) -> Union[float, None]: - if self.temp_mos is not None: - return self.temp_mos - else: + try: + if self.temp_mos is not None: + return self.temp_mos + else: + return None + except: return None def log_cell_data(self) -> bool: diff --git a/etc/dbus-serialbattery/dbushelper.py b/etc/dbus-serialbattery/dbushelper.py index 246a1aad..578ab782 100644 --- a/etc/dbus-serialbattery/dbushelper.py +++ b/etc/dbus-serialbattery/dbushelper.py @@ -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() @@ -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() @@ -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() From d647b1e62593da9145df1895897852b9217998ee Mon Sep 17 00:00:00 2001 From: Bernd Stahlbock Date: Wed, 3 May 2023 21:13:35 +0000 Subject: [PATCH 3/3] fix flake errors --- etc/dbus-serialbattery/battery.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/etc/dbus-serialbattery/battery.py b/etc/dbus-serialbattery/battery.py index cf269c96..44275371 100644 --- a/etc/dbus-serialbattery/battery.py +++ b/etc/dbus-serialbattery/battery.py @@ -798,7 +798,7 @@ def get_temp(self) -> Union[float, None]: (float(temp1) + float(temp2)) / 2, 2 ) ) - except: + except TypeError: return None def get_min_temp(self) -> Union[float, None]: @@ -806,7 +806,7 @@ def get_min_temp(self) -> Union[float, None]: return self.extract_from_temp_values( extractor=lambda temp1, temp2: min(temp1, temp2) ) - except: + except TypeError: return None def get_min_temp_id(self) -> Union[str, None]: @@ -815,7 +815,7 @@ def get_min_temp_id(self) -> Union[str, None]: return utils.TEMP_1_NAME else: return utils.TEMP_2_NAME - except: + except TypeError: return None def get_max_temp(self) -> Union[float, None]: @@ -823,7 +823,7 @@ def get_max_temp(self) -> Union[float, None]: return self.extract_from_temp_values( extractor=lambda temp1, temp2: max(temp1, temp2) ) - except: + except TypeError: return None def get_max_temp_id(self) -> Union[str, None]: @@ -832,16 +832,13 @@ def get_max_temp_id(self) -> Union[str, None]: return utils.TEMP_1_NAME else: return utils.TEMP_2_NAME - except: + except TypeError: return None def get_mos_temp(self) -> Union[float, None]: - try: - if self.temp_mos is not None: - return self.temp_mos - else: - return None - except: + if self.temp_mos is not None: + return self.temp_mos + else: return None def log_cell_data(self) -> bool: