From e91e58daa3fb7d80c9683fc10d966430ca63cf0f Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 18 Jun 2019 15:56:02 +0100 Subject: [PATCH] Added a long running task to schema example --- .../plugins/testing/schema_example/api.py | 26 ++++++++++++++++++- .../plugins/testing/schema_example/plugin.py | 16 ++++++++++-- .../testing/schema_example/schema.json | 24 ++++++----------- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/openflexure_microscope/plugins/testing/schema_example/api.py b/openflexure_microscope/plugins/testing/schema_example/api.py index 6c518043..67f92b62 100644 --- a/openflexure_microscope/plugins/testing/schema_example/api.py +++ b/openflexure_microscope/plugins/testing/schema_example/api.py @@ -3,6 +3,7 @@ from openflexure_microscope.api.v1.views import MicroscopeViewPlugin from openflexure_microscope.exceptions import TaskDeniedException from flask import request, Response, escape, jsonify +import logging class DoAPI(MicroscopeViewPlugin): """ @@ -37,4 +38,27 @@ class DoAPI(MicroscopeViewPlugin): return jsonify({ 'response': 'completed' - }) \ No newline at end of file + }) + +class TaskAPI(MicroscopeViewPlugin): + """ + A task example API plugin + """ + + def get(self): + return jsonify({ + 'run_time': self.plugin.run_time + }) + + def post(self): + # Get payload JSON + payload = JsonPayload(request) + + # Extract a values from the JSON payload. + val_int = payload.param('run_time', default=5, convert=int) + + logging.info("Running task...") + task = self.microscope.task.start(self.plugin.generate_random_numbers_for_a_while, val_int) + + # return a handle on the autofocus task + return jsonify(task.state), 202 \ No newline at end of file diff --git a/openflexure_microscope/plugins/testing/schema_example/plugin.py b/openflexure_microscope/plugins/testing/schema_example/plugin.py index e3d2b936..da2264c9 100644 --- a/openflexure_microscope/plugins/testing/schema_example/plugin.py +++ b/openflexure_microscope/plugins/testing/schema_example/plugin.py @@ -4,7 +4,7 @@ import os import json from openflexure_microscope.plugins import MicroscopePlugin -from .api import DoAPI +from .api import DoAPI, TaskAPI HERE = os.path.dirname(os.path.realpath(__file__)) SCHEMA_PATH = os.path.join(HERE, "schema.json") @@ -20,6 +20,7 @@ class ExamplePlugin(MicroscopePlugin): api_views = { '/do': DoAPI, + '/task': TaskAPI } def __init__(self): @@ -30,6 +31,8 @@ class ExamplePlugin(MicroscopePlugin): self.val_select = "Most" self.val_unused = "I'm an unused string, here to confuse the form parsing" + self.run_time = 5 + def set_values(self, val_int, val_str, val_radio, val_check, val_select, val_disposable): """ Demonstrate a plugin with schema @@ -57,4 +60,13 @@ class ExamplePlugin(MicroscopePlugin): 'val_check': self.val_check, 'val_select': self.val_select, 'val_unused': self.val_unused - } \ No newline at end of file + } + + def generate_random_numbers_for_a_while(self, run_time: int): + self.run_time = run_time + vals = [] + for _ in range(run_time): + vals.append(random.random()) + time.sleep(1) + + return vals \ No newline at end of file diff --git a/openflexure_microscope/plugins/testing/schema_example/schema.json b/openflexure_microscope/plugins/testing/schema_example/schema.json index 34d67fd1..1628740f 100644 --- a/openflexure_microscope/plugins/testing/schema_example/schema.json +++ b/openflexure_microscope/plugins/testing/schema_example/schema.json @@ -60,27 +60,19 @@ ] }, { - "name": "Different form", - "isTask": false, + "name": "Task form", + "isTask": true, "selfUpdate": true, - "route": "/do", - "submitLabel": "Do different things", + "route": "/task", + "submitLabel": "Start task", "schema": [ - [ { "fieldType": "numberInput", - "placeholder": "Some integer", - "name": "val_int", - "label": "Number value", - "minValue": 0 - }, - { - "fieldType": "textInput", - "placeholder": "Some string", - "label": "String value", - "name": "val_str" + "placeholder": "", + "name": "run_time", + "label": "Run time (seconds)", + "minValue": 1 } - ] ] } ]