Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Re-subscription event notification support #19833

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/controller/python/chip/clusters/Attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ def UpdateCachedData(self):

class SubscriptionTransaction:
def __init__(self, transaction: 'AsyncReadTransaction', subscriptionId, devCtrl):
self._onResubscriptionAttemptedCb = DefaultResubscriptionAttemptedCallback
self._onAttributeChangeCb = DefaultAttributeChangeCallback
self._onEventChangeCb = DefaultEventChangeCallback
self._readTransaction = transaction
Expand All @@ -491,6 +492,15 @@ def GetAttribute(self, path: TypedAttributePath) -> Any:
def GetEvents(self):
return self._readTransaction.GetAllEventValues()

def SetResubscriptionAttemptedCallback(self, callback: Callable[[SubscriptionTransaction, int, int], None]):
'''
Sets the callback function that gets invoked anytime a re-subscription is attempted. The callback is expected
to have the following signature:
def Callback(transaction: SubscriptionTransaction, errorEncountered: int, nextResubscribeIntervalMsec: int)
'''
if callback is not None:
self._onResubscriptionAttemptedCb = callback

def SetAttributeUpdateCallback(self, callback: Callable[[TypedAttributePath, SubscriptionTransaction], None]):
'''
Sets the callback function for the attribute value change event, accepts a Callable accepts an attribute path and the cached data.
Expand Down Expand Up @@ -528,6 +538,10 @@ def __repr__(self):
return f'<Subscription (Id={self._subscriptionId})>'


def DefaultResubscriptionAttemptedCallback(transaction: SubscriptionTransaction, terminationError, nextResubscribeIntervalMsec):
print(f"Previous subscription failed with Error: {terminationError} - re-subscribing in {nextResubscribeIntervalMsec}ms...")


def DefaultAttributeChangeCallback(path: TypedAttributePath, transaction: SubscriptionTransaction):
data = transaction.GetAttribute(path)
value = {
Expand Down Expand Up @@ -677,8 +691,9 @@ def handleSubscriptionEstablished(self, subscriptionId):
self._event_loop.call_soon_threadsafe(
self._handleSubscriptionEstablished, subscriptionId)

def handleResubscriptionAttempted(self, terminationCause, nextResubscribeIntervalMsec):
print("would resubscribe with error " + str(terminationCause) + " in " + str(nextResubscribeIntervalMsec))
def handleResubscriptionAttempted(self, terminationCause: int, nextResubscribeIntervalMsec: int):
self._event_loop.call_soon_threadsafe(
self._subscription_handler._onResubscriptionAttemptedCb, self._subscription_handler, terminationCause, nextResubscribeIntervalMsec)

def _handleReportBegin(self):
pass
Expand Down Expand Up @@ -781,7 +796,7 @@ def _OnSubscriptionEstablishedCallback(closure, subscriptionId):


@_OnResubscriptionAttemptedCallbackFunct
def _OnResubscriptionAttemptedCallback(closure, terminationCause, nextResubscribeIntervalMsec):
def _OnResubscriptionAttemptedCallback(closure, terminationCause: int, nextResubscribeIntervalMsec: int):
closure.handleResubscriptionAttempted(terminationCause, nextResubscribeIntervalMsec)


Expand Down