Status and settings members can be accessed via URI

This commit is contained in:
Joel Collins 2019-11-21 15:53:22 +00:00
parent de73ae4e0a
commit dc99a12985
4 changed files with 80 additions and 6 deletions

View file

@ -53,6 +53,4 @@ class MoveStageAPI(MicroscopeView):
with self.microscope.stage.lock:
self.microscope.stage.move_rel(position)
out = filter_dict(self.microscope.state, ["stage", "position"])
return jsonify(out)
return jsonify(self.microscope.status["stage"]["position"])

View file

@ -6,7 +6,9 @@ from openflexure_microscope.api.utilities import gen, JsonResponse
from openflexure_microscope.api.views import MicroscopeView
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
@ -144,7 +146,38 @@ class SettingsAPI(MicroscopeView):
self.microscope.apply_settings(payload.json)
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):
@ -155,4 +188,11 @@ def construct_blueprint(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

View file

@ -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.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):
@ -58,6 +59,19 @@ class StatusAPI(MicroscopeView):
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):
blueprint = blueprint_for_module(__name__)
@ -66,4 +80,9 @@ def construct_blueprint(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