Commit 65816123 authored by jtc42's avatar jtc42
Browse files

Implemented root API v2 routes

parent 5aa783c2
Loading
Loading
Loading
Loading
+27 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ from openflexure_microscope import Microscope
from openflexure_microscope.camera.capture import build_captures_from_exif
from openflexure_microscope.config import settings_file_path, JSONEncoder
from openflexure_microscope.api.v1 import blueprints
from openflexure_microscope.api import v2

# Import device modules
# NB this will eventually be handled by the RC file, so you can choose what device
@@ -141,6 +142,31 @@ 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"))

### V2
# Tasks routes
v2_stream_blueprint = v2.blueprints.stream.construct_blueprint(api_microscope)
app.register_blueprint(v2_stream_blueprint, url_prefix=uri("/", "v2"))

# Captures routes
v2_captures_blueprint = v2.blueprints.captures.construct_blueprint(api_microscope)
app.register_blueprint(v2_captures_blueprint, url_prefix=uri("/captures", "v2"))

# Settings routes
v2_settings_blueprint = v2.blueprints.settings.construct_blueprint(api_microscope)
app.register_blueprint(v2_settings_blueprint, url_prefix=uri("/settings", "v2"))

# Status routes
v2_status_blueprint = v2.blueprints.status.construct_blueprint(api_microscope)
app.register_blueprint(v2_status_blueprint, url_prefix=uri("/status", "v2"))

# Plugins routes
v2_plugin_blueprint = v2.blueprints.plugins.construct_blueprint(api_microscope)
app.register_blueprint(v2_plugin_blueprint, url_prefix=uri("/plugins", "v2"))

# Tasks routes
v2_tasks_blueprint = v2.blueprints.tasks.construct_blueprint(api_microscope)
app.register_blueprint(v2_tasks_blueprint, url_prefix=uri("/tasks", "v2"))


@app.route("/routes")
def routes():
@@ -184,7 +210,7 @@ def cleanup():

    # Save config
    logging.debug("Saving config for teardown...")
    api_microscope.save_config(backup=True)
    api_microscope.save_settings()

    logging.debug("Settling...")
    time.sleep(0.5)
+4 −4
Original line number Diff line number Diff line
@@ -152,7 +152,7 @@ class ConfigAPI(MicroscopeView):
        :>header Content-Type: application/json
        :status 200: state available
        """
        return jsonify(self.microscope.read_config(json_safe=True))
        return jsonify(self.microscope.read_settings(json_safe=True))

    def post(self):
        """
@@ -223,10 +223,10 @@ class ConfigAPI(MicroscopeView):
        logging.debug("Updating settings from POST request:")
        logging.debug(payload.json)

        self.microscope.apply_config(payload.json)
        self.microscope.save_config()
        self.microscope.apply_settings(payload.json)
        self.microscope.save_settings()

        return jsonify(self.microscope.read_config(json_safe=True))
        return jsonify(self.microscope.read_settings(json_safe=True))


def construct_blueprint(microscope_obj):
+7 −4
Original line number Diff line number Diff line
@@ -130,16 +130,19 @@ class ListAPI(MicroscopeView):
                output.file, use_video_port=use_video_port, resize=resize, bayer=bayer
            )

            metadata.update(
                {
                    "microscope_settings": self.microscope.read_config(),
            # Inject system metadata
            system_metadata = {
                    "microscope_settings": self.microscope.read_settings(),
                    "microscope_state": self.microscope.state,
                    "microscope_id": self.microscope.id,
                    "microscope_name": self.microscope.name,
                }
            )
            output.system_metadata.update(system_metadata)

            # Insert custom metadata
            output.put_metadata(metadata)

            # Insert custom tags
            output.put_tags(tags)

        return jsonify(output.state)
+2 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ class ZoomAPI(MicroscopeView):
        :<header Content-Type: application/json
        :status 200: preview started/stopped
        """
        zoom_value = self.microscope.camera.state["zoom_value"]
        zoom_value = self.microscope.camera.status["zoom_value"]

        return jsonify({"zoom_value": zoom_value})

@@ -47,7 +47,7 @@ class ZoomAPI(MicroscopeView):

        self.microscope.camera.set_zoom(zoom_value)

        return jsonify(self.microscope.camera.state)
        return jsonify(self.microscope.camera.status)


class OverlayAPI(MicroscopeView):
+9 −5
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ from openflexure_microscope.api.views import MicroscopeViewPlugin
from flask import Blueprint, jsonify
from openflexure_microscope.api.views import MicroscopeView

import copy
import logging
import warnings

@@ -21,7 +22,7 @@ class PluginFormAPI(MicroscopeView):
        A complete list of enabled plugins can be found in the microscope state.

        """
        out = self.microscope.plugin.forms
        out = self.microscope.plugins.forms
        return jsonify(out)


@@ -38,7 +39,10 @@ def construct_blueprint(microscope_obj):
    all_routes = []

    # For each plugin attached to the microscope object
    for plugin_name, plugin_obj in microscope_obj.plugin.plugins:
    for plugin_representation in microscope_obj.plugins.active:

        plugin_obj = plugin_representation["plugin"]
        plugin_name = plugin_representation["name"]

        # If plugin contains valid endpoints
        if hasattr(plugin_obj, "api_views") and isinstance(plugin_obj.api_views, dict):
@@ -94,7 +98,7 @@ def construct_blueprint(microscope_obj):
            if hasattr(plugin_obj, "api_form") and isinstance(
                plugin_obj.api_form, dict
            ):
                api_form_info = plugin_obj.api_form
                api_form_info = copy.deepcopy(plugin_obj.api_form)
                api_form_info["id"] = plugin_name
                if "forms" in api_form_info and isinstance(
                    api_form_info["forms"], list
@@ -110,8 +114,8 @@ def construct_blueprint(microscope_obj):
                            )

                # Store the complete form in Microscope().plugin.form
                microscope_obj.plugin.forms.append(api_form_info)
                print(microscope_obj.plugin.forms)
                microscope_obj.plugins.forms.append(api_form_info)
                print(microscope_obj.plugins.forms)

        else:
            warnings.warn(
Loading