make statemachine a contextmanager class

This commit is contained in:
petrucci4prez 2017-06-10 01:35:46 -04:00
parent 6b489fe3ab
commit 24fbb4b707
2 changed files with 11 additions and 19 deletions

14
main.py
View File

@ -12,11 +12,6 @@ logger = logging.getLogger(__name__)
def clean(): def clean():
GPIO.cleanup() GPIO.cleanup()
try:
stateMachine.__del__()
except NameError:
pass
try: try:
logger.info('Terminated root process - PID: %s', os.getpid()) logger.info('Terminated root process - PID: %s', os.getpid())
unmountGluster() unmountGluster()
@ -35,12 +30,9 @@ if __name__ == '__main__':
GPIO.setwarnings(False) GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
stateMachine = StateMachine() with StateMachine() as stateMachine:
stateMachine.start() signal.signal(signal.SIGTERM, sigtermHandler)
excChildListener()
signal.signal(signal.SIGTERM, sigtermHandler)
excChildListener()
except Exception: except Exception:
logger.critical(traceback.format_exc()) logger.critical(traceback.format_exc())

View File

@ -167,7 +167,7 @@ class StateMachine:
self.currentState = getattr(self.states, stateFile['state']) self.currentState = getattr(self.states, stateFile['state'])
def start(self): def __enter__(self):
resetUSBDevice('1-1', logger) resetUSBDevice('1-1', logger)
self.soundLib.start() self.soundLib.start()
@ -203,6 +203,13 @@ class StateMachine:
self.currentState.entry() self.currentState.entry()
def __exit__(self, exception_type, exception_value, traceback):
for i in ['LED', 'camera', 'fileDump', 'soundLib', 'secretListener', 'keypadListener']:
try:
getattr(self, i).__del__()
except AttributeError:
pass
def selectState(self, signal): def selectState(self, signal):
with self._lock: with self._lock:
nextState = self.currentState.next(signal) nextState = self.currentState.next(signal)
@ -212,10 +219,3 @@ class StateMachine:
self.currentState.entry() self.currentState.entry()
stateFile['state'] = self.currentState.name stateFile['state'] = self.currentState.name
def __del__(self):
for i in ['LED', 'camera', 'fileDump', 'soundLib', 'secretListener', 'keypadListener']:
try:
getattr(self, i).__del__()
except AttributeError:
pass