Skip to content

Commit

Permalink
Bind device instance to unique_identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Nov 26, 2023
1 parent 28a5174 commit 74f8dc3
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 165 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Changelog

## Notes

* The Bluetooth and CAN connections are still not stable on some systems. If you want to have a stable connection use the serial connection.

## Breaking changes

* Driver version greater or equal to `v1.0.20231126beta`

The custom name is not saved to the config file anymore, but to the dbus service com.victronenergy.settings. You have to re-enter it once.

* Driver version greater or equal to `v1.0.20230629beta` and smaller or equal to `v1.0.20230926beta`:

With `v1.0.20230927beta` the following values changed names:
Expand All @@ -26,6 +34,7 @@
* Added: Load to SOC reset voltage every x days to reset the SoC to 100% for some BMS by @mr-manuel
* Added: Save custom name and make it restart persistant by @mr-manuel
* Added: Temperature names to dbus and mqtt by @mr-manuel
* Added: The device instance does not change anymore when you plug the BMS into another USB port. Fixed https://github.com/Louisvdw/dbus-serialbattery/issues/718 by @mr-manuel
* Added: Use current average of the last 300 cycles for time to go and time to SoC calculation by @mr-manuel
* Added: Validate current, voltage, capacity and SoC for all BMS. This prevents that a device, which is no BMS, is detected as BMS. Fixes also https://github.com/Louisvdw/dbus-serialbattery/issues/479 by @mr-manuel
* Changed: `VOLTAGE_DROP` now behaves differently. Before it reduced the voltage for the check, now the voltage for the charger is increased in order to get the target voltage on the BMS by @mr-manuel
Expand Down
142 changes: 0 additions & 142 deletions etc/dbus-serialbattery/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import math
from time import time
from abc import ABC, abstractmethod
import re
import sys


class Protection(object):
Expand Down Expand Up @@ -1253,146 +1251,6 @@ def log_settings(self) -> None:

return

# save custom name to config file
def custom_name_callback(self, path, value):
try:
if path == "/CustomName":
file = open(
"/data/etc/dbus-serialbattery/" + utils.PATH_CONFIG_USER, "r"
)
lines = file.readlines()
last = len(lines)

# remove not allowed characters
value = value.replace(":", "").replace("=", "").replace(",", "").strip()

# empty string to save new config file
config_file_new = ""

# make sure we are in the [DEFAULT] section
current_line_in_default_section = False
default_section_checked = False

# check if already exists
exists = False

# count lines
i = 0
# looping through the file
for line in lines:
# increment by one
i += 1

# stripping line break
line = line.strip()

# check, if current line is after the [DEFAULT] section
if line == "[DEFAULT]":
current_line_in_default_section = True

# check, if current line starts a new section
if line != "[DEFAULT]" and re.match(r"^\[.*\]", line):
# set default_section_checked to true, if it was already checked and a new section comes on
if current_line_in_default_section and not exists:
default_section_checked = True
current_line_in_default_section = False

# check, if the current line is the last line
if i == last:
default_section_checked = True

# insert or replace only in [DEFAULT] section
if current_line_in_default_section and re.match(
r"^CUSTOM_BATTERY_NAMES.*", line
):
# set that the setting was found, else a new one is created
exists = True

# remove setting name
line = re.sub(
"^CUSTOM_BATTERY_NAMES\s*=\s*", "", line # noqa: W605
)

# change only the name of the current BMS
result = []
bms_name_list = line.split(",")
for bms_name_pair in bms_name_list:
tmp = bms_name_pair.split(":")
if tmp[0] == self.port:
result.append(tmp[0] + ":" + value)
else:
result.append(bms_name_pair)

new_line = "CUSTOM_BATTERY_NAMES = " + ",".join(result)

else:
if default_section_checked and not exists:
exists = True

# add before current line
if i != last:
new_line = (
"CUSTOM_BATTERY_NAMES = "
+ self.port
+ ":"
+ value
+ "\n\n"
+ line
)

# add at the end if last line
else:
new_line = (
line
+ "\n\n"
+ "CUSTOM_BATTERY_NAMES = "
+ self.port
+ ":"
+ value
)
else:
new_line = line
# concatenate the new string and add an end-line break
config_file_new = config_file_new + new_line + "\n"

# close the file
file.close()
# Open file in write mode
write_file = open(
"/data/etc/dbus-serialbattery/" + utils.PATH_CONFIG_USER, "w"
)
# overwriting the old file contents with the new/replaced content
write_file.write(config_file_new)
# close the file
write_file.close()

# logger.error("value (saved): " + str(value))

"""
# this removes all comments and tranfsorm the values to lowercase
utils.config.set(
"DEFAULT",
"CUSTOM_BATTERY_NAMES",
self.port + ":" + value,
)
# Writing our configuration file to 'example.ini'
with open(
"/data/etc/dbus-serialbattery/" + utils.PATH_CONFIG_USER, "w"
) as configfile:
type(utils.config.write(configfile))
"""

except Exception:
exception_type, exception_object, exception_traceback = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)

return value

def reset_soc_callback(self, path, value):
# callback for handling reset soc request
return
Expand Down
Loading

0 comments on commit 74f8dc3

Please sign in to comment.