|
@@ -1,10 +1,13 @@
|
|
|
# -*- 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 TemperatureAdjustmentController
|
|
|
+from app.models.domain.feedback import FeedbackValue
|
|
|
from app.models.domain.targets import TargetReadjustResponse
|
|
|
from app.utils.date import get_time_str
|
|
|
|
|
@@ -13,18 +16,22 @@ 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),
|
|
|
+ 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')
|
|
|
):
|
|
|
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()
|
|
|
+ if project_id == 'Pj1101050030':
|
|
|
+ if feedback:
|
|
|
+ controller = TemperatureAdjustmentController(project_id, space_id, feedback)
|
|
|
+ need_run_room_control = await controller.run()
|
|
|
+ else:
|
|
|
+ need_run_room_control = True
|
|
|
else:
|
|
|
- need_run_room_control = await readjust_all_target(projectId, roomId, time)
|
|
|
+ if not timestamp:
|
|
|
+ timestamp = get_time_str()
|
|
|
+ need_run_room_control = await readjust_all_target(project_id, space_id, timestamp)
|
|
|
except Exception as e:
|
|
|
logger.error(e)
|
|
|
raise HTTPException(
|
|
@@ -33,10 +40,10 @@ async def readjust_target(
|
|
|
)
|
|
|
|
|
|
response = {
|
|
|
- 'projectId': projectId,
|
|
|
- 'roomId': roomId,
|
|
|
+ 'projectId': project_id,
|
|
|
+ 'roomId': space_id,
|
|
|
'flag': 1 if need_run_room_control else 0,
|
|
|
- 'time': time,
|
|
|
+ 'time': timestamp,
|
|
|
}
|
|
|
return response
|
|
|
|
|
@@ -73,21 +80,22 @@ async def test_readjust_target(
|
|
|
|
|
|
@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')
|
|
|
+ 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)
|
|
|
):
|
|
|
- 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!'
|
|
|
- )
|
|
|
+ if feedback:
|
|
|
+ try:
|
|
|
+ controller = TemperatureAdjustmentController(project_id, space_id, 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!'
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ need_run_room_control = True
|
|
|
|
|
|
response = {
|
|
|
'projectId': project_id,
|