在VOLTTRON中禁用计划功能

问题描述

要定期执行该功能,请使用以下命令。

self.core.schedule(periodic(t),periodic_function)

我想在满足某些条件时禁用上述功能。有人知道吗?

解决方法

@GYOON

我将执行此处的代码:https://github.com/VOLTTRON/volttron/blob/master/services/core/VolttronCentralPlatform/vcplatform/agent.py#L320

基本上发生了什么事,是在代理的onstart / onconfig方法中,要在稍后生成的小组件中调用要执行的功能

class MyAgent(Agent):
  def __init__(self,**kwargs):
    self._periodic_event = None
    self.enabled = True

  @Core.receiver('onstart')
  def onstart(self,sender,**kwargs):
    self.core.spawn_later(1,self._my_periodic_event)
  
  def _my_periodic_event(self):
    if self._periodic_event is not None:
      self._periodic_event.cancel()

    # do stuff here within this event loop
    
    if self.enabled:
      # note this is an internal volttron function see the referenced link for
      # import
      now = get_aware_utc_now()
      next_update_time = now + datetime.timedelta(seconds=20)
    
    
      self._periodic_event = self.core.schedule(next_update_time,self._my_periodic_event)

这样做的好处是,它使您可以完全控制调度过程。启用,禁用,何时启动等。如果需要使用成员变量,则可以更改秒数。

对于此问题的最新回应再次感到抱歉,但希望这会有所帮助!