aboutsummaryrefslogtreecommitdiff
path: root/behaviors/TimeSwitch.py
diff options
context:
space:
mode:
authorGravatar rcoh <rcoh@mit.edu>2011-02-16 18:29:41 -0500
committerGravatar rcoh <rcoh@mit.edu>2011-02-16 18:29:41 -0500
commitfffc14c4672294d9bbf8c60edfe8c309f0d54698 (patch)
tree19057b2e98735cc30fa1e0b3932d416946f8d705 /behaviors/TimeSwitch.py
parent24aa31808de6a4dc06a651076e5b292aebd9240d (diff)
parent2df9e408a0ff74539862c4a4e562a878cc11a329 (diff)
Merge branch 'conner5' of https://github.com/dxiao/SmootLight into dxiao-conner5
Conflicts: config/C5Sign.xml layouts/SpecifiedLayout.py
Diffstat (limited to 'behaviors/TimeSwitch.py')
-rw-r--r--behaviors/TimeSwitch.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/behaviors/TimeSwitch.py b/behaviors/TimeSwitch.py
new file mode 100644
index 0000000..cfbfe4a
--- /dev/null
+++ b/behaviors/TimeSwitch.py
@@ -0,0 +1,25 @@
+from operationscore.Behavior import *
+import util.TimeOps as clock
+import util.ComponentRegistry as compReg
+from logger import main_log
+class TimeSwitch(Behavior):
+ """TimeSwitch is a behavior that alternates between different behaviors for a set amount of time
+ (specify time in seconds. Specify in a python-style dict:
+ <Behaviors>{'behaviorId1':60, 'behaviorId2':120}</Behaviors>
+ Would alternate between the 2 behaviors, spending 1 minute on b1 and 2 minutes on b2.
+ """
+ def behaviorInit(self):
+ self.keyIndex = 0
+ self.currentBehaviorId = self['TimeMap'].keys()[self.keyIndex]
+ self.behaviorStart = clock.time()
+
+ def processResponse(self, sensors, recurs):
+ if self.behaviorStart + self['TimeMap'][self.currentBehaviorId]*1000 <= clock.time():
+ self.keyIndex += 1
+ self.keyIndex = self.keyIndex % len(self['TimeMap'])
+ self.currentBehaviorId = self['TimeMap'].keys()[self.keyIndex]
+ self.behaviorStart = clock.time()
+ main_log.info('Switching behaviors')
+ sensors = [s for s in sensors if s['InputId'] == self['InputMap'][self.currentBehaviorId]]
+ return compReg.getComponent(self.currentBehaviorId).immediateProcessInput(sensors, recurs)
+