From e9d74c07e6794e496aa882a9d0a4f74589b8d597 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Thu, 31 Jan 2019 13:51:02 +0000 Subject: [PATCH] Separated updating/writing from saving config --- openflexure_microscope/api/app.py | 2 +- .../api/v1/blueprints/base.py | 3 +- openflexure_microscope/config.py | 11 +++---- openflexure_microscope/microscope.py | 31 ++++++++++++------- .../plugins/camera_calibration/plugin.py | 1 + 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 911d58cc..4f293f31 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -65,7 +65,7 @@ def attach_microscope(): logging.debug("First request made. Populating microscope with hardware...") logging.debug("Creating camera object...") - api_camera = StreamingCamera(config=api_microscope.rc.asdict()) + api_camera = StreamingCamera(config=api_microscope.rc.read()) logging.debug("Creating stage object...") api_stage = Stage("/dev/ttyUSB0") diff --git a/openflexure_microscope/api/v1/blueprints/base.py b/openflexure_microscope/api/v1/blueprints/base.py index bee8c37b..b3a841b9 100644 --- a/openflexure_microscope/api/v1/blueprints/base.py +++ b/openflexure_microscope/api/v1/blueprints/base.py @@ -172,7 +172,8 @@ class ConfigAPI(MicroscopeView): print(payload.json) - self.microscope.apply_config(payload.json) + self.microscope.write_config(payload.json) + self.microscope.save_config() return jsonify(self.microscope.config) diff --git a/openflexure_microscope/config.py b/openflexure_microscope/config.py index 7ac2e4ea..507d5dd3 100644 --- a/openflexure_microscope/config.py +++ b/openflexure_microscope/config.py @@ -109,17 +109,14 @@ class OpenflexureConfig: @property def config(self): + return self.read() + + def read(self): return self._config - def asdict(self): - return self._config - - def update(self, update_dict: dict): + def write(self, update_dict: dict): self._config.update(update_dict) - def overwrite(self, new_dict: dict): - self._config = new_dict - def load(self): # Unexpanded config dictionary (used at load/save time) self._config = load_yaml_file(self.config_path) diff --git a/openflexure_microscope/microscope.py b/openflexure_microscope/microscope.py index 8e77808d..dd6815e8 100644 --- a/openflexure_microscope/microscope.py +++ b/openflexure_microscope/microscope.py @@ -56,10 +56,10 @@ class Microscope(object): # Set default parameters if 'name' not in self.rc.config: - self.rc.update({'name': uuid.uuid4().hex}) + self.rc.write({'name': uuid.uuid4().hex}) if 'fov' not in self.rc.config: - self.rc.update({'fov': [0, 0]}) + self.rc.write({'fov': [0, 0]}) # Initialise with an empty composite lock self.lock = CompositeLock([]) #: :py:class:`openflexure_microscope.lock.CompositeLock`: Composite lock controlling thread access to multiple pieces of hardware @@ -96,7 +96,7 @@ class Microscope(object): if isinstance(self.camera, StreamingCamera): logging.info("Attached camera {}".format(camera)) logging.info("Updating camera settings") - self.apply_config(self.camera.read_config()) + self.write_config(self.camera.read_config()) elif not self.camera: logging.info("Attached dummy camera.") # If camera has a lock @@ -180,9 +180,9 @@ class Microscope(object): return state - def apply_config(self, config: dict): + def write_config(self, config: dict): """ - Updates and applies a config dictionary. Missing parameters will be left untouched. + Writes and applies a config dictionary. Missing parameters will be left untouched. """ # If attached to a camera if self.camera: @@ -198,9 +198,7 @@ class Microscope(object): self.stage.backlash = backlash # Cache config - self.rc.update(config) - # Save to disk - self.rc.save() + self.rc.write(config) def read_config(self): """ @@ -209,7 +207,7 @@ class Microscope(object): # If attached to a camera if self.camera: # Update camera config params from StreamingCamera object - self.rc.update(self.camera.config) + self.rc.write(self.camera.config) # If attached to a stage if self.stage: @@ -225,9 +223,18 @@ class Microscope(object): } } - self.rc.update(backlash) + self.rc.write(backlash) - return self.rc.asdict() + return self.rc.read() + + def save_config(self): + """ + Saves the current runtime-config back to disk + """ + # Get any changed device settings + self.read_config() + # Save to disk + self.rc.save() @property def config(self) -> dict: @@ -235,4 +242,4 @@ class Microscope(object): @config.setter def config(self, config: dict) -> None: - self.apply_config(config) + self.write_config(config) diff --git a/openflexure_microscope/plugins/camera_calibration/plugin.py b/openflexure_microscope/plugins/camera_calibration/plugin.py index a60c9d99..d5740502 100644 --- a/openflexure_microscope/plugins/camera_calibration/plugin.py +++ b/openflexure_microscope/plugins/camera_calibration/plugin.py @@ -52,6 +52,7 @@ class Plugin(MicroscopePlugin): recalibrate_camera(scamera.camera) finally: scamera.camera.resolution=old_resolution + self. if streaming: logging.info("Restarting stream after recalibration") scamera.start_stream_recording()