2016-12-30 02:51:56 -05:00
|
|
|
import RPi.GPIO as GPIO
|
2017-06-10 01:13:51 -04:00
|
|
|
import time, logging, enum, weakref
|
2016-12-30 02:51:56 -05:00
|
|
|
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__)
|
|
|
|
|
2017-06-08 01:43:14 -04:00
|
|
|
class SIGNALS(enum.Enum):
|
|
|
|
ARM = enum.auto()
|
|
|
|
INSTANT_ARM = enum.auto()
|
|
|
|
DISARM = enum.auto()
|
|
|
|
TIMOUT = enum.auto()
|
|
|
|
TRIGGER = enum.auto()
|
2016-12-30 02:51:56 -05:00
|
|
|
|
|
|
|
class State:
|
2017-06-10 01:18:36 -04:00
|
|
|
def __init__(self, name, entryCallbacks=[], exitCallbacks=[], sound=None):
|
2016-12-30 02:51:56 -05:00
|
|
|
self.name = name
|
|
|
|
self.entryCallbacks = entryCallbacks
|
|
|
|
self.exitCallbacks = exitCallbacks
|
2017-06-10 01:13:51 -04:00
|
|
|
self._transTbl = {}
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-06-10 01:18:36 -04:00
|
|
|
self._sound = sound
|
2016-12-30 02:51:56 -05:00
|
|
|
|
|
|
|
def entry(self):
|
2017-05-30 02:11:15 -04:00
|
|
|
logger.info('entering ' + self.name)
|
2017-06-10 01:18:36 -04:00
|
|
|
if self._sound:
|
|
|
|
self._sound.play()
|
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-06-10 01:18:36 -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):
|
2017-06-08 01:43:14 -04:00
|
|
|
if signal in SIGNALS:
|
2017-06-10 01:13:51 -04:00
|
|
|
return self if signal not in self._transTbl else self._transTbl[signal]
|
2017-06-08 01:43:14 -04:00
|
|
|
else:
|
|
|
|
raise Exception('Illegal signal')
|
2017-06-10 01:13:51 -04:00
|
|
|
|
|
|
|
def addTransition(self, signal, state):
|
|
|
|
self._transTbl[signal] = weakref.ref(state)
|
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 02:33:05 -04:00
|
|
|
# add signals to self to avoid calling partial every time
|
|
|
|
for s in SIGNALS:
|
|
|
|
setattr(self, s.name, partial(self.selectState, s))
|
|
|
|
|
2017-06-08 01:02:31 -04:00
|
|
|
secretTable = {
|
2017-06-08 02:33:05 -04:00
|
|
|
'dynamoHum': self.DISARM,
|
|
|
|
'zombyWoof': self.ARM,
|
|
|
|
'imTheSlime': self.INSTANT_ARM
|
2017-06-08 01:02:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
2017-06-08 02:33:05 -04:00
|
|
|
self.secretListener = PipeListener(callback=secretCallback, name= 'secret')
|
2017-06-08 01:02:31 -04:00
|
|
|
|
|
|
|
self.keypadListener = KeypadListener(
|
|
|
|
stateMachine = self,
|
2017-06-08 02:33:05 -04:00
|
|
|
callbackDisarm = self.DISARM,
|
|
|
|
callbackArm = self.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):
|
2017-06-08 02:33:05 -04:00
|
|
|
self._timer = CountdownTimer(t, self.TIMOUT, sound)
|
2016-12-30 02:51:56 -05:00
|
|
|
|
|
|
|
def stopTimer():
|
|
|
|
if self._timer.is_alive():
|
|
|
|
self._timer.stop()
|
|
|
|
self._timer = None
|
2017-06-03 19:11:06 -04:00
|
|
|
|
2017-06-08 02:15:50 -04:00
|
|
|
blinkingLED = partial(self.LED.setBlink, True)
|
|
|
|
sfx = self.soundLib.soundEffects
|
|
|
|
|
2017-06-03 19:11:06 -04:00
|
|
|
stateObjs = [
|
2016-12-30 02:51:56 -05:00
|
|
|
State(
|
|
|
|
name = 'disarmed',
|
2017-06-10 01:18:36 -04:00
|
|
|
entryCallbacks = [partial(self.LED.setBlink, False)],
|
|
|
|
sound = sfx['disarmed']
|
2016-12-30 02:51:56 -05:00
|
|
|
),
|
|
|
|
State(
|
2017-06-08 02:15:50 -04:00
|
|
|
name = 'disarmedCountdown',
|
2017-06-10 01:18:36 -04:00
|
|
|
entryCallbacks = [blinkingLED, partial(startTimer, 30, sfx['disarmedCountdown'])],
|
|
|
|
exitCallbacks = [stopTimer],
|
|
|
|
sound = sfx['disarmedCountdown']
|
2016-12-30 02:51:56 -05:00
|
|
|
),
|
|
|
|
State(
|
2017-06-08 02:15:50 -04:00
|
|
|
name = 'armed',
|
2017-06-10 01:18:36 -04:00
|
|
|
entryCallbacks = [blinkingLED],
|
|
|
|
sound = sfx['armed']
|
2016-12-30 02:51:56 -05:00
|
|
|
),
|
|
|
|
State(
|
|
|
|
name = 'armedCountdown',
|
2017-06-10 01:18:36 -04:00
|
|
|
entryCallbacks = [blinkingLED, partial(startTimer, 30, sfx['armedCountdown'])],
|
|
|
|
exitCallbacks = [stopTimer],
|
|
|
|
sound = sfx['armedCountdown']
|
2016-12-30 02:51:56 -05:00
|
|
|
),
|
|
|
|
State(
|
|
|
|
name = 'triggered',
|
2017-06-10 01:18:36 -04:00
|
|
|
entryCallbacks = [blinkingLED, intruderAlert],
|
|
|
|
sound = sfx['triggered']
|
2016-12-30 02:51:56 -05:00
|
|
|
)
|
2017-06-03 19:11:06 -04:00
|
|
|
]
|
|
|
|
|
2017-06-08 02:15:50 -04:00
|
|
|
for s in stateObjs:
|
|
|
|
s.entryCallbacks.append(self.keypadListener.resetBuffer)
|
|
|
|
|
2017-06-10 01:13:51 -04:00
|
|
|
self.states = s = namedtuple('States', [s.name for s in stateObjs])(*stateObjs)
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-06-10 01:13:51 -04:00
|
|
|
s.disarmed.addTransition( SIGNALS.ARM, s.disarmedCountdown)
|
|
|
|
s.disarmed.addTransition( SIGNALS.INSTANT_ARM, s.armed)
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-06-10 01:13:51 -04:00
|
|
|
s.disarmedCountdown.addTransition( SIGNALS.DISARM, s.disarmed)
|
|
|
|
s.disarmedCountdown.addTransition( SIGNALS.TIMOUT, s.armed)
|
|
|
|
s.disarmedCountdown.addTransition( SIGNALS.INSTANT_ARM, s.armed)
|
|
|
|
|
|
|
|
s.armed.addTransition( SIGNALS.DISARM, s.disarmed)
|
|
|
|
s.armed.addTransition( SIGNALS.TRIGGER, s.armedCountdown)
|
|
|
|
|
|
|
|
s.armedCountdown.addTransition( SIGNALS.DISARM, s.disarmed)
|
|
|
|
s.armedCountdown.addTransition( SIGNALS.TIMOUT, s.triggered)
|
|
|
|
s.armedCountdown.addTransition( SIGNALS.ARM, s.armed)
|
|
|
|
s.armedCountdown.addTransition( SIGNALS.INSTANT_ARM, s.armed)
|
|
|
|
|
|
|
|
s.triggered.addTransition( SIGNALS.DISARM, s.disarmed)
|
|
|
|
s.triggered.addTransition( SIGNALS.ARM, s.armed)
|
|
|
|
s.triggered.addTransition( SIGNALS.INSTANT_ARM, s.armed)
|
|
|
|
|
|
|
|
self.currentState = getattr(self.states, stateFile['state'])
|
2016-12-30 02:51:56 -05:00
|
|
|
|
2017-06-10 01:35:46 -04:00
|
|
|
def __enter__(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()
|
2017-06-10 01:13:51 -04:00
|
|
|
|
2017-06-10 01:35:46 -04:00
|
|
|
def __exit__(self, exception_type, exception_value, traceback):
|
2017-06-10 01:52:06 -04:00
|
|
|
for i in ['LED', 'camera', 'fileDump', 'soundLib', 'secretListener', 'keypadListener']:
|
|
|
|
try:
|
|
|
|
getattr(self, i).stop()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2017-06-10 01:35:46 -04:00
|
|
|
|
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
|