NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

timers.py (3942B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\behaviors\monitors\timers.py
      2 import uthread2
      3 from behaviors import status
      4 from behaviors.tasks import Task
      5 from ccpProfile import TimedFunction
      6 
      7 class ResetAfterTimeout(Task):
      8 
      9     def __init__(self, attributes):
     10         super(ResetAfterTimeout, self).__init__(attributes)
     11         self.timeoutThread = None
     12 
     13     @TimedFunction('behaviors::monitors::timers::ResetAfterTimeout::OnEnter')
     14     def OnEnter(self):
     15         self.status = status.TaskSuccessStatus
     16         timerValue = self.GetLastBlackboardValue(self.attributes.timerAddress)
     17         secondsUntilReset = self.attributes.timeoutSeconds
     18         if timerValue is None:
     19             self._StartTimer(secondsUntilReset)
     20 
     21     @TimedFunction('behaviors::monitors::timers::ResetAfterTimeout::TimeoutThread')
     22     def TimeoutThread(self, secondsUntilReset):
     23         uthread2.SleepSim(secondsUntilReset)
     24         if self.status == status.TaskInvalidStatus:
     25             return
     26         self.SendBlackboardValue(self.attributes.timerAddress, None)
     27         self.behaviorTree.RequestReset(requestedBy=self)
     28 
     29     def CleanUp(self):
     30         self.timeoutThread = None
     31         self.status = status.TaskInvalidStatus
     32 
     33     def _StartTimer(self, secondsUntilReset):
     34         self.timeoutThread = uthread2.StartTasklet(self.TimeoutThread, secondsUntilReset, stackless_tracing_enabled=False)
     35         self.SendBlackboardValue(self.attributes.timerAddress, secondsUntilReset)
     36 
     37 
     38 class ResetAfterTimeoutBlocks(ResetAfterTimeout):
     39 
     40     @TimedFunction('behaviors::monitors::timers::ResetAfterTimeoutBlocks::OnEnter')
     41     def OnEnter(self):
     42         self.status = status.TaskFailureStatus
     43         timerValue = self.GetLastBlackboardValue(self.attributes.timerAddress)
     44         secondsUntilReset = self.attributes.timeoutSeconds
     45         if timerValue is None:
     46             self.status = status.TaskSuccessStatus
     47             self._StartTimer(secondsUntilReset)
     48 
     49 
     50 class StartTimer(Task):
     51 
     52     def __init__(self, attributes):
     53         super(StartTimer, self).__init__(attributes)
     54 
     55     @TimedFunction('behaviors::monitors::timers::StartTimer::OnEnter')
     56     def OnEnter(self):
     57         self.status = status.TaskSuccessStatus
     58         timerValue = self.GetLastBlackboardValue(self.attributes.timerAddress)
     59         secondsUntilReset = self.attributes.timeoutSeconds
     60         if timerValue is None:
     61             channel = self.GetChannelFromAddress(self.attributes.timerAddress)
     62             self.context.timerManager.StartTimer(secondsUntilReset, channel)
     63 
     64     def CleanUp(self):
     65         self.status = status.TaskInvalidStatus
     66 
     67 
     68 class StartTimerBlocks(StartTimer):
     69 
     70     @TimedFunction('behaviors::monitors::timers::StartTimerBlocks::OnEnter')
     71     def OnEnter(self):
     72         self.status = status.TaskFailureStatus
     73         timerValue = self.GetLastBlackboardValue(self.attributes.timerAddress)
     74         secondsUntilReset = self.attributes.timeoutSeconds
     75         if timerValue is None:
     76             self.status = status.TaskSuccessStatus
     77             channel = self.GetChannelFromAddress(self.attributes.timerAddress)
     78             self.context.timerManager.StartTimer(secondsUntilReset, channel)
     79 
     80 
     81 class SetBooleanValueAndResetAfterTimeout(Task):
     82 
     83     @TimedFunction('behaviors::decorators::timers::SetBooleanValueAndResetAfterTimeout::OnEnter')
     84     def OnEnter(self):
     85         self.SetStatusToSuccess()
     86         channel = self.GetChannelFromAddress(self.attributes.timerAddress)
     87         timer_value = channel.GetLastMessageValue()
     88         if timer_value is None:
     89             self.context.timerManager.StartTimer(self.attributes.timeoutSeconds, channel)
     90         channel.AddObserver(self._on_time_out)
     91 
     92     def _on_time_out(self, name, message):
     93         self.SendBlackboardValue(self.attributes.channelAddress, self.attributes.trueOrFalse)
     94         if message.value is None and not self.IsInvalid():
     95             self.behaviorTree.RequestReset(requestedBy=self)