Skip to content

Commit

Permalink
changed unique identifier from string to function
Browse files Browse the repository at this point in the history
function can be overridden by BMS battery class
  • Loading branch information
mr-manuel committed Jun 10, 2023
1 parent fee9623 commit a81bf05
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v1.0.x
* Added: Bluetooth: Show signal strenght of BMS in log by @mr-manuel
* Added: Create unique identifier, if not provided from BMS by @mr-manuel
* Added: Exclude a device from beeing used by the dbus-serialbattery driver by @mr-manuel
* Added: Implement callback function for update by @seidler2547
* Added: JKBMS BLE - Show last five characters from the MAC address in the custom name (which is displayed in the device list) by @mr-manuel
Expand Down
24 changes: 17 additions & 7 deletions etc/dbus-serialbattery/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,12 @@ def __init__(self, port, baud, address):
self.max_battery_discharge_current = None
self.has_settings = 0

self.init_values()

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

# fetched from the BMS from a field where the user can input a custom string
# only if available
self.custom_field = None

self.init_values()

def init_values(self):
self.voltage = None
self.current = None
Expand Down Expand Up @@ -131,6 +128,20 @@ def test_connection(self) -> bool:
# return false when failed, true if successful
return False

def unique_identifier(self) -> str:
"""
Used to identify a BMS when multiple BMS are connected
If not provided by the BMS/driver then the hardware version and capacity is used,
since it can be changed by small amounts to make a battery unique.
On +/- 5 Ah you can identify 11 batteries
"""
return (
"".join(filter(str.isalnum, self.hardware_version))
+ "_"
+ str(self.capacity)
+ "Ah"
)

def connection_name(self) -> str:
return "Serial " + self.port

Expand Down Expand Up @@ -1005,8 +1016,7 @@ def log_settings(self) -> None:
logger.info(
f"> CCCM SOC: {str(utils.CCCM_SOC_ENABLE).ljust(5)} | DCCM SOC: {utils.DCCM_SOC_ENABLE}"
)
if self.unique_identifier is not None:
logger.info(f"Serial Number/Unique Identifier: {self.unique_identifier}")
logger.info(f"Serial Number/Unique Identifier: {self.unique_identifier()}")

return

Expand Down
14 changes: 9 additions & 5 deletions etc/dbus-serialbattery/bms/battery_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ def test_connection(self):

return result

def unique_identifier(self) -> str:
"""
Used to identify a BMS when multiple BMS are connected
Provide a unique identifier from the BMS to identify a BMS, if multiple same BMS are connected
e.g. the serial number
If there is no such value, please remove this function
"""
return self.serialnumber

def get_settings(self):
# After successful connection get_settings will be call to set up the battery.
# Set the current limits, populate cell count, etc
Expand All @@ -53,11 +62,6 @@ def get_settings(self):
self.max_battery_voltage = utils.MAX_CELL_VOLTAGE * self.cell_count
self.min_battery_voltage = utils.MIN_CELL_VOLTAGE * self.cell_count

# provide a unique identifier from the BMS to identify a BMS, if multiple same BMS are connected
# e.g. the serial number
# If there is no such value, please leave the line commented. In this case the capacity is used,
# since it can be changed by small amounts to make a battery unique. On +/- 5 Ah you can identify 11 batteries
# self.unique_identifier = str()
return True

def refresh_data(self):
Expand Down
14 changes: 9 additions & 5 deletions etc/dbus-serialbattery/bms/daly.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,17 @@ def read_battery_code(self, ser):
" ",
(battery_code.strip()),
)
self.unique_identifier = self.custom_field.replace(" ", "_")
else:
self.unique_identifier = (
str(self.production) + "_" + str(int(self.capacity))
)
return True

def unique_identifier(self) -> str:
"""
Used to identify a BMS when multiple BMS are connected
"""
if self.custom_field != "":
return self.custom_field.replace(" ", "_")
else:
return str(self.production) + "_" + str(int(self.capacity))

def reset_soc_callback(self, path, value):
if value is None:
return False
Expand Down
11 changes: 9 additions & 2 deletions etc/dbus-serialbattery/bms/heltecmodbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class HeltecModbus(Battery):
def __init__(self, port, baud, address):
super(HeltecModbus, self).__init__(port, baud, address)
self.type = "Heltec_Smart"
self.unique_identifier_tmp = ""

