Browse Source

Merge branch 'dev'

highing666 4 years ago
parent
commit
3d9779587a
3 changed files with 136 additions and 0 deletions
  1. 17 0
      app/api/routers/devices.py
  2. 64 0
      app/controllers/equipment/pau/freq_set.py
  3. 55 0
      app/services/rwd.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

+ 55 - 0
app/services/rwd.py

@@ -0,0 +1,55 @@
+from typing import Dict, List, Optional, Tuple
+
+import arrow
+import numpy as np
+from httpx import AsyncClient, URL
+from loguru import logger
+
+from app.core.config import settings
+from app.services.service import Service
+from app.utils.date import get_time_str, TIME_FMT
+
+
+class RealWorldService(Service):
+
+    def __init__(
+            self,
+            client: AsyncClient,
+            group_code: str,
+            project_id: str,
+            app_id: Optional[str] = None,
+            user_id: Optional[str] = None,
+            server_settings=settings
+    ):
+        super(RealWorldService, self).__init__(client)
+        self._group_code = group_code
+        self._project_id = project_id
+        self._app_id = app_id
+        self._user_id = user_id
+        self._base_url = URL(server_settings.PLATFORM_HOST)
+        self._now_time = get_time_str()
+
+    def _common_parameters(self) -> Dict:
+        return {
+            'groupCode': self._group_code,
+            'projectId': self._project_id
+        }
+
+    async def query_instance(
+            self,
+            object_id: Optional[List] = None,
+            class_code: Optional[str] = None
+    ):
+        url = self._base_url.join('/rwd/instance/object/query')
+        params = self._common_parameters()
+        criteria = dict()
+        if object_id:
+            criteria.update({'id': object_id})
+        if class_code:
+            criteria.update({'classCode': class_code})
+        payload = {
+            'criteria': criteria
+        }
+        raw_info = await self._post(url, params, payload)
+
+        return raw_info