Browse Source

add PAU freq set controller

highing666 4 years ago
parent
commit
b8e670527e
2 changed files with 81 additions and 0 deletions
  1. 17 0
      app/api/routers/devices.py
  2. 64 0
      app/controllers/equipment/pau/freq_set.py

+ 17 - 0
app/api/routers/devices.py

@@ -5,6 +5,7 @@ from app.controllers.equipment.ahu.thermal_mode import get_thermal_mode
 from app.controllers.equipment.fcu.on_ratio import start_on_ratio_mode
 from app.controllers.equipment.pau.switch import get_switch_action
 from app.controllers.equipment.pau.supply_air_temperature_set import get_next_acatfu_supply_air_temperature_set
+from app.controllers.equipment.pau.freq_set import get_next_acatfu_freq_set
 from app.models.domain.devices import DevicesInstructionsBaseResponse
 
 
@@ -83,3 +84,19 @@ async def get_acatfu_supply_air_temperature_set(
             'supplyAirTemperatureSet': temperature_set
         }
     }
+
+
+@router.get('/instructions/acatfu/freq-set', response_model=DevicesInstructionsBaseResponse)
+async def get_acatfu_freq_set(
+        project_id: str = Query(..., max_length=50, regex='^Pj', alias='projectId'),
+        device_id: str = Query(..., max_length=50, regex='^Eq', alias='equipId')
+):
+    freq_set = await get_next_acatfu_freq_set(project_id, device_id)
+
+    return {
+        'projectId': project_id,
+        'equipId': device_id,
+        'output': {
+            'fanFreqSet': freq_set
+        }
+    }

+ 64 - 0
app/controllers/equipment/pau/freq_set.py

@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+
+from typing import Tuple
+
+from httpx import AsyncClient
+from loguru import logger
+
+from app.services.platform import DataPlatformService, InfoCode
+from app.services.transfer import Duoduo
+
+
+class ACATFUFanFreqController:
+    """
+    Fan freq setting logic for zhonghai.
+    """
+
+    def __init__(self, current_freq: float, hot_rate: float, cold_rate: float):
+        self.current_freq = current_freq
+        self.hot_rate = hot_rate
+        self.cold_rate = cold_rate
+
+    def get_next_set(self) -> float:
+        try:
+            cold_hot_ratio = self.cold_rate / self.hot_rate
+        except ZeroDivisionError:
+            cold_hot_ratio = 99
+
+        if cold_hot_ratio < 0.5:
+            diff = 3
+        elif cold_hot_ratio < 0.9:
+            diff = 2
+        elif cold_hot_ratio < 1.1:
+            diff = 0
+        elif cold_hot_ratio < 1.5:
+            diff = -2
+        elif cold_hot_ratio >= 1.5:
+            diff = -3
+        else:  # If cold hot rate is NAN.
+            diff = 0
+
+        next_freq_set = self.current_freq + diff
+        next_freq_set = max(30.0, min(45.0, next_freq_set))
+
+        return next_freq_set
+
+
+async def fetch_params(project_id: str, device_id: str) -> Tuple[float, float, float]:
+    async with AsyncClient() as client:
+        platform = DataPlatformService(client, project_id)
+        duoduo = Duoduo(client, project_id)
+
+        current_freq = await platform.get_realtime_data(InfoCode.fan_freq, device_id)
+        hot_rate, cold_rate = await duoduo.query_fill_rate_by_device(device_id)
+
+        return current_freq, hot_rate, cold_rate
+
+
+@logger.catch()
+async def get_next_acatfu_freq_set(project_id: str, device_id: str) -> float:
+    current_freq, hot_rate, cold_rate = await fetch_params(project_id, device_id)
+    controller = ACATFUFanFreqController(current_freq, hot_rate, cold_rate)
+    next_freq_set = controller.get_next_set()
+
+    return next_freq_set