targets.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if not wechat_timestamp:
  16. wechat_timestamp = get_time_str()
  17. need_run_room_control = await readjust_all_target(project_id, space_id, wechat_timestamp)
  18. except Exception as e:
  19. logger.error(e)
  20. raise HTTPException(
  21. status_code=500,
  22. detail='Oops, something wrong has happened'
  23. )
  24. response = {
  25. 'projectId': project_id,
  26. 'roomId': space_id,
  27. 'flag': 1 if need_run_room_control else 0,
  28. 'time': wechat_timestamp,
  29. }
  30. return response
  31. @router.get('/readjust/test/{project_id}/{space_id}', tags=['targets'])
  32. async def test_readjust(project_id: str, space_id: str, wechat_time: str = Depends(get_time_str)):
  33. feedback = {
  34. 'a little cold': 0,
  35. 'so cold': 0,
  36. 'a little hot': 0,
  37. 'so hot': 0,
  38. 'noisy or blowy': 1,
  39. 'so stuffy': 0,
  40. 'switch off': 0,
  41. 'switch on': 0,
  42. }
  43. await readjust_all_target(project_id, space_id, wechat_time, feedback)
  44. return {'Message': 'Running background'}