Added basic thing description to root
This commit is contained in:
parent
d52453849c
commit
f2af359b8b
24 changed files with 677 additions and 183 deletions
65
openflexure_microscope/api/v2/views/actions/__init__.py
Normal file
65
openflexure_microscope/api/v2/views/actions/__init__.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
"""
|
||||
Top-level representation of enabled actions
|
||||
"""
|
||||
|
||||
from flask import Blueprint, url_for, jsonify
|
||||
|
||||
from openflexure_microscope.api.utilities import blueprint_for_module
|
||||
from openflexure_microscope.utilities import get_docstring, description_from_view
|
||||
from openflexure_microscope.api.views import MicroscopeView
|
||||
|
||||
from . import camera, stage, system
|
||||
|
||||
_actions = {
|
||||
"capture": {
|
||||
"rule": "/camera/capture/",
|
||||
"view_class": camera.CaptureAPI,
|
||||
"conditions": True,
|
||||
},
|
||||
"previewStart": {
|
||||
"rule": "/camera/preview/start",
|
||||
"view_class": camera.GPUPreviewStartAPI,
|
||||
"conditions": True,
|
||||
},
|
||||
"previewStop": {
|
||||
"rule": "/camera/preview/stop",
|
||||
"view_class": camera.GPUPreviewStopAPI,
|
||||
"conditions": True,
|
||||
},
|
||||
"move": {
|
||||
"rule": "/stage/move/",
|
||||
"view_class": stage.MoveStageAPI,
|
||||
"conditions": True,
|
||||
},
|
||||
"zeroStage": {
|
||||
"rule": "/stage/zero/",
|
||||
"view_class": stage.ZeroStageAPI,
|
||||
"conditions": True,
|
||||
},
|
||||
"shutdown": {
|
||||
"rule": "/system/shutdown/",
|
||||
"view_class": system.ShutdownAPI,
|
||||
"conditions": system.is_raspberrypi(),
|
||||
},
|
||||
"reboot": {
|
||||
"rule": "/system/reboot/",
|
||||
"view_class": system.RebootAPI,
|
||||
"conditions": system.is_raspberrypi(),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def enabled_actions():
|
||||
global _actions
|
||||
return {k: v for k, v in _actions.items() if v["conditions"]}
|
||||
|
||||
|
||||
def add_actions_to_labthing(labthing, prefix=""):
|
||||
"""
|
||||
Add all capture resources to a labthing
|
||||
"""
|
||||
for name, action in enabled_actions().items():
|
||||
view_class = action["view_class"]
|
||||
rule = action["rule"]
|
||||
labthing.add_resource(view_class, f"{prefix}/actions{rule}")
|
||||
labthing.register_action(view_class)
|
||||
82
openflexure_microscope/api/v2/views/actions/camera.py
Normal file
82
openflexure_microscope/api/v2/views/actions/camera.py
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
from openflexure_microscope.api.utilities import get_bool, JsonResponse
|
||||
from openflexure_microscope.common.labthings.resource import Resource
|
||||
from openflexure_microscope.common.labthings.find import find_device
|
||||
from openflexure_microscope.utilities import filter_dict
|
||||
|
||||
from openflexure_microscope.api.v2.views.captures import capture_schema
|
||||
|
||||
import logging
|
||||
from flask import jsonify, request, abort, url_for, redirect, send_file
|
||||
|
||||
|
||||
class CaptureAPI(Resource):
|
||||
"""
|
||||
Create a new image capture.
|
||||
"""
|
||||
|
||||
def post(self):
|
||||
microscope = find_device("openflexure_microscope")
|
||||
payload = JsonResponse(request)
|
||||
|
||||
filename = payload.param("filename")
|
||||
temporary = payload.param("temporary", default=False, convert=bool)
|
||||
use_video_port = payload.param("use_video_port", default=False, convert=bool)
|
||||
bayer = payload.param("bayer", default=True, convert=bool)
|
||||
metadata = payload.param("metadata", default={}, convert=dict)
|
||||
tags = payload.param("tags", default=[], convert=list)
|
||||
|
||||
resize = payload.param("size", default=None)
|
||||
if resize:
|
||||
if ("width" in resize) and ("height" in resize):
|
||||
resize = (
|
||||
int(resize["width"]),
|
||||
int(resize["height"]),
|
||||
) # Convert dict to tuple
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
# Explicitally acquire lock (prevents empty files being created if lock is unavailable)
|
||||
with microscope.camera.lock:
|
||||
output = microscope.camera.new_image(temporary=temporary, filename=filename)
|
||||
|
||||
microscope.camera.capture(
|
||||
output.file, use_video_port=use_video_port, resize=resize, bayer=bayer
|
||||
)
|
||||
|
||||
# Inject system metadata
|
||||
output.put_metadata(microscope.metadata, system=True)
|
||||
|
||||
# Insert custom metadata
|
||||
output.put_metadata(metadata)
|
||||
|
||||
# Insert custom tags
|
||||
output.put_tags(tags)
|
||||
|
||||
return capture_schema.jsonify(output)
|
||||
|
||||
|
||||
class GPUPreviewStartAPI(Resource):
|
||||
def post(self):
|
||||
microscope = find_device("openflexure_microscope")
|
||||
payload = JsonResponse(request)
|
||||
|
||||
window = payload.param("window", default=[])
|
||||
logging.debug(window)
|
||||
|
||||
if len(window) != 4:
|
||||
fullscreen = True
|
||||
window = None
|
||||
else:
|
||||
fullscreen = False
|
||||
window = [int(w) for w in window]
|
||||
|
||||
microscope.camera.start_preview(fullscreen=fullscreen, window=window)
|
||||
|
||||
return jsonify(microscope.state)
|
||||
|
||||
|
||||
class GPUPreviewStopAPI(Resource):
|
||||
def post(self):
|
||||
microscope = find_device("openflexure_microscope")
|
||||
microscope.camera.stop_preview()
|
||||
return jsonify(microscope.state)
|
||||
55
openflexure_microscope/api/v2/views/actions/stage.py
Normal file
55
openflexure_microscope/api/v2/views/actions/stage.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
from openflexure_microscope.api.utilities import JsonResponse
|
||||
from openflexure_microscope.common.labthings.resource import Resource
|
||||
from openflexure_microscope.common.labthings.find import find_device
|
||||
from openflexure_microscope.utilities import axes_to_array, filter_dict
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
import logging
|
||||
|
||||
|
||||
class MoveStageAPI(Resource):
|
||||
def post(self):
|
||||
microscope = find_device("openflexure_microscope")
|
||||
# Create response object
|
||||
payload = JsonResponse(request)
|
||||
logging.debug(payload.json)
|
||||
|
||||
# Handle absolute positioning (calculate a relative move from current position and target)
|
||||
if (payload.param("absolute") is True) and (
|
||||
microscope.stage
|
||||
): # Only if stage exists
|
||||
target_position = axes_to_array(payload.json, ["x", "y", "z"])
|
||||
logging.debug("TARGET: {}".format(target_position))
|
||||
position = [
|
||||
target_position[i] - microscope.stage.position[i] for i in range(3)
|
||||
]
|
||||
logging.debug("DELTA: {}".format(position))
|
||||
|
||||
else:
|
||||
# Get coordinates from payload
|
||||
position = axes_to_array(payload.json, ["x", "y", "z"], [0, 0, 0])
|
||||
|
||||
logging.debug(position)
|
||||
|
||||
# Move if stage exists
|
||||
if microscope.stage:
|
||||
# Explicitally acquire lock
|
||||
with microscope.stage.lock:
|
||||
microscope.stage.move_rel(position)
|
||||
else:
|
||||
logging.warning("Unable to move. No stage found.")
|
||||
|
||||
return jsonify(microscope.status["stage"]["position"])
|
||||
|
||||
|
||||
class ZeroStageAPI(Resource):
|
||||
"""
|
||||
Zero stage coordinates
|
||||
"""
|
||||
|
||||
def post(self):
|
||||
microscope = find_device("openflexure_microscope")
|
||||
microscope.stage.zero_position()
|
||||
|
||||
return jsonify(microscope.status["stage"])
|
||||
46
openflexure_microscope/api/v2/views/actions/system.py
Normal file
46
openflexure_microscope/api/v2/views/actions/system.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
from openflexure_microscope.common.labthings.resource import Resource
|
||||
import subprocess
|
||||
import os
|
||||
from sys import platform
|
||||
|
||||
|
||||
def is_raspberrypi(raise_on_errors=False):
|
||||
"""
|
||||
Checks if Raspberry Pi.
|
||||
"""
|
||||
# I mean, if it works, it works...
|
||||
return os.path.exists("/usr/bin/raspi-config")
|
||||
|
||||
|
||||
class ShutdownAPI(Resource):
|
||||
"""
|
||||
Attempt to shutdown the device
|
||||
"""
|
||||
|
||||
def post(self):
|
||||
"""
|
||||
Attempt to shutdown the device
|
||||
|
||||
.. :quickref: Actions; Shutdown
|
||||
|
||||
"""
|
||||
subprocess.Popen(["sudo", "shutdown", "-h", "now"])
|
||||
|
||||
return "{}", 201
|
||||
|
||||
|
||||
class RebootAPI(Resource):
|
||||
"""
|
||||
Attempt to reboot the device
|
||||
"""
|
||||
|
||||
def post(self):
|
||||
"""
|
||||
Attempt to shutdown the device
|
||||
|
||||
.. :quickref: Actions; Shutdown
|
||||
|
||||
"""
|
||||
subprocess.Popen(["sudo", "shutdown", "-r", "now"])
|
||||
|
||||
return "{}", 201
|
||||
Loading…
Add table
Add a link
Reference in a new issue