Last active
August 29, 2015 14:22
-
-
Save kwarodom/0dadf4af8127331a0e68 to your computer and use it in GitHub Desktop.
Example of an agent code to publish a message with certain 'topic', 'header', and 'message' to the information exchange bus (IEB)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- {{{ | |
from datetime import datetime | |
import logging | |
import sys | |
from volttron.lite.agent import BaseAgent, PublishMixin, periodic | |
from volttron.lite.agent import utils, matching | |
from volttron.lite.messaging import headers as headers_mod | |
utils.setup_logging() | |
_log = logging.getLogger(__name__) | |
hearbeat_period = 5 # in second | |
class ListenerAgent(PublishMixin, BaseAgent): | |
'''Listens to everything and publishes a heartbeat according to the | |
heartbeat period specified in the settings module. | |
''' | |
def __init__(self, config_path, **kwargs): | |
super(ListenerAgent, self).__init__(**kwargs) | |
self.config = utils.load_config(config_path) | |
def setup(self): | |
# Demonstrate accessing a value from the config file | |
_log.info(self.config['message']) | |
self._agent_id = self.config['agentid'] | |
# Always call the base class setup() | |
super(ListenerAgent, self).setup() | |
@matching.match_all | |
def on_match(self, topic, headers, message, match): | |
# Use match_all to receive all messages and print them out. | |
_log.debug("Topic: {topic}, Headers: {headers}, " | |
"Message: {message}".format( | |
topic=topic, headers=headers, message=message)) | |
# Demonstrate periodic decorator and settings access | |
@periodic(hearbeat_period) | |
def publish_heartbeat(self): | |
''' Send heartbeat message every HEARTBEAT_PERIOD seconds. | |
HEARTBEAT_PERIOD is set and can be adjusted ''' | |
now = datetime.utcnow().isoformat(' ') + 'Z' | |
topic = 'ui/agent/bemoss/999/thermostat/1NSTXXXXX/device_status' | |
headers = { | |
'AgentID': self._agent_id, | |
headers_mod.CONTENT_TYPE: headers_mod.CONTENT_TYPE.PLAIN_TEXT, | |
headers_mod.DATE: now, | |
} | |
message = 'Update thermostat data' | |
self.publish(topic, headers, message) | |
def main(argv=sys.argv): | |
'''Main method called by the eggsecutable.''' | |
try: | |
utils.default_main(ListenerAgent, | |
description='Example VOLTTRON platform™ heartbeat agent', | |
argv=argv) | |
except Exception as e: | |
_log.exception('unhandled exception') | |
if __name__ == '__main__': | |
# Entry point for script | |
try: | |
sys.exit(main()) | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment