Moved task routes into base labthing

This commit is contained in:
Joel Collins 2019-12-18 14:56:45 +00:00
parent f2af359b8b
commit 1047257242
21 changed files with 123 additions and 1481 deletions

View file

@ -2,8 +2,7 @@ from flask import current_app, _app_ctx_stack, request, url_for, jsonify
from .plugins import BasePlugin
from .views.plugins import PluginListResource
from .resource import Resource
from .views.tasks import TaskList, TaskResource
from ..utilities import get_docstring
@ -53,6 +52,11 @@ class LabThing(object):
self.app.add_url_rule(self._complete_url("/", ""), "td", self.td)
# Add plugin overview
self.add_resource(PluginListResource, "/plugins")
self.register_property(PluginListResource)
# Add task routes
self.add_resource(TaskList, "/tasks", endpoint="TasksProperty")
self.register_property(TaskList)
self.add_resource(TaskResource, "/tasks/<id>", endpoint="TaskResource")
### Device stuff
@ -209,6 +213,7 @@ class LabThing(object):
actions[key]["links"] = [{"href": self.url_for(prop, _external=True)}]
td = {
"id": url_for("td", _external=True),
"title": self.title,
"description": self.description,
"properties": props,

View file

@ -39,7 +39,7 @@ def plugins_representation(plugin_dict):
for view_id, view_data in plugin.views.items():
logging.debug(f"Representing view {view_id}")
uri = url_for(f"pluginlistresource") + "/" + view_data["rule"][1:]
uri = url_for(f"PluginListResource") + "/" + view_data["rule"][1:]
# uri = view_data["rule"]
# Make links dictionary if it doesn't yet exist
view_d = {"links": {"self": uri}}

View file

@ -0,0 +1,61 @@
import logging
from flask import abort, request, redirect, url_for, send_file, jsonify
from openflexure_microscope.api.utilities import get_bool, JsonResponse
from openflexure_microscope.common.labthings.schema import Schema
from openflexure_microscope.common.labthings import fields
from openflexure_microscope.common.labthings.resource import Resource
from openflexure_microscope.common import tasks
class TaskSchema(Schema):
_ID = fields.String(data_key="id")
target_string = fields.String(data_key="function")
_status = fields.String(data_key="status")
progress = fields.String()
data = fields.Raw()
_return_value = fields.Raw(data_key="return")
_start_time = fields.String(data_key="start_time")
_end_time = fields.String(data_key="end_time")
# TODO: Add HTTP methods
links = fields.Hyperlinks(
{
"self": {
"href": fields.AbsoluteUrlFor("TaskResource", id="<id>"),
"mimetype": "application/json",
}
}
)
task_schema = TaskSchema()
task_list_schema = TaskSchema(many=True)
class TaskList(Resource):
def get(self):
return task_list_schema.jsonify(tasks.tasks())
class TaskResource(Resource):
def get(self, id):
try:
task = tasks.dict()[id]
except KeyError:
return abort(404) # 404 Not Found
# Return task state
return task_schema.jsonify(task)
def delete(self, id):
try:
task = tasks.dict()[id]
except KeyError:
return abort(404) # 404 Not Found
task.terminate()
return task_schema.jsonify(task)