Переглянути джерело

save space temporary weights to 1 when this space receive a temperature feedback

chenhaiyang 4 роки тому
батько
коміт
40efae99db
1 змінених файлів з 22 додано та 8 видалено
  1. 22 8
      app/controllers/targets/temperature.py

+ 22 - 8
app/controllers/targets/temperature.py

@@ -10,7 +10,7 @@ from httpx import AsyncClient
 from loguru import logger
 from sqlalchemy.orm import Session
 
-from app.crud.space.weight import get_weight_by_space, update_weight
+from app.crud.space.weight import get_weights_by_space, update_weight
 from app.models.domain.feedback import FeedbackValue
 from app.resources.params import TEMPERATURE_TARGET_WEIGHT
 from app.schemas.sapce_weight import SpaceWeightUpdate
@@ -335,15 +335,27 @@ class WeightFlagDeliver:
     Change a space temporary weight when the space receives a feedback about temperature.
     """
 
-    def __init__(self, db: Session):
+    def __init__(self, db: Session, feedback: FeedbackValue):
         self.db = db
+        self.feedback = feedback
+
+    def is_temperature_feedback(self) -> bool:
+        if (self.feedback == FeedbackValue.a_little_hot
+                or self.feedback == FeedbackValue.so_hot
+                or self.feedback == FeedbackValue.a_little_cold
+                or self.feedback == FeedbackValue.so_cold):
+            flag = True
+        else:
+            flag = False
+
+        return flag
 
     def save(self, space: str):
-        weight = get_weight_by_space(self.db, space_id=space)
-        if not weight:
-            logger.error(f'{space} is not in vav_room_weights table')
-        weight_in = SpaceWeightUpdate(temporary_weight=1.0)
-        update_weight(self.db, db_weight=weight, weight_in=weight_in)
+        if self.is_temperature_feedback():
+            weights = get_weights_by_space(self.db, space_id=space)
+            for weight in weights:
+                weight_in = SpaceWeightUpdate(temporary_weight=1.0)
+                update_weight(self.db, db_weight=weight, weight_in=weight_in)
 
 
 class TemperatureTargetController:
@@ -466,7 +478,7 @@ class TemperatureTargetControllerV2:
 
 
 @logger.catch()
-async def temperature_target_control_v1(project_id: str, space_id: str, feedback: FeedbackValue) -> bool:
+async def temperature_target_control_v1(project_id: str, space_id: str, feedback: FeedbackValue, db: Session) -> bool:
     temperature_target_raw_data = await TemperatureTargetCarrier(project_id, space_id).get_result()
     temperature_target_data = TemperatureTargetPacker(temperature_target_raw_data).get_result()
     controller = TemperatureTargetController(temperature_target_data)
@@ -474,6 +486,8 @@ async def temperature_target_control_v1(project_id: str, space_id: str, feedback
     controlled_result = controller.get_result()
     await TargetDeliver(project_id, space_id).send(controlled_result)
 
+    WeightFlagDeliver(db, feedback).save(space_id)
+
     return controlled_result['need_run_room_control']