Skip to content

Commit

Permalink
Support multiple parameter callback
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Singh <singhabhinav9051571833@gmail.com>
  • Loading branch information
suab321321 committed Nov 13, 2019
2 parents 3a941ab + e566f3e commit 60dc705
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions rclpy/rclpy/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def __init__(
self.__guards: List[GuardCondition] = []
self.__waitables: List[Waitable] = []
self._default_callback_group = MutuallyExclusiveCallbackGroup()
self._parameters_callback: List[Callable[[List[Parameter]], SetParametersResult]] = []
self._parameters_callbacks: List[Callable[[List[Parameter]], SetParametersResult]] = []
self._rate_group = ReentrantCallbackGroup()
self._allow_undeclared_parameters = allow_undeclared_parameters
self._parameter_overrides = {}
Expand Down Expand Up @@ -714,8 +714,8 @@ def _set_parameters_atomically(

if not result.successful:
return result
elif self._parameters_callback:
for callback in self._parameters_callback:
elif self._parameters_callbacks:
for callback in self._parameters_callbacks:
result = callback(parameter_list)
if(not result.successful):
return result
Expand Down Expand Up @@ -763,24 +763,32 @@ def _set_parameters_atomically(

return result

def add_on_set_paramters_callback(
def add_on_set_parameters_callback(
self,
callback: Callable[[List[Parameter]], SetParametersResult]
) -> None:
"""Add the callback to list."""
prevCallBack = self._parameters_callback
prevCallBack.insert(0, callback)
self._parameters_callback = prevCallBack
"""
Add a callback in front to the list of callback.
Calling this function will add a callback in self._parameter_callbacks list.
:param callback: The function that is called whenever parameters are set for the node.
"""
self._parameters_callback.insert(0, callback)

def remove_on_set_parameters_callback(
self,
callback: Callable[[List[Parameter]], SetParametersResult]
) -> None:
"""Remove callback from list."""
if callback in self._parameters_callback:
self._parameters_callback.remove(callback)
else:
print('Callback does not exist')
"""
Remove a callback from list of callback.
Calling this function will remove the callback from self._parameter_callbacks list.
:param callback: The function that is called whenever parameters are set for the node.
:raises: ValueError if a callback is not present in the list of callback.
"""
self._parameters_callbacks.remove(callback)

def _apply_descriptors(
self,
Expand Down Expand Up @@ -1040,11 +1048,11 @@ def set_parameters_callback(
"""
Register a set parameters callback.
Calling this function will override any previously registered callback.
Calling this function will add a callback in self._parameter_callbacks list.
:param callback: The function that is called whenever parameters are set for the node.
"""
self._parameters_callback = callback
self.add_on_set_parameters_callback(callback)

def _validate_topic_or_service_name(self, topic_or_service_name, *, is_service=False):
name = self.get_name()
Expand Down

0 comments on commit 60dc705

Please sign in to comment.