make statemachine a contextmanager class
This commit is contained in:
parent
6b489fe3ab
commit
24fbb4b707
10
main.py
10
main.py
|
@ -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,11 +30,8 @@ if __name__ == '__main__':
|
|||
GPIO.setwarnings(False)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
|
||||
stateMachine = StateMachine()
|
||||
stateMachine.start()
|
||||
|
||||
with StateMachine() as stateMachine:
|
||||
signal.signal(signal.SIGTERM, sigtermHandler)
|
||||
|
||||
excChildListener()
|
||||
|
||||
except Exception:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue