From 81546a1c8db82cdcb80c13917bc0fc5ac0c50e25 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 26 May 2020 13:18:09 +0100 Subject: [PATCH 1/2] Add timeout support --- .../stage/sangaboard/extensible_serial_instrument.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/openflexure_microscope/stage/sangaboard/extensible_serial_instrument.py b/openflexure_microscope/stage/sangaboard/extensible_serial_instrument.py index 6e993d3e..ce98acbe 100644 --- a/openflexure_microscope/stage/sangaboard/extensible_serial_instrument.py +++ b/openflexure_microscope/stage/sangaboard/extensible_serial_instrument.py @@ -78,6 +78,7 @@ class ExtensibleSerialInstrument(object): then we don't warn when ports are opened multiple times. """ with self.communications_lock: + logging.debug(f"Opening port {port}") if hasattr(self, "_ser") and self._ser.isOpen(): if not quiet: logging.warning("Attempted to open an already-open port!") @@ -133,9 +134,12 @@ class ExtensibleSerialInstrument(object): assert ( self._ser.isOpen() ), "Attempted to write to the serial port before it was opened. Perhaps you need to call the 'open' method first?" + logging.debug("Encoding message...") data = query_string + self.termination_character data = data.encode() + logging.debug(f"Writing message: {data}") self._ser.write(data) + logging.debug("Write successful") def flush_input_buffer(self): """Make sure there's nothing waiting to be read, and clear the buffer if there is.""" @@ -145,6 +149,7 @@ class ExtensibleSerialInstrument(object): def readline(self, timeout=None): """Read one line from the serial port.""" + self._ser.timeout = timeout with self.communications_lock: return ( self._ser.readline() @@ -192,7 +197,9 @@ class ExtensibleSerialInstrument(object): """ with self.communications_lock: self.flush_input_buffer() + logging.debug(f"Writing query: {queryString}") self.write(queryString) + logging.debug("Waiting for query response...") if self.ignore_echo == True: # Needs Implementing for a multiline read! first_line = self.readline(timeout).strip() if first_line == queryString: From a0702fbc6977e586bfbc182cb49ba2850fc2fe28 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 26 May 2020 13:22:14 +0100 Subject: [PATCH 2/2] Set logging level further up the script --- openflexure_microscope/api/app.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 0f08b7f7..329c68aa 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -6,6 +6,11 @@ monkey.patch_all() import time import atexit import logging, logging.handlers + +# Set root logger level +logger = logging.getLogger() +logger.setLevel(logging.DEBUG) + import os import pkg_resources @@ -42,16 +47,12 @@ formatter = logging.Formatter( ) -# Get root logger -logger = logging.getLogger() -logger.setLevel(logging.INFO) - # Create file handler fh = logging.handlers.RotatingFileHandler( ROOT_LOGFILE, maxBytes=1_000_000, backupCount=5 ) fh.setFormatter(formatter) -fh.setLevel(logging.INFO) +fh.setLevel(logging.DEBUG) fh.propagate = False # Create access log file handler @@ -59,7 +60,7 @@ afh = logging.handlers.RotatingFileHandler( ACCESS_LOGFILE, maxBytes=1_000_000, backupCount=5 ) afh.setFormatter(formatter) -afh.setLevel(logging.INFO) +afh.setLevel(logging.DEBUG) afh.propagate = False # Add file handler to root logger @@ -157,10 +158,12 @@ def err_log(): attachment_filename="openflexure_microscope_{}.log".format(timestamp), ) -@app.route('/api/v1/', defaults={'path': ''}) -@app.route('/api/v1/') + +@app.route("/api/v1/", defaults={"path": ""}) +@app.route("/api/v1/") def api_v1_catch_all(path): - abort(410, 'API v1 is no longer in use. Please upgrade your client.') + abort(410, "API v1 is no longer in use. Please upgrade your client.") + # Automatically clean up microscope at exit def cleanup():