Browse Source

add vrf logic for zhijiang

chenhaiyang 2 years ago
parent
commit
ae47faad66
2 changed files with 87 additions and 1 deletions
  1. 13 1
      app/api/routers/devices.py
  2. 74 0
      app/controllers/equipment/vrf/basic.py

+ 13 - 1
app/api/routers/devices.py

@@ -34,7 +34,7 @@ from app.controllers.equipment.vav import (
     build_acatva_instructions_for_jm,
 )
 from app.controllers.equipment.ventilation_fan.switch import build_acvtsf_switch_set
-from app.controllers.equipment.vrf.basic import build_acatvi_instructions
+from app.controllers.equipment.vrf.basic import build_acatvi_instructions, build_acatvi_instructions_v2
 from app.controllers.equipment.vrf.mode import build_acatvi_mode
 
 router = APIRouter()
@@ -216,6 +216,18 @@ async def get_acatvi_instructions(params: domain_devices.ACATVIInstructionsReque
 
 
 @router.post(
+    "/instructions/acatvi/v2", response_model=domain_devices.ACATVIInstructionsResponse
+)
+async def get_acatvi_instructions_v2(params: domain_devices.ACATVIInstructionsRequest):
+    instructions = await build_acatvi_instructions_v2(params)
+
+    logger.info(params)
+    logger.info(f"{params.device_id} - {instructions}")
+
+    return instructions
+
+
+@router.post(
     "/instructions/acatvi/mode", response_model=domain_devices.ACATVIModeResponse
 )
 async def get_acatvi_mode(params: domain_devices.ACATVIModeRequest):

+ 74 - 0
app/controllers/equipment/vrf/basic.py

@@ -277,6 +277,55 @@ class VRFController(EquipmentController):
         return self.device
 
 
+class VRFControllerV2(VRFController):
+    """
+    For Zhijiang.
+    """
+
+    def __init__(
+            self,
+            vrf: VRF,
+            target: float,
+            realtime: float,
+            feedback: FeedbackValue,
+            on_time: str,
+            off_time: str,
+    ):
+        super().__init__(vrf, target, realtime, feedback, on_time, off_time)
+
+    def get_temperature_set(self) -> float:
+        if self.device.work_mode == VRFMode.ventilation:
+            new_temperature_set = np.NAN
+        elif self.device.work_mode == VRFMode.cooling or self.device.work_mode == VRFMode.heating:
+            new_temperature_set = np.NAN
+            if self.target is None:
+                return new_temperature_set
+
+            # Default temperature set.
+            if not self.device.running_status:
+                if self.device.work_mode == VRFMode.cooling:
+                    new_temperature_set = 26.0
+                else:
+                    new_temperature_set = 20.0
+
+            # feedback
+            if self.feedback.value != "null" and self.device.current_temperature_set:
+                if self.feedback == FeedbackValue.a_little_cold or self.feedback == FeedbackValue.so_cold:
+                    new_temperature_set = self.device.current_temperature_set + 1.0
+                elif self.feedback == FeedbackValue.a_little_hot or self.feedback == FeedbackValue.so_hot:
+                    new_temperature_set = self.device.current_temperature_set - 1.0
+
+            if not np.isnan(new_temperature_set):
+                new_temperature_set = max(16.0, min(32.0, new_temperature_set))
+        else:
+            new_temperature_set = np.NAN
+
+        new_temperature_set = round_half_up(new_temperature_set)
+        self.device.temperature_set = new_temperature_set
+
+        return new_temperature_set
+
+
 async def query_status_time(db: Session, device_id: str) -> tuple[datetime, datetime]:
     feedback_time_in_db = blowy_feedback_time.get_time_by_device(db, device_id)
     if feedback_time_in_db:
@@ -327,3 +376,28 @@ async def build_acatvi_instructions(params: ACATVIInstructionsRequest) -> dict[s
         instructions.update({"temperature_set": regulated_vrf.temperature_set})
 
     return instructions
+
+
+async def build_acatvi_instructions_v2(params: ACATVIInstructionsRequest) -> dict[str, str | float]:
+    # For Zhijiang.
+    vrf = VRF(
+        return_air_temp=params.return_air_temperature,
+        current_temperature_set=params.current_temperature_set,
+        speed=params.current_speed,
+        running_status=params.running_status,
+        work_mode=params.work_mode,
+    )
+
+    controller = VRFControllerV2(vrf, params.space_temperature_target, params.space_realtime_temperature,
+                                 params.feedback,
+                                 params.on_time, params.off_time)
+    await controller.run()
+    regulated_vrf = controller.get_results()
+
+    instructions = dict()
+    instructions.update({"switch_set": regulated_vrf.equip_switch_set})
+    instructions.update({"speed_set": regulated_vrf.speed_set})
+    if regulated_vrf.temperature_set and not np.isnan(regulated_vrf.temperature_set):
+        instructions.update({"temperature_set": regulated_vrf.temperature_set})
+
+    return instructions