targets.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, HTTPException, Query
  3. from loguru import logger
  4. from app.controllers.targets import readjust_all_target
  5. from app.models.domain.targets import TargetReadjustResponse
  6. from app.utils.date import get_time_str
  7. router = APIRouter()
  8. @router.get('/adjust', response_model=TargetReadjustResponse, tags=['targets'])
  9. async def readjust_target(
  10. projectId: str = Query(..., max_length=50, regex='^Pj'),
  11. roomId: str = Query(..., max_length=50, regex='^Sp'),
  12. time: str = Query(None, min_length=14, max_length=14),
  13. ):
  14. try:
  15. if not time:
  16. time = get_time_str()
  17. need_run_room_control = await readjust_all_target(projectId, roomId, time)
  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': projectId,
  26. 'roomId': roomId,
  27. 'flag': 1 if need_run_room_control else 0,
  28. 'time': time,
  29. }
  30. return response
  31. @router.get('/adjust/test', response_model=TargetReadjustResponse, tags=['targets'])
  32. async def test_readjust_target(
  33. project_id: str = Query(..., max_length=50, regex='^Pj'),
  34. space_id: str = Query(..., max_length=50, regex='^Sp')
  35. ):
  36. feedback = {
  37. 'a little cold': 1,
  38. 'so cold': 0,
  39. 'a little hot': 0,
  40. 'so hot': 0,
  41. 'switch on': 0,
  42. }
  43. try:
  44. need_run_room_control = await readjust_all_target(project_id, space_id, feedback=feedback)
  45. except Exception as e:
  46. logger.error(e)
  47. raise HTTPException(
  48. status_code=500,
  49. detail='Oops, something wrong has happened'
  50. )
  51. response = {
  52. 'projectId': project_id,
  53. 'roomId': space_id,
  54. 'flag': 1 if need_run_room_control else 0,
  55. 'time': get_time_str()
  56. }
  57. return response