Skip to content

Commit

Permalink
Fix problem with velib_python integration
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Jul 12, 2024
1 parent 0b9efa7 commit fbd3e84
Show file tree
Hide file tree
Showing 11 changed files with 916 additions and 166 deletions.
5 changes: 2 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ exclude =
./etc/dbus-serialbattery/bms/mnb_test_max17853.py,
./etc/dbus-serialbattery/bms/mnb_utils_max17853.py,
./etc/dbus-serialbattery/bms/revov.py,
./etc/dbus-serialbattery/minimalmodbus.py,
./velib_python
venv
./etc/dbus-serialbattery/ext/velib_python/,
./etc/dbus-serialbattery/minimalmodbus.py
extend-ignore:
# E203 whitespace before ':' conflicts with black code formatting. Will be ignored in flake8
E203
8 changes: 4 additions & 4 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
with:
python-version: "3.8.13"

- name: Clone velib_python and add it to PYTHONPATH for subsequent steps
run: |
git clone https://github.com/victronenergy/velib_python.git
echo PYTHONPATH=$PYTHONPATH:$(pwd)/velib_python >> $GITHUB_ENV
# - name: Clone velib_python and add it to PYTHONPATH for subsequent steps
# run: |
# git clone https://github.com/victronenergy/velib_python.git
# echo PYTHONPATH=$PYTHONPATH:$(pwd)/velib_python >> $GITHUB_ENV

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
147 changes: 0 additions & 147 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,115 +1,8 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
Expand All @@ -120,48 +13,8 @@ ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# VS Code
.vscode/

# Custom for this repo
venus-data.tar.gz
BMS-trials
.DS_Store
.project
.pydevproject
*.prefs
etc/dbus-serialbattery/config.ini

# Local Clone of velib_python
velib_python
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"[python]": { "editor.formatOnSave": true },

"python.autoComplete.extraPaths": [
"etc/dbus-serialbattery/ext/velib_python",
],
"python.analysis.extraPaths": [
"etc/dbus-serialbattery/ext/velib_python",
]
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

## v1.4.x

* Changed: Fixed how `velib_python` was integrated in this driver by @mr-manuel
* Changed: JKBMS BLE - Fixes wrong max battery voltage https://github.com/Louisvdw/dbus-serialbattery/issues/1094 by @mr-manuel
* Changed: JKBMS PB Model fixes by @KoljaWindeler

Expand Down
20 changes: 8 additions & 12 deletions etc/dbus-serialbattery/dbushelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,8 @@
from xml.etree import ElementTree

# Victron packages
sys.path.insert(
1,
os.path.join(
os.path.dirname(__file__),
"/opt/victronenergy/dbus-systemcalc-py/ext/velib_python",
),
)
from vedbus import VeDbusService # noqa: E402
from settingsdevice import ( # noqa: E402
SettingsDevice,
)
from ext.velib_python.vedbus import VeDbusService
from ext.velib_python.settingsdevice import SettingsDevice


class SystemBus(dbus.bus.BusConnection):
Expand Down Expand Up @@ -51,7 +42,7 @@ def __init__(self, battery, bms_address=None):
+ self.battery.port[self.battery.port.rfind("/") + 1 :]
+ ("__" + str(bms_address) if bms_address is not None else "")
)
self._dbusservice = VeDbusService(self._dbusname, get_bus())
self._dbusservice = VeDbusService(self._dbusname, get_bus(), register=False)
self.bms_id = "".join(
# remove all non alphanumeric characters from the identifier
c if c.isalnum() else "_"
Expand Down Expand Up @@ -778,6 +769,11 @@ def setup_vedbus(self) -> bool:
onchangecallback=self.battery.reset_soc_callback,
)

# register VeDbusService after all paths where added
# https://github.com/victronenergy/velib_python/commit/494f9aef38f46d6cfcddd8b1242336a0a3a79563
# https://github.com/victronenergy/velib_python/commit/88a183d099ea5c60139e4d7494f9044e2dedd2d4
self._dbusservice.register()

return True

def publish_battery(self, loop):
Expand Down
20 changes: 20 additions & 0 deletions etc/dbus-serialbattery/ext/velib_python/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Victron Energy BV

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
102 changes: 102 additions & 0 deletions etc/dbus-serialbattery/ext/velib_python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
velib_python
============

