72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
from fastapi import APIRouter, Depends, Query
|
|
from fastapi.responses import Response
|
|
from typing import Optional
|
|
|
|
from auth.models import TokenPayload
|
|
from auth.dependencies import require_permission
|
|
from manufacturing.models import (
|
|
BatchCreate, BatchResponse,
|
|
DeviceInventoryItem, DeviceInventoryListResponse,
|
|
DeviceStatusUpdate,
|
|
)
|
|
from manufacturing import service
|
|
|
|
router = APIRouter(prefix="/api/manufacturing", tags=["manufacturing"])
|
|
|
|
|
|
@router.post("/batch", response_model=BatchResponse, status_code=201)
|
|
def create_batch(
|
|
body: BatchCreate,
|
|
_user: TokenPayload = Depends(require_permission("manufacturing", "add")),
|
|
):
|
|
return service.create_batch(body)
|
|
|
|
|
|
@router.get("/devices", response_model=DeviceInventoryListResponse)
|
|
def list_devices(
|
|
status: Optional[str] = Query(None),
|
|
hw_type: Optional[str] = Query(None),
|
|
search: Optional[str] = Query(None),
|
|
limit: int = Query(100, ge=1, le=500),
|
|
offset: int = Query(0, ge=0),
|
|
_user: TokenPayload = Depends(require_permission("manufacturing", "view")),
|
|
):
|
|
items = service.list_devices(
|
|
status=status,
|
|
hw_type=hw_type,
|
|
search=search,
|
|
limit=limit,
|
|
offset=offset,
|
|
)
|
|
return DeviceInventoryListResponse(devices=items, total=len(items))
|
|
|
|
|
|
@router.get("/devices/{sn}", response_model=DeviceInventoryItem)
|
|
def get_device(
|
|
sn: str,
|
|
_user: TokenPayload = Depends(require_permission("manufacturing", "view")),
|
|
):
|
|
return service.get_device_by_sn(sn)
|
|
|
|
|
|
@router.patch("/devices/{sn}/status", response_model=DeviceInventoryItem)
|
|
def update_status(
|
|
sn: str,
|
|
body: DeviceStatusUpdate,
|
|
_user: TokenPayload = Depends(require_permission("manufacturing", "edit")),
|
|
):
|
|
return service.update_device_status(sn, body)
|
|
|
|
|
|
@router.get("/devices/{sn}/nvs.bin")
|
|
def download_nvs(
|
|
sn: str,
|
|
_user: TokenPayload = Depends(require_permission("manufacturing", "view")),
|
|
):
|
|
binary = service.get_nvs_binary(sn)
|
|
return Response(
|
|
content=binary,
|
|
media_type="application/octet-stream",
|
|
headers={"Content-Disposition": f'attachment; filename="{sn}_nvs.bin"'},
|
|
)
|