Skip to content

Commit 9a442c9

Browse files
adding endpoint to create calibrations
1 parent b146227 commit 9a442c9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pioreactorui/unit_api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from flask.typing import ResponseReturnValue
2020
from huey.exceptions import HueyException
2121
from huey.exceptions import TaskException
22+
from msgspec import convert
2223
from msgspec import to_builtins
2324
from msgspec.yaml import decode as yaml_decode
2425
from pioreactor.calibrations import CALIBRATION_PATH
@@ -547,6 +548,32 @@ def get_ui_version() -> ResponseReturnValue:
547548
### CALIBRATIONS
548549

549550

551+
@unit_api.route("/calibrations/<device>", methods=["POST"])
552+
def create_calibration(device) -> ResponseReturnValue:
553+
"""
554+
Create a new calibration for the specified device.
555+
The request must contain a JSON payload with the calibration data.
556+
"""
557+
calibration_dir = Path(env["DOT_PIOREACTOR"]) / "storage" / "calibrations" / device
558+
calibration_dir.mkdir(parents=True, exist_ok=True)
559+
560+
try:
561+
data = request.get_json()
562+
calibration_name = data.get("calibration_name")
563+
if not calibration_name:
564+
abort(400, description="Missing 'calibration_name' in request payload.")
565+
566+
calibration_data = convert(data, AllCalibrations)
567+
path = calibration_data.save_to_disk_for_device(device)
568+
569+
# Respond with success and the created calibration details
570+
return jsonify({"msg": "Calibration created successfully.", "path": str(path)}), 201
571+
572+
except Exception as e:
573+
publish_to_error_log(f"Error creating calibration: {e}", "create_calibration")
574+
abort(500, description="Failed to create calibration.")
575+
576+
550577
@unit_api.route("/calibrations", methods=["GET"])
551578
def get_all_calibrations() -> ResponseReturnValue:
552579
calibration_dir = Path(env["DOT_PIOREACTOR"]) / "storage" / "calibrations"

0 commit comments

Comments
 (0)