86 lines
2.2 KiB
Python
86 lines
2.2 KiB
Python
from flask import Blueprint, url_for, jsonify
|
|
from sys import platform
|
|
|
|
from openflexure_microscope.utilities import get_docstring
|
|
from openflexure_microscope.api.views import MicroscopeView
|
|
|
|
from . import camera, stage, system
|
|
|
|
|
|
_actions = {
|
|
"capture": {
|
|
"rule": "/camera/capture/",
|
|
"view_class": camera.CaptureAPI,
|
|
"conditions": True,
|
|
},
|
|
"preview_start": {
|
|
"rule": "/camera/preview/start",
|
|
"view_class": camera.GPUPreviewStartAPI,
|
|
"conditions": True,
|
|
},
|
|
"preview_stop": {
|
|
"rule": "/camera/preview/stop",
|
|
"view_class": camera.GPUPreviewStopAPI,
|
|
"conditions": True,
|
|
},
|
|
"move": {
|
|
"rule": "/stage/move/",
|
|
"view_class": stage.MoveStageAPI,
|
|
"conditions": True,
|
|
},
|
|
"shutdown": {
|
|
"rule": "/system/shutdown/",
|
|
"view_class": system.ShutdownAPI,
|
|
"conditions": (platform == "linux"),
|
|
},
|
|
"reboot": {
|
|
"rule": "/system/reboot/",
|
|
"view_class": system.RebootAPI,
|
|
"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": get_docstring(action["view_class"]),
|
|
"rule": action["rule"],
|
|
"view_class": str(action["view_class"]),
|
|
}
|
|
|
|
actions[name] = d
|
|
|
|
return actions
|
|
|
|
class ActionsAPI(MicroscopeView):
|
|
def get(self):
|
|
return jsonify(actions_representation())
|
|
|
|
def construct_blueprint(microscope_obj):
|
|
global _actions
|
|
|
|
blueprint = Blueprint("v2_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.add_url_rule(
|
|
"/", view_func=ActionsAPI.as_view("actions", microscope=microscope_obj)
|
|
)
|
|
|
|
return blueprint
|