# -*- coding: utf-8 -*- from fastapi import APIRouter, HTTPException, Query from loguru import logger from app.controllers.targets.temperature import ( temperature_target_control_v1, temperature_target_control_v2, temperature_target_control_v3, get_target_after_feedback, ) from app.models.domain.feedback import FeedbackValue from app.models.domain.targets import ( TargetReadjustResponse, RegulatedTargetResponse, TargetReadjustRequestV2, TargetReadjustResponseV2, ) from app.utils.date import get_time_str router = APIRouter() @router.get("/adjust", response_model=TargetReadjustResponse) async def readjust_target( project_id: str = Query(..., max_length=50, regex="^Pj", alias="projectId"), space_id: str = Query(..., max_length=50, regex="^Sp", alias="roomId"), timestamp: str | None = Query(None, min_length=14, max_length=14, alias="time"), feedback: FeedbackValue | None = Query(None, alias="itemId"), ): if not timestamp: timestamp = get_time_str() try: if feedback != FeedbackValue.null: if project_id == "Pj1101050030" or project_id == "Pj1101050039" or project_id == "Pj1101140020": need_run_room_control = await temperature_target_control_v1( project_id, space_id, feedback ) elif project_id == "Pj3301100002": need_run_room_control = await temperature_target_control_v3( project_id, space_id, feedback ) else: need_run_room_control = await temperature_target_control_v2( project_id, space_id, feedback ) else: need_run_room_control = True except Exception as e: logger.error(e) raise HTTPException( status_code=500, detail="Oops, something wrong has happened" ) response = { "projectId": project_id, "roomId": space_id, "flag": 1 if need_run_room_control else 0, "time": timestamp, } return response @router.get("/adjust/v2", response_model=TargetReadjustResponse) async def readjust_target_v2( project_id: str = Query(..., max_length=50, regex="^Pj", alias="project-id"), space_id: str = Query(..., max_length=50, regex="^Sp", alias="space-id"), feedback: FeedbackValue | None = Query(None), ): if feedback != FeedbackValue.null: try: need_run_room_control = await temperature_target_control_v2( project_id, space_id, feedback ) except Exception as e: logger.error(e) raise HTTPException( status_code=500, detail="Oops, something wrong has happened!" ) else: need_run_room_control = True response = { "projectId": project_id, "roomId": space_id, "flag": 1 if need_run_room_control else 0, "time": get_time_str(), } return response @router.post("/readjust", response_model=TargetReadjustResponseV2) async def get_readjusted_target(params: TargetReadjustRequestV2): pass @router.get("/regulated/value", response_model=RegulatedTargetResponse) async def read_regulated_value( project_id: str = Query(..., max_length=50, regex="^Pj", alias="projectId"), space_id: str = Query(..., max_length=50, regex="^Sp", alias="spaceId"), feedback: FeedbackValue | None = Query(None), ): new_actual_target = await get_target_after_feedback(project_id, space_id, feedback) response = { "projectId": project_id, "spaceId": space_id, "isTemporary": False, "temperature": {"min": new_actual_target, "max": new_actual_target}, } return response