Ver código fonte

amend switch schema

highing666 3 anos atrás
pai
commit
2673caf28b

+ 9 - 32
app/controllers/equipment/ahu/switch.py

@@ -5,7 +5,7 @@ from typing import Tuple
 from httpx import AsyncClient
 from loguru import logger
 
-from app.controllers.equipment.switch import Switch, fetch_data, send_switch_command
+from app.controllers.equipment.switch import Switch, SwitchSet
 from app.models.domain.devices import ACATAHSwitchSetRequest
 from app.schemas.equipment import AHU
 from app.services.platform import DataPlatformService
@@ -15,7 +15,7 @@ class AHUSwitch(Switch):
     def __init__(self, equipment: AHU):
         super(AHUSwitch, self).__init__(equipment)
 
-    def break_time_action(self, begin: str, end: str, is_workday: bool) -> str:
+    def break_time_action(self, begin: str, end: str, is_workday: bool) -> SwitchSet:
         if self._equip.in_cloud_status:
             if is_workday:
                 if begin and end:
@@ -28,44 +28,21 @@ class AHUSwitch(Switch):
                             switch_flag = False
 
                     if not switch_flag:
-                        action = "off"
+                        action = SwitchSet.off
                     else:
-                        action = "hold"
+                        action = SwitchSet.hold
                 else:
-                    action = "hold"
+                    action = SwitchSet.hold
             else:
-                action = "hold"
+                action = SwitchSet.hold
         else:
-            action = "hold"
+            action = SwitchSet.hold
 
         return action
 
 
-async def fetch_break_time(project_id: str, device_id: str) -> Tuple[str, str]:
-    async with AsyncClient() as client:
-        platform = DataPlatformService(client, project_id)
-        begin = await platform.get_static_info("ctm-BeginBreakTime", device_id)
-        end = await platform.get_static_info("ctm-EndBreakTime", device_id)
-
-    return begin, end
-
-
-@logger.catch()
-async def ahu_switch_control(project_id: str, equipment_id: str) -> None:
-    equip_params, day_type = await fetch_data(project_id, equipment_id)
-    is_workday = True if day_type.get("day_type") == "WeekDay" else False
-    begin, end = await fetch_break_time(project_id, equipment_id)
-    switch_controller = AHUSwitch(AHU(**equip_params))
-    action = await switch_controller.build_next_action(is_workday)
-    break_action = switch_controller.break_time_action(begin, end, is_workday)
-    if break_action == "off":
-        action = "off"
-    logger.debug(f"AHU-{equipment_id}: {action}")
-    await send_switch_command(project_id, equipment_id, action)
-
-
 @logger.catch()
-async def build_acatah_switch_set(params: ACATAHSwitchSetRequest) -> str:
+async def build_acatah_switch_set(params: ACATAHSwitchSetRequest) -> SwitchSet:
     ahu = AHU(
         running_status=params.running_status,
         in_cloud_status=params.in_cloud_status,
@@ -79,6 +56,6 @@ async def build_acatah_switch_set(params: ACATAHSwitchSetRequest) -> str:
     )
 
     if break_action == "off":
-        action = "off"
+        action = SwitchSet.off
 
     return action

+ 3 - 22
app/controllers/equipment/pau/switch.py

@@ -2,7 +2,7 @@
 
 from loguru import logger
 
-from app.controllers.equipment.switch import Switch, fetch_data, send_switch_command
+from app.controllers.equipment.switch import Switch, SwitchSet
 from app.models.domain.devices import ACATFUSwitchSetRequest
 from app.schemas.equipment import PAU
 
@@ -13,26 +13,7 @@ class PAUSwitch(Switch):
 
 
 @logger.catch()
