Example #1
0
    def _run(self):
        def on_command(message):
            if message['$'] == 'before':
                self.log_append()
            if message['name'] == command_types.PAUSE:
                self.set_state('paused')
            if message['name'] == command_types.RESUME:
                self.set_state('running')

        self._reset()

        _unsubscribe = self._broker.subscribe(command_types.COMMAND,
                                              on_command)

        self.startTime = now()
        self.set_state('running')

        try:
            self.resume()
            self._pre_run_hooks()
            if ff.use_protocol_api_v2():
                bundled_data = None
                bundled_labware = None
                if isinstance(self._protocol, PythonProtocol):
                    bundled_data = self._protocol.bundled_data
                    bundled_labware = self._protocol.bundled_labware
                self._hardware.cache_instruments()
                ctx = ProtocolContext(loop=self._loop,
                                      broker=self._broker,
                                      bundled_labware=bundled_labware,
                                      bundled_data=bundled_data)
                ctx.connect(self._hardware)
                ctx.home()
                run_protocol(self._protocol, context=ctx)
            else:
                self._hardware.broker = self._broker
                if isinstance(self._protocol, JsonProtocol):
                    execute_protocol(self._protocol)
                else:
                    exec(self._protocol.contents, {})
            self.set_state('finished')
            self._hardware.home()
        except Exception as e:
            log.exception("Exception during run:")
            self.error_append(e)
            self.set_state('error')
            raise e
        finally:
            _unsubscribe()