首页 > 解决方案 > volttron代理开发调用配置方法onstart

问题描述

我正在试验volttron 代理开发中的 cron 调度功能,以在特定时间运行 volttron 控制代理。

configure当代理通过onstart我的 agent.py 文件上的方法启动时,如何调用该方法?

@Core.receiver("onstart")
def onstart(self, sender, **kwargs):
    """
    This is method is called once the Agent has successfully connected to the platform.
    This is a good place to setup subscriptions if they are not dynamic or
    do any other startup activities that require a connection to the message bus.
    Called after any configurations methods that are called at startup.
    Usually not needed if using the configuration store.
    """

这是我的configure方法。我知道这个问题很可能是显而易见的,但是config_name, action, contents我应该通过什么onstart来调用该configure方法?

def configure(self, config_name, action, contents):
    """
    Called after the Agent has connected to the message bus. If a configuration exists at startup
    this will be called before onstart.

    Is called every time the configuration in the store changes.
    """
    config = self.default_config.copy()
    config.update(contents)

    _log.debug("*** [Setter Agent INFO] *** - Configuring CRON to schedule Agent")


    try:
        cron_schedule = str(config["cron_schedule"])


    except ValueError as e:
        _log.error("ERROR PROCESSING CONFIGURATION: {}".format(e))
        return


    self.cron_schedule = cron_schedule
    self.core.schedule(cron(self.cron_schedule), self.raise_setpoints_up)
    _log.info(f'*** [Setter Agent INFO] *** -  raise_setpoints_up CRON schedule from onstart sucess!')

我的配置文件是config

{

  "cron_schedule": "50 13 * * *"

}

标签: pythonvolttron

解决方案


此功能“应该”与配置存储相关联。请参阅https://volttron.readthedocs.io/en/main/platform-features/config-store/agent-configuration-store.html

config_name 是配置存储中被修改的位置。因此,当您通过命令或通过我在下面给出的示例中的“set”调用之一更新配置存储中的配置条目时,将使用更新的数据调用此函数。

通过代理处理配置存储的相关方法是https://volttron.readthedocs.io/en/main/platform-features/config-store/agent-configuration-store.html#configuration-subsystem-agent-methods

# From your agent onstart method

#self.vip.config.set( config_name, contents, trigger_callback=False ) 

# Using True will have the configstore callback (call the configure function)
self.vip.config.set('my_config_file_entry', {"an": "entry"}, trigger_callback=True)

当然,没有什么可以阻止您在 onstart 方法中自己使用该功能,但这不是它的设计方式。

确保这有效的另一点是确保您在代理初始化函数中具有订阅,以便正确触发回调。


推荐阅读