targets.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 TemperatureAdjustmentController
  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. try:
  19. if project_id == 'Pj1101050030':
  20. if feedback:
  21. controller = TemperatureAdjustmentController(project_id, space_id, feedback)
  22. need_run_room_control = await controller.run()
  23. else:
  24. need_run_room_control = True
  25. else:
  26. if not timestamp:
  27. timestamp = get_time_str()
  28. need_run_room_control = await readjust_all_target(project_id, space_id, timestamp)
  29. except Exception as e:
  30. logger.error(e)
  31. raise HTTPException(
  32. status_code=500,
  33. detail='Oops, something wrong has happened'
  34. )
  35. response = {
  36. 'projectId': project_id,
  37. 'roomId': space_id,
  38. 'flag': 1 if need_run_room_control else 0,
  39. 'time': timestamp,
  40. }
  41. return response
  42. @router.get('/adjust/test', response_model=TargetReadjustResponse)
  43. async def test_readjust_target(
  44. project_id: str = Query(..., max_length=50, regex='^Pj'),
  45. space_id: str = Query(..., max_length=50, regex='^Sp')
  46. ):
  47. feedback = {
  48. 'a little cold': 0,
  49. 'so cold': 0,
  50. 'a little hot': 1,
  51. 'so hot': 0,
  52. 'switch on': 0,
  53. }
  54. try:
  55. need_run_room_control = await readjust_all_target(project_id, space_id, feedback=feedback)
  56. except Exception as e:
  57. logger.error(e)
  58. raise HTTPException(
  59. status_code=500,
  60. detail='Oops, something wrong has happened'
  61. )
  62. response = {
  63. 'projectId': project_id,
  64. 'roomId': space_id,
  65. 'flag': 1 if need_run_room_control else 0,
  66. 'time': get_time_str()
  67. }
  68. return response
  69. @router.get('/adjust/v2', response_model=TargetReadjustResponse)
  70. async def readjust_target_v2(
  71. project_id: str = Query(..., max_length=50, regex='^Pj', alias='project-id'),
  72. space_id: str = Query(..., max_length=50, regex='^Sp', alias='space-id'),
  73. feedback: Optional[FeedbackValue] = Query(None)
  74. ):
  75. if feedback:
  76. try:
  77. controller = TemperatureAdjustmentController(project_id, space_id, feedback)
  78. need_run_room_control = await controller.run()
  79. except Exception as e:
  80. logger.error(e)
  81. raise HTTPException(
  82. status_code=500,
  83. detail='Oops, something wrong has happened!'
  84. )
  85. else:
  86. need_run_room_control = True
  87. response = {
  88. 'projectId': project_id,
  89. 'roomId': space_id,
  90. 'flag': 1 if need_run_room_control else 0,
  91. 'time': get_time_str()
  92. }
  93. return response