From 463b2ec895383c3e02cca02c0eb0981f40131bb2 Mon Sep 17 00:00:00 2001 From: Joel Bender Date: Fri, 29 Jun 2018 20:11:08 -0400 Subject: [PATCH] add a generator for easier access to active subscriptions --- py25/bacpypes/service/cov.py | 88 +++++++++++++++++++-------------- py25/bacpypes/service/detect.py | 1 - py27/bacpypes/service/cov.py | 84 +++++++++++++++++-------------- py27/bacpypes/service/detect.py | 1 - py34/bacpypes/service/cov.py | 84 +++++++++++++++++-------------- py34/bacpypes/service/detect.py | 1 - 6 files changed, 142 insertions(+), 117 deletions(-) diff --git a/py25/bacpypes/service/cov.py b/py25/bacpypes/service/cov.py index 42da966..3e79c9c 100644 --- a/py25/bacpypes/service/cov.py +++ b/py25/bacpypes/service/cov.py @@ -444,49 +444,48 @@ class ActiveCOVSubscriptions(Property): # start with an empty sequence cov_subscriptions = ListOf(COVSubscription)() - # loop through the object and detection list - for obj, cov_detection in obj._app.cov_detections.items(): - for cov in cov_detection.cov_subscriptions: - # calculate time remaining - if not cov.lifetime: - time_remaining = 0 - else: - time_remaining = int(cov.taskTime - current_time) + # loop through the subscriptions + for cov in obj._app.subscriptions(): + # calculate time remaining + if not cov.lifetime: + time_remaining = 0 + else: + time_remaining = int(cov.taskTime - current_time) - # make sure it is at least one second - if not time_remaining: - time_remaining = 1 + # make sure it is at least one second + if not time_remaining: + time_remaining = 1 - recipient = Recipient( - address=DeviceAddress( - networkNumber=cov.client_addr.addrNet or 0, - macAddress=cov.client_addr.addrAddr, - ), - ) - if _debug: ActiveCOVSubscriptions._debug(" - recipient: %r", recipient) - if _debug: ActiveCOVSubscriptions._debug(" - client MAC address: %r", cov.client_addr.addrAddr) + recipient = Recipient( + address=DeviceAddress( + networkNumber=cov.client_addr.addrNet or 0, + macAddress=cov.client_addr.addrAddr, + ), + ) + if _debug: ActiveCOVSubscriptions._debug(" - recipient: %r", recipient) + if _debug: ActiveCOVSubscriptions._debug(" - client MAC address: %r", cov.client_addr.addrAddr) - recipient_process = RecipientProcess( - recipient=recipient, - processIdentifier=cov.proc_id, - ) - if _debug: ActiveCOVSubscriptions._debug(" - recipient_process: %r", recipient_process) + recipient_process = RecipientProcess( + recipient=recipient, + processIdentifier=cov.proc_id, + ) + if _debug: ActiveCOVSubscriptions._debug(" - recipient_process: %r", recipient_process) - cov_subscription = COVSubscription( - recipient=recipient_process, - monitoredPropertyReference=ObjectPropertyReference( - objectIdentifier=cov.obj_id, - propertyIdentifier=cov_detection.monitored_property_reference, - ), - issueConfirmedNotifications=cov.confirmed, - timeRemaining=time_remaining, - ) - if hasattr(cov_detection, 'covIncrement'): - cov_subscription.covIncrement = cov_detection.covIncrement - if _debug: ActiveCOVSubscriptions._debug(" - cov_subscription: %r", cov_subscription) + cov_subscription = COVSubscription( + recipient=recipient_process, + monitoredPropertyReference=ObjectPropertyReference( + objectIdentifier=cov.obj_id, + propertyIdentifier=cov_detection.monitored_property_reference, + ), + issueConfirmedNotifications=cov.confirmed, + timeRemaining=time_remaining, + ) + if hasattr(cov_detection, 'covIncrement'): + cov_subscription.covIncrement = cov_detection.covIncrement + if _debug: ActiveCOVSubscriptions._debug(" - cov_subscription: %r", cov_subscription) - # add the list - cov_subscriptions.append(cov_subscription) + # add the list + cov_subscriptions.append(cov_subscription) return cov_subscriptions @@ -544,6 +543,19 @@ class ChangeOfValueServices(Capability): # delete it from the object map del self.cov_detections[cov.obj_ref] + def subscriptions(self): + """Generator for the active subscriptions.""" + if _debug: ChangeOfValueServices._debug("subscriptions") + + subscription_list = [] + + # loop through the object and detection list + for obj, cov_detection in self.cov_detections.items(): + for cov in cov_detection.cov_subscriptions: + subscription_list.append(cov) + + return subscription_list + def cov_notification(self, cov, request): if _debug: ChangeOfValueServices._debug("cov_notification %s %s", str(cov), str(request)) diff --git a/py25/bacpypes/service/detect.py b/py25/bacpypes/service/detect.py index 2de5929..4b743e8 100755 --- a/py25/bacpypes/service/detect.py +++ b/py25/bacpypes/service/detect.py @@ -37,7 +37,6 @@ class DetectionMonitor: # if the algorithm is already triggered, don't bother checking for more if self.algorithm._triggered: if _debug: DetectionMonitor._debug(" - already triggered") - self.algorithm._triggered = False return # if there is a special filter, use it, otherwise use != diff --git a/py27/bacpypes/service/cov.py b/py27/bacpypes/service/cov.py index 0283602..0bb07d5 100644 --- a/py27/bacpypes/service/cov.py +++ b/py27/bacpypes/service/cov.py @@ -442,49 +442,48 @@ class ActiveCOVSubscriptions(Property): # start with an empty sequence cov_subscriptions = ListOf(COVSubscription)() - # loop through the object and detection list - for obj, cov_detection in obj._app.cov_detections.items(): - for cov in cov_detection.cov_subscriptions: - # calculate time remaining - if not cov.lifetime: - time_remaining = 0 - else: - time_remaining = int(cov.taskTime - current_time) + # loop through the subscriptions + for cov in obj._app.subscriptions(): + # calculate time remaining + if not cov.lifetime: + time_remaining = 0 + else: + time_remaining = int(cov.taskTime - current_time) - # make sure it is at least one second - if not time_remaining: - time_remaining = 1 + # make sure it is at least one second + if not time_remaining: + time_remaining = 1 - recipient = Recipient( - address=DeviceAddress( - networkNumber=cov.client_addr.addrNet or 0, - macAddress=cov.client_addr.addrAddr, - ), - ) - if _debug: ActiveCOVSubscriptions._debug(" - recipient: %r", recipient) - if _debug: ActiveCOVSubscriptions._debug(" - client MAC address: %r", cov.client_addr.addrAddr) + recipient = Recipient( + address=DeviceAddress( + networkNumber=cov.client_addr.addrNet or 0, + macAddress=cov.client_addr.addrAddr, + ), + ) + if _debug: ActiveCOVSubscriptions._debug(" - recipient: %r", recipient) + if _debug: ActiveCOVSubscriptions._debug(" - client MAC address: %r", cov.client_addr.addrAddr) - recipient_process = RecipientProcess( - recipient=recipient, - processIdentifier=cov.proc_id, - ) - if _debug: ActiveCOVSubscriptions._debug(" - recipient_process: %r", recipient_process) + recipient_process = RecipientProcess( + recipient=recipient, + processIdentifier=cov.proc_id, + ) + if _debug: ActiveCOVSubscriptions._debug(" - recipient_process: %r", recipient_process) - cov_subscription = COVSubscription( - recipient=recipient_process, - monitoredPropertyReference=ObjectPropertyReference( - objectIdentifier=cov.obj_id, - propertyIdentifier=cov_detection.monitored_property_reference, - ), - issueConfirmedNotifications=cov.confirmed, - timeRemaining=time_remaining, - ) - if hasattr(cov_detection, 'covIncrement'): - cov_subscription.covIncrement = cov_detection.covIncrement - if _debug: ActiveCOVSubscriptions._debug(" - cov_subscription: %r", cov_subscription) + cov_subscription = COVSubscription( + recipient=recipient_process, + monitoredPropertyReference=ObjectPropertyReference( + objectIdentifier=cov.obj_id, + propertyIdentifier=cov_detection.monitored_property_reference, + ), + issueConfirmedNotifications=cov.confirmed, + timeRemaining=time_remaining, + ) + if hasattr(cov_detection, 'covIncrement'): + cov_subscription.covIncrement = cov_detection.covIncrement + if _debug: ActiveCOVSubscriptions._debug(" - cov_subscription: %r", cov_subscription) - # add the list - cov_subscriptions.append(cov_subscription) + # add the list + cov_subscriptions.append(cov_subscription) return cov_subscriptions @@ -541,6 +540,15 @@ class ChangeOfValueServices(Capability): # delete it from the object map del self.cov_detections[cov.obj_ref] + def subscriptions(self): + """Generator for the active subscriptions.""" + if _debug: ChangeOfValueServices._debug("subscriptions") + + # loop through the object and detection list + for obj, cov_detection in self.cov_detections.items(): + for cov in cov_detection.cov_subscriptions: + yield cov + def cov_notification(self, cov, request): if _debug: ChangeOfValueServices._debug("cov_notification %s %s", str(cov), str(request)) diff --git a/py27/bacpypes/service/detect.py b/py27/bacpypes/service/detect.py index 9ff8829..0be0551 100755 --- a/py27/bacpypes/service/detect.py +++ b/py27/bacpypes/service/detect.py @@ -38,7 +38,6 @@ class DetectionMonitor: # if the algorithm is already triggered, don't bother checking for more if self.algorithm._triggered: if _debug: DetectionMonitor._debug(" - already triggered") - self.algorithm._triggered = False return # if there is a special filter, use it, otherwise use != diff --git a/py34/bacpypes/service/cov.py b/py34/bacpypes/service/cov.py index 0283602..0bb07d5 100644 --- a/py34/bacpypes/service/cov.py +++ b/py34/bacpypes/service/cov.py @@ -442,49 +442,48 @@ class ActiveCOVSubscriptions(Property): # start with an empty sequence cov_subscriptions = ListOf(COVSubscription)() - # loop through the object and detection list - for obj, cov_detection in obj._app.cov_detections.items(): - for cov in cov_detection.cov_subscriptions: - # calculate time remaining - if not cov.lifetime: - time_remaining = 0 - else: - time_remaining = int(cov.taskTime - current_time) + # loop through the subscriptions + for cov in obj._app.subscriptions(): + # calculate time remaining + if not cov.lifetime: + time_remaining = 0 + else: + time_remaining = int(cov.taskTime - current_time) - # make sure it is at least one second - if not time_remaining: - time_remaining = 1 + # make sure it is at least one second + if not time_remaining: + time_remaining = 1 - recipient = Recipient( - address=DeviceAddress( - networkNumber=cov.client_addr.addrNet or 0, - macAddress=cov.client_addr.addrAddr, - ), - ) - if _debug: ActiveCOVSubscriptions._debug(" - recipient: %r", recipient) - if _debug: ActiveCOVSubscriptions._debug(" - client MAC address: %r", cov.client_addr.addrAddr) + recipient = Recipient( + address=DeviceAddress( + networkNumber=cov.client_addr.addrNet or 0, + macAddress=cov.client_addr.addrAddr, + ), + ) + if _debug: ActiveCOVSubscriptions._debug(" - recipient: %r", recipient) + if _debug: ActiveCOVSubscriptions._debug(" - client MAC address: %r", cov.client_addr.addrAddr) - recipient_process = RecipientProcess( - recipient=recipient, - processIdentifier=cov.proc_id, - ) - if _debug: ActiveCOVSubscriptions._debug(" - recipient_process: %r", recipient_process) + recipient_process = RecipientProcess( + recipient=recipient, + processIdentifier=cov.proc_id, + ) + if _debug: ActiveCOVSubscriptions._debug(" - recipient_process: %r", recipient_process) - cov_subscription = COVSubscription( - recipient=recipient_process, - monitoredPropertyReference=ObjectPropertyReference( - objectIdentifier=cov.obj_id, - propertyIdentifier=cov_detection.monitored_property_reference, - ), - issueConfirmedNotifications=cov.confirmed, - timeRemaining=time_remaining, - ) - if hasattr(cov_detection, 'covIncrement'): - cov_subscription.covIncrement = cov_detection.covIncrement - if _debug: ActiveCOVSubscriptions._debug(" - cov_subscription: %r", cov_subscription) + cov_subscription = COVSubscription( + recipient=recipient_process, + monitoredPropertyReference=ObjectPropertyReference( + objectIdentifier=cov.obj_id, + propertyIdentifier=cov_detection.monitored_property_reference, + ), + issueConfirmedNotifications=cov.confirmed, + timeRemaining=time_remaining, + ) + if hasattr(cov_detection, 'covIncrement'): + cov_subscription.covIncrement = cov_detection.covIncrement + if _debug: ActiveCOVSubscriptions._debug(" - cov_subscription: %r", cov_subscription) - # add the list - cov_subscriptions.append(cov_subscription) + # add the list + cov_subscriptions.append(cov_subscription) return cov_subscriptions @@ -541,6 +540,15 @@ class ChangeOfValueServices(Capability): # delete it from the object map del self.cov_detections[cov.obj_ref] + def subscriptions(self): + """Generator for the active subscriptions.""" + if _debug: ChangeOfValueServices._debug("subscriptions") + + # loop through the object and detection list + for obj, cov_detection in self.cov_detections.items(): + for cov in cov_detection.cov_subscriptions: + yield cov + def cov_notification(self, cov, request): if _debug: ChangeOfValueServices._debug("cov_notification %s %s", str(cov), str(request)) diff --git a/py34/bacpypes/service/detect.py b/py34/bacpypes/service/detect.py index 9ff8829..0be0551 100755 --- a/py34/bacpypes/service/detect.py +++ b/py34/bacpypes/service/detect.py @@ -38,7 +38,6 @@ class DetectionMonitor: # if the algorithm is already triggered, don't bother checking for more if self.algorithm._triggered: if _debug: DetectionMonitor._debug(" - already triggered") - self.algorithm._triggered = False return # if there is a special filter, use it, otherwise use !=