move sensor event logging features out of sensor module

This commit is contained in:
petrucci4prez 2017-06-13 02:48:56 -04:00
parent 96d31adff7
commit f936233244
2 changed files with 31 additions and 25 deletions

View File

@ -5,8 +5,10 @@ from threading import Timer
logger = logging.getLogger(__name__)
# this should never be higher than INFO or motion will never be logged
logger.setLevel(logging.DEBUG)
# this importantly controls which sensor events get logged. DEBUG logs
# everything, INFO logs only events that occur when state machine in
# "sensitive states" (armed, armedCountdown, triggered)
logger.setLevel(logging.INFO)
# delay GPIO init to avoid false positive during powerup
INIT_DELAY = 60
@ -32,15 +34,14 @@ def startMotionSensor(pin, location, action):
def trip(channel):
if lowPassFilter(pin, 1):
logger.info('detected motion: ' + location)
action()
action(location, logger)
logger.debug('waiting %s for %s to power on', INIT_DELAY, name)
t = Timer(INIT_DELAY, partial(_initGPIO, name, pin, GPIO.RISING, trip))
t.daemon = True
t.start()
def startDoorSensor(pin, action, sound=None):
def startDoorSensor(pin, action):
def trip(channel):
nonlocal closed
val = GPIO.input(pin)
@ -48,15 +49,7 @@ def startDoorSensor(pin, action, sound=None):
if val != closed:
if lowPassFilter(pin, val):
closed = val
if closed:
logger.info('door closed')
if sound:
sound.play()
else:
logger.info('door opened')
if sound:
sound.play()
action()
action(closed, logger)
_initGPIO('DoorSensor', pin, GPIO.BOTH, trip)
closed = GPIO.input(pin)

View File

@ -264,28 +264,41 @@ class StateMachine:
# start all managed threads (we retain ref to these to stop them later)
self._startManaged()
def action():
sensitiveStates = (self.states.armed, self.states.armedCountdown, self.states.triggered)
def sensorAction(location, logger):
level = logging.INFO if self.currentState in sensitiveStates else logging.DEBUG
logger.log(level, 'detected motion: ' + location)
if self.currentState == self.states.armed:
self.selectState(_SIGNALS.TRIGGER)
sensitiveStates = (self.states.armed, self.states.armedCountdown, self.states.triggered)
def actionVideo(pin):
def videoAction(location, logger, pin):
sensorAction(location, logger)
if self.currentState in sensitiveStates:
self.selectState(_SIGNALS.TRIGGER)
self.fileDump.addInitiator(pin)
while GPIO.input(pin) and self.currentState in sensitiveStates:
time.sleep(0.1)
self.fileDump.removeInitiator(pin)
# start non-managed threads (we forget about these because they can exit with no cleanup)
startMotionSensor(5, 'Nate\'s room', action)
startMotionSensor(19, 'front door', action)
startMotionSensor(26, 'Laura\'s room', action)
startMotionSensor(6, 'deck window', partial(actionVideo, 6))
startMotionSensor(13, 'kitchen bar', partial(actionVideo, 13))
def doorAction(closed, logger):
self.soundLib.soundEffects['door'].play()
startDoorSensor(22, action, self.soundLib.soundEffects['door'])
level = logging.INFO if self.currentState in sensitiveStates else logging.DEBUG
entry = 'door closed' if closed else 'door opened'
logger.log(level, entry)
if not closed and self.currentState == self.states.armed:
self.selectState(_SIGNALS.TRIGGER)
# start non-managed threads (we forget about these because they can exit with no cleanup)
startMotionSensor(5, 'Nate\'s room', sensorAction)
startMotionSensor(19, 'front door', sensorAction)
startMotionSensor(26, 'Laura\'s room', sensorAction)
startMotionSensor(6, 'deck window', partial(videoAction, pin=6))
startMotionSensor(13, 'kitchen bar', partial(videoAction, pin=13))
startDoorSensor(22, doorAction)
startWebInterface(self)