123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- # -*- coding: utf-8 -*-
- from typing import Optional
- from fastapi import APIRouter, HTTPException, Query
- from loguru import logger
- from app.controllers.targets.target import readjust_all_target
- from app.controllers.targets.temperature import temperature_target_control_v1, temperature_target_control_v2
- from app.models.domain.feedback import FeedbackValue
- from app.models.domain.targets import TargetReadjustResponse
- 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: Optional[str] = Query(None, min_length=14, max_length=14, alias='time'),
- feedback: Optional[FeedbackValue] = Query(None, alias='itemId')
- ):
- if not timestamp:
- timestamp = get_time_str()
- try:
- if feedback != FeedbackValue.null:
- if project_id == 'Pj1101050030':
- need_run_room_control = await temperature_target_control_v1(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/test', response_model=TargetReadjustResponse)
- async def test_readjust_target(
- project_id: str = Query(..., max_length=50, regex='^Pj'),
- space_id: str = Query(..., max_length=50, regex='^Sp')
- ):
- feedback = {
- 'a little cold': 0,
- 'so cold': 0,
- 'a little hot': 1,
- 'so hot': 0,
- 'switch on': 0,
- }
- try:
- need_run_room_control = await readjust_all_target(project_id, space_id, feedback=feedback)
- 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': get_time_str()
- }
- 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: Optional[FeedbackValue] = 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
|