Voltron代理开发调用onstart上的配置方法

2024-05-06 23:21:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

@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方法。我知道这个问题很可能是显而易见的,但是我应该通过什么来调用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 * * *"

}

1条回答
网友
1楼 · 发布于 2024-05-06 23:21:24

此函数“应该”与配置存储相关联。请参阅https://volttron.readthedocs.io/en/main/platform-features/config-store/agent-configuration-store.html了解这一点

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

通过代理处理配置存储的相关方法是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方法中没有任何东西阻止您自己使用该函数,但是它不是这样设计的

要确保这一点起作用的另一点是确保在代理init函数中有订阅,以便正确触发回调

相关问题 更多 >