diff --git a/ofm_config_full.json b/ofm_config_full.json new file mode 100644 index 00000000..05be1804 --- /dev/null +++ b/ofm_config_full.json @@ -0,0 +1,20 @@ +{ + "things": { + "/camera/": "labthings_picamera2.thing.StreamingPiCamera2", + "/stage/": "labthings_sangaboard.SangaboardThing", + "/auto_recentre_stage/": "openflexure_microscope_server.things.auto_recentre_stage:RecentringThing", + "/autofocus/": "openflexure_microscope_server.things.autofocus:AutofocusThing", + "/camera_stage_mapping/": "openflexure_microscope_server.things.camera_stage_mapping:CameraStageMapper", + "/system_control/": "openflexure_microscope_server.things.system_control:SystemControlThing", + "/settings/": "openflexure_microscope_server.things.settings_manager:SettingsManager", + "/smart_scan/": { + "class": "openflexure_microscope_server.things.smart_scan:SmartScan", + "kwargs": { + "path_to_openflexure_stitch": "application/openflexure-stitching/.venv/bin/openflexure-stitch" + } + }, + "/background_detect/": "openflexure_microscope_server.things.smart_scan:BackgroundDetectThing", + "/api_test/": "openflexure_microscope_server.things.test:APITestThing" + }, + "settings_folder": "./openflexure_settings/" +} \ No newline at end of file diff --git a/ofm_config_stub.json b/ofm_config_stub.json new file mode 100644 index 00000000..7b324974 --- /dev/null +++ b/ofm_config_stub.json @@ -0,0 +1,11 @@ +{ + "things": { + "/camera/": "openflexure_microscope_server.things.opencv_camera:OpenCVCamera", + "/stage/": "openflexure_microscope_server.things.dummy_stage:DummyStage", + + "/system_control/": "openflexure_microscope_server.things.system_control:SystemControlThing", + "/settings/": "openflexure_microscope_server.things.settings_manager:SettingsManager", + "/api_test/": "openflexure_microscope_server.things.test:APITestThing" + }, + "settings_folder": "./openflexure_settings/" +} \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 2cfccdde..71549167 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,9 @@ pi = [ "labthings-picamera2", ] +[project.scripts] +openflexure-microscope-server = "openflexure_microscope_server.server:serve_from_cli" + [project.urls] "Homepage" = "https://openflexure.org/" diff --git a/src/openflexure_microscope_server/server/__init__.py b/src/openflexure_microscope_server/server/__init__.py index de55b1a0..e3f427a5 100644 --- a/src/openflexure_microscope_server/server/__init__.py +++ b/src/openflexure_microscope_server/server/__init__.py @@ -1,43 +1,47 @@ from __future__ import annotations -from labthings_fastapi.thing_server import ThingServer -from labthings_sangaboard import SangaboardThing -from labthings_picamera2.thing import StreamingPiCamera2 +from typing import Optional -from ..things.autofocus import AutofocusThing -from ..things.camera_stage_mapping import CameraStageMapper -from ..things.system_control import SystemControlThing -from ..things.settings_manager import SettingsManager -from ..things.auto_recentre_stage import RecentringThing -from ..things.smart_scan import SmartScanThing, BackgroundDetectThing -from ..things.test import APITestThing +from labthings_fastapi.server import cli, ThingServer +import uvicorn from .serve_static_files import add_static_files from .legacy_api import add_v2_endpoints from ..logging import configure_logging, retrieve_log -configure_logging() -thing_server = ThingServer() -thing_server.add_thing(StreamingPiCamera2(), "/camera/") -thing_server.add_thing(SangaboardThing(), "/stage/") -thing_server.add_thing(RecentringThing(), "/auto_recentre_stage/") -thing_server.add_thing(AutofocusThing(), "/autofocus/") -thing_server.add_thing(CameraStageMapper(), "/camera_stage_mapping/") -thing_server.add_thing(SystemControlThing(), "/system_control/") -thing_server.add_thing(SettingsManager(), "/settings/") -thing_server.add_thing(SmartScanThing("application/openflexure-stitching/.venv/bin/openflexure-stitch"), "/smart_scan/") -thing_server.add_thing(BackgroundDetectThing(), "/background_detect/") -thing_server.add_thing(APITestThing(), "/api_test/") -try: - add_static_files(thing_server.app) -except RuntimeError: - print("Failed to add static files - you will have to do without them!") +def customise_server(server: ThingServer): + """Customise the server with additional endpoints, etc.""" + configure_logging() + add_v2_endpoints(server) + try: + add_static_files(server.app) + except RuntimeError: + print("Failed to add static files - you will have to do without them!") -add_v2_endpoints(thing_server) # Add the v2 endpoints for compatibility with OpenFlexure Connect - -# Add an endpoint to get the log -thing_server.app.get("/log/")(retrieve_log) + # Add an endpoint to get the log + server.app.get("/log/")(retrieve_log) -app = thing_server.app +def serve_from_cli(argv: Optional[list[str]] = None): + """Start the server from the command line""" + args = cli.parse_args(argv) + try: + config, server = None, None + config = cli.config_from_args(args) + server = cli.server_from_config(config) + customise_server(server) + uvicorn.run(server.app, host=args.host, port=args.port) + except BaseException as e: + if args.fallback: + print(f"Error: {e}") + fallback_server = "labthings_fastapi.server.fallback:app" + print(f"Starting fallback server {fallback_server}.") + app = cli.object_reference_to_object(fallback_server) + app.labthings_config = config + app.labthings_server = server + app.labthings_error = e + uvicorn.run(app, host=args.host, port=args.port) + else: + raise e +