0

I have an control agent that is listening on the message bus to the BACnet proxy/actuator publish of all of the devices/campus/building/somedevice/all scraping of all of the BACnet data. Code looks like this below, where I can just filter for zone temperature sensor readings:

def _create_subscriptions(self, topics):
    """
    Unsubscribe from all pub/sub topics and create a subscription to a topic in the configuration which triggers
    the _handle_publish callback
    """
    self.vip.pubsub.unsubscribe("pubsub", None, None)

    for topic in topics:
        _log.debug(f'*** [Roller Agent INFO] *** -  _create_subscriptions {topic}')
        self.vip.pubsub.subscribe(peer='pubsub',
                                prefix=topic,
                                callback=self._handle_publish)

def _handle_publish(self, peer, sender, bus, topic, headers, message):
    """
    When we recieve an update from our all publish subscription, log something so we can see that we are
    successfully scraping CSV points with the Platform Driver
    :param peer: unused
    :param sender: unused
    :param bus: unused
    :param topic: unused
    :param headers: unused
    :param message: "All" messaged published by the Platform Driver for the CSV Driver containing values for all
    registers on the device
    """
    
    topic = topic.strip('/all')
    _log.debug(f"*** [Handle Pub Sub INFO] *** topic_formatted {topic}")

    for point,sensor_reading in message[0].items():
        if point == 'ZN-T' or point == 'Space Temperature Local':
            _log.debug(f"*** [Handle Pub Sub INFO] *** Found a Zone Temp Published {point} that is {sensor_reading}")
            if point == 'Space Temperature Local': # Fix Trane controller data that comes through in Metric
                sensor_reading = (9/5) * sensor_reading + 32

    self.znt_values[topic] = float(sensor_reading)
    _log.debug(f"*** [Handle Pub Sub INFO] *** self.znt_values {self.znt_values}")

Is there any chance I could get a tip on how this agent could listen on the message bus for another agent publishing data? Sorry I bet this is super basic material, still learning :)

I have another agent that checks an API where on the log files/message bus this shows up:

(setteroccvavagent-0.1 922213) __main__ DEBUG: *** [SIG CHECKER Agent INFO] *** - signal_payload from Flask App is 0!

How do I get this info of the setteroccvavagent signal payload? Would I need to write a separate method and assign a topic to the setteroccvavagent?

Any tips appreciated what the code would look like.

For what its worth this is the code for my setteroccagent that calls the API:

AtCore.receiver("onstart")
def onstart(self, sender, **kwargs):
    self.core.periodic(60, self.dr_signal_checker)
    _log.debug(f'*** [SIG CHECKER Agent INFO] *** -  AGENT ONSTART CALLED SUCCESS!')


def dr_signal_checker(self):

    try:
        requests = (grequests.get(self.url),)
        result, = grequests.map(requests)
        contents = result.json()
        _log.debug(f"Flask App API contents: {contents}")
        _log.debug(f"Flask App API SUCCESS")
        sig_payload = contents["current_state"]

    except Exception as error:
        _log.debug(f"*** [SIG CHECKER Agent INFO] *** - Error trying Flask App API {error}")
        _log.debug(f"*** [SIG CHECKER Agent INFO] *** - RESORTING TO NO DEMAND RESPONSE EVENT")
        sig_payload = 0

    _log.debug(f'*** [SIG CHECKER Agent INFO] *** - signal_payload from Flask App is {sig_payload}!')
bbartling
  • 3,288
  • 9
  • 43
  • 88

1 Answers1

0

You will need the agent to subscribe to whatever prefix to listen for. For example the historians subscribe to devices/ and listen for the "all" message.

The subscribe function is available on all agents under the pubsub subsystem. The subscribe function looks like the following:

def subscribe(self, peer, prefix, 
              callback, bus='', all_platforms=False, persistent_queue=None)

So generally you would call the subscribe function as follows:

self.vip.pubsub.subscribe('pubsub', 'things', _handle_publish):

There is no restriction on content or topic for your agent to publish. The 'things' argument above would match things* as it is a prefix.

Craig
  • 949
  • 1
  • 5
  • 13
  • Where would I configure a value to for the agent I am trying to listen too? I.E. `setteroccvavagent` – bbartling Jul 20 '21 at 15:58
  • You could use the config store, grab it from a file, from a web service, do it from a command ... really where you get it is irrelevant from the publish point of view. If you are wanting to listen to a device through the platform driver, then you can set the point via the actuator (or directly to the platform.driver). It looks like you are using requests to get data...you can publish that to the message bus from any agent via self.vip.pubsub.publish(peer='pubsub', topic="foo", message="a message") – Craig Jul 21 '21 at 18:48