Skip to content

Commit

Permalink
Add a new callback on the SubscriptionTransaction object to permit (#…
Browse files Browse the repository at this point in the history
…19833)

registering for resubscription notifications.
  • Loading branch information
mrjerryjohns authored and pull[bot] committed Jan 16, 2024
1 parent 2fea441 commit 4383683
Showing 1 changed file with 18 additions and 3 deletions.
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._onErrorCb = DefaultErrorCallback
Expand All @@ -492,6 +493,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 @@ -540,6 +550,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 @@ -682,8 +696,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 @@ -810,7 +825,7 @@ def _OnSubscriptionEstablishedCallback(closure, subscriptionId):


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


Expand Down

0 comments on commit 4383683

Please sign in to comment.