2016-12-30 02:51:56 -05:00
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
import time, logging
|
|
|
|
from threading import Lock
|
|
|
|
from functools import partial
|
|
|
|
from collections import namedtuple
|
|
|
|
|
2017-06-03 17:13:45 -04:00
|
|
|
from auxilary import CountdownTimer, resetUSBDevice
|
2017-06-03 17:34:30 -04:00
|
|
|
from config import stateFile
|
2017-06-07 01:42:58 -04:00
|
|
|
from sensors import startDoorSensor, startMotionSensor
|
2017-05-31 00:41:42 -04:00
|
|
|
from gmail import intruderAlert
|
2016-12-30 02:51:56 -05:00
|
|
|
from listeners import KeypadListener, PipeListener
|
|
|
|
from blinkenLights import Blinkenlights
|
|
|
|
from soundLib import SoundLib
|
2017-06-07 01:42:58 -04:00
|
|
|
from webInterface import startWebInterface
|
2017-06-03 01:28:57 -04:00
|
|
|
from stream import Camera, FileDump
|
2016-12-30 02:51:56 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class SIGNALS:
|
|
|
|
ARM = 1
|
|
|
|
INSTANT_ARM = 2
|
|
|
|
DISARM = 3
|
|
|
|
TIMOUT = 4
|
|
|
|
TRIGGER = 5
|
|
|
|
|
|
|
|
class State:
|
|
|
|
def __init__(self, stateMachine, name, entryCallbacks=[], exitCallbacks=[], blinkLED=True, sound=None):
|
|
|
|
self.stateMachine = stateMachine
|
|
|
|
self.name = name
|
|
|
|
self.entryCallbacks = entryCallbacks
|
|
|
|
self.exitCallbacks = exitCallbacks
|
|
|
|
self.blinkLED = blinkLED
|
|
|
|
|
|
|
|
if not sound and name in stateMachine.soundLib.soundEffects:
|
|
|
|
self.sound = stateMachine.soundLib.soundEffects[name]
|
|
|
|
else:
|
|
|
|
self.sound = sound
|
|
|
|
|
|
|
|
def entry(self):
|
2017-05-30 02:11:15 -04:00
|
|
|
logger.info('entering ' + self.name)
|
2017-05-21 14:29:17 -04:00
|
|
|
if self.sound:
|
|
|
|
self.sound.play()
|
|
|
|
self.stateMachine.LED.blink = self.blinkLED
|
|
|
|
self.stateMachine.keypadListener.resetBuffer()
|
2016-12-30 02:51:56 -05:00
|
|
|
for c in self.entryCallbacks:
|
|
|
|
c()
|
|
|
|
|
|
|
|
def exit(self):
|
2017-05-30 02:11:15 -04:00
|
|
|
logger.info('exiting ' + self.name)
|
2017-05-21 14:29:17 -04:00
|
|
|
if self.sound:
|
|
|
|
self.sound.stop()
|
2016-12-30 02:51:56 -05:00
|
|
|
for c in self.exitCallbacks:
|
|
|
|
c()
|
|
|
|
|
|
|
|
def next(self, signal):
|
|
|
|
t = (self, signal)
|
2017-06-08 01:22:43 -04:00
|
|
|
return self if t not in self.stateMachine.transitionTable else self.stateMachine.transitionTable[t]
|
2016-12-30 02:51:56 -05:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.name == other
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
|
|
|
class StateMachine:
|
2017-05-21 14:29:17 -04:00
|
|
|
def __init__(self):
|
2017-06-07 01:42:58 -04:00
|
|
|
self._lock = Lock()
|
|
|
|
|
2017-05-21 14:29:17 -04:00
|
|
|
self.soundLib = SoundLib()
|
2017-06-07 01:42:58 -04:00
|
|
|
self.LED = Blinkenlights(17)
|
|
|
|
self.camera = Camera()
|
|
|
|
self.fileDump = FileDump()
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-06-08 01:02:31 -04:00
|
|
|
secretTable = {
|
|
|
|
"dynamoHum": partial(self.selectState, SIGNALS.DISARM),
|
|
|
|
"zombyWoof": partial(self.selectState, SIGNALS.ARM),
|
|
|
|
"imTheSlime": partial(self.selectState, SIGNALS.INSTANT_ARM)
|
|
|
|
}
|
|
|
|
|
|
|
|
def secretCallback(secret, logger):
|
|
|
|
if secret in secretTable:
|
|
|
|
secretTable[secret]()
|
|
|
|
logger.debug('Secret pipe listener received: \"%s\"', secret)
|
|
|
|
elif logger:
|
|
|
|
logger.debug('Secret pipe listener received invalid secret')
|
|
|
|
|
|
|
|
self.secretListener = PipeListener(
|
|
|
|
callback = secretCallback,
|
|
|
|
name = 'secret'
|
|
|
|
)
|
|
|
|
|
|
|
|
self.keypadListener = KeypadListener(
|
|
|
|
stateMachine = self,
|
2017-06-08 01:22:40 -04:00
|
|
|
callbackDisarm = partial(self.selectState, SIGNALS.DISARM),
|
|
|
|
callbackArm = partial(self.selectState, SIGNALS.ARM),
|
2017-06-08 01:02:31 -04:00
|
|
|
soundLib = self.soundLib,
|
|
|
|
passwd = '5918462'
|
|
|
|
)
|
|
|
|
|
2016-12-30 02:51:56 -05:00
|
|
|
def startTimer(t, sound):
|
|
|
|
self._timer = CountdownTimer(t, partial(self.selectState, SIGNALS.TIMOUT), sound)
|
|
|
|
|
|
|
|
def stopTimer():
|
|
|
|
if self._timer.is_alive():
|
|
|
|
self._timer.stop()
|
|
|
|
self._timer = None
|
2017-06-03 19:11:06 -04:00
|
|
|
|
|
|
|
stateObjs = [
|
2016-12-30 02:51:56 -05:00
|
|
|
State(
|
|
|
|
self,
|
|
|
|
name = 'disarmed',
|
|
|
|
blinkLED = False
|
|
|
|
),
|
|
|
|
State(
|
|
|
|
self,
|
|
|
|
name='disarmedCountdown',
|
|
|
|
entryCallbacks = [partial(startTimer, 30, self.soundLib.soundEffects['disarmedCountdown'])],
|
|
|
|
exitCallbacks = [stopTimer]
|
|
|
|
),
|
|
|
|
State(
|
|
|
|
self,
|
|
|
|
name = 'armed'
|
|
|
|
),
|
|
|
|
State(
|
|
|
|
self,
|
|
|
|
name = 'armedCountdown',
|
|
|
|
entryCallbacks = [partial(startTimer, 30, self.soundLib.soundEffects['armedCountdown'])],
|
|
|
|
exitCallbacks = [stopTimer]
|
|
|
|
),
|
|
|
|
State(
|
|
|
|
self,
|
|
|
|
name = 'triggered',
|
|
|
|
entryCallbacks = [intruderAlert]
|
|
|
|
)
|
2017-06-03 19:11:06 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
self.states = namedtuple('States', [s.name for s in stateObjs])(*stateObjs)
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-06-03 17:34:30 -04:00
|
|
|
self.currentState = getattr(self.states, stateFile['state'])
|
2016-12-30 02:51:56 -05:00
|
|
|
|
|
|
|
self.transitionTable = {
|
|
|
|
(self.states.disarmed, SIGNALS.ARM): self.states.disarmedCountdown,
|
|
|
|
(self.states.disarmed, SIGNALS.INSTANT_ARM): self.states.armed,
|
|
|
|
|
|
|
|
(self.states.disarmedCountdown, SIGNALS.DISARM): self.states.disarmed,
|
|
|
|
(self.states.disarmedCountdown, SIGNALS.TIMOUT): self.states.armed,
|
|
|
|
(self.states.disarmedCountdown, SIGNALS.INSTANT_ARM): self.states.armed,
|
|
|
|
|
|
|
|
(self.states.armed, SIGNALS.DISARM): self.states.disarmed,
|
|
|
|
(self.states.armed, SIGNALS.TRIGGER): self.states.armedCountdown,
|
|
|
|
|
|
|
|
(self.states.armedCountdown, SIGNALS.DISARM): self.states.disarmed,
|
|
|
|
(self.states.armedCountdown, SIGNALS.ARM): self.states.armed,
|
|
|
|
(self.states.armedCountdown, SIGNALS.TIMOUT): self.states.triggered,
|
|
|
|
|
|
|
|
(self.states.triggered, SIGNALS.DISARM): self.states.disarmed,
|
|
|
|
(self.states.triggered, SIGNALS.ARM): self.states.armed,
|
|
|
|
}
|
|
|
|
|
2017-06-07 01:42:58 -04:00
|
|
|
def start(self):
|
2017-06-08 01:02:31 -04:00
|
|
|
resetUSBDevice('1-1', logger)
|
2017-06-07 01:42:58 -04:00
|
|
|
|
|
|
|
self.soundLib.start()
|
|
|
|
self.LED.start()
|
|
|
|
self.keypadListener.start()
|
|
|
|
self.secretListener.start()
|
|
|
|
self.camera.start()
|
|
|
|
self.fileDump.start()
|
|
|
|
|
|
|
|
def action():
|
|
|
|
if self.currentState == self.states.armed:
|
|
|
|
self.selectState(SIGNALS.TRIGGER)
|
|
|
|
|
|
|
|
sensitiveStates = (self.states.armed, self.states.armedCountdown, self.states.triggered)
|
2017-05-21 14:29:17 -04:00
|
|
|
|
2017-06-07 01:42:58 -04:00
|
|
|
def actionVideo(pin):
|
|
|
|
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)
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-06-07 01:42:58 -04:00
|
|
|
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))
|
|
|
|
|
|
|
|
startDoorSensor(22, action, self.soundLib.soundEffects['door'])
|
|
|
|
|
|
|
|
startWebInterface(self)
|
|
|
|
|
|
|
|
self.currentState.entry()
|
|
|
|
|
2016-12-30 02:51:56 -05:00
|
|
|
def selectState(self, signal):
|
2017-06-03 17:13:45 -04:00
|
|
|
with self._lock:
|
2016-12-30 02:51:56 -05:00
|
|
|
nextState = self.currentState.next(signal)
|
|
|
|
if nextState != self.currentState:
|
|
|
|
self.currentState.exit()
|
|
|
|
self.currentState = nextState
|
|
|
|
self.currentState.entry()
|
|
|
|
|
2017-06-03 17:34:30 -04:00
|
|
|
stateFile['state'] = self.currentState.name
|
2016-12-30 02:51:56 -05:00
|
|
|
|
|
|
|
logger.info('state changed to %s', self.currentState)
|
|
|
|
|
|
|
|
def __del__(self):
|
2017-06-03 19:11:06 -04:00
|
|
|
for i in ['LED', 'camera', 'fileDump', 'soundLib', 'secretListener', 'keypadListener']:
|
|
|
|
try:
|
|
|
|
getattr(self, i).__del__()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|