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

View File

@ -167,7 +167,7 @@ class StateMachine:
self.currentState = getattr(self.states, stateFile['state'])
def start(self):
def __enter__(self):
resetUSBDevice('1-1', logger)
self.soundLib.start()
@ -203,6 +203,13 @@ class StateMachine:
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):
with self._lock:
nextState = self.currentState.next(signal)
@ -212,10 +219,3 @@ class StateMachine:
self.currentState.entry()
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