-async def pau_switch_control(project_id: str, equipment_id: str) -> None:
-    equip_params, day_type = await fetch_data(project_id, equipment_id)
-    action = await PAUSwitch(PAU(**equip_params)).build_next_action(
-        day_type.get("day_type")
-    )
-    logger.debug(f"PAU-{equipment_id}: {action}")
-    await send_switch_command(project_id, equipment_id, action)
-
-
-@logger.catch()
-async def get_switch_action(project_id: str, device_id: str) -> str:
-    device_params, day_type = await fetch_data(project_id, device_id)
-    is_workday = True if day_type.get("day_type") == "WeekDay" else False
-    action = await PAUSwitch(PAU(**device_params)).build_next_action(is_workday)
-
-    return action
-
-
-@logger.catch()
-async def build_acatfu_switch_set(params: ACATFUSwitchSetRequest) -> str:
+async def build_acatfu_switch_set(params: ACATFUSwitchSetRequest) -> SwitchSet:
     pau = PAU(
         running_status=params.running_status,
         in_cloud_status=params.in_cloud_status,
@@ -41,6 +22,6 @@ async def build_acatfu_switch_set(params: ACATFUSwitchSetRequest) -> str:
     )
     action = await PAUSwitch(pau).build_next_action(params.is_workday)
     if not params.is_workday:
-        action = "off"
+        action = SwitchSet.off
 
     return action

+ 13 - 6
app/controllers/equipment/switch.py

@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+from enum import Enum
 from typing import Dict, Tuple
 
 import arrow
@@ -11,13 +12,19 @@ from app.services.transfer import Duoduo
 from app.utils.date import get_time_str, TIME_FMT
 
 
+class SwitchSet(str, Enum):
+    on = "on"
+    off = "off"
+    hold = "hold"
+
+
 class Switch:
     def __init__(self, equipment: BaseEquipment):
         super(Switch, self).__init__()
         self._equip = equipment
         self._now_time = arrow.get(get_time_str(), TIME_FMT).time().strftime("%H%M%S")
 
-    async def build_next_action(self, is_workday: bool) -> str:
+    async def build_next_action(self, is_workday: bool) -> SwitchSet:
         if self._equip.in_cloud_status:
             if is_workday:
                 if self._equip.on_time <= self._equip.off_time:
@@ -32,15 +39,15 @@ class Switch:
                         switch_flag = True
 
                 if switch_flag and not self._equip.running_status:
-                    action = "on"
+                    action = SwitchSet.on
                 elif not switch_flag and self._equip.running_status:
-                    action = "off"
+                    action = SwitchSet.off
                 else:
-                    action = "hold"
+                    action = SwitchSet.hold
             else:
-                action = "hold"
+                action = SwitchSet.hold
         else:
-            action = "hold"
+            action = SwitchSet.off
 
         return action
 

+ 3 - 16
app/controllers/equipment/ventilation_fan/switch.py

@@ -3,7 +3,7 @@
 from loguru import logger
 
 from app.models.domain.devices import ACVTSFSwitchSetRequest
-from app.controllers.equipment.switch import Switch, fetch_data, send_switch_command
+from app.controllers.equipment.switch import Switch, SwitchSet
 from app.schemas.equipment import VentilationFan
 
 
@@ -13,20 +13,7 @@ class VentilationFanSwitch(Switch):
 
 
 @logger.catch()
-async def ventilation_fan_switch_control(project_id: str, equipment_id: str) -> None:
-    equip_params, day_type = await fetch_data(project_id, equipment_id)
-    is_workday = True if day_type.get("day_type") == "WeekDay" else False
-    action = await VentilationFanSwitch(
-        VentilationFan(**equip_params)
-    ).build_next_action(is_workday)
-    if not is_workday:
-        action = "off"
-    logger.debug(f"VTSF-{equipment_id}: {action}")
-    await send_switch_command(project_id, equipment_id, action)
-
-
-@logger.catch()
-async def build_acvtsf_switch_set(params: ACVTSFSwitchSetRequest) -> str:
+async def build_acvtsf_switch_set(params: ACVTSFSwitchSetRequest) -> SwitchSet:
     vrf = VentilationFan(
         running_status=params.running_status,
         in_cloud_status=params.in_cloud_status,
@@ -35,6 +22,6 @@ async def build_acvtsf_switch_set(params: ACVTSFSwitchSetRequest) -> str:
     )
     action = await VentilationFanSwitch(vrf).build_next_action(params.is_workday)
     if not params.is_workday:
-        action = "off"
+        action = SwitchSet.off
 
     return action