move all start and stop ops in statemachine to a thread manager

This commit is contained in:
petrucci4prez 2017-06-11 03:07:04 -04:00
parent 32e717fca3
commit b39db8d87e
1 changed files with 32 additions and 25 deletions

View File

@ -106,11 +106,12 @@ class _State:
class StateMachine: class StateMachine:
def __init__(self): def __init__(self):
self._lock = Lock() self._lock = Lock()
self._managed = []
self.soundLib = SoundLib() self.soundLib = self._addManaged(SoundLib())
self.LED = Blinkenlights(17) self.fileDump = self._addManaged(FileDump())
self.camera = Camera()
self.fileDump = FileDump() self._addManaged(Camera())
# add signals to self to avoid calling partial every time # add signals to self to avoid calling partial every time
for sig in _SIGNALS: for sig in _SIGNALS:
@ -129,14 +130,16 @@ class StateMachine:
elif logger: elif logger:
logger.debug('Secret pipe listener received invalid secret') logger.debug('Secret pipe listener received invalid secret')
self.secretListener = PipeListener(callback=secretCallback, name= 'secret') self._addManaged(PipeListener(callback=secretCallback, name= 'secret'))
self.keypadListener = KeypadListener( keypadListener = self._addManaged(
stateMachine = self, KeypadListener(
callbackDisarm = self.DISARM, stateMachine = self,
callbackArm = self.ARM, callbackDisarm = self.DISARM,
soundLib = self.soundLib, callbackArm = self.ARM,
passwd = '5918462' soundLib = self.soundLib,
passwd = '5918462'
)
) )
def startTimer(t, sound): def startTimer(t, sound):
@ -147,13 +150,14 @@ class StateMachine:
self._timer.stop() self._timer.stop()
self._timer = None self._timer = None
blinkingLED = partial(self.LED.setBlink, True) LED = self._addManaged(Blinkenlights(17))
blinkingLED = partial(LED.setBlink, True)
sfx = self.soundLib.soundEffects sfx = self.soundLib.soundEffects
stateObjs = [ stateObjs = [
_State( _State(
name = 'disarmed', name = 'disarmed',
entryCallbacks = [partial(self.LED.setBlink, False)], entryCallbacks = [partial(LED.setBlink, False)],
sound = sfx['disarmed'] sound = sfx['disarmed']
), ),
_State( _State(
@ -181,7 +185,7 @@ class StateMachine:
] ]
for obj in stateObjs: for obj in stateObjs:
obj.entryCallbacks.append(self.keypadListener.resetBuffer) obj.entryCallbacks.append(keypadListener.resetBuffer)
self.states = st = namedtuple('States', [obj.name for obj in stateObjs])(*stateObjs) self.states = st = namedtuple('States', [obj.name for obj in stateObjs])(*stateObjs)
@ -209,12 +213,7 @@ class StateMachine:
def __enter__(self): def __enter__(self):
_resetUSBDevice('1-1') _resetUSBDevice('1-1')
self.soundLib.start() self._startManaged()
self.LED.start()
self.keypadListener.start()
self.secretListener.start()
self.camera.start()
self.fileDump.start()
def action(): def action():
if self.currentState == self.states.armed: if self.currentState == self.states.armed:
@ -243,11 +242,7 @@ class StateMachine:
self.currentState.entry() self.currentState.entry()
def __exit__(self, exception_type, exception_value, traceback): def __exit__(self, exception_type, exception_value, traceback):
for i in ['LED', 'camera', 'fileDump', 'soundLib', 'secretListener', 'keypadListener']: self._stopManaged()
try:
getattr(self, i).stop()
except AttributeError:
pass
def selectState(self, signal): def selectState(self, signal):
with self._lock: with self._lock:
@ -258,3 +253,15 @@ class StateMachine:
self.currentState.entry() self.currentState.entry()
stateFile['state'] = self.currentState.name stateFile['state'] = self.currentState.name
def _addManaged(self, obj):
self._managed.append(obj)
return obj
def _startManaged(self):
for m in self._managed:
m.start()
def _stopManaged(self):
for m in self._managed:
m.stop()