"""Proxmark3 API endpoints.""" from fastapi import APIRouter, HTTPException, Depends from pydantic import BaseModel from typing import Optional import asyncio from ..workers.pm3_worker import PM3Worker, PM3Command from ..managers.session_manager import SessionManager router = APIRouter() pm3_worker = PM3Worker() session_manager = SessionManager() class CommandRequest(BaseModel): command: str session_id: Optional[str] = None class CommandResponse(BaseModel): success: bool output: str error: Optional[str] = None class StatusResponse(BaseModel): connected: bool device: str version: Optional[str] = None session_active: bool @router.get("/status", response_model=StatusResponse) async def get_status(): """Get Proxmark3 status.""" is_connected = await pm3_worker.is_connected() version = None if is_connected: # Try to get version info result = await pm3_worker.execute_command("hw version") if result.success: version = result.output.split("\n")[0] if result.output else None return StatusResponse( connected=is_connected, device=pm3_worker.device_path, version=version, session_active=session_manager.has_active_session() ) @router.post("/connect") async def connect(): """Connect to Proxmark3 device.""" try: await pm3_worker.connect() return {"success": True, "message": "Connected to Proxmark3"} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @router.post("/disconnect") async def disconnect(): """Disconnect from Proxmark3 device.""" try: await pm3_worker.disconnect() return {"success": True, "message": "Disconnected from Proxmark3"} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @router.post("/command", response_model=CommandResponse) async def execute_command(request: CommandRequest): """Execute a Proxmark3 command.""" try: # Check if another session is active if not session_manager.can_execute(request.session_id): raise HTTPException( status_code=423, detail="Another session is active. Please take over or wait." ) # Execute command result = await pm3_worker.execute_command(request.command) # Update session activity if request.session_id: session_manager.update_activity(request.session_id) return CommandResponse( success=result.success, output=result.output, error=result.error ) except Exception as e: return CommandResponse( success=False, output="", error=str(e) ) @router.get("/commands/history") async def get_command_history(limit: int = 50): """Get recent command history.""" # TODO: Implement database query return {"history": []}