|
19 | 19 | from flask.typing import ResponseReturnValue |
20 | 20 | from huey.exceptions import HueyException |
21 | 21 | from huey.exceptions import TaskException |
| 22 | +from msgspec import convert |
22 | 23 | from msgspec import to_builtins |
23 | 24 | from msgspec.yaml import decode as yaml_decode |
24 | 25 | from pioreactor.calibrations import CALIBRATION_PATH |
@@ -547,6 +548,32 @@ def get_ui_version() -> ResponseReturnValue: |
547 | 548 | ### CALIBRATIONS |
548 | 549 |
|
549 | 550 |
|
| 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 | + |
550 | 577 | @unit_api.route("/calibrations", methods=["GET"]) |
551 | 578 | def get_all_calibrations() -> ResponseReturnValue: |
552 | 579 | calibration_dir = Path(env["DOT_PIOREACTOR"]) / "storage" / "calibrations" |
|
0 commit comments