Added basic thing description to root

This commit is contained in:
Joel Collins 2019-12-18 14:30:33 +00:00
parent d52453849c
commit f2af359b8b
24 changed files with 677 additions and 183 deletions

View file

@ -0,0 +1,94 @@
from openflexure_microscope.api.utilities import JsonResponse
from openflexure_microscope.utilities import get_by_path, set_by_path, create_from_path
from openflexure_microscope.common.labthings.find import find_device
from openflexure_microscope.common.labthings.resource import Resource
from flask import jsonify, request, abort
import logging
class SettingsProperty(Resource):
def get(self):
microscope = find_device("openflexure_microscope")
return jsonify(microscope.read_settings())
def put(self):
microscope = find_device("openflexure_microscope")
payload = JsonResponse(request)
logging.debug("Updating settings from PUT request:")
logging.debug(payload.json)
microscope.apply_settings(payload.json)
microscope.save_settings()
return self.get()
class NestedSettingsProperty(Resource):
def get(self, route):
microscope = find_device("openflexure_microscope")
keys = route.split("/")
try:
value = get_by_path(microscope.read_settings(), keys)
except KeyError:
return abort(404)
return jsonify(value)
def put(self, route):
microscope = find_device("openflexure_microscope")
keys = route.split("/")
payload = JsonResponse(request)
dictionary = create_from_path(keys)
set_by_path(dictionary, keys, payload.json)
microscope.apply_settings(dictionary)
microscope.save_settings()
return self.get(route)
class StatusProperty(Resource):
def get(self):
microscope = find_device("openflexure_microscope")
return jsonify(microscope.status)
class NestedStatusProperty(Resource):
def get(self, route):
microscope = find_device("openflexure_microscope")
keys = route.split("/")
try:
value = get_by_path(microscope.status, keys)
except KeyError:
return abort(404)
return jsonify(value)
def add_states_to_labthing(labthing, prefix=""):
"""
Add all settings and status resources to a labthing
"""
labthing.add_resource(
SettingsProperty, f"{prefix}/settings", endpoint="SettingsProperty"
)
labthing.register_property(SettingsProperty)
labthing.add_resource(
NestedSettingsProperty,
f"{prefix}/settings/<path:route>",
endpoint="NestedSettingsProperty",
)
labthing.add_resource(StatusProperty, f"{prefix}/status", endpoint="StatusProperty")
labthing.register_property(StatusProperty)
labthing.add_resource(
NestedStatusProperty,
f"{prefix}/status/<path:route>",
endpoint="NestedStatusProperty",
)