1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- # -*- coding: utf-8 -*-
- 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 TemperatureAdjustmentController
- 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(
- projectId: str = Query(..., max_length=50, regex='^Pj'),
- roomId: str = Query(..., max_length=50, regex='^Sp'),
- time: str = Query(None, min_length=14, max_length=14),
- ):
- try:
- if not time:
- time = get_time_str()
- if projectId == 'Pj1101050030':
- controller = TemperatureAdjustmentController(projectId, roomId, wechat_time=time)
- need_run_room_control = await controller.run()
- else:
- need_run_room_control = await readjust_all_target(projectId, roomId, time)
- except Exception as e:
- logger.error(e)
- raise HTTPException(
- status_code=500,
- detail='Oops, something wrong has happened'
- )
- response = {
- 'projectId': projectId,
- 'roomId': roomId,
- 'flag': 1 if need_run_room_control else 0,
- 'time': time,
- }
- 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'),
- space_id: str = Query(..., max_length=50, regex='^Sp')
- ):
- test_feedback = {
- 'a little hot': 1
- }
- try:
- controller = TemperatureAdjustmentController(project_id, space_id, raw_feedback=test_feedback)
- need_run_room_control = await controller.run()
- 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
|