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__) logger = logging.getLogger(__name__)
# this should never be higher than INFO or motion will never be logged # this importantly controls which sensor events get logged. DEBUG logs
logger.setLevel(logging.DEBUG) # 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 # delay GPIO init to avoid false positive during powerup
INIT_DELAY = 60 INIT_DELAY = 60
@ -32,15 +34,14 @@ def startMotionSensor(pin, location, action):
def trip(channel): def trip(channel):
if lowPassFilter(pin, 1): if lowPassFilter(pin, 1):
logger.info('detected motion: ' + location) action(location, logger)
action()
logger.debug('waiting %s for %s to power on', INIT_DELAY, name) logger.debug('waiting %s for %s to power on', INIT_DELAY, name)
t = Timer(INIT_DELAY, partial(_initGPIO, name, pin, GPIO.RISING, trip)) t = Timer(INIT_DELAY, partial(_initGPIO, name, pin, GPIO.RISING, trip))
t.daemon = True t.daemon = True
t.start() t.start()
def startDoorSensor(pin, action, sound=None): def startDoorSensor(pin, action):
def trip(channel): def trip(channel):
nonlocal closed nonlocal closed
val = GPIO.input(pin) val = GPIO.input(pin)
@ -48,15 +49,7 @@ def startDoorSensor(pin, action, sound=None):
if val != closed: if val != closed:
if lowPassFilter(pin, val): if lowPassFilter(pin, val):
closed = val closed = val
if closed: action(closed, logger)
logger.info('door closed')
if sound:
sound.play()
else:
logger.info('door opened')
if sound:
sound.play()
action()
_initGPIO('DoorSensor', pin, GPIO.BOTH, trip) _initGPIO('DoorSensor', pin, GPIO.BOTH, trip)
closed = GPIO.input(pin) 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) # start all managed threads (we retain ref to these to stop them later)
self._startManaged() 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: if self.currentState == self.states.armed:
self.selectState(_SIGNALS.TRIGGER) 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: if self.currentState in sensitiveStates:
self.selectState(_SIGNALS.TRIGGER)
self.fileDump.addInitiator(pin) self.fileDump.addInitiator(pin)
while GPIO.input(pin) and self.currentState in sensitiveStates: while GPIO.input(pin) and self.currentState in sensitiveStates:
time.sleep(0.1) time.sleep(0.1)
self.fileDump.removeInitiator(pin) self.fileDump.removeInitiator(pin)
def doorAction(closed, logger):
self.soundLib.soundEffects['door'].play()
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) # start non-managed threads (we forget about these because they can exit with no cleanup)
startMotionSensor(5, 'Nate\'s room', action) startMotionSensor(5, 'Nate\'s room', sensorAction)
startMotionSensor(19, 'front door', action) startMotionSensor(19, 'front door', sensorAction)
startMotionSensor(26, 'Laura\'s room', action) startMotionSensor(26, 'Laura\'s room', sensorAction)
startMotionSensor(6, 'deck window', partial(actionVideo, 6))
startMotionSensor(13, 'kitchen bar', partial(actionVideo, 13))
startDoorSensor(22, action, self.soundLib.soundEffects['door']) startMotionSensor(6, 'deck window', partial(videoAction, pin=6))
startMotionSensor(13, 'kitchen bar', partial(videoAction, pin=13))
startDoorSensor(22, doorAction)
startWebInterface(self) startWebInterface(self)