From c3692aa3ef3c57063de070f18bf6638b472ca5b3 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 4 Dec 2018 15:10:52 +0000 Subject: [PATCH] Added GPU preview routes --- openflexure_microscope/api/v1.py | 138 ++++++++++++++++++++++++++----- 1 file changed, 118 insertions(+), 20 deletions(-) diff --git a/openflexure_microscope/api/v1.py b/openflexure_microscope/api/v1.py index e2cbebb6..edf936ba 100644 --- a/openflexure_microscope/api/v1.py +++ b/openflexure_microscope/api/v1.py @@ -75,6 +75,7 @@ def attach_microscope(): StreamingCamera(config=openflexurerc), OpenFlexureStage("/dev/ttyUSB0") ) + logging.debug("Microscope successfully attached!") @@ -154,14 +155,24 @@ class StateAPI(MicroscopeView): Content-Type: application/json { - "position": { - "x": 0, - "y": 0, - "z": 0 + "camera": { + "preview_active": false, + "record_active": false, + "stream_active": true }, - "preview_active": false, - "record_active": false, - "stream_active": true + "plugin": {}, + "stage": { + "backlash": { + "x": 128, + "y": 128, + "z": 128 + }, + "position": { + "x": -8080, + "y": 5665, + "z": -12600 + } + } } :>header Accept: application/json @@ -171,11 +182,10 @@ class StateAPI(MicroscopeView): return jsonify(self.microscope.state) app.add_url_rule( - uri('/state/'), + uri('/state/'), view_func=StateAPI.as_view('state', microscope=api_microscope)) -#TODO: Wrap in some kind of requires_stage decorator. Do same for camera. class PositionAPI(MicroscopeView): def get(self): @@ -188,7 +198,7 @@ class PositionAPI(MicroscopeView): .. sourcecode:: http - GET /position/ HTTP/1.1 + GET /stage/position/ HTTP/1.1 Accept: application/json **Example response**: @@ -209,7 +219,7 @@ class PositionAPI(MicroscopeView): :>json int y: y steps :>json int z: z steps """ - return jsonify(self.microscope.state['position']) + return jsonify(self.microscope.state['stage']['position']) def post(self): """ @@ -260,13 +270,84 @@ class PositionAPI(MicroscopeView): self.microscope.stage.move_rel(position) - return jsonify(self.microscope.state['position']) + return jsonify(self.microscope.state['stage']['position']) app.add_url_rule( - uri('/position/'), + uri('/stage/position/'), view_func=PositionAPI.as_view('position', microscope=api_microscope)) +class StageParamsAPI(MicroscopeView): + + def get(self): + """ + Return current parameters of the stage. + + .. :quickref: Stage params; Get current stage parameters + + **Example request**: + + .. sourcecode:: http + + GET /stage/params HTTP/1.1 + Accept: application/json + + **Example response**: + + .. sourcecode:: http + + HTTP/1.1 200 OK + Vary: Accept + Content-Type: application/json + + { + "backlash": { + "x": 0, + "y": 0, + "z": 128 + }, + } + + """ + + return jsonify(self.microscope.state['stage']) + + def post(self): + """ + Set parameters of the stage. + + .. :quickref: Stage params; Set current stage parameters + + :reqheader Accept: application/json + :/'), + uri('/camera/capture//'), view_func=CaptureAPI.as_view('capture', microscope=api_microscope)) @@ -508,7 +589,7 @@ class CaptureDownloadRedirectAPI(MicroscopeView): # Route for shortcut where no filename is specified app.add_url_rule( - uri('/capture//download'), + uri('/camera/capture//download'), view_func=CaptureDownloadRedirectAPI.as_view('capture_download_redirect', microscope=api_microscope)) @@ -527,7 +608,7 @@ class CaptureDownloadAPI(MicroscopeView): .. sourcecode:: http - GET /capture/d0b2067abbb946f19351e075c5e7cd5b/download/2018-11-20_16-04-17.jpeg HTTP/1.1 + GET /camera/capture/d0b2067abbb946f19351e075c5e7cd5b/download/2018-11-20_16-04-17.jpeg HTTP/1.1 Accept: image/jpeg :>header Accept: image/jpeg @@ -563,10 +644,27 @@ class CaptureDownloadAPI(MicroscopeView): attachment_filename=filename) app.add_url_rule( - uri('/capture//download/'), + uri('/camera/capture//download/'), view_func=CaptureDownloadAPI.as_view('capture_download', microscope=api_microscope)) +# Preview +class GPUPreviewAPI(MicroscopeView): + + def post(self, operation): + """ + TODO: Documentation + """ + if operation == "start": + self.microscope.camera.start_preview() + elif operation == "stop": + self.microscope.camera.stop_preview() + return jsonify(self.microscope.state) + +app.add_url_rule( + uri('/camera/preview/'), + view_func=GPUPreviewAPI.as_view('gpu_preview', microscope=api_microscope)) + # Automatically clean up microscope at exit def cleanup(): global api_microscope