targets.py 3.0 KB

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