[![Build Status](https://travis-ci.com/victronenergy/velib_python.svg?branch=master)](https://travis-ci.org/victronenergy/velib_python)

This is the general python library within Victron. It contains code that is related to D-Bus and the Color
Control GX. See http://www.victronenergy.com/panel-systems-remote-monitoring/colorcontrol/ for more
infomation about that panel.

Files busitem.py, dbusitem.py and tracing.py are deprecated.

The main files are vedbus.py, dbusmonitor.py and settingsdevice.py.

- Use VeDbusService to put your process on dbus and let other services interact with you.
- Use VeDbusItemImport to read a single value from other processes the dbus, and monitor its signals.
- Use DbusMonitor to monitor multiple values from other processes
- Use SettingsDevice to store your settings in flash, via the com.victronenergy.settings dbus service. See
https://github.com/victronenergy/localsettings for more info.

Code style
==========

Comply with PEP8, except:
- use tabs instead of spaces, since we use tabs for all projects within Victron.
- max line length = 110

Run this command to set git diff to tabsize is 4 spaces. Replace --local with --global to do it globally for the current
user account.

git config --local core.pager 'less -x4'

Run this command to check your code agains PEP8

pep8 --max-line-length=110 --ignore=W191 *.py

D-Bus
=====

D-Bus is an/the inter process communication bus used on Linux for many things. Victron uses it on the CCGX to have all the different processes exchange data. Protocol drivers publish data read from products (for example battery voltage) on the D-Bus, and other processes (the GUI for example) takes it from the D-Bus to show it on the display.

Libraries that implement D-Bus connectivity are available in many programming languages (C, Python, etc). There are also many commandline tools available to talk to a running process via D-bus. See for example the dbuscli (executeable name dbus): http://code.google.com/p/dbus-tools/wiki/DBusCli, and also dbus-monitor and dbus-send.

There are two sides in using the D-Bus, putting information on it (exporting as service with objects) and reading/writing to a process exporting a service. Note that instead of reading with GetValue, you can also subscribe to receive a signal when datachanges. Doing this saves unncessary context-switches in most cases.

To get an idea of how to publish data on the dbus, run the example:

matthijs@matthijs-VirtualBox:~/dev/velib_python/examples$ python vedbusservice_example.py
vedbusservice_example.py starting up
/Position value is 5
/Position value is now 10
try changing our RPM by executing the following command from a terminal

dbus-send --print-reply --dest=com.victronenergy.example /RPM com.victronenergy.BusItem.SetValue int32:1200
Reply will be <> 0 for values > 1000: not accepted. And reply will be 0 for values < 1000: accepted.

Leave that terminal open, start a second terminal, and interrogate above service from the commandline:

matthijs@matthijs-VirtualBox:~/dev/velib_python/examples$ dbus
org.freedesktop.DBus
org.freedesktop.PowerManagement
com.victronenergy.example
org.xfce.Terminal5
org.xfce.Xfconf
[and many more services in which we are not interested]

To get more details, add the servicename:

matthijs@matthijs-VirtualBox:~/dev/velib_python/examples$ dbus com.victronenergy.example
/
/Float
/Int
/NegativeInt
/Position
/RPM
/String

And get the value for the position:

matthijs@matthijs-VirtualBox:~/dev/velib_python/examples$ dbus com.victronenergy.example /RPM GetValue
100

And setting the value is also possible, the % makes dbus evaluate what comes behind it, resulting in an int instead of the default (a string).:

matthijs@matthijs-VirtualBox:~/dev/velib_python/examples$ dbus com.victronenergy.example /RPM SetValue %1
0

In this example, the 0 indicates succes. When trying an unsupported value, 2000, this is what happens:

matthijs@matthijs-VirtualBox:~/dev/velib_python/examples$ dbus com.victronenergy.example /RPM SetValue %2000
2

Exporting services, and the object paths (/Float, /Position, /Group1/Value1, etcetera) is standard D-Bus functionality. At Victron we designed and implemented a D-Bus interface, called com.victronenergy.BusItem. Example showing all interfaces supported by an object:

matthijs@matthijs-VirtualBox:~/dev/velib_python/examples$ dbus com.victronenergy.example /RPM
Interface org.freedesktop.DBus.Introspectable:
String Introspect()

Interface com.victronenergy.BusItem:
Int32 SetValue(Variant newvalue)
String GetDescription(String language, Int32 length)
String GetText()
Variant GetValue()
Loading

0 comments on commit fbd3e84

Please sign in to comment.