Commit 5672b82f authored by Joel Collins's avatar Joel Collins
Browse files

Added many many debug statements to help debug issue #32

parent 0948c930
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -99,17 +99,19 @@ app.register_blueprint(plugin_blueprint, url_prefix=uri('/plugin', 'v1'))
task_blueprint = blueprints.task.construct_blueprint(api_microscope)
app.register_blueprint(task_blueprint, url_prefix=uri('/task', 'v1'))


# Automatically clean up microscope at exit
def cleanup():
    global api_microscope
    logging.debug("App teardown started...")
    # Save config
    api_microscope.rc.save(backup=True)
    # Close down the microscope
    api_microscope.close()
    logging.debug("App teardown complete.")


atexit.register(cleanup)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False)
+19 −7
Original line number Diff line number Diff line
@@ -135,12 +135,14 @@ class BaseCamera(object):

    def close(self):
        """Close the BaseCamera and all attached StreamObjects."""
        logging.info("Closing {}".format(self))
        # Close all StreamObjects
        for capture_list in [self.images, self.videos]:
            for stream_object in capture_list:
                stream_object.close()
        # Stop worker thread
        self.stop_worker()
        logging.info("Closed {}".format(self))

    def wait_for_camera(self, timeout=5):
        """Wait for camera object, with 5 second timeout."""
@@ -160,9 +162,11 @@ class BaseCamera(object):
        self.last_access = time.time()
        self.stop = False
        
        if self.thread is None:
        #if self.thread is None:
        if not self.state['stream_active']:
            # start background frame thread
            self.thread = threading.Thread(target=self._thread)
            self.thread.daemon = True
            self.thread.start()

            # wait until frames are available
@@ -171,7 +175,7 @@ class BaseCamera(object):
                if time.time() > timeout_time:
                    raise TimeoutError("Timeout waiting for frames.")
                else:
                    time.sleep(0)
                    time.sleep(0.1)
        return True

    def stop_worker(self, timeout: int = 5) -> bool:
@@ -179,12 +183,19 @@ class BaseCamera(object):
        logging.debug("Stopping worker thread")
        timeout_time = time.time() + timeout

        #if self.thread:
        if self.state['stream_active']:
            self.stop = True
        while self.thread:
            self.thread.join()  # Wait for stream thread to exit
            logging.debug("Waiting for stream thread to exit.")

        #while self.thread:
        while self.state['stream_active']:
            if time.time() > timeout_time:
                logging.debug("Timeout waiting for worker thread close.")
                raise TimeoutError("Timeout waiting for worker thread close.")
            else:
                time.sleep(0)
                time.sleep(0.1)
        return True

    # HANDLE STREAM FRAMES
@@ -387,4 +398,5 @@ class BaseCamera(object):
        # Set stream_activate state
        self.state['stream_active'] = False
        # Reset thread
        self.thread = None
        #self.thread = None
        logging.debug("Stream thread is None")
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ def clear_tmp():
    for f in files:
        os.remove(f)
        logging.debug("Removed {}".format(f))
    logging.debug("Cleared {}.".format(TEMP_CAPTURE_PATH))


def capture_from_dict(capture_dict):
+2 −0
Original line number Diff line number Diff line
@@ -73,10 +73,12 @@ class Microscope(object):

    def close(self):
        """Shut down the microscope hardware."""
        logging.info("Closing {}".format(self))
        if self.camera:
            self.camera.close()
        if self.stage:
            self.stage.close()
        logging.info("Closed {}".format(self))

    def attach(self, camera: StreamingCamera, stage: OpenFlexureStage):
        """