GYOON
GYOON

Reputation: 19

Disable schedule function in VOLTTRON

To execute the function periodically, the following command is used.

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

I would like to disable the above function when certain conditions are met. Anyone knows how?

Upvotes: 0

Views: 38

Answers (1)

Craig
Craig

Reputation: 949

@GYOON

I would do what the code here does: https://github.com/VOLTTRON/volttron/blob/master/services/core/VolttronCentralPlatform/vcplatform/agent.py#L320

Basically what is going on is in the agent's onstart/onconfig method a function that is to be executed is called in a spawn later greenlet

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)

The good thing about this is it allows you to have complete control over the scheduling process. The enable, disable, when to start etc. You can change the number of seconds if you need to with member variables.

Again sorry for the late response on this, but hopefully this helps!

Upvotes: 1

Related Questions