Package SmootLight :: Package behaviors :: Module TimeSwitch
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.behaviors.TimeSwitch

 1  from operationscore.Behavior import * 
 2  import util.TimeOps as clock 
 3  import util.ComponentRegistry as compReg 
 4  from logger import main_log 
5 -class TimeSwitch(Behavior):
6 """TimeSwitch is a behavior that alternates between different behaviors for a set amount of time 7 (specify time in seconds. Specify in a python-style dict: 8 <Behaviors>{'behaviorId1':60, 'behaviorId2':120}</Behaviors> 9 Would alternate between the 2 behaviors, spending 1 minute on b1 and 2 minutes on b2. 10 """
11 - def behaviorInit(self):
12 self.keyIndex = 0 13 self.currentBehaviorId = self['TimeMap'].keys()[self.keyIndex] 14 self.behaviorStart = clock.time()
15
16 - def processResponse(self, sensors, recurs):
17 if self.behaviorStart + self['TimeMap'][self.currentBehaviorId]*1000 <= clock.time(): 18 self.keyIndex += 1 19 self.keyIndex = self.keyIndex % len(self['TimeMap']) 20 self.currentBehaviorId = self['TimeMap'].keys()[self.keyIndex] 21 self.behaviorStart = clock.time() 22 main_log.info('Switching behaviors') 23 sensors = [s for s in sensors if s['InputId'] == self['InputMap'][self.currentBehaviorId]] 24 return compReg.getComponent(self.currentBehaviorId).immediateProcessInput(sensors, recurs)
25