better names for states

This commit is contained in:
petrucci4prez 2017-06-18 15:16:42 -04:00
parent 4b180efe19
commit dc8ff22ed8
3 changed files with 39 additions and 39 deletions

View File

@ -7,7 +7,7 @@ logger = logging.getLogger(__name__)
# this importantly controls which sensor events get logged. DEBUG logs # this importantly controls which sensor events get logged. DEBUG logs
# everything, INFO logs only events that occur when state machine in # everything, INFO logs only events that occur when state machine in
# "sensitive states" (armed, armedCountdown, triggered) # "sensitive states" (armed, trippedCountdown, tripped)
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
# delay GPIO init to avoid false positive during powerup # delay GPIO init to avoid false positive during powerup

View File

@ -95,13 +95,13 @@ class SoundLib:
mixer.init() mixer.init()
self.soundEffects = { self.soundEffects = {
'disarmedCountdown': SoundEffect(path='soundfx/smb_kick.wav'),
'disarmed': SoundEffect(path='soundfx/smb_pause.wav'), 'disarmed': SoundEffect(path='soundfx/smb_pause.wav'),
'armedCountdown': SoundEffect(path='soundfx/smb_kick.wav'),
'armed': SoundEffect(path='soundfx/smb_powerup.wav'), 'armed': SoundEffect(path='soundfx/smb_powerup.wav'),
'armedCountdown': SoundEffect(path='soundfx/smb2_door_appears.wav'),
'locked': SoundEffect(path='soundfx/smb_1-up.wav'),
'lockedCountdown': SoundEffect(path='soundfx/smb_stomp.wav'), 'lockedCountdown': SoundEffect(path='soundfx/smb_stomp.wav'),
'triggered': SoundEffect(path='soundfx/alarms/burgler_alarm.ogg', volume=1.0, loops=-1), 'locked': SoundEffect(path='soundfx/smb_1-up.wav'),
'trippedCountdown': SoundEffect(path='soundfx/smb2_door_appears.wav'),
'tripped': SoundEffect(path='soundfx/alarms/burgler_alarm.ogg', volume=1.0, loops=-1),
'door': SoundEffect(path='soundfx/smb_pipe.wav'), 'door': SoundEffect(path='soundfx/smb_pipe.wav'),
'numKey': SoundEffect(path='soundfx/smb_bump.wav'), 'numKey': SoundEffect(path='soundfx/smb_bump.wav'),
'ctrlKey': SoundEffect(path='soundfx/smb_fireball.wav'), 'ctrlKey': SoundEffect(path='soundfx/smb_fireball.wav'),
@ -141,7 +141,7 @@ class SoundLib:
@async(daemon=False) @async(daemon=False)
def _fader(self, lowerVolume, totalDuration, fadeDuration=0.2, stepSize=5): def _fader(self, lowerVolume, totalDuration, fadeDuration=0.2, stepSize=5):
with self._lock: with self._lock:
alarm = self.soundEffects['triggered'] alarm = self.soundEffects['tripped']
alarmVolume = alarm.volume alarmVolume = alarm.volume
alarmVolumeDelta = alarmVolume - lowerVolume alarmVolumeDelta = alarmVolume - lowerVolume

View File

@ -38,7 +38,7 @@ class _SIGNALS(enum.Enum):
INSTANT_LOCK = enum.auto() INSTANT_LOCK = enum.auto()
DISARM = enum.auto() DISARM = enum.auto()
TIMOUT = enum.auto() TIMOUT = enum.auto()
TRIGGER = enum.auto() TRIP = enum.auto()
class _CountdownTimer(ExceptionThread): class _CountdownTimer(ExceptionThread):
''' '''
@ -216,10 +216,10 @@ class StateMachine:
sound = sfx['disarmed'] sound = sfx['disarmed']
), ),
_State( _State(
name = 'disarmedCountdown', name = 'armedCountdown',
entryCallbacks = [partial(squareBlink, 1), partial(startTimer, 30, sfx['disarmedCountdown'])], entryCallbacks = [partial(squareBlink, 1), partial(startTimer, 30, sfx['armedCountdown'])],
exitCallbacks = [stopTimer], exitCallbacks = [stopTimer],
sound = sfx['disarmedCountdown'] sound = sfx['armedCountdown']
), ),
_State( _State(
name = 'armed', name = 'armed',
@ -238,59 +238,59 @@ class StateMachine:
sound = sfx['locked'] sound = sfx['locked']
), ),
_State( _State(
name = 'armedCountdown', name = 'trippedCountdown',
entryCallbacks = [partial(squareBlink, 1), partial(startTimer, 30, sfx['armedCountdown'])], entryCallbacks = [partial(squareBlink, 1), partial(startTimer, 30, sfx['trippedCountdown'])],
exitCallbacks = [stopTimer], exitCallbacks = [stopTimer],
sound = sfx['armedCountdown'] sound = sfx['trippedCountdown']
), ),
_State( _State(
name = 'triggered', name = 'tripped',
entryCallbacks = [partial(linearBlink, 0.5), intruderAlert], entryCallbacks = [partial(linearBlink, 0.5), intruderAlert],
sound = sfx['triggered'] sound = sfx['tripped']
) )
] ]
self.states = st = namedtuple('States', [obj.name for obj in stateObjs])(*stateObjs) self.states = st = namedtuple('States', [obj.name for obj in stateObjs])(*stateObjs)
st.disarmed.addTransition( _SIGNALS.ARM, st.disarmedCountdown) st.disarmed.addTransition( _SIGNALS.ARM, st.armedCountdown)
st.disarmed.addTransition( _SIGNALS.INSTANT_ARM, st.armed) st.disarmed.addTransition( _SIGNALS.INSTANT_ARM, st.armed)
st.disarmed.addTransition( _SIGNALS.LOCK, st.lockedCountdown) st.disarmed.addTransition( _SIGNALS.LOCK, st.lockedCountdown)
st.disarmed.addTransition( _SIGNALS.INSTANT_LOCK, st.locked) st.disarmed.addTransition( _SIGNALS.INSTANT_LOCK, st.locked)
st.disarmedCountdown.addTransition( _SIGNALS.DISARM, st.disarmed) st.armedCountdown.addTransition( _SIGNALS.DISARM, st.disarmed)
st.disarmedCountdown.addTransition( _SIGNALS.TIMOUT, st.armed) st.armedCountdown.addTransition( _SIGNALS.TIMOUT, st.armed)
st.disarmedCountdown.addTransition( _SIGNALS.INSTANT_ARM, st.armed) st.armedCountdown.addTransition( _SIGNALS.INSTANT_ARM, st.armed)
st.disarmedCountdown.addTransition( _SIGNALS.LOCK, st.lockedCountdown) st.armedCountdown.addTransition( _SIGNALS.LOCK, st.lockedCountdown)
st.disarmedCountdown.addTransition( _SIGNALS.INSTANT_LOCK, st.locked) st.armedCountdown.addTransition( _SIGNALS.INSTANT_LOCK, st.locked)
st.armed.addTransition( _SIGNALS.DISARM, st.disarmed) st.armed.addTransition( _SIGNALS.DISARM, st.disarmed)
st.armed.addTransition( _SIGNALS.TRIGGER, st.armedCountdown) st.armed.addTransition( _SIGNALS.TRIP, st.trippedCountdown)
st.armed.addTransition( _SIGNALS.LOCK, st.lockedCountdown) st.armed.addTransition( _SIGNALS.LOCK, st.lockedCountdown)
st.armed.addTransition( _SIGNALS.INSTANT_LOCK, st.locked) st.armed.addTransition( _SIGNALS.INSTANT_LOCK, st.locked)
st.lockedCountdown.addTransition( _SIGNALS.DISARM, st.disarmed) st.lockedCountdown.addTransition( _SIGNALS.DISARM, st.disarmed)
st.lockedCountdown.addTransition( _SIGNALS.TIMOUT, st.locked) st.lockedCountdown.addTransition( _SIGNALS.TIMOUT, st.locked)
st.lockedCountdown.addTransition( _SIGNALS.INSTANT_LOCK, st.locked) st.lockedCountdown.addTransition( _SIGNALS.INSTANT_LOCK, st.locked)
st.lockedCountdown.addTransition( _SIGNALS.ARM, st.disarmedCountdown) st.lockedCountdown.addTransition( _SIGNALS.ARM, st.armedCountdown)
st.lockedCountdown.addTransition( _SIGNALS.INSTANT_ARM, st.armed) st.lockedCountdown.addTransition( _SIGNALS.INSTANT_ARM, st.armed)
st.locked.addTransition( _SIGNALS.DISARM, st.disarmed) st.locked.addTransition( _SIGNALS.DISARM, st.disarmed)
st.locked.addTransition( _SIGNALS.TRIGGER, st.armedCountdown) st.locked.addTransition( _SIGNALS.TRIP, st.trippedCountdown)
st.locked.addTransition( _SIGNALS.ARM, st.disarmedCountdown) st.locked.addTransition( _SIGNALS.ARM, st.armedCountdown)
st.locked.addTransition( _SIGNALS.INSTANT_ARM, st.armed) st.locked.addTransition( _SIGNALS.INSTANT_ARM, st.armed)
st.armedCountdown.addTransition( _SIGNALS.DISARM, st.disarmed) st.trippedCountdown.addTransition( _SIGNALS.DISARM, st.disarmed)
st.armedCountdown.addTransition( _SIGNALS.TIMOUT, st.triggered) st.trippedCountdown.addTransition( _SIGNALS.TIMOUT, st.tripped)
st.armedCountdown.addTransition( _SIGNALS.ARM, st.armed) st.trippedCountdown.addTransition( _SIGNALS.ARM, st.armed)
st.armedCountdown.addTransition( _SIGNALS.INSTANT_ARM, st.armed) st.trippedCountdown.addTransition( _SIGNALS.INSTANT_ARM, st.armed)
st.armedCountdown.addTransition( _SIGNALS.LOCK, st.locked) st.trippedCountdown.addTransition( _SIGNALS.LOCK, st.locked)
st.armedCountdown.addTransition( _SIGNALS.INSTANT_LOCK, st.locked) st.trippedCountdown.addTransition( _SIGNALS.INSTANT_LOCK, st.locked)
st.triggered.addTransition( _SIGNALS.DISARM, st.disarmed) st.tripped.addTransition( _SIGNALS.DISARM, st.disarmed)
st.triggered.addTransition( _SIGNALS.ARM, st.armed) st.tripped.addTransition( _SIGNALS.ARM, st.armed)
st.triggered.addTransition( _SIGNALS.INSTANT_ARM, st.armed) st.tripped.addTransition( _SIGNALS.INSTANT_ARM, st.armed)
st.triggered.addTransition( _SIGNALS.LOCK, st.locked) st.tripped.addTransition( _SIGNALS.LOCK, st.locked)
st.triggered.addTransition( _SIGNALS.INSTANT_LOCK, st.locked) st.tripped.addTransition( _SIGNALS.INSTANT_LOCK, st.locked)
self.currentState = getattr(self.states, stateFile['state']) self.currentState = getattr(self.states, stateFile['state'])
@ -300,14 +300,14 @@ class StateMachine:
# start all managed threads (we retain ref to these to stop them later) # start all managed threads (we retain ref to these to stop them later)
self._startManaged() self._startManaged()
activeSensorStates = (self.states.armed, self.states.armedCountdown, self.states.triggered) activeSensorStates = (self.states.armed, self.states.trippedCountdown, self.states.tripped)
def sensorAction(location, logger): def sensorAction(location, logger):
cst = self.currentState cst = self.currentState
level = logging.INFO if cst in activeSensorStates else logging.DEBUG level = logging.INFO if cst in activeSensorStates else logging.DEBUG
logger.log(level, 'detected motion: ' + location) logger.log(level, 'detected motion: ' + location)
if cst == self.states.armed: if cst == self.states.armed:
self.selectState(_SIGNALS.TRIGGER) self.selectState(_SIGNALS.TRIP)
def videoAction(location, logger, pin): def videoAction(location, logger, pin):
sensorAction(location, logger) sensorAction(location, logger)
@ -327,7 +327,7 @@ class StateMachine:
entry = 'door closed' if closed else 'door opened' entry = 'door closed' if closed else 'door opened'
logger.log(level, entry) logger.log(level, entry)
if not closed and cst == self.states.armed or cst == self.states.locked: if not closed and cst == self.states.armed or cst == self.states.locked:
self.selectState(_SIGNALS.TRIGGER) self.selectState(_SIGNALS.TRIP)
# start non-managed threads (we forget about these because they can exit with no cleanup) # start non-managed threads (we forget about these because they can exit with no cleanup)
startMotionSensor(5, 'Nate\'s room', sensorAction) startMotionSensor(5, 'Nate\'s room', sensorAction)