Status and settings members can be accessed via URI
This commit is contained in:
parent
de73ae4e0a
commit
dc99a12985
4 changed files with 80 additions and 6 deletions
|
|
@ -53,6 +53,4 @@ class MoveStageAPI(MicroscopeView):
|
||||||
with self.microscope.stage.lock:
|
with self.microscope.stage.lock:
|
||||||
self.microscope.stage.move_rel(position)
|
self.microscope.stage.move_rel(position)
|
||||||
|
|
||||||
out = filter_dict(self.microscope.state, ["stage", "position"])
|
return jsonify(self.microscope.status["stage"]["position"])
|
||||||
|
|
||||||
return jsonify(out)
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,9 @@ from openflexure_microscope.api.utilities import gen, JsonResponse
|
||||||
from openflexure_microscope.api.views import MicroscopeView
|
from openflexure_microscope.api.views import MicroscopeView
|
||||||
from openflexure_microscope.api.utilities import blueprint_for_module
|
from openflexure_microscope.api.utilities import blueprint_for_module
|
||||||
|
|
||||||
from flask import Blueprint, jsonify, request
|
from openflexure_microscope.utilities import get_by_path, set_by_path, create_from_path
|
||||||
|
|
||||||
|
from flask import Blueprint, jsonify, request, abort
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -144,7 +146,38 @@ class SettingsAPI(MicroscopeView):
|
||||||
self.microscope.apply_settings(payload.json)
|
self.microscope.apply_settings(payload.json)
|
||||||
self.microscope.save_settings()
|
self.microscope.save_settings()
|
||||||
|
|
||||||
return jsonify(self.microscope.read_settings(json_safe=True))
|
return jsonify(self.microscope.read_settings())
|
||||||
|
|
||||||
|
|
||||||
|
class NestedSettingsAPI(MicroscopeView):
|
||||||
|
def get(self, route):
|
||||||
|
|
||||||
|
keys = route.split("/")
|
||||||
|
|
||||||
|
try:
|
||||||
|
value = get_by_path(self.microscope.read_settings(), keys)
|
||||||
|
except KeyError:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
return jsonify(value)
|
||||||
|
|
||||||
|
def put(self, route):
|
||||||
|
keys = route.split("/")
|
||||||
|
|
||||||
|
payload = JsonResponse(request)
|
||||||
|
|
||||||
|
logging.debug("Updating settings from PUT request:")
|
||||||
|
logging.debug(payload.json)
|
||||||
|
|
||||||
|
dictionary = create_from_path(keys)
|
||||||
|
set_by_path(dictionary, keys, payload.json)
|
||||||
|
|
||||||
|
logging.debug(f"Applying settings: {dictionary}")
|
||||||
|
|
||||||
|
self.microscope.apply_settings(dictionary)
|
||||||
|
self.microscope.save_settings()
|
||||||
|
|
||||||
|
return self.get(route)
|
||||||
|
|
||||||
|
|
||||||
def construct_blueprint(microscope_obj):
|
def construct_blueprint(microscope_obj):
|
||||||
|
|
@ -155,4 +188,11 @@ def construct_blueprint(microscope_obj):
|
||||||
"/", view_func=SettingsAPI.as_view("settings", microscope=microscope_obj)
|
"/", view_func=SettingsAPI.as_view("settings", microscope=microscope_obj)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
blueprint.add_url_rule(
|
||||||
|
"/<path:route>",
|
||||||
|
view_func=NestedSettingsAPI.as_view(
|
||||||
|
"nested_settings", microscope=microscope_obj
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
return blueprint
|
return blueprint
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,9 @@ Read-only status of the microscope, and attached hardware info
|
||||||
|
|
||||||
from openflexure_microscope.api.views import MicroscopeView
|
from openflexure_microscope.api.views import MicroscopeView
|
||||||
from openflexure_microscope.api.utilities import blueprint_for_module
|
from openflexure_microscope.api.utilities import blueprint_for_module
|
||||||
|
from openflexure_microscope.utilities import get_by_path
|
||||||
|
|
||||||
from flask import Blueprint, jsonify
|
from flask import Blueprint, jsonify, abort
|
||||||
|
|
||||||
|
|
||||||
class StatusAPI(MicroscopeView):
|
class StatusAPI(MicroscopeView):
|
||||||
|
|
@ -58,6 +59,19 @@ class StatusAPI(MicroscopeView):
|
||||||
return jsonify(self.microscope.status)
|
return jsonify(self.microscope.status)
|
||||||
|
|
||||||
|
|
||||||
|
class NestedStatusAPI(MicroscopeView):
|
||||||
|
def get(self, route):
|
||||||
|
|
||||||
|
keys = route.split("/")
|
||||||
|
|
||||||
|
try:
|
||||||
|
value = get_by_path(self.microscope.status, keys)
|
||||||
|
except KeyError:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
return jsonify(value)
|
||||||
|
|
||||||
|
|
||||||
def construct_blueprint(microscope_obj):
|
def construct_blueprint(microscope_obj):
|
||||||
|
|
||||||
blueprint = blueprint_for_module(__name__)
|
blueprint = blueprint_for_module(__name__)
|
||||||
|
|
@ -66,4 +80,9 @@ def construct_blueprint(microscope_obj):
|
||||||
"/", view_func=StatusAPI.as_view("status", microscope=microscope_obj)
|
"/", view_func=StatusAPI.as_view("status", microscope=microscope_obj)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
blueprint.add_url_rule(
|
||||||
|
"/<path:route>",
|
||||||
|
view_func=NestedStatusAPI.as_view("nested_status", microscope=microscope_obj),
|
||||||
|
)
|
||||||
|
|
||||||
return blueprint
|
return blueprint
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,23 @@ from functools import reduce
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
|
||||||
|
def get_by_path(root, items):
|
||||||
|
"""Access a nested object in root by item sequence."""
|
||||||
|
return reduce(operator.getitem, items, root)
|
||||||
|
|
||||||
|
|
||||||
|
def set_by_path(root, items, value):
|
||||||
|
"""Set a value in a nested object in root by item sequence."""
|
||||||
|
get_by_path(root, items[:-1])[items[-1]] = value
|
||||||
|
|
||||||
|
|
||||||
|
def create_from_path(items):
|
||||||
|
tree_dict = {}
|
||||||
|
for key in reversed(items):
|
||||||
|
tree_dict = {key: tree_dict}
|
||||||
|
return tree_dict
|
||||||
|
|
||||||
|
|
||||||
def bottom_level_name(obj):
|
def bottom_level_name(obj):
|
||||||
return obj.__name__.split(".")[-1]
|
return obj.__name__.split(".")[-1]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue