diff --git a/photobooth/StateMachine.py b/photobooth/StateMachine.py index 8a25023..bed4eaa 100644 --- a/photobooth/StateMachine.py +++ b/photobooth/StateMachine.py @@ -333,7 +333,9 @@ class CountdownState(State): def handleEvent(self, event, context): - if isinstance(event, GuiEvent) and event.name == 'capture': + if isinstance(event, GuiEvent) and event.name == 'countdown': + pass + elif isinstance(event, GuiEvent) and event.name == 'capture': context.state == CaptureState() else: raise TypeError('Unknown Event type "{}"'.format(event)) diff --git a/photobooth/camera/__init__.py b/photobooth/camera/__init__.py index 270b87c..47bd308 100644 --- a/photobooth/camera/__init__.py +++ b/photobooth/camera/__init__.py @@ -44,21 +44,34 @@ class Camera: super().__init__() self._comm = comm - self._cap = CameraModule() - self._pic_dims = PictureDimensions(config, self._cap.getPicture().size) + self._cfg = config + self._cam = CameraModule - self._is_preview = (config.getBool('Photobooth', 'show_preview') and - self._cap.hasPreview) - self._is_keep_pictures = config.getBool('Photobooth', 'keep_pictures') + self._cap = None + self._pic_dims = None + + self._is_preview = self._cfg.getBool('Photobooth', 'show_preview') + self._is_keep_pictures = self._cfg.getBool('Photobooth', + 'keep_pictures') + + def startup(self): + + self._cap = self._cam() + self._pic_dims = PictureDimensions(self._cfg, + self._cap.getPicture().size) + self._is_preview = self._is_preview and self._cap.hasPreview logging.info('Using camera {} preview functionality'.format( 'with' if self._is_preview else 'without')) self.setIdle() + self._comm.send(Workers.MASTER, StateMachine.CameraEvent('ready')) + def teardown(self, state): - self._cap.cleanup() + if not self._cap is None: + self._cap.cleanup() if state.target == StateMachine.TeardownEvent.EXIT: sys.exit(0) elif state.target == StateMachine.TeardownEvent.RESTART: @@ -71,7 +84,9 @@ class Camera: def handleState(self, state): - if isinstance(state, StateMachine.GreeterState): + if isinstance(state, StateMachine.StartupState): + self.startup() + elif isinstance(state, StateMachine.GreeterState): self.prepareCapture() elif isinstance(state, StateMachine.CountdownState): self.capturePreview() diff --git a/photobooth/main.py b/photobooth/main.py index 101fc85..0e21116 100644 --- a/photobooth/main.py +++ b/photobooth/main.py @@ -30,70 +30,33 @@ import sys from . import camera, gui from .Config import Config -# from .Photobooth import Photobooth from .util import lookup_and_import -from .StateMachine import Context, ErrorEvent +from .StateMachine import Context, ErrorEvent, StartupState from .Threading import Communicator, Workers from .Worker import Worker class CameraProcess(mp.Process): - def __init__(self, config, comm): # conn, worker_queue): + def __init__(self, config, comm): super().__init__() self.daemon = True - self.cfg = config - self.comm = comm - # self.conn = conn - # self.worker_queue = worker_queue - - # def run_camera(self): - - # try: - # # cap = lookup_and_import( - # # camera.modules, self.cfg.get('Camera', 'module'), 'camera') - - # # photobooth = Photobooth( - # # self.cfg, cap, self.conn, self.worker_queue) - # # return photobooth.run() - - # except BaseException as e: - # self.conn.send(gui.GuiState.ErrorState('Camera error', str(e))) - # event = self.conn.recv() - # if str(event) in ('cancel', 'ack'): - # return 123 - # else: - # logging.error('Unknown event received: %s', str(event)) - # raise RuntimeError('Unknown event received', str(event)) + self._cfg = config + self._comm = comm def run(self): - CameraModule = lookup_and_import(camera.modules, - self.cfg.get('Camera', 'module'), - 'camera') - cap = camera.Camera(self.cfg, self.comm, CameraModule) + CameraModule = lookup_and_import( + camera.modules, self._cfg.get('Camera', 'module'), 'camera') + cap = camera.Camera(self._cfg, self._comm, CameraModule) while True: try: cap.run() except Exception as e: - self.comm.send(Workers.MASTER, ErrorEvent(e)) - - # status_code = 123 - - # while status_code == 123: - # event = self.conn.recv() - - # if str(event) != 'start': - # logging.warning('Unknown event received: %s', str(event)) - # continue - - # status_code = self.run_camera() - # logging.info('Camera exited with status code %d', status_code) - - # sys.exit(status_code) + self._comm.send(Workers.MASTER, ErrorEvent(e)) class WorkerProcess(mp.Process): @@ -108,7 +71,11 @@ class WorkerProcess(mp.Process): def run(self): - sys.exit(Worker(self.cfg, self.comm).run()) + while True: + try: + Worker(self.cfg, self.comm).run() + except Exception as e: + self._comm.send(Workers.MASTER, ErrorEvent(e)) class GuiProcess(mp.Process):