From d79fd5530103781a280ef4f90ce4cef8fc2bc01d Mon Sep 17 00:00:00 2001 From: jtc42 Date: Fri, 15 Nov 2019 16:40:25 +0000 Subject: [PATCH] Drafted actions routes --- openflexure_microscope/api/app.py | 6 +- .../api/v2/blueprints/actions/__init__.py | 87 +++++++++ .../api/v2/blueprints/actions/camera.py | 166 ++++++++++++++++++ .../api/v2/blueprints/actions/stage.py | 54 ++++++ .../api/v2/blueprints/actions/system.py | 33 ++++ .../api/v2/blueprints/captures.py | 29 +-- .../api/v2/blueprints/tasks.py | 2 +- openflexure_microscope/camera/pi.py | 8 +- 8 files changed, 360 insertions(+), 25 deletions(-) diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 8d9c8ec1..9e91b40a 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -143,7 +143,7 @@ task_blueprint = blueprints.task.construct_blueprint(api_microscope) app.register_blueprint(task_blueprint, url_prefix=uri("/task", "v1")) ### V2 -# Tasks routes +# Root routes v2_stream_blueprint = v2.blueprints.stream.construct_blueprint(api_microscope) app.register_blueprint(v2_stream_blueprint, url_prefix=uri("/", "v2")) @@ -167,6 +167,10 @@ app.register_blueprint(v2_plugin_blueprint, url_prefix=uri("/plugins", "v2")) v2_tasks_blueprint = v2.blueprints.tasks.construct_blueprint(api_microscope) app.register_blueprint(v2_tasks_blueprint, url_prefix=uri("/tasks", "v2")) +# Actions routes +v2_actions_blueprint = v2.blueprints.actions.construct_blueprint(api_microscope) +app.register_blueprint(v2_actions_blueprint, url_prefix=uri("/actions", "v2")) + @app.route("/routes") def routes(): diff --git a/openflexure_microscope/api/v2/blueprints/actions/__init__.py b/openflexure_microscope/api/v2/blueprints/actions/__init__.py index e69de29b..6900e796 100644 --- a/openflexure_microscope/api/v2/blueprints/actions/__init__.py +++ b/openflexure_microscope/api/v2/blueprints/actions/__init__.py @@ -0,0 +1,87 @@ +from flask import Blueprint, url_for, jsonify +from sys import platform + +from . import camera, stage, system + +_actions = { + "capture": { + "rule": "/camera/capture/", + "view_class": camera.CaptureAPI, + "description": "Take a single still capture", + "conditions": True + }, + "preview_start": { + "rule": "/camera/preview/start", + "view_class": camera.GPUPreviewStartAPI, + "description": "Start the on-board GPU preview", + "conditions": True + }, + "preview_stop": { + "rule": "/camera/preview/stop", + "view_class": camera.GPUPreviewStopAPI, + "description": "Stop the on-board GPU preview", + "conditions": True + }, + "move": { + "rule": "/stage/move/", + "view_class": stage.MoveStageAPI, + "description": "Move the microscope stage", + "conditions": True + }, + "shutdown": { + "rule": "/system/shutdown/", + "view_class": system.ShutdownAPI, + "description": "Shutdown the device", + "conditions": (platform == "linux") + }, + "reboot": { + "rule": "/system/reboot/", + "view_class": system.RebootAPI, + "description": "Reboot the device", + "conditions": (platform == "linux") + } +} + + +def enabled_actions(): + global _actions + + return {k: v for k, v in _actions.items() if v["conditions"]} + + +def actions_representation(): + global _actions + + actions = {} + + for name, action in enabled_actions().items(): + d = { + "links": {"self": url_for(f".{name}")}, + "description": action["description"], + "rule": action["rule"], + "view_class": str(action["view_class"]) + } + + actions[name] = d + + return actions + + +def construct_blueprint(microscope_obj): + global _actions + + blueprint = Blueprint("actions_blueprint", __name__) + + # For each enabled action route defined in our dictionary above + for name, action in enabled_actions().items(): + # Add the action to our blueprint + blueprint.add_url_rule( + action["rule"], + view_func=action["view_class"].as_view(name, microscope=microscope_obj), + ) + + @blueprint.route("/") + def representation(): + return jsonify(actions_representation()) + + return blueprint diff --git a/openflexure_microscope/api/v2/blueprints/actions/camera.py b/openflexure_microscope/api/v2/blueprints/actions/camera.py index e69de29b..1a91a832 100644 --- a/openflexure_microscope/api/v2/blueprints/actions/camera.py +++ b/openflexure_microscope/api/v2/blueprints/actions/camera.py @@ -0,0 +1,166 @@ +from openflexure_microscope.api.utilities import get_bool, JsonResponse +from openflexure_microscope.api.views import MicroscopeView +from openflexure_microscope.utilities import filter_dict + +import logging +from flask import jsonify, request, abort, url_for, redirect, send_file + + +class CaptureAPI(MicroscopeView): + def post(self): + """ + Create a new image capture. + + .. :quickref: Actions; New capture + + **Example request**: + + .. sourcecode:: http + + POST /actions/camera/capture HTTP/1.1 + Accept: application/json + + { + "filename": "myfirstcapture", + "temporary": false, + "use_video_port": true, + "bayer": true, + "size": { + "width": 640, + "height": 480 + } + } + + :>header Accept: application/json + + :json boolean available: availability of capture data + :>json string filename: filename of capture + :>json string id: unique id of the capture object + :>json boolean temporary: delete the capture file on microscope after closing + :>json boolean locked: file locked for modifications (mostly used for video recording) + :>json string path: path on pi storage to the capture file, if available + :>json boolean stream: capture stored in-memory as a BytesIO stream + :>json json uri: - **download** *(string)*: api uri to the capture file download + - **state** *(string)*: api uri to the capture json representation + + :
header Accept: application/json + + :
header Accept: application/json + + :