space.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Depends, HTTPException, Query
  3. from sqlalchemy.orm import Session
  4. from app.api.dependencies.db import get_db
  5. from app.crud.space.weight import (
  6. get_weights_by_space,
  7. get_weights_by_vav,
  8. create_weight,
  9. update_weight,
  10. )
  11. from app.models.domain.space import SpaceControlResponse
  12. from app.schemas.sapce_weight import SpaceWeight, SpaceWeightCreate, SpaceWeightUpdate
  13. router = APIRouter()
  14. @router.get("/control", response_model=SpaceControlResponse)
  15. async def get_space_command(
  16. project_id: str = Query(..., max_length=50, regex="^Pj", alias="projectId"),
  17. space_id: str = Query(..., max_length=50, regex="^Sp", alias="roomId"),
  18. timestamp: str = Query(None, min_length=14, max_length=14, alias="time"),
  19. method: int = Query(3),
  20. ):
  21. response = {
  22. "projectId": project_id,
  23. "roomId": space_id,
  24. "flag": 1,
  25. "time": timestamp,
  26. "method": method,
  27. }
  28. return response
  29. @router.post("/weight", response_model=SpaceWeight)
  30. async def create_space_weight(weight: SpaceWeightCreate, db: Session = Depends(get_db)):
  31. return create_weight(db=db, weight=weight)
  32. @router.get("/weight/{space_id}", response_model=list[SpaceWeight])
  33. async def read_weight_by_space(space_id: str, db: Session = Depends(get_db)):
  34. db_weights = get_weights_by_space(db, space_id=space_id)
  35. return db_weights
  36. @router.get("/weight/vav/{vav_id}", response_model=list[SpaceWeight])
  37. async def read_weight_by_vav(vav_id: str, db: Session = Depends(get_db)):
  38. db_weights = get_weights_by_vav(db, vav_id=vav_id)
  39. return db_weights
  40. @router.put("/weight/{space_id}/{vav_id}", response_model=SpaceWeight)
  41. async def update_weight_by_space(
  42. space_id: str,
  43. vav_id: str,
  44. weight_in: SpaceWeightUpdate,
  45. db: Session = Depends(get_db),
  46. ):
  47. weights = get_weights_by_space(db, space_id=space_id)
  48. new_weight = None
  49. for weight in weights:
  50. if weight.vav_box_id == vav_id:
  51. new_weight = update_weight(db, db_weight=weight, weight_in=weight_in)
  52. break
  53. if not new_weight:
  54. raise HTTPException(status_code=404, detail="Wight not found")
  55. return new_weight