targets.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, HTTPException, Query, Depends
  3. from loguru import logger
  4. from app.controllers.targets import readjust_all_target
  5. from app.models.targets import TargetReadjustInResponse
  6. from app.utils.date import get_time_str
  7. router = APIRouter()
  8. @router.get('/readjust', response_model=TargetReadjustInResponse, tags=['targets'])
  9. async def readjust_target(
  10. project_id: str = Query(..., max_length=50, regex='^Pj'),
  11. space_id: str = Query(..., max_length=50, regex='^Sp'),
  12. wechat_timestamp: str = Query(None, min_length=14, max_length=14),
  13. ):
  14. try:
  15. need_run_room_control = await readjust_all_target(project_id, space_id, wechat_timestamp)
  16. except Exception as e:
  17. logger.error(e)
  18. raise HTTPException(
  19. status_code=500,
  20. detail='Oops, something wrong has happened'
  21. )
  22. response = {
  23. 'projectId': project_id,
  24. 'roomId': space_id,
  25. 'flag': 1 if need_run_room_control else 0,
  26. 'time': wechat_timestamp,
  27. }
  28. return response
  29. @router.get('/readjust/test/{project_id}/{space_id}', tags=['targets'])
  30. async def test_readjust(project_id: str, space_id: str, wechat_time: str = Depends(get_time_str)):
  31. feedback = {
  32. 'a little cold': 1,
  33. 'so cold': 0,
  34. 'a little hot': 1,
  35. 'so hot': 0,
  36. 'noisy or blowy': 0,
  37. 'so stuffy': 0,
  38. 'switch off': 0,
  39. 'switch on': 0,
  40. }
  41. await readjust_all_target(project_id, space_id, wechat_time, feedback)
  42. return {'Message': 'Running background'}