Skip to content

Commit

Permalink
Changed: Fix registration to dbus
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Aug 19, 2024
1 parent 63fa1cd commit 2ac7356
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## v0.1.6 (not released yet)
## v0.1.6
* Added: Set device type to `grid`, `genset` or `acload`
* Changed: Fix registration to dbus https://github.com/victronenergy/velib_python/commit/494f9aef38f46d6cfcddd8b1242336a0a3a79563

## v0.1.5
* Added: Error handling
Expand Down
5 changes: 4 additions & 1 deletion dbus-mqtt-grid/dbus-mqtt-grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def __init__(
customname="MQTT " + device_type_name,
connection="MQTT " + device_type_name + " service",
):
self._dbusservice = VeDbusService(servicename)
self._dbusservice = VeDbusService(servicename, register=False)
self._paths = paths

logging.debug("%s /DeviceInstance = %d" % (servicename, deviceinstance))
Expand Down Expand Up @@ -421,6 +421,9 @@ def __init__(
onchangecallback=self._handlechangedvalue,
)

# register VeDbusService after all paths where added
self._dbusservice.register()

GLib.timeout_add(1000, self._update) # pause 1000ms before the next request

def _update(self):
Expand Down
40 changes: 20 additions & 20 deletions dbus-mqtt-grid/ext/velib_python/LICENSE
Original file line number Diff line number Diff line change
@@ -1,20 +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.
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.
55 changes: 47 additions & 8 deletions dbus-mqtt-grid/ext/velib_python/vedbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@

# Export ourselves as a D-Bus service.
class VeDbusService(object):
def __init__(self, servicename, bus=None):
def __init__(self, servicename, bus=None, register=True):
# dict containing the VeDbusItemExport objects, with their path as the key.
self._dbusobjects = {}
self._dbusnodes = {}
self._ratelimiters = []
self._dbusname = None
self.name = servicename

# dict containing the onchange callbacks, for each object. Object path is the key
self._onchangecallbacks = {}
Expand All @@ -74,13 +75,21 @@ def __init__(self, servicename, bus=None):
# make the dbus connection available to outside, could make this a true property instead, but ach..
self.dbusconn = self._dbusconn

# Register ourselves on the dbus, trigger an error if already in use (do_not_queue)
self._dbusname = dbus.service.BusName(servicename, self._dbusconn, do_not_queue=True)

# Add the root item that will return all items as a tree
self._dbusnodes['/'] = VeDbusRootExport(self._dbusconn, '/', self)

logging.info("registered ourselves on D-Bus as %s" % servicename)
# Immediately register the service unless requested not to
if register:
logging.warning("USING OUTDATED REGISTRATION METHOD!")
logging.warning("Please set register=False, then call the register method "
"after adding all mandatory paths. See "
"https://github.com/victronenergy/venus/wiki/dbus-api")
self.register()

def register(self):
# Register ourselves on the dbus, trigger an error if already in use (do_not_queue)
self._dbusname = dbus.service.BusName(self.name, self._dbusconn, do_not_queue=True)
logging.info("registered ourselves on D-Bus as %s" % self.name)

# To force immediate deregistering of this dbus service and all its object paths, explicitly
# call __del__().
Expand All @@ -95,17 +104,20 @@ def __del__(self):
self._dbusname.__del__() # Forces call to self._bus.release_name(self._name), see source code
self._dbusname = None

def get_name(self):
return self._dbusname.get_name()

# @param callbackonchange function that will be called when this value is changed. First parameter will
# be the path of the object, second the new value. This callback should return
# True to accept the change, False to reject it.
def add_path(self, path, value, description="", writeable=False,
onchangecallback=None, gettextcallback=None, valuetype=None):
onchangecallback=None, gettextcallback=None, valuetype=None, itemtype=None):

if onchangecallback is not None:
self._onchangecallbacks[path] = onchangecallback

item = VeDbusItemExport(
self._dbusconn, path, value, description, writeable,
itemtype = itemtype or VeDbusItemExport
item = itemtype(self._dbusconn, path, value, description, writeable,
self._value_changed, gettextcallback, deletecallback=self._item_deleted, valuetype=valuetype)

spl = path.split('/')
Expand All @@ -115,6 +127,7 @@ def add_path(self, path, value, description="", writeable=False,
self._dbusnodes[subPath] = VeDbusTreeExport(self._dbusconn, subPath, self)
self._dbusobjects[path] = item
logging.debug('added %s with start value %s. Writeable is %s' % (path, value, writeable))
return item

# Add the mandatory paths, as per victron dbus api doc
def add_mandatory_paths(self, processname, processversion, connection,
Expand Down Expand Up @@ -179,6 +192,9 @@ def __init__(self, parent):
self.parent = parent
self.changes = {}

def __contains__(self, path):
return path in self.parent

def __getitem__(self, path):
return self.parent[path]

Expand All @@ -187,9 +203,32 @@ def __setitem__(self, path, newvalue):
if c is not None:
self.changes[path] = c

def __delitem__(self, path):
if path in self.changes:
del self.changes[path]
del self.parent[path]

def flush(self):
if self.changes:
self.parent._dbusnodes['/'].ItemsChanged(self.changes)
self.changes.clear()

def add_path(self, path, value, *args, **kwargs):
self.parent.add_path(path, value, *args, **kwargs)
self.changes[path] = {
'Value': wrap_dbus_value(value),
'Text': self.parent._dbusobjects[path].GetText()
}

def del_tree(self, root):
root = root.rstrip('/')
for p in list(self.parent._dbusobjects.keys()):
if p == root or p.startswith(root + '/'):
self[p] = None
self.parent._dbusobjects[p].__del__()

def get_name(self):
return self.parent.get_name()

class TrackerDict(defaultdict):
""" Same as defaultdict, but passes the key to default_factory. """
Expand Down

0 comments on commit 2ac7356

Please sign in to comment.