targets.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. from typing import Optional
  3. from fastapi import APIRouter, HTTPException, Query
  4. from loguru import logger
  5. from app.controllers.targets.target import readjust_all_target
  6. from app.controllers.targets.temperature import temperature_target_control_v1, temperature_target_control_v2
  7. from app.models.domain.feedback import FeedbackValue
  8. from app.models.domain.targets import TargetReadjustResponse
  9. from app.utils.date import get_time_str
  10. router = APIRouter()
  11. @router.get('/adjust', response_model=TargetReadjustResponse)
  12. async def readjust_target(
  13. project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
  14. space_id: str = Query(..., max_length=50, regex='^Sp', alias='roomId'),
  15. timestamp: Optional[str] = Query(None, min_length=14, max_length=14, alias='time'),
  16. feedback: Optional[FeedbackValue] = Query(None, alias='itemId')
  17. ):
  18. if not timestamp:
  19. timestamp = get_time_str()
  20. try:
  21. if feedback != FeedbackValue.null:
  22. if project_id == 'Pj1101050030':
  23. need_run_room_control = await temperature_target_control_v1(project_id, space_id, feedback)
  24. else:
  25. need_run_room_control = await temperature_target_control_v2(project_id, space_id, feedback)
  26. else:
  27. need_run_room_control = True
  28. except Exception as e:
  29. logger.error(e)
  30. raise HTTPException(
  31. status_code=500,
  32. detail='Oops, something wrong has happened'
  33. )
  34. response = {
  35. 'projectId': project_id,
  36. 'roomId': space_id,
  37. 'flag': 1 if need_run_room_control else 0,
  38. 'time': timestamp,
  39. }
  40. return response
  41. @router.get('/adjust/test', response_model=TargetReadjustResponse)
  42. async def test_readjust_target(
  43. project_id: str = Query(..., max_length=50, regex='^Pj'),
  44. space_id: str = Query(..., max_length=50, regex='^Sp')
  45. ):
  46. feedback = {
  47. 'a little cold': 0,
  48. 'so cold': 0,
  49. 'a little hot': 1,
  50. 'so hot': 0,
  51. 'switch on': 0,
  52. }
  53. try:
  54. need_run_room_control = await readjust_all_target(project_id, space_id, feedback=feedback)
  55. except Exception as e:
  56. logger.error(e)
  57. raise HTTPException(
  58. status_code=500,
  59. detail='Oops, something wrong has happened'
  60. )
  61. response = {
  62. 'projectId': project_id,
  63. 'roomId': space_id,
  64. 'flag': 1 if need_run_room_control else 0,
  65. 'time': get_time_str()
  66. }
  67. return response
  68. @router.get('/adjust/v2', response_model=TargetReadjustResponse)
  69. async def readjust_target_v2(
  70. project_id: str = Query(..., max_length=50, regex='^Pj', alias='project-id'),
  71. space_id: str = Query(..., max_length=50, regex='^Sp', alias='space-id'),
  72. feedback: Optional[FeedbackValue] = Query(None)
  73. ):
  74. if feedback != FeedbackValue.null:
  75. try:
  76. need_run_room_control = await temperature_target_control_v2(project_id, space_id, feedback)
  77. except Exception as e:
  78. logger.error(e)
  79. raise HTTPException(
  80. status_code=500,
  81. detail='Oops, something wrong has happened!'
  82. )
  83. else:
  84. need_run_room_control = True
  85. response = {
  86. 'projectId': project_id,
  87. 'roomId': space_id,
  88. 'flag': 1 if need_run_room_control else 0,
  89. 'time': get_time_str()
  90. }
  91. return response