Commit 72097003 authored by Joel Collins's avatar Joel Collins
Browse files

Skeleton for autostorage extension

parent 356ff333
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import traceback
from .autofocus import autofocus_extension_v2
from .scan import scan_extension_v2
from .zip_builder import zip_extension_v2
from .autostorage import autostorage_extension_v2

# "Gracefully" handle cases where picamera cannot be imported (eg test server)
try:
+37 −0
Original line number Diff line number Diff line
from labthings.server.extensions import BaseExtension
from openflexure_microscope.paths import settings_file_path
from openflexure_microscope.config import OpenflexureSettingsFile

import logging

AS_SETTINGS_PATH = settings_file_path("autostorage_settings.json")


class AutostorageExtension(BaseExtension):
    def __init__(self):
        BaseExtension.__init__(
            self,
            "org.openflexure.autostorage",
            version="2.0.0-beta.1",
            description="Handle switching capture storage devices",
        )

        self.settings = OpenflexureSettingsFile(
            AS_SETTINGS_PATH, defaults={"preferred": None}
        )

        # We'll store a reference to a camera object, who's capture paths will be modified
        self.camera = None

        # Register the on_microscope function to run when the microscope is attached
        self.on_component("org.openflexure.microscope", self.on_microscope)

    def on_microscope(self, microscope_obj):
        """Function to automatically call when the parent LabThing has a microscope attached."""
        logging.debug(f"Autostorage extension found microscope {microscope_obj}")
        if hasattr(microscope_obj, "camera"):
            self.camera = microscope_obj.camera
            logging.debug(f"Autostorage extension bound to camera {self.camera}")


autostorage_extension_v2 = AutostorageExtension()
+2 −1
Original line number Diff line number Diff line
@@ -32,7 +32,8 @@ class OpenflexureSettingsFile:
        self.path = path

        # Initialise basic config file with defaults if it doesn't exist
        initialise_file(self.path, populate=defaults)
        defaults_str = json.dumps(defaults, cls=JSONEncoder, indent=2, sort_keys=True)
        initialise_file(self.path, populate=defaults_str)

    def load(self) -> dict:
        """