targets.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, HTTPException, Query
  3. from loguru import logger
  4. from app.controllers.targets.temperature import (
  5. temperature_target_control_v1,
  6. temperature_target_control_v2,
  7. temperature_target_control_v3,
  8. get_target_after_feedback,
  9. )
  10. from app.models.domain.feedback import FeedbackValue
  11. from app.models.domain.targets import (
  12. TargetReadjustResponse,
  13. RegulatedTargetResponse,
  14. TargetReadjustRequestV2,
  15. TargetReadjustResponseV2,
  16. )
  17. from app.utils.date import get_time_str
  18. router = APIRouter()
  19. @router.get("/adjust", response_model=TargetReadjustResponse)
  20. async def readjust_target(
  21. project_id: str = Query(..., max_length=50, regex="^Pj", alias="projectId"),
  22. space_id: str = Query(..., max_length=50, regex="^Sp", alias="roomId"),
  23. timestamp: str | None = Query(None, min_length=14, max_length=14, alias="time"),
  24. feedback: FeedbackValue | None = Query(None, alias="itemId"),
  25. ):
  26. if not timestamp:
  27. timestamp = get_time_str()
  28. try:
  29. if feedback != FeedbackValue.null:
  30. if project_id == "Pj1101050030" or project_id == "Pj1101050039" or project_id == "Pj1101140020":
  31. need_run_room_control = await temperature_target_control_v1(
  32. project_id, space_id, feedback
  33. )
  34. elif project_id == "Pj3301100002":
  35. need_run_room_control = await temperature_target_control_v3(
  36. project_id, space_id, feedback
  37. )
  38. else:
  39. need_run_room_control = await temperature_target_control_v2(
  40. project_id, space_id, feedback
  41. )
  42. else:
  43. need_run_room_control = True
  44. except Exception as e:
  45. logger.error(e)
  46. raise HTTPException(
  47. status_code=500, detail="Oops, something wrong has happened"
  48. )
  49. response = {
  50. "projectId": project_id,
  51. "roomId": space_id,
  52. "flag": 1 if need_run_room_control else 0,
  53. "time": timestamp,
  54. }
  55. return response
  56. @router.get("/adjust/v2", response_model=TargetReadjustResponse)
  57. async def readjust_target_v2(
  58. project_id: str = Query(..., max_length=50, regex="^Pj", alias="project-id"),
  59. space_id: str = Query(..., max_length=50, regex="^Sp", alias="space-id"),
  60. feedback: FeedbackValue | None = Query(None),
  61. ):
  62. if feedback != FeedbackValue.null:
  63. try:
  64. need_run_room_control = await temperature_target_control_v2(
  65. project_id, space_id, feedback
  66. )
  67. except Exception as e:
  68. logger.error(e)
  69. raise HTTPException(
  70. status_code=500, detail="Oops, something wrong has happened!"
  71. )
  72. else:
  73. need_run_room_control = True
  74. response = {
  75. "projectId": project_id,
  76. "roomId": space_id,
  77. "flag": 1 if need_run_room_control else 0,
  78. "time": get_time_str(),
  79. }
  80. return response
  81. @router.post("/readjust", response_model=TargetReadjustResponseV2)
  82. async def get_readjusted_target(params: TargetReadjustRequestV2):
  83. pass
  84. @router.get("/regulated/value", response_model=RegulatedTargetResponse)
  85. async def read_regulated_value(
  86. project_id: str = Query(..., max_length=50, regex="^Pj", alias="projectId"),
  87. space_id: str = Query(..., max_length=50, regex="^Sp", alias="spaceId"),
  88. feedback: FeedbackValue | None = Query(None),
  89. ):
  90. new_actual_target = await get_target_after_feedback(project_id, space_id, feedback)
  91. response = {
  92. "projectId": project_id,
  93. "spaceId": space_id,
  94. "isTemporary": False,
  95. "temperature": {"min": new_actual_target, "max": new_actual_target},
  96. }
  97. return response