def test_connection(self):
# call a function that will connect to the battery, send a command and retrieve the result.
Expand Down Expand Up @@ -174,7 +175,7 @@ def read_status_data(self):
time.sleep(SLPTIME)

serial1 = mbdev.read_registers(2, number_of_registers=4)
self.unique_identifier = "-".join(
self.unique_identifier_tmp = "-".join(
"{:04x}".format(x) for x in serial1
)
time.sleep(SLPTIME)
Expand Down Expand Up @@ -234,7 +235,7 @@ def read_status_data(self):
logger.info(self.hardware_version)
logger.info("Heltec-" + self.hwTypeName)
logger.info(" Dev name: " + self.devName)
logger.info(" Serial: " + self.unique_identifier)
logger.info(" Serial: " + self.unique_identifier_tmp)
logger.info(" Made on: " + self.production_date)
logger.info(" Cell count: " + str(self.cell_count))
logger.info(" Cell type: " + self.cellType)
Expand All @@ -245,6 +246,12 @@ def read_status_data(self):

return True

def unique_identifier(self) -> str:
"""
Used to identify a BMS when multiple BMS are connected
"""
return self.unique_identifier_tmp

def read_soc_data(self):
mbdev = mbdevs[self.address]

Expand Down
11 changes: 9 additions & 2 deletions etc/dbus-serialbattery/bms/jkbms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Jkbms(Battery):
def __init__(self, port, baud, address):
super(Jkbms, self).__init__(port, baud, address)
self.type = self.BATTERYTYPE
self.unique_identifier_tmp = ""

BATTERYTYPE = "Jkbms"
LENGTH_CHECK = 1
Expand Down Expand Up @@ -184,9 +185,9 @@ def read_status_data(self):
)[0].decode()

offset = cellbyte_count + 197
self.unique_identifier = sub(
self.unique_identifier_tmp = sub(
" +",
" ",
"_",
(
unpack_from(">24s", self.get_data(status_data, b"\xBA", offset, 24))[0]
.decode()
Expand All @@ -209,6 +210,12 @@ def read_status_data(self):
# logger.info(self.hardware_version)
return True

def unique_identifier(self) -> str:
"""
Used to identify a BMS when multiple BMS are connected
"""
return self.unique_identifier_tmp

def to_fet_bits(self, byte_data):
tmp = bin(byte_data)[2:].rjust(3, utils.zero_char)
self.charge_fet = is_bit_set(tmp[2])
Expand Down
11 changes: 10 additions & 1 deletion etc/dbus-serialbattery/bms/jkbms_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, port, baud, address):
self.address = address
self.type = self.BATTERYTYPE
self.jk = Jkbms_Brn(address)
self.unique_identifier_tmp = ""

logger.info("Init of Jkbms_Ble at " + address)

Expand Down Expand Up @@ -91,7 +92,9 @@ def get_settings(self):
tmp = self.jk.get_status()["device_info"]["manufacturing_date"]
self.production = "20" + tmp if tmp and tmp != "" else None

self.unique_identifier = self.jk.get_status()["device_info"]["serial_number"]
self.unique_identifier_tmp = self.jk.get_status()["device_info"][
"serial_number"
]

for c in range(self.cell_count):
self.cells.append(Cell(False))
Expand All @@ -109,6 +112,12 @@ def get_settings(self):
logger.info("BAT: " + self.hardware_version)
return True

def unique_identifier(self) -> str:
"""
Used to identify a BMS when multiple BMS are connected
"""
return self.unique_identifier_tmp

def use_callback(self, callback: Callable) -> bool:
self.jk.set_callback(callback)
return callback is not None
Expand Down
2 changes: 1 addition & 1 deletion etc/dbus-serialbattery/dbushelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def setup_vedbus(self):
onchangecallback=self.battery.custom_name_callback,
)
self._dbusservice.add_path(
"/Serial", self.battery.unique_identifier, writeable=True
"/Serial", self.battery.unique_identifier(), writeable=True
)
self._dbusservice.add_path(
"/DeviceName", self.battery.custom_field, writeable=True
Expand Down

0 comments on commit a81bf05

Please sign in to comment.