diff --git a/src/main.py b/src/main.py index 5743813..e046803 100644 --- a/src/main.py +++ b/src/main.py @@ -249,372 +249,8 @@ async def process_xml_request(body: bytes, db: Session): # Health check endpoint @app.get("/health", tags=["Health"]) def health_check(): - - if command == "createtaskgroup": - try: - taskgroup_data = autostore_data[command_key] - - tasks_in = taskgroup_data.get("Tasks", {}).get("Task", []) - if not isinstance(tasks_in, list): - tasks_in = [tasks_in] - - tasks = [schemas.TaskCreate(id=t["Task"], bin_id=t["Bin"], task_type="OUTBOUND") for t in tasks_in] - - task_group_create = schemas.TaskGroupCreate( - id=taskgroup_data["Taskgroup"], - port_id=taskgroup_data["Port"], - tasks=tasks - ) - - execution_time = crud.create_task_group(db=db, task_group=task_group_create) - - response_dict = { - "AutoStore": { - "CreateTaskgroup": { - "ExecutionTime": execution_time - } - } - } - except ValueError as e: - raise HTTPException(status_code=404, detail=str(e)) - - elif command == "gettaskinfo": - task_id = autostore_data[command_key].get("Task") - group_id = autostore_data[command_key].get("Taskgroup") - - if not task_id and not group_id: - raise ValueError("Either Task or Taskgroup ID must be provided for GetTaskInfo.") - - if group_id: - task_group = crud.get_task_group(db, group_id=group_id) - if not task_group: - raise ValueError(f"Taskgroup not found: {group_id}") - - response_dict = { - "AutoStore": { - "GetTaskInfo": { - "Taskgroup": { - "Taskgroup": task_group.id, - "Port": task_group.port_id, - "Status": task_group.status, - "Tasks": { - "Task": [ - { - "Task": t.id, - "Bin": t.bin_id, - "Status": t.status - } for t in task_group.tasks - ] - } - } - } - } - } - else: # task_id must be present - task = crud.get_task(db, task_id=task_id) - if not task: - raise ValueError(f"Task not found: {task_id}") - - response_dict = { - "AutoStore": { - "GetTaskInfo": { - "Task": { - "Task": task.id, - "Bin": task.bin_id, - "Status": task.status, - "Taskgroup": task.task_group_id - } - } - } - } - - elif command == "getbininfo": - bin_info_data = autostore_data[command_key] - - # Handle case where GetBinInfo is empty or None - if not bin_info_data or not isinstance(bin_info_data, dict): - raise HTTPException(status_code=400, detail="Bin ID is required for GetBinInfo.") - - bin_id = bin_info_data.get("Bin") - - if not bin_id: - raise HTTPException(status_code=400, detail="Bin ID is required for GetBinInfo.") - - bin_obj = crud.get_bin(db, bin_id=bin_id) - if not bin_obj: - raise ValueError(f"Bin not found: {bin_id}") - - response_dict = { - "AutoStore": { - "GetBinInfo": { - "Bin": bin_obj.id, - "ContentCode": str(bin_obj.content_code) - } - } - } - - elif command == "deletetaskgroup": - delete_taskgroup_data = autostore_data[command_key] - - # Handle case where DeleteTaskgroup is empty or None - if not delete_taskgroup_data or not isinstance(delete_taskgroup_data, dict): - raise HTTPException(status_code=400, detail="Taskgroup ID is required for DeleteTaskgroup.") - - group_id = delete_taskgroup_data.get("Taskgroup") - - if not group_id: - raise HTTPException(status_code=400, detail="Taskgroup ID is required for DeleteTaskgroup.") - - deleted_task_group = crud.delete_task_group(db, group_id=group_id) - if not deleted_task_group: - raise ValueError(f"Taskgroup not found: {group_id}") - - response_dict = { - "AutoStore": { - "DeleteTaskgroup": { - "Success": True - } - } - } - - elif command == "getnexttask": - get_next_task_data = autostore_data[command_key] - - # Handle case where GetNextTask is empty or None - if not get_next_task_data or not isinstance(get_next_task_data, dict): - raise HTTPException(status_code=400, detail="Port ID is required for GetNextTask.") - - port_id = get_next_task_data.get("Port") - - if not port_id: - raise HTTPException(status_code=400, detail="Port ID is required for GetNextTask.") - - # Check if port exists - port = crud.get_port(db, port_id=port_id) - if not port: - raise ValueError(f"Port not found: {port_id}") - - # Get next available task for this port - next_task = crud.get_next_task_for_port(db, port_id=port_id) - - if next_task: - response_dict = { - "AutoStore": { - "GetNextTask": { - "Task": next_task.id, - "Bin": next_task.bin_id, - "Taskgroup": next_task.task_group_id - } - } - } - else: - response_dict = { - "AutoStore": { - "GetNextTask": { - "NoTask": True - } - } - } - - elif command == "setportmode": - set_port_mode_data = autostore_data[command_key] - - # Handle case where SetPortMode is empty or None - if not set_port_mode_data or not isinstance(set_port_mode_data, dict): - raise HTTPException(status_code=400, detail="Port ID and Mode are required for SetPortMode.") - - port_id = set_port_mode_data.get("Port") - mode = set_port_mode_data.get("Mode") - - if not port_id: - raise HTTPException(status_code=400, detail="Port ID is required for SetPortMode.") - - if not mode: - raise HTTPException(status_code=400, detail="Mode is required for SetPortMode.") - - try: - updated_port = crud.set_port_mode(db, port_id=port_id, mode=mode) - if not updated_port: - raise ValueError(f"Port not found: {port_id}") - - response_dict = { - "AutoStore": { - "SetPortMode": { - "Success": True - } - } - } - except ValueError as e: - if "Invalid port mode" in str(e): - raise HTTPException(status_code=400, detail=str(e)) - else: - raise ValueError(str(e)) - - elif command == "openbin": - open_bin_data = autostore_data[command_key] - - if not open_bin_data or not isinstance(open_bin_data, dict): - raise HTTPException(status_code=400, detail="Port ID or Bin ID is required for OpenBin.") - - port_id = open_bin_data.get("Port") - bin_id = open_bin_data.get("Bin") - - if port_id: - # Port-basierter Zugriff: Hole nächste verfügbare Bin für Port - logger.info(f"OpenBin command received for port: {port_id}") - - # Prüfe ob Port existiert - port = crud.get_port(db, port_id=port_id) - if not port: - raise ValueError(f"Port not found: {port_id}") - - # Hole nächste verfügbare leere Bin (content_code = 0 oder None) - next_bin = crud.get_next_empty_bin(db) - if not next_bin: - raise ValueError("No empty bins available") - - # TODO: Bin zum Port zuweisen in der Datenbank - - # Antwort im erwarteten Format für PL/SQL - response_dict = { - "response": { - "params": { - "bin_id": next_bin.id, - "port_id": port_id, - "success": True - } - } - } - - elif bin_id: - # Bin-basierter Zugriff (wie bisher) - logger.info(f"OpenBin command received for bin: {bin_id}") - - bin_obj = crud.get_bin(db, bin_id=bin_id) - if not bin_obj: - raise ValueError(f"Bin not found: {bin_id}") - - response_dict = { - "response": { - "params": { - "bin_id": bin_id, - "success": True - } - } - } - - else: - raise HTTPException(status_code=400, detail="Either Port ID or Bin ID is required for OpenBin.") - - elif command == "closebin": - close_bin_data = autostore_data[command_key] - - if not close_bin_data or not isinstance(close_bin_data, dict): - raise HTTPException(status_code=400, detail="Bin ID is required for CloseBin.") - - bin_id = close_bin_data.get("Bin") - - if not bin_id: - raise HTTPException(status_code=400, detail="Bin ID is required for CloseBin.") - - # Check if bin exists - bin_obj = crud.get_bin(db, bin_id=bin_id) - if not bin_obj: - raise ValueError(f"Bin not found: {bin_id}") - - # Log the close bin action - logger.info(f"CloseBin command received for bin: {bin_id}") - - response_dict = { - "AutoStore": { - "CloseBin": { - "Bin": bin_id, - "Success": True - } - } - } - - elif command == "setbincontentcode": - set_content_data = autostore_data[command_key] - - if not set_content_data or not isinstance(set_content_data, dict): - raise HTTPException(status_code=400, detail="Bin ID and ContentCode are required for SetBinContentCode.") - - bin_id = set_content_data.get("Bin") - content_code = set_content_data.get("ContentCode") - - if not bin_id: - raise HTTPException(status_code=400, detail="Bin ID is required for SetBinContentCode.") - - if content_code is None: - raise HTTPException(status_code=400, detail="ContentCode is required for SetBinContentCode.") - - try: - content_code = int(content_code) - except ValueError: - raise HTTPException(status_code=400, detail="ContentCode must be a valid integer.") - - # Update bin content code - updated_bin = crud.update_bin_content(db, bin_id=bin_id, content_code=content_code) - if not updated_bin: - raise ValueError(f"Bin not found: {bin_id}") - - response_dict = { - "AutoStore": { - "SetBinContentCode": { - "Bin": bin_id, - "ContentCode": str(content_code), - "Success": True - } - } - } - - else: - # Placeholder for other commands - raise HTTPException(status_code=400, detail=f"Unknown or unimplemented command: {command}") - - response_xml = xmltodict.unparse(response_dict, pretty=True) - return Response(content=response_xml, media_type="application/xml") - - except (HTTPException, ValueError) as e: - # Convert FastAPI's default JSON error response to our XML format - status_code = e.status_code if isinstance(e, HTTPException) else 404 - detail = e.detail if isinstance(e, HTTPException) else str(e) - - error_response = { - "AutoStore": { - "Error": { - "Code": "RequestError", - "Description": detail - } - } - } - error_xml = xmltodict.unparse(error_response, pretty=True) - return Response(content=error_xml, status_code=status_code, media_type="application/xml") - except Exception as e: - logger.error(f"Error processing XML request: {e}", exc_info=True) - # Create a generic XML error response - error_response = { - "AutoStore": { - "Error": { - "Code": "InternalError", - "Description": str(e) - } - } - } - error_xml = xmltodict.unparse(error_response, pretty=True) - return Response(content=error_xml, status_code=500, media_type="application/xml") - - -@app.get("/health", tags=["System"]) -def read_health(): - """ - Health check endpoint to verify that the API is running. - """ return {"status": "ok"} -# --- BIN Endpoints (for admin/debug) --- - @app.post("/bins/", response_model=schemas.Bin, tags=["Admin - Bins"]) def create_bin_endpoint(bin: schemas.BinCreate, db: Session = Depends(get_db)): db_bin = crud.get_bin(db, bin_id=bin